KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > portlet > PortletContainer


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.portlet;
10
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.portlet.Portlet;
17 import javax.portlet.PortletConfig;
18 import javax.portlet.PortletException;
19
20 import org.jboss.portal.portlet.impl.PortletConfigImpl;
21 import org.jboss.portal.portlet.metadata.ParameterMetaData;
22 import org.jboss.portal.portlet.metadata.PortletMetaData;
23 import org.jboss.portal.portlet.metadata.SecurityRoleRefMetaData;
24 import org.jboss.portal.portlet.plugins.language.ResourceBundles;
25 import org.jboss.portal.server.Component;
26 import org.jboss.portal.server.metadata.ServerObjectMetaData;
27
28 /**
29  * A portlet component.
30  *
31  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
32  * @version $Revision: 1.6 $
33  */

34 public class PortletContainer
35    extends Component
36 {
37
38    /** The portlet metadata. */
39    protected PortletMetaData portletMD;
40
41    /** The portlet implementation class name. */
42    protected String JavaDoc className;
43
44    /** The portlet config. */
45    protected PortletConfig config;
46
47    /** The portlet instance. */
48    protected Portlet portlet;
49
50    /** Give the correspondance between role name and role link. */
51    protected Map JavaDoc securityRoleRefsMap;
52
53    public PortletContainer(PortletMetaData portletMD)
54    {
55       super(portletMD.getName());
56       this.portletMD = portletMD;
57    }
58
59    public ServerObjectMetaData getMetaData()
60    {
61       return portletMD;
62    }
63
64    public void start() throws Exception JavaDoc, PortletInitializationException
65    {
66       // Configure the parent
67
super.start();
68
69       // Make checks
70
if (portletMD.getClassName() == null)
71       {
72          throw new Exception JavaDoc("No class name specified");
73       }
74       if (portletMD.getClassName() == null)
75       {
76          throw new Exception JavaDoc("No class name specified");
77       }
78
79       // Set class name
80
this.className = portletMD.getClassName();
81
82       // Security role ref
83
Map JavaDoc securityRoleRefsMap = new HashMap JavaDoc();
84       for (Iterator JavaDoc i = portletMD.getSecurityRoleRefs().iterator();i.hasNext();)
85       {
86          SecurityRoleRefMetaData securityRoleRefMD = (SecurityRoleRefMetaData)i.next();
87          securityRoleRefsMap.put(securityRoleRefMD.getRoleName(), securityRoleRefMD.getRoleLink());
88       }
89
90       // Init parameters
91
Map JavaDoc initParameters = new HashMap JavaDoc();
92       for (Iterator JavaDoc i = portletMD.getParameters().values().iterator();i.hasNext();)
93       {
94          ParameterMetaData parameterMD = (ParameterMetaData)i.next();
95          initParameters.put(parameterMD.getName(), parameterMD.getValue());
96       }
97
98       // Portlet config object
99
ResourceBundles resourceBundles = (ResourceBundles)getPlugin(PortletConstants.PLUGIN_LANG);
100       PortletConfig config = new PortletConfigImpl(
101             name,
102             ((PortletApplication)application).getPortletContext(),
103             initParameters,
104             resourceBundles);
105
106       // Parse the expiration cache value
107
Integer JavaDoc expirationCache = null;
108       try
109       {
110          String JavaDoc value = portletMD.getExpirationCache();
111          if (value != null && value.length() > 0)
112          {
113             expirationCache = Integer.valueOf(value);
114          }
115       }
116       catch (NumberFormatException JavaDoc e)
117       {
118          log.error("Invalid expiration cache value " + portletMD.getExpirationCache());
119       }
120
121       // Finally initialize the porlet instance
122
try
123       {
124          log.debug("Loading portlet class " + className);
125          Class JavaDoc portletClass = application.getClassLoader().loadClass(className);
126          log.debug("Creating portlet object " + className);
127          Portlet portlet = (Portlet)portletClass.newInstance();
128          log.debug("Created portlet object " + className);
129          initPortlet(portlet, config);
130          log.debug("Initialized portlet object " + className);
131
132          // We are safe, update state
133
this.portlet = portlet;
134          this.config = config;
135          this.securityRoleRefsMap = Collections.unmodifiableMap(securityRoleRefsMap);
136          this.expirationCache = expirationCache;
137
138          // Let invocation flow in
139
valve.open();
140       }
141       catch (IllegalAccessException JavaDoc e)
142       {
143          throw new PortletInitializationException("Portlet class not accessible " + className, e);
144       }
145       catch (ClassNotFoundException JavaDoc e)
146       {
147          throw new PortletInitializationException("Portlet class not found " + className, e);
148       }
149       catch (InstantiationException JavaDoc e)
150       {
151          throw new PortletInitializationException("Portlet class cannot be instantiated " + className, e);
152       }
153       catch (PortletException e)
154       {
155          throw new PortletInitializationException("The portlet threw a portlet exception during init", e);
156       }
157       catch (RuntimeException JavaDoc e)
158       {
159          throw new PortletInitializationException("The portlet threw a runtime exception during init", e);
160       }
161       catch (Error JavaDoc e)
162       {
163          throw new PortletInitializationException("The portlet threw an error during init", e);
164       }
165    }
166
167    public void stop()
168    {
169       // Wait at most 60 seconds before all invocations are done
170
boolean done = valve.closing(60000);
171       if (!done)
172       {
173          log.warn("The valve is still holding invocations, continue anyway");
174       }
175
176       //
177
valve.closed();
178
179       // Destroy the portlet object
180
destroyPortlet(portlet);
181
182       // Update state
183
className = null;
184       portlet = null;
185       securityRoleRefsMap = null;
186       config = null;
187       expirationCache = null;
188    }
189
190    public Portlet getPortlet()
191    {
192       return portlet;
193    }
194
195    public Map JavaDoc getSecurityRoleRefsMap()
196    {
197       return securityRoleRefsMap;
198    }
199
200    public String JavaDoc getClassName()
201    {
202       return className;
203    }
204
205    public PortletConfig getConfig()
206    {
207       return config;
208    }
209
210    /**
211     * Initialize the portlet.
212     */

213    private void initPortlet(Portlet portlet, PortletConfig config) throws PortletException
214    {
215       ClassLoader JavaDoc oldLoader = Thread.currentThread().getContextClassLoader();
216       try
217       {
218          // Set the war loader for the request
219
ClassLoader JavaDoc newLoader = application.getClassLoader();
220          Thread.currentThread().setContextClassLoader(newLoader);
221          portlet.init(config);
222       }
223       finally
224       {
225          Thread.currentThread().setContextClassLoader(oldLoader);
226       }
227    }
228
229    /**
230     * Destroy the portlet.
231     */

232    private void destroyPortlet(Portlet portlet)
233    {
234       ClassLoader JavaDoc oldLoader = Thread.currentThread().getContextClassLoader();
235       try
236       {
237          // Set the war loader for the request
238
ClassLoader JavaDoc newLoader = application.getClassLoader();
239          Thread.currentThread().setContextClassLoader(newLoader);
240          if (portlet != null)
241          {
242             portlet.destroy();
243          }
244          else
245          {
246             log.debug("Cannot call destroy, portlet was null");
247          }
248       }
249       catch (RuntimeException JavaDoc e)
250       {
251          log.error("The portlet threw a runtime exception", e);
252       }
253       finally
254       {
255          Thread.currentThread().setContextClassLoader(oldLoader);
256       }
257    }
258 }
259
Popular Tags