001: /**
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: PortletWebApplicationImpl.java 6407 2008-01-16 15:20:21Z wehrens $
004: */package org.gridsphere.portletcontainer.impl;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.gridsphere.portlet.service.PortletServiceException;
009: import org.gridsphere.portlet.service.spi.PortletServiceFactory;
010: import org.gridsphere.portlet.service.spi.impl.descriptor.PortletServiceCollection;
011: import org.gridsphere.portletcontainer.ApplicationPortlet;
012: import org.gridsphere.portletcontainer.PortletStatus;
013: import org.gridsphere.portletcontainer.PortletWebApplication;
014: import org.gridsphere.portletcontainer.impl.descriptor.*;
015:
016: import javax.portlet.PortletException;
017: import javax.servlet.RequestDispatcher;
018: import javax.servlet.ServletContext;
019: import java.io.File;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Hashtable;
023: import java.util.Map;
024:
025: /**
026: * The <code>PortletWebApplicationImpl</code> is an implementation of a <code>PortletWebApplication</code> that
027: * represents a collection of portlets contained in a packaged WAR file. Currently
028: * under development is the notion of dynamically managing portlet web applications.
029: */
030: public class PortletWebApplicationImpl implements PortletWebApplication {
031:
032: private Log log = LogFactory
033: .getLog(PortletWebApplicationImpl.class);
034: private PortletApp portletWebApp = null;
035:
036: protected Map<String, PortletDefinition> portletDefinitions = new Hashtable<String, PortletDefinition>();
037: protected Map<String, ApplicationPortlet> appPortlets = new Hashtable<String, ApplicationPortlet>();
038:
039: protected String webApplicationName = "Unknown portlet web application";
040: protected String webAppDescription = "Unknown portlet web application description";
041:
042: protected PortletStatus status = PortletStatus.SUCCESS;
043: protected String statusMessage = "Portlet web application loaded successfully";
044:
045: protected RequestDispatcher rd = null;
046:
047: public PortletWebApplicationImpl(ServletContext context,
048: ClassLoader loader) {
049:
050: // Make all jsr portlets have only one concrete instance
051: String realPath = context.getRealPath("");
052: int l = realPath.lastIndexOf(File.separator);
053: webApplicationName = realPath.substring(l + 1);
054:
055: this .webAppDescription = context.getServletContextName();
056: try {
057: // load portlet.xml
058: loadPortlets(context, loader);
059:
060: // load services.xml
061: loadServices(context, loader);
062: } catch (PortletException e) {
063: status = PortletStatus.FAILURE;
064: statusMessage = e.getMessage();
065: }
066: }
067:
068: /**
069: * Loads collection of portlets from portlet descriptor file using the associated <code>ServletContext</code>
070: *
071: * @param ctx the <code>ServletContext</code>
072: * @param loader the classloader of the web application
073: * @throws PortletException if an error occurs loading the portlets
074: */
075: protected void loadPortlets(ServletContext ctx, ClassLoader loader)
076: throws PortletException {
077: // load in the portlet.xml file
078: String portletXMLfile = ctx.getRealPath("/WEB-INF/portlet.xml");
079:
080: PortletDeploymentDescriptor pdd = null;
081: try {
082: pdd = new PortletDeploymentDescriptor(portletXMLfile);
083: } catch (Exception e) {
084: status = PortletStatus.FAILURE;
085: statusMessage = "Unable to load portlets from: "
086: + webApplicationName + " due to mapping error";
087: throw new PortletException(statusMessage, e);
088: }
089:
090: this .portletWebApp = pdd.getPortletWebApplication();
091: // Every SportletDefinition has a PortletApplication and possibly multiple ConcretePortletConfig's
092: PortletDefinition[] portletDefs = pdd
093: .getPortletDefinitionList();
094:
095: // Iterate thru portlet definitions for portlet applications
096: for (int i = 0; i < portletDefs.length; i++) {
097: ApplicationPortlet portletApp = new ApplicationPortletImpl(
098: pdd, portletDefs[i], webApplicationName, ctx);
099:
100: String portletClass = portletApp.getApplicationPortletID();
101: String portletName = portletApp.getApplicationPortletName();
102: portletDefinitions.put(portletName, portletDefs[i]);
103: appPortlets.put(portletName, portletApp);
104:
105: log.debug("sticking " + portletName + " class: "
106: + portletClass + " in hash");
107: }
108:
109: }
110:
111: public PortletDefinition getPortletDefinition(String portletName) {
112: return portletDefinitions.get(portletName);
113: }
114:
115: public void init() {
116: }
117:
118: public void destroy() {
119: portletWebApp = null;
120: appPortlets = null;
121: portletDefinitions = null;
122: rd = null;
123: }
124:
125: public CustomPortletMode[] getCustomPortletModes() {
126: return portletWebApp.getCustomPortletMode();
127: }
128:
129: public CustomWindowState[] getCustomWindowStates() {
130: return portletWebApp.getCustomWindowState();
131: }
132:
133: public UserAttribute[] getUserAttributes() {
134: return portletWebApp.getUserAttribute();
135: }
136:
137: public SecurityConstraint[] getSecurityConstraints() {
138: return portletWebApp.getSecurityConstraint();
139: }
140:
141: /**
142: * Loads in a service descriptor file from the associated servlet context
143: *
144: * @param ctx the <code>ServletContext</code>
145: * @param loader the classloader of the web application
146: * @throws PortletException if an error occurs loading the portlets
147: */
148: protected void loadServices(ServletContext ctx, ClassLoader loader)
149: throws PortletException {
150: // load in the portlet-services.xml file
151: String descriptorPath = ctx
152: .getRealPath("/WEB-INF/PortletServices.xml");
153: File f = new File(descriptorPath);
154: if (f.exists()) {
155: PortletServiceDescriptor descriptor = null;
156: try {
157: System.err.println("loading from: " + descriptorPath);
158: descriptor = new PortletServiceDescriptor(
159: descriptorPath);
160: } catch (Exception e) {
161: //log.error("error unmarshalling " + servicesPath + " using " + servicesMappingPath + " : " + e.getMessage());
162: status = PortletStatus.FAILURE;
163: statusMessage = "Error unmarshalling " + descriptorPath;
164: throw new PortletServiceException(statusMessage, e);
165: }
166: PortletServiceCollection serviceCollection = descriptor
167: .getServiceCollection();
168: PortletServiceFactory.addServices(webApplicationName, ctx,
169: serviceCollection, loader);
170: } else {
171: descriptorPath = ctx
172: .getRealPath("/WEB-INF/portlet-services");
173: f = new File(descriptorPath);
174: if (f.exists()) {
175: String[] servicePaths = f.list();
176: for (int i = 0; i < servicePaths.length; i++) {
177: servicePaths[i] = descriptorPath + File.separator
178: + servicePaths[i];
179: }
180: for (int i = 0; i < servicePaths.length; i++) {
181: PortletServiceDescriptor descriptor = null;
182: try {
183: System.err.println("loading from: "
184: + servicePaths[i]);
185: descriptor = new PortletServiceDescriptor(
186: servicePaths[i]);
187: } catch (Exception e) {
188: status = PortletStatus.FAILURE;
189: statusMessage = "Error unmarshalling "
190: + servicePaths[i];
191: throw new PortletServiceException(
192: "error unmarshalling "
193: + servicePaths[i], e);
194: }
195: PortletServiceCollection serviceCollection = descriptor
196: .getServiceCollection();
197: PortletServiceFactory.addServices(
198: webApplicationName, ctx, serviceCollection,
199: loader);
200: }
201: } else {
202: log
203: .debug("Did not find PortletServices.xml or portlet-services directory for: "
204: + ctx.getServletContextName());
205: }
206: }
207: String loadSpring = ctx.getInitParameter("spring4gridsphere");
208: if (loadSpring != null && loadSpring.equalsIgnoreCase("true")) {
209: PortletServiceFactory.addSpringServices(ctx);
210: }
211: }
212:
213: /**
214: * Returns the portlet web application name
215: *
216: * @return the portlet web application name
217: */
218: public String getWebApplicationName() {
219: return webApplicationName;
220: }
221:
222: /**
223: * Returns the portlet web application description
224: *
225: * @return the portlet web application description
226: */
227: public String getWebApplicationDescription() {
228: return webAppDescription;
229: }
230:
231: /**
232: * Returns the collection of application portlets contained by this portlet web application
233: *
234: * @return the collection of application portlets
235: */
236: public Collection<ApplicationPortlet> getAllApplicationPortlets() {
237: return (appPortlets != null ? appPortlets.values()
238: : new ArrayList<ApplicationPortlet>());
239: }
240:
241: public void setWebApplicationStatus(PortletStatus status) {
242: this .status = status;
243: }
244:
245: public void setWebApplicationStatusMessage(String statusMessage) {
246: this .statusMessage = statusMessage;
247: }
248:
249: public PortletStatus getWebApplicationStatus() {
250: return status;
251: }
252:
253: public String getWebApplicationStatusMessage() {
254: return statusMessage;
255: }
256: }
|