001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.container;
018:
019: import java.io.InputStream;
020: import java.util.Collection;
021: import java.util.Enumeration;
022: import java.util.Iterator;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.RequestDispatcher;
026:
027: import javax.portlet.PortletContext;
028: import javax.portlet.PortletRequestDispatcher;
029:
030: import org.apache.jetspeed.dispatcher.JetspeedRequestDispatcher;
031: import org.apache.jetspeed.om.common.JetspeedServiceReference;
032: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
033: import org.apache.jetspeed.services.JetspeedPortletServices;
034: import org.apache.jetspeed.services.PortletServices;
035: import org.apache.pluto.om.portlet.PortletApplicationDefinition;
036:
037: /**
038: * Implements the Portlet API Portlet Context class
039: *
040: * TODO: on LOCAL apps, we need to merge in web.xml props. See PLT 10.3
041: *
042: * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
043: * @version $Id: JetspeedPortletContext.java 516448 2007-03-09 16:25:47Z ate $
044: */
045: public class JetspeedPortletContext implements PortletContext,
046: InternalPortletContext {
047: /**
048: * The path to the Local Portlet Apps directory
049: */
050: public static final String LOCAL_PA_ROOT = "/WEB-INF/apps";
051:
052: private ServletContext servletContext;
053: private MutablePortletApplication application;
054:
055: public JetspeedPortletContext(ServletContext servletContext,
056: PortletApplicationDefinition application) {
057: this .servletContext = servletContext;
058: this .application = (MutablePortletApplication) application;
059: }
060:
061: public int getMajorVersion() {
062: return ContainerInfo.getMajorSpecificationVersion();
063: }
064:
065: public int getMinorVersion() {
066: return ContainerInfo.getMinorSpecificationVersion();
067: }
068:
069: // Delegated methods
070:
071: public java.util.Set getResourcePaths(String path) {
072: return servletContext.getResourcePaths(path);
073: }
074:
075: public javax.portlet.PortletRequestDispatcher getRequestDispatcher(
076: String path) {
077: String localizedPath = localizePath(path, this .application);
078: RequestDispatcher rd = null;
079:
080: try {
081: rd = servletContext.getRequestDispatcher(localizedPath);
082: } catch (Exception e) {
083: // Portlet API says: return null
084: }
085:
086: // TODO: factory
087: if (rd != null) {
088: return new JetspeedRequestDispatcher(rd);
089: }
090: return null;
091: }
092:
093: public PortletRequestDispatcher getNamedDispatcher(String name) {
094: // TODO: localize name
095:
096: RequestDispatcher rd = null;
097:
098: try {
099: rd = servletContext.getNamedDispatcher(name);
100: } catch (Exception e) {
101: // Portlet API says: return null
102: }
103:
104: // TODO: factory
105:
106: if (rd != null) {
107: return new JetspeedRequestDispatcher(rd);
108: }
109: return null;
110: }
111:
112: public String getMimeType(String file) {
113: return servletContext.getMimeType(file);
114: }
115:
116: public InputStream getResourceAsStream(String path) {
117: return servletContext.getResourceAsStream(localizePath(path,
118: this .application));
119: }
120:
121: public java.lang.Object getAttribute(java.lang.String name) {
122: if (name == null) {
123: throw new IllegalArgumentException(
124: "Required parameter name is null");
125: }
126:
127: if (name.startsWith("cps:")) {
128: String serviceName = name.substring("cps:".length());
129:
130: // validate service
131: Collection validServices = application
132: .getJetspeedServices();
133: if (null == validServices) {
134: return null;
135: }
136: boolean found = false;
137: Iterator iterator = validServices.iterator();
138: while (iterator.hasNext()) {
139: JetspeedServiceReference validService = (JetspeedServiceReference) iterator
140: .next();
141: if (validService.getName().equals(serviceName)) {
142: found = true;
143: break;
144: }
145: }
146:
147: if (!found) {
148: return null;
149: }
150:
151: // return the service
152: PortletServices services = JetspeedPortletServices
153: .getSingleton();
154: return services.getService(serviceName);
155: }
156: return servletContext.getAttribute(name);
157: }
158:
159: public void log(java.lang.String msg) {
160: // TODO: setup a logger for the portlet application
161: servletContext.log(msg);
162: }
163:
164: public void log(java.lang.String message,
165: java.lang.Throwable throwable) {
166: // TODO: setup a logger for the portlet application
167: servletContext.log(message, throwable);
168: }
169:
170: public String getRealPath(String path) {
171: return servletContext.getRealPath(localizePath(path,
172: this .application));
173: }
174:
175: public java.net.URL getResource(String path)
176: throws java.net.MalformedURLException {
177: return servletContext.getResource(localizePath(path,
178: this .application));
179: }
180:
181: public Enumeration getAttributeNames() {
182: return servletContext.getAttributeNames();
183: }
184:
185: public java.lang.String getInitParameter(java.lang.String name) {
186: if (name == null) {
187: throw new IllegalArgumentException(
188: "Required parameter name is null");
189: }
190: return servletContext.getInitParameter(name);
191: }
192:
193: public java.util.Enumeration getInitParameterNames() {
194: return servletContext.getInitParameterNames();
195: }
196:
197: public void removeAttribute(java.lang.String name) {
198: if (name == null) {
199: throw new IllegalArgumentException("Attribute name == null");
200: }
201:
202: servletContext.removeAttribute(name);
203: }
204:
205: public void setAttribute(java.lang.String name,
206: java.lang.Object object) {
207: if (name == null) {
208: throw new IllegalArgumentException("Attribute name == null");
209: }
210: servletContext.setAttribute(name, object);
211: }
212:
213: public String getServerInfo() {
214: return ContainerInfo.getServerInfo();
215: }
216:
217: // internal portlet context implementation
218:
219: public ServletContext getServletContext() {
220: return this .servletContext;
221: }
222:
223: /**
224: * @see org.apache.jetspeed.container.InternalPortletContext#getApplication()
225: */
226: public PortletApplicationDefinition getApplication() {
227: return this .application;
228: }
229:
230: public String getPortletContextName() {
231: return servletContext.getServletContextName();
232: }
233:
234: private String localizePath(String path,
235: MutablePortletApplication app) {
236: if (path == null) {
237: return "/";
238: }
239: return path;
240: // TODO: local PA with own/extra resource paths support
241: /*
242: if (app.getApplicationType() == MutablePortletApplication.WEBAPP)
243: {
244: return path;
245: }
246:
247: StringBuffer pathBuffer = new StringBuffer(LOCAL_PA_ROOT);
248: pathBuffer.append(app.getWebApplicationDefinition().getContextRoot());
249: if (!path.startsWith("/"))
250: {
251: pathBuffer.append("/");
252: }
253: pathBuffer.append(path);
254: String result = pathBuffer.toString();
255: return result;
256: */
257: }
258: }
|