1 5 package org.exoplatform.container; 6 7 import java.net.URL ; 8 import java.util.List ; 9 import javax.management.MBeanServer ; 10 import javax.management.MBeanServerFactory ; 11 import javax.servlet.ServletContext ; 12 import org.exoplatform.container.configuration.ConfigurationManager; 13 import org.exoplatform.container.configuration.ConfigurationManagerImpl; 14 import org.exoplatform.container.configuration.MockConfigurationManagerImpl; 15 import org.exoplatform.container.groovy.GroovyManager; 16 import org.exoplatform.container.groovy.GroovyManagerContainer; 17 import org.exoplatform.container.jmx.MX4JComponentAdapterFactory; 18 import org.exoplatform.container.monitor.jvm.OperatingSystemInfo; 19 import org.exoplatform.container.monitor.jvm.J2EEServerInfo; 20 import org.exoplatform.container.util.ContainerUtil; 21 import org.exoplatform.test.mocks.servlet.MockServletContext; 22 29 public class RootContainer extends ExoContainer { 30 private static RootContainer singleton_ ; 31 private static boolean jdk15_ ; 32 33 private MBeanServer mbeanServer_ ; 34 private OperatingSystemInfo osenv_ ; 35 private J2EEServerInfo serverenv_ ; 36 37 public RootContainer() { 38 super(new MX4JComponentAdapterFactory(), null) ; 39 mbeanServer_ = MBeanServerFactory.createMBeanServer("exomx"); 40 Runtime.getRuntime().addShutdownHook(new ShutdownThread(this)) ; 41 } 42 43 public OperatingSystemInfo getOSEnvironment() { 44 if( osenv_ == null) { 45 osenv_ = (OperatingSystemInfo) this.getComponentInstanceOfType(OperatingSystemInfo.class) ; 46 } 47 return osenv_ ; 48 } 49 50 public J2EEServerInfo getServerEnvironment() { 51 if(serverenv_ == null) { 52 serverenv_ = (J2EEServerInfo) this.getComponentInstanceOfType(J2EEServerInfo.class) ; 53 } 54 return serverenv_ ; 55 } 56 57 public PortalContainer getPortalContainer(String name) { 58 PortalContainer pcontainer = (PortalContainer)this.getComponentInstance(name) ; 59 if(pcontainer == null) { 60 J2EEServerInfo senv = getServerEnvironment() ; 61 if("standalone".equals(senv.getServerName())) { 62 try { 63 MockServletContext scontext = new MockServletContext(name) ; 64 pcontainer = new PortalContainer(this, scontext) ; 65 ConfigurationManagerImpl cService = new MockConfigurationManagerImpl(scontext) ; 66 cService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/configuration.xml")) ; 67 cService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/test-configuration.xml")) ; 68 cService.processRemoveConfiguration() ; 69 pcontainer.registerComponentInstance(ConfigurationManager.class, cService) ; 70 71 ConfigurationManagerImpl scService = new MockConfigurationManagerImpl(scontext) ; 72 scService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/session/configuration.xml")) ; 73 scService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/session/test-configuration.xml")) ; 74 scService.processRemoveConfiguration() ; 75 pcontainer.registerComponentInstance(PortalContainer.SESSION_CONTAINER_CONFIG, scService) ; 76 77 GroovyManagerContainer gcontainer = 78 (GroovyManagerContainer)getComponentInstanceOfType(GroovyManagerContainer.class) ; 79 URL url = new URL ("file:" + getServerEnvironment().getServerHome() + "/web/portal/src/webapp/WEB-INF/groovy/") ; 80 GroovyManager gmanager = gcontainer.getGroovyManager(url) ; 81 pcontainer.setGroovyManager(gmanager) ; 82 registerComponentInstance(name, pcontainer) ; 83 ContainerUtil.populateGroovy(pcontainer, cService) ; 84 ContainerUtil.populate(pcontainer, cService) ; 85 pcontainer.start() ; 86 } catch(Exception ex) { 87 ex.printStackTrace() ; 88 } 89 } 90 } 91 return pcontainer ; 92 } 93 94 synchronized public PortalContainer createPortalContainer(ServletContext context) { 95 try { 96 PortalContainer pcontainer = new PortalContainer(this, context) ; 97 ConfigurationManagerImpl cService = new ConfigurationManagerImpl(context) ; 98 cService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/configuration.xml")) ; 99 try { 100 cService.addConfiguration("war:/conf/configuration.xml") ; 101 } catch(Exception ex){} 102 cService.processRemoveConfiguration() ; 103 pcontainer.registerComponentInstance(ConfigurationManager.class, cService) ; 104 105 ConfigurationManagerImpl scService = new ConfigurationManagerImpl(context) ; 106 scService.addConfiguration(ContainerUtil.getConfigurationURL("conf/portal/session/configuration.xml")) ; 107 scService.processRemoveConfiguration() ; 108 pcontainer.registerComponentInstance(PortalContainer.SESSION_CONTAINER_CONFIG, scService) ; 109 110 GroovyManagerContainer gcontainer = 111 (GroovyManagerContainer)getComponentInstanceOfType(GroovyManagerContainer.class) ; 112 URL url = new URL ("file:" + context.getRealPath("/WEB-INF/groovy") + "/"); 113 GroovyManager gmanager = gcontainer.getGroovyManager(url) ; 114 pcontainer.setGroovyManager(gmanager) ; 115 registerComponentInstance(context.getServletContextName(), pcontainer) ; 116 PortalContainer.setInstance(pcontainer) ; 117 ContainerUtil.populate(pcontainer, cService) ; 119 pcontainer.start() ; 120 return pcontainer ; 121 } catch (Exception ex) { 122 ex.printStackTrace() ; 123 } 124 return null ; 125 } 126 127 synchronized public void removePortalContainer(ServletContext servletContext) { 128 this.unregisterComponent(servletContext.getServletContextName()) ; 129 } 130 131 public MBeanServer getMBeanServer() { return mbeanServer_; } 132 133 public static Object getComponent(Class key) { 134 return getInstance().getComponentInstanceOfType(key) ; 135 } 136 137 138 public static RootContainer getInstance() { 139 if(singleton_ == null) { 140 synchronized(RootContainer.class) { 141 if(singleton_ == null) { 142 try { 143 Class.forName("java.lang.management.ManagementFactory"); 144 jdk15_ = true ; 145 } catch (Exception ex) { 146 jdk15_ = false ; 147 } 148 try { 149 singleton_ = new RootContainer() ; 150 ConfigurationManagerImpl service = new ConfigurationManagerImpl(null) ; 151 service.addConfiguration(ContainerUtil.getConfigurationURL("conf/configuration.xml")) ; 152 if(jdk15_) { 153 service.addConfiguration(ContainerUtil.getConfigurationURL("conf/configuration-jvm15.xml")) ; 154 } 155 if(System.getProperty("maven.exoplatform.dir") != null) { 156 service.addConfiguration(ContainerUtil.getConfigurationURL("conf/test-configuration.xml")) ; 157 } 158 service.processRemoveConfiguration() ; 159 singleton_.registerComponentInstance(ConfigurationManager.class, service) ; 160 ContainerUtil.populate(singleton_, service) ; 161 List initializers = 162 singleton_.getComponentInstancesOfType(RootContainerInitializer.class); 163 for(int i = 0; i < initializers.size(); i++) { 164 RootContainerInitializer initializer = (RootContainerInitializer) initializers.get(i) ; 165 initializer.initialize(singleton_) ; 166 } 167 singleton_.start() ; 168 } catch (Throwable ex) { ex.printStackTrace() ;} 169 } 170 } 171 } 172 return singleton_ ; 173 } 174 175 static public boolean isJDK15() { 176 try { 177 178 } catch (Exception ex) { 179 180 } 181 return false ; 182 } 183 184 static class ShutdownThread extends Thread { 185 RootContainer container_ ; 186 ShutdownThread(RootContainer container) { 187 container_ = container ; 188 } 189 190 public void run() { 191 container_.stop() ; 192 } 193 } 194 } | Popular Tags |