container/common/src/main/java/org/mobicents/slee/container/management/jmx/DeploymentMBeanImpl.java
public void uninstall(DeployableUnitID deployableUnitID)
			throws NullPointerException, UnrecognizedDeployableUnitException,
			DependencyException, InvalidStateException, ManagementException {

		logger.info("Uninstalling " +deployableUnitID);

		final SleeContainer sleeContainer = getSleeContainer();
		final SleeTransactionManager sleeTransactionManager = sleeContainer
				.getTransactionManager();
		final ServiceManagement serviceManagement = sleeContainer
				.getServiceManagement();
		final ResourceManagement resourceManagement = sleeContainer
				.getResourceManagement();
		final DeployableUnitManagement deployableUnitManagement = sleeContainer
				.getDeployableUnitManagement();
		final ComponentRepository componentRepositoryImpl = sleeContainer
				.getComponentRepository();

		// we sync on the container's monitor object
		synchronized (sleeContainer.getManagementMonitor()) {

			if (this.isInstalled(deployableUnitID)) {

				Thread currentThread = Thread.currentThread();
				ClassLoader currentClassLoader = currentThread
						.getContextClassLoader();

				DeployableUnit deployableUnit = null;
				boolean rollback = true;
				try {
					// start transaction
					sleeTransactionManager.begin();
					// get du
					deployableUnit = deployableUnitManagement
							.getDeployableUnit(deployableUnitID);

					// Check if its safe to remove the deployable unit.
					for (SleeComponent sleeComponent : deployableUnit
							.getDeployableUnitComponents()) {
						for (SleeComponent referringComponent : sleeContainer
								.getComponentRepository()
								.getReferringComponents(sleeComponent)) {
							if (!referringComponent.getDeployableUnit()
									.getDeployableUnitID().equals(
											deployableUnitID)) {
								throw new DependencyException("Component "
										+ referringComponent
										+ " refers DU component "
										+ sleeComponent);
							}
						}
					}

					for (ServiceComponent component : deployableUnit
							.getServiceComponents().values()) {
						serviceManagement.uninstallService(component);
						componentRepositoryImpl.removeComponent(component
								.getServiceID());
						logger.info("Uninstalled " + component);
					}

					for (SbbComponent component : deployableUnit
							.getSbbComponents().values()) {
						currentThread.setContextClassLoader(component
								.getClassLoader());
						removeSecurityPermissions(component, false);
						sleeContainer.getSbbManagement()
								.uninstallSbb(component);
						componentRepositoryImpl.removeComponent(component
								.getSbbID());
						logger.info("Uninstalled " + component);
					}

					for (ResourceAdaptorComponent component : deployableUnit
							.getResourceAdaptorComponents().values()) {
						removeSecurityPermissions(component, false);
						resourceManagement.uninstallResourceAdaptor(component);
						componentRepositoryImpl.removeComponent(component
								.getResourceAdaptorID());
						logger.info("Uninstalled " + component);
					}

					for (ProfileSpecificationComponent component : deployableUnit
							.getProfileSpecificationComponents().values()) {
						currentThread.setContextClassLoader(component
								.getClassLoader());
						removeSecurityPermissions(component, false);
						sleeContainer.getSleeProfileTableManager()
								.uninstallProfileSpecification(component);
						componentRepositoryImpl.removeComponent(component
								.getProfileSpecificationID());
						logger.info("Uninstalled " + component);
					}

					for (ResourceAdaptorTypeComponent component : deployableUnit
							.getResourceAdaptorTypeComponents().values()) {
						resourceManagement
								.uninstallResourceAdaptorType(component);
						componentRepositoryImpl.removeComponent(component
								.getResourceAdaptorTypeID());
						logger.info("Uninstalled " + component);
					}
					
					for (EventTypeID componentID : deployableUnit
							.getEventTypeComponents().keySet()) {
						componentRepositoryImpl.removeComponent(componentID);
						logger.info("Uninstalled " + componentID);
					}
					
					for (LibraryID componentID : deployableUnit
							.getLibraryComponents().keySet()) {
						removeSecurityPermissions(componentRepositoryImpl.getComponentByID(componentID), false);
						componentRepositoryImpl.removeComponent(componentID);
						logger.info("Uninstalled " + componentID);
					}
					removeSecurityPermissions(null,true);
					// remove du
					deployableUnitManagement
							.removeDeployableUnit(deployableUnitID);

					rollback = false;

					logger.info("Uninstalled " +deployableUnitID);

				} catch (InvalidStateException ex) {
					logger.error(ex.getMessage(), ex);
					throw ex;
				} catch (DependencyException ex) {
					logger.error(ex.getMessage(), ex);
					throw ex;
				} catch (Throwable ex) {
					if (logger.isDebugEnabled())
						logger.debug(ex.getMessage(),ex);
					throw new ManagementException(
							"Exception removing deployable Unit ", ex);
				} finally {

					currentThread.setContextClassLoader(currentClassLoader);

					try {
						if (rollback) {
							if (deployableUnit != null) {	
								// put all components in the repo again
								for (LibraryComponent component : deployableUnit
										.getLibraryComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
										logger.info("Reinstalled " + component
												+ " due to tx rollback");
									}
								}
								for (EventTypeComponent component : deployableUnit
										.getEventTypeComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ResourceAdaptorTypeComponent component : deployableUnit
										.getResourceAdaptorTypeComponents()
										.values()) {
									if (componentRepositoryImpl.putComponent(component)) {
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ProfileSpecificationComponent component : deployableUnit
										.getProfileSpecificationComponents()
										.values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ResourceAdaptorComponent component : deployableUnit
										.getResourceAdaptorComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (SbbComponent component : deployableUnit
										.getSbbComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ServiceComponent component : deployableUnit
										.getServiceComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								updateSecurityPermissions(null, true);
								deployableUnitManagement
								.addDeployableUnit(deployableUnit);
							}
							sleeTransactionManager.rollback();
						} else {
							sleeTransactionManager.commit();

							// FIXME: For JBoss 7.2.0.Final:
							// we have a problem with org.hibernate.service.UnknownServiceException:
							// Unknown service requested [org.hibernate.event.service.spi.EventListenerRegistry]
							// see https://hibernate.atlassian.net/browse/HHH-8586
							for (ProfileSpecificationComponent component : deployableUnit
									.getProfileSpecificationComponents().values()) {
								currentThread.setContextClassLoader(component
										.getClassLoader());
								removeSecurityPermissions(component, false);
								sleeContainer.getSleeProfileTableManager()
										.closeEntityManagerFactory(component);
								logger.info("Finalized " + component);
							}

							// Clean up all the class files.
							deployableUnit.undeploy();
						}
					} catch (Throwable ex) {
						throw new ManagementException(
								"Exception while completing transaction", ex);
					}

				}
			} else {
				throw new UnrecognizedDeployableUnitException(
						"deployable unit " + deployableUnitID);
			}
		}
	}
public void uninstall(DeployableUnitID deployableUnitID)
			throws NullPointerException, UnrecognizedDeployableUnitException,
			DependencyException, InvalidStateException, ManagementException {

		logger.info("Uninstalling " +deployableUnitID);

		final SleeContainer sleeContainer = getSleeContainer();
		final SleeTransactionManager sleeTransactionManager = sleeContainer
				.getTransactionManager();
		final ServiceManagement serviceManagement = sleeContainer
				.getServiceManagement();
		final ResourceManagement resourceManagement = sleeContainer
				.getResourceManagement();
		final DeployableUnitManagement deployableUnitManagement = sleeContainer
				.getDeployableUnitManagement();
		final ComponentRepository componentRepositoryImpl = sleeContainer
				.getComponentRepository();

		// we sync on the container's monitor object
		synchronized (sleeContainer.getManagementMonitor()) {

			if (this.isInstalled(deployableUnitID)) {

				Thread currentThread = Thread.currentThread();
				ClassLoader currentClassLoader = currentThread
						.getContextClassLoader();

				DeployableUnit deployableUnit = null;
				boolean rollback = true;
				try {
					// start transaction
					sleeTransactionManager.begin();
					// get du
					deployableUnit = deployableUnitManagement
							.getDeployableUnit(deployableUnitID);

					// Check if its safe to remove the deployable unit.
					for (SleeComponent sleeComponent : deployableUnit
							.getDeployableUnitComponents()) {
						for (SleeComponent referringComponent : sleeContainer
								.getComponentRepository()
								.getReferringComponents(sleeComponent)) {
							if (!referringComponent.getDeployableUnit()
									.getDeployableUnitID().equals(
											deployableUnitID)) {
								throw new DependencyException("Component "
										+ referringComponent
										+ " refers DU component "
										+ sleeComponent);
							}
						}
					}

					for (ServiceComponent component : deployableUnit
							.getServiceComponents().values()) {
						serviceManagement.uninstallService(component);
						componentRepositoryImpl.removeComponent(component
								.getServiceID());
						logger.info("Uninstalled " + component);
					}

					for (SbbComponent component : deployableUnit
							.getSbbComponents().values()) {
						currentThread.setContextClassLoader(component
								.getClassLoader());
						removeSecurityPermissions(component, false);
						sleeContainer.getSbbManagement()
								.uninstallSbb(component);
						componentRepositoryImpl.removeComponent(component
								.getSbbID());
						logger.info("Uninstalled " + component);
					}

					for (ResourceAdaptorComponent component : deployableUnit
							.getResourceAdaptorComponents().values()) {
						removeSecurityPermissions(component, false);
						resourceManagement.uninstallResourceAdaptor(component);
						componentRepositoryImpl.removeComponent(component
								.getResourceAdaptorID());
						logger.info("Uninstalled " + component);
					}

					for (ProfileSpecificationComponent component : deployableUnit
							.getProfileSpecificationComponents().values()) {
						currentThread.setContextClassLoader(component
								.getClassLoader());
						removeSecurityPermissions(component, false);
						sleeContainer.getSleeProfileTableManager()
								.uninstallProfileSpecification(component);
						componentRepositoryImpl.removeComponent(component
								.getProfileSpecificationID());
						logger.info("Uninstalled " + component);
					}

					for (ResourceAdaptorTypeComponent component : deployableUnit
							.getResourceAdaptorTypeComponents().values()) {
						resourceManagement
								.uninstallResourceAdaptorType(component);
						componentRepositoryImpl.removeComponent(component
								.getResourceAdaptorTypeID());
						logger.info("Uninstalled " + component);
					}
					
					for (EventTypeID componentID : deployableUnit
							.getEventTypeComponents().keySet()) {
						componentRepositoryImpl.removeComponent(componentID);
						logger.info("Uninstalled " + componentID);
					}
					
					for (LibraryID componentID : deployableUnit
							.getLibraryComponents().keySet()) {
						removeSecurityPermissions(componentRepositoryImpl.getComponentByID(componentID), false);
						componentRepositoryImpl.removeComponent(componentID);
						logger.info("Uninstalled " + componentID);
					}
					removeSecurityPermissions(null,true);
					// remove du
					deployableUnitManagement
							.removeDeployableUnit(deployableUnitID);

					rollback = false;

					logger.info("Uninstalled " +deployableUnitID);

				} catch (InvalidStateException ex) {
					logger.error(ex.getMessage(), ex);
					throw ex;
				} catch (DependencyException ex) {
					logger.error(ex.getMessage(), ex);
					throw ex;
				} catch (Throwable ex) {
					if (logger.isDebugEnabled())
						logger.debug(ex.getMessage(),ex);
					throw new ManagementException(
							"Exception removing deployable Unit ", ex);
				} finally {

					currentThread.setContextClassLoader(currentClassLoader);

					try {
						if (rollback) {
							if (deployableUnit != null) {	
								// put all components in the repo again
								for (LibraryComponent component : deployableUnit
										.getLibraryComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
										logger.info("Reinstalled " + component
												+ " due to tx rollback");
									}
								}
								for (EventTypeComponent component : deployableUnit
										.getEventTypeComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ResourceAdaptorTypeComponent component : deployableUnit
										.getResourceAdaptorTypeComponents()
										.values()) {
									if (componentRepositoryImpl.putComponent(component)) {
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ProfileSpecificationComponent component : deployableUnit
										.getProfileSpecificationComponents()
										.values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ResourceAdaptorComponent component : deployableUnit
										.getResourceAdaptorComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (SbbComponent component : deployableUnit
										.getSbbComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
										updateSecurityPermissions(component, false);
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								for (ServiceComponent component : deployableUnit
										.getServiceComponents().values()) {
									if (componentRepositoryImpl.putComponent(component)) {
									logger.info("Reinstalled " + component
											+ " due to tx rollback");
									}
								}
								updateSecurityPermissions(null, true);
								deployableUnitManagement
								.addDeployableUnit(deployableUnit);
							}
							sleeTransactionManager.rollback();
						} else {
							sleeTransactionManager.commit();

							// FIXME: For JBoss 7.2.0.Final:
							// we have a problem with org.hibernate.service.UnknownServiceException:
							// Unknown service requested [org.hibernate.event.service.spi.EventListenerRegistry]
							// see https://hibernate.atlassian.net/browse/HHH-8586
							for (ProfileSpecificationComponent component : deployableUnit
									.getProfileSpecificationComponents().values()) {
								currentThread.setContextClassLoader(component
										.getClassLoader());
								removeSecurityPermissions(component, false);
								sleeContainer.getSleeProfileTableManager()
										.closeEntityManagerFactory(component);
								logger.info("Finalized " + component);
							}

							// Clean up all the class files.
							deployableUnit.undeploy();
						}
					} catch (Throwable ex) {
						throw new ManagementException(
								"Exception while completing transaction", ex);
					}

				}
			} else {
				throw new UnrecognizedDeployableUnitException(
						"deployable unit " + deployableUnitID);
			}
		}
	}
/*
(non-Javadoc)

@see javax.slee.management.DeploymentMBean#uninstall(javax.slee.management.DeployableUnitID)