1 5 package org.exoplatform.container; 6 7 import java.util.List ; 8 import javax.management.MBeanServer ; 9 import javax.management.MBeanServerFactory ; 10 import javax.servlet.ServletContext ; 11 import org.exoplatform.container.configuration.ConfigurationManager; 12 import org.exoplatform.container.configuration.PortalContainerInfo; 13 import org.exoplatform.container.groovy.ExoGroovyComponentAdapter; 14 import org.exoplatform.container.groovy.GroovyManager; 15 import org.exoplatform.container.groovy.GroovyManagerListener; 16 import org.exoplatform.container.groovy.GroovyObject; 17 import org.exoplatform.container.jmx.MX4JComponentAdapterFactory; 18 import org.exoplatform.container.monitor.PortalMonitor; 19 import org.exoplatform.container.util.ContainerUtil; 20 import org.picocontainer.ComponentAdapter; 21 import org.picocontainer.PicoContainer; 22 import org.picocontainer.PicoException; 23 import org.picocontainer.Startable; 24 import org.picocontainer.defaults.DuplicateComponentKeyRegistrationException; 25 32 public class PortalContainer extends ExoContainer { 33 final static public String SESSION_CONTAINER_CONFIG = "session.container.config" ; 34 private static ThreadLocal currentContainer_ = new ThreadLocal () ; 35 36 private MBeanServer mbeanServer ; 37 private ServletContext portalServletContext_ ; 38 private GroovyManager gmanager_ ; 39 private PortalMonitor monitor_ ; 40 private boolean started_ = false ; 41 42 public PortalContainer(PicoContainer parent, ServletContext portalContext) { 43 super(new MX4JComponentAdapterFactory(),parent) ; 44 portalServletContext_ = portalContext ; 45 mbeanServer = MBeanServerFactory.createMBeanServer("portalmx"); 46 registerComponentInstance(ServletContext .class, portalContext) ; 47 monitor_ = new PortalMonitor() ; 48 registerComponentInstance(PortalMonitor.class, monitor_) ; 49 PortalContainerInfo info = new PortalContainerInfo(portalContext.getServletContextName()) ; 50 registerComponentInstance(PortalContainerInfo.class, info); 51 } 52 53 public ServletContext getPortalServletContext() { return portalServletContext_ ; } 54 55 public PortalMonitor getMonitor() { return monitor_ ; } 56 57 public SessionContainer createSessionContainer(String id, String owner) { 58 SessionContainer scontainer = (SessionContainer)this.getComponentInstance(id) ; 59 if(scontainer != null) { 60 unregisterComponent(id) ; 61 scontainer.stop() ; 62 } 63 scontainer = new SessionContainer(this, id, owner) ; 64 ConfigurationManager cService = 65 (ConfigurationManager)this.getComponentInstance(SESSION_CONTAINER_CONFIG); 66 ContainerUtil.populate(scontainer, cService) ; 67 scontainer.setPortalName(portalServletContext_.getServletContextName()) ; 68 registerComponentInstance(id, scontainer) ; 69 scontainer.start() ; 70 SessionContainer.setInstance(scontainer) ; 71 return scontainer ; 72 } 73 74 public void removeSessionContainer(String id) { 75 unregisterComponent(id) ; 76 } 77 78 public java.util.List getLiveSessions() { 79 return getComponentInstancesOfType(SessionContainer.class) ; 80 } 81 82 public MBeanServer getMBeanServer() { return mbeanServer; } 83 84 public GroovyManager getGroovyManager() { return gmanager_ ; } 85 public void setGroovyManager(GroovyManager gmanager) { 86 gmanager_ = gmanager ; 87 gmanager_.removeAllListener() ; 88 gmanager_.addListener(new PortalGroovyObjectListener()) ; 89 } 90 91 private ExoContainer getThisContainer() { return this ; } 92 93 public static PortalContainer getInstance() { 94 PortalContainer container = (PortalContainer) currentContainer_.get() ; 95 if(container == null) { 96 container = RootContainer.getInstance().getPortalContainer("default") ; 97 currentContainer_.set(container) ; 98 } 99 return container ; 100 } 101 102 public boolean isStarted() { return started_ ; } 103 104 public void start() { 105 super.start() ; 106 started_ = true ; 107 } 108 109 public void stop() { 110 gmanager_.setDispose(true) ; 111 super.stop() ; 112 started_ = false ; 113 } 114 115 synchronized public ComponentAdapter getComponentAdapterOfType(Class componentType) { 116 return super.getComponentAdapterOfType(componentType); 117 } 118 119 synchronized public List getComponentAdaptersOfType(Class componentType) { 120 return super.getComponentAdaptersOfType(componentType) ; 121 } 122 123 synchronized public ComponentAdapter unregisterComponent(Object componentKey) { 124 return super.unregisterComponent(componentKey) ; 125 } 126 127 synchronized public ComponentAdapter registerComponent(ComponentAdapter componentAdapter) 128 throws DuplicateComponentKeyRegistrationException 129 { 130 return super.registerComponent(componentAdapter) ; 131 } 132 133 synchronized public List getComponentInstancesOfType(Class componentType) throws PicoException { 134 return super.getComponentInstancesOfType(componentType) ; 135 } 136 137 public static void setInstance(PortalContainer instance) { 138 currentContainer_.set(instance) ; 139 } 140 141 public static Object getComponent(Class key) { 142 PortalContainer pcontainer = (PortalContainer)currentContainer_.get() ; 143 return pcontainer.getComponentInstanceOfType(key) ; 144 } 145 146 public class PortalGroovyObjectListener extends GroovyManagerListener { 147 public void load(GroovyObject gobject) throws Exception { 148 registerComponent(new ExoGroovyComponentAdapter(gobject)) ; 149 } 150 151 public void reload(GroovyObject gobject) throws Exception { 152 registerComponent(new ExoGroovyComponentAdapter(gobject)) ; 153 Class type = gobject.getType() ; 154 if(Startable.class.isAssignableFrom(type)) { 155 ExoGroovyComponentAdapter adapter = 157 (ExoGroovyComponentAdapter)getComponentAdapter(gobject.getType()) ; 158 adapter.getComponentInstance(getThisContainer()) ; 159 } 160 } 161 162 public void unload(GroovyObject gobject) throws Exception { 163 unregisterComponent(gobject.getType()) ; 164 gobject.setObject(null) ; 165 } 166 } 167 } | Popular Tags |