container/services/src/main/java/org/mobicents/slee/container/management/ServiceManagementImpl.java
public void deactivate(final ServiceID serviceID)
			throws NullPointerException, UnrecognizedServiceException,
			InvalidStateException, ManagementException {

		if (logger.isDebugEnabled()) {
			logger.debug("Deactivating " + serviceID);
		}

		if (serviceID == null)
			throw new NullPointerException("NullPointerException");

		synchronized (sleeContainer.getManagementMonitor()) {

			final ServiceComponent serviceComponent = componentRepositoryImpl
					.getComponentByID(serviceID);

			if (serviceComponent == null)
				throw new UnrecognizedServiceException("Service not found for "
						+ serviceID);				
			
			if (logger.isDebugEnabled())
				logger.debug(serviceID.toString() + " state = "
						+ serviceComponent.getServiceState());

			final SleeState sleeState = sleeContainer.getSleeState();

			if (serviceComponent.getServiceState() == ServiceState.STOPPING)
				throw new InvalidStateException("Service is STOPPING");

			if (serviceComponent.getServiceState() == ServiceState.INACTIVE) {
				throw new InvalidStateException("Service already deactivated");
			}

			serviceComponent.setServiceState(ServiceState.STOPPING);

			// warn ra entities about state change
			final ResourceManagement resourceManagement = sleeContainer
					.getResourceManagement();
			for (String raEntityName : resourceManagement
					.getResourceAdaptorEntities()) {
				resourceManagement.getResourceAdaptorEntity(raEntityName)
						.serviceStopping(serviceID);
			}

			// only end activity if slee was running and is single node in
			// cluster, otherwise not needed (cluster) or
			// slee already did it
			if (sleeContainer.getCluster().isSingleMember() && (sleeState == SleeState.RUNNING || sleeState == SleeState.STOPPING)) {
				if (sleeState == SleeState.RUNNING) {
					endServiceActivity(serviceID);
				}
				else {
					// chance of concurrency with activity end
					synchronized (serviceComponent) {
						if(serviceComponent.isActivityEnded()) {
							// activity already ended but service was not in stopping state
							completeServiceStop(serviceComponent);
						}
					}
				}
			} else {
				serviceComponent.setServiceState(ServiceState.INACTIVE);
				// warn ra entities about state change
				for (String raEntityName : resourceManagement
						.getResourceAdaptorEntities()) {
					resourceManagement.getResourceAdaptorEntity(raEntityName)
							.serviceInactive(serviceID);
				}
				logger.info("Deactivated " + serviceID);
			}

			// remove runtime cache related with this service
			for (EventEntryDescriptor mEventEntry : serviceComponent
					.getRootSbbComponent().getDescriptor().getEventEntries()
					.values()) {
				if (mEventEntry.isInitialEvent()) {
					EventTypeComponent eventTypeComponent = componentRepositoryImpl
							.getComponentByID(mEventEntry.getEventReference());
					eventTypeComponent
							.deactivatedServiceWhichDefineEventAsInitial(serviceComponent);
				}
			}
						
		}
	}
public void deactivate(final ServiceID serviceID)
			throws NullPointerException, UnrecognizedServiceException,
			InvalidStateException, ManagementException {

		if (logger.isDebugEnabled()) {
			logger.debug("Deactivating " + serviceID);
		}

		if (serviceID == null)
			throw new NullPointerException("NullPointerException");

		synchronized (sleeContainer.getManagementMonitor()) {

			final ServiceComponent serviceComponent = componentRepositoryImpl
					.getComponentByID(serviceID);

			if (serviceComponent == null)
				throw new UnrecognizedServiceException("Service not found for "
						+ serviceID);				
			
			if (logger.isDebugEnabled())
				logger.debug(serviceID.toString() + " state = "
						+ serviceComponent.getServiceState());

			final SleeState sleeState = sleeContainer.getSleeState();

			if (serviceComponent.getServiceState() == ServiceState.STOPPING)
				throw new InvalidStateException("Service is STOPPING");

			if (serviceComponent.getServiceState() == ServiceState.INACTIVE) {
				throw new InvalidStateException("Service already deactivated");
			}

			serviceComponent.setServiceState(ServiceState.STOPPING);

			// warn ra entities about state change
			final ResourceManagement resourceManagement = sleeContainer
					.getResourceManagement();
			for (String raEntityName : resourceManagement
					.getResourceAdaptorEntities()) {
				resourceManagement.getResourceAdaptorEntity(raEntityName)
						.serviceStopping(serviceID);
			}

			// only end activity if slee was running and is single node in
			// cluster, otherwise not needed (cluster) or
			// slee already did it
			if (sleeContainer.getCluster().isSingleMember() && (sleeState == SleeState.RUNNING || sleeState == SleeState.STOPPING)) {
				if (sleeState == SleeState.RUNNING) {
					endServiceActivity(serviceID);
				}
				else {
					// chance of concurrency with activity end
					synchronized (serviceComponent) {
						if(serviceComponent.isActivityEnded()) {
							// activity already ended but service was not in stopping state
							completeServiceStop(serviceComponent);
						}
					}
				}
			} else {
				serviceComponent.setServiceState(ServiceState.INACTIVE);
				// warn ra entities about state change
				for (String raEntityName : resourceManagement
						.getResourceAdaptorEntities()) {
					resourceManagement.getResourceAdaptorEntity(raEntityName)
							.serviceInactive(serviceID);
				}
				logger.info("Deactivated " + serviceID);
			}

			// remove runtime cache related with this service
			for (EventEntryDescriptor mEventEntry : serviceComponent
					.getRootSbbComponent().getDescriptor().getEventEntries()
					.values()) {
				if (mEventEntry.isInitialEvent()) {
					EventTypeComponent eventTypeComponent = componentRepositoryImpl
							.getComponentByID(mEventEntry.getEventReference());
					eventTypeComponent
							.deactivatedServiceWhichDefineEventAsInitial(serviceComponent);
				}
			}
						
		}
	}
/*
(non-Javadoc)

@see
javax.slee.management.ServiceManagementMBean#deactivate(javax.slee.ServiceID
)