KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > container > PortalContainer


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.container;
6
7 import java.util.List JavaDoc;
8 import javax.management.MBeanServer JavaDoc;
9 import javax.management.MBeanServerFactory JavaDoc;
10 import javax.servlet.ServletContext JavaDoc;
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 /**
26  * Created by The eXo Platform SARL
27  * Author : Mestrallet Benjamin
28  * benjmestrallet@users.sourceforge.net
29  * Date: Jul 31, 2003
30  * Time: 12:15:28 AM
31  */

32 public class PortalContainer extends ExoContainer {
33   final static public String JavaDoc SESSION_CONTAINER_CONFIG = "session.container.config" ;
34   private static ThreadLocal JavaDoc currentContainer_ = new ThreadLocal JavaDoc() ;
35   
36   private MBeanServer JavaDoc mbeanServer ;
37   private ServletContext JavaDoc portalServletContext_ ;
38   private GroovyManager gmanager_ ;
39   private PortalMonitor monitor_ ;
40   private boolean started_ = false ;
41   
42   public PortalContainer(PicoContainer parent, ServletContext JavaDoc portalContext) {
43     super(new MX4JComponentAdapterFactory(),parent) ;
44     portalServletContext_ = portalContext ;
45     mbeanServer = MBeanServerFactory.createMBeanServer("portalmx");
46     registerComponentInstance(ServletContext JavaDoc.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 JavaDoc getPortalServletContext() { return portalServletContext_ ; }
54   
55   public PortalMonitor getMonitor() { return monitor_ ; }
56   
57   public SessionContainer createSessionContainer(String JavaDoc id, String JavaDoc 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 JavaDoc id) {
75     unregisterComponent(id) ;
76   }
77   
78   public java.util.List JavaDoc getLiveSessions() {
79     return getComponentInstancesOfType(SessionContainer.class) ;
80   }
81   
82   public MBeanServer JavaDoc 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 JavaDoc componentType) {
116     return super.getComponentAdapterOfType(componentType);
117   }
118   
119   synchronized public List JavaDoc getComponentAdaptersOfType(Class JavaDoc componentType) {
120     return super.getComponentAdaptersOfType(componentType) ;
121   }
122   
123   synchronized public ComponentAdapter unregisterComponent(Object JavaDoc 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 JavaDoc getComponentInstancesOfType(Class JavaDoc 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 JavaDoc getComponent(Class JavaDoc 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 JavaDoc {
148       registerComponent(new ExoGroovyComponentAdapter(gobject)) ;
149     }
150     
151     public void reload(GroovyObject gobject) throws Exception JavaDoc {
152       registerComponent(new ExoGroovyComponentAdapter(gobject)) ;
153       Class JavaDoc type = gobject.getType() ;
154       if(Startable.class.isAssignableFrom(type)) {
155         //look for the component to force object start
156
ExoGroovyComponentAdapter adapter =
157           (ExoGroovyComponentAdapter)getComponentAdapter(gobject.getType()) ;
158         adapter.getComponentInstance(getThisContainer()) ;
159       }
160     }
161     
162     public void unload(GroovyObject gobject) throws Exception JavaDoc {
163       unregisterComponent(gobject.getType()) ;
164       gobject.setObject(null) ;
165     }
166   }
167 }
Popular Tags