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.pluto.internal.impl;
018:
019: import java.io.InputStream;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.Enumeration;
023: import java.util.Set;
024:
025: import javax.portlet.PortletContext;
026: import javax.portlet.PortletRequestDispatcher;
027: import javax.servlet.RequestDispatcher;
028: import javax.servlet.ServletContext;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.pluto.descriptors.portlet.PortletAppDD;
033: import org.apache.pluto.internal.InternalPortletContext;
034:
035: /**
036: * Pluto's Portlet Context Implementation. This class implements the
037: * <code>InternalPortletContext</code> which provides container specific
038: * information needed for processing.
039: *
040: * @version 1.1
041: */
042: public class PortletContextImpl implements PortletContext,
043: InternalPortletContext {
044:
045: /**
046: * Logger.
047: */
048: private static final Log LOG = LogFactory
049: .getLog(PortletContextImpl.class);
050:
051: // Private Member Variables ------------------------------------------------
052:
053: private final String applicationId;
054: private String applicationName;
055:
056: private final PortletAppDD portletAppDD;
057: private final ServletContext servletContext;
058: private ClassLoader contextClassLoader;
059:
060: // Constructor -------------------------------------------------------------
061:
062: /**
063: * Constructs an instance.
064: *
065: * @param servletContext the servlet context in which we are contained.
066: * @param portletAppDD the portlet application descriptor.
067: * @param portletApplicationId applicationId
068: */
069: public PortletContextImpl(String portletApplicationId,
070: ServletContext servletContext, PortletAppDD portletAppDD) {
071: this .servletContext = servletContext;
072: this .portletAppDD = portletAppDD;
073: this .applicationId = portletApplicationId;
074: this .applicationName = servletContext.getServletContextName();
075:
076: if (applicationName == null) {
077: applicationName = applicationId;
078: }
079: init();
080: }
081:
082: private void init() {
083: setContextClassLoader(Thread.currentThread()
084: .getContextClassLoader());
085: }
086:
087: public String getApplicationId() {
088: return applicationId;
089: }
090:
091: public String getApplicationName() {
092: return applicationName;
093: }
094:
095: /**
096: * ClassLoader associated with this context.
097: * @return
098: */
099: public ClassLoader getContextClassLoader() {
100: return contextClassLoader;
101: }
102:
103: /**
104: * ClassLoader associated with this context.
105: * @param contextClassLoader
106: */
107: public void setContextClassLoader(ClassLoader contextClassLoader) {
108: this .contextClassLoader = contextClassLoader;
109: }
110:
111: // PortletContext Impl -----------------------------------------------------
112:
113: /**
114: * Retrieve the PortletContainer's server info.
115: *
116: * @return the server info in the form of <i>Server/Version</i>
117: * @see Environment#getServerInfo()
118: */
119: public String getServerInfo() {
120: return Environment.getServerInfo();
121: }
122:
123: public PortletRequestDispatcher getRequestDispatcher(String path) {
124:
125: if (LOG.isDebugEnabled()) {
126: LOG.debug("PortletRequestDispatcher requested: " + path);
127: }
128:
129: // Check if the path name is valid. A valid path name must not be null
130: // and must start with a slash '/' as defined by the portlet spec.
131: if (path == null || !path.startsWith("/")) {
132: if (LOG.isInfoEnabled()) {
133: LOG
134: .info("Failed to retrieve PortletRequestDispatcher: "
135: + "path name must begin with a slash '/'.");
136: }
137: return null;
138: }
139:
140: // Extract query string which contains appended parameters.
141: String queryString = null;
142: int index = path.indexOf("?");
143: if (index > 0 && index < path.length() - 1) {
144: queryString = path.substring(index + 1);
145: }
146:
147: // Construct PortletRequestDispatcher.
148: PortletRequestDispatcher portletRequestDispatcher = null;
149: try {
150: RequestDispatcher servletRequestDispatcher = servletContext
151: .getRequestDispatcher(path);
152: if (servletRequestDispatcher != null) {
153: portletRequestDispatcher = new PortletRequestDispatcherImpl(
154: servletRequestDispatcher, queryString);
155: } else {
156: if (LOG.isInfoEnabled()) {
157: LOG
158: .info("No matching request dispatcher found for: "
159: + path);
160: }
161: }
162: } catch (Exception ex) {
163: // We need to catch exception because of a Tomcat 4.x bug.
164: // Tomcat throws an exception instead of return null if the path
165: // was not found.
166: if (LOG.isInfoEnabled()) {
167: LOG
168: .info("Failed to retrieve PortletRequestDispatcher: "
169: + ex.getMessage());
170: }
171: portletRequestDispatcher = null;
172: }
173: return portletRequestDispatcher;
174: }
175:
176: public PortletRequestDispatcher getNamedDispatcher(String name) {
177: RequestDispatcher dispatcher = servletContext
178: .getNamedDispatcher(name);
179: if (dispatcher != null) {
180: return new PortletRequestDispatcherImpl(dispatcher);
181: } else {
182: if (LOG.isInfoEnabled()) {
183: LOG
184: .info("No matching request dispatcher found for name: "
185: + name);
186: }
187: }
188: return null;
189: }
190:
191: public InputStream getResourceAsStream(String path) {
192: return servletContext.getResourceAsStream(path);
193: }
194:
195: public int getMajorVersion() {
196: return Environment.getMajorSpecificationVersion();
197: }
198:
199: public int getMinorVersion() {
200: return Environment.getMinorSpecificationVersion();
201: }
202:
203: public String getMimeType(String file) {
204: return servletContext.getMimeType(file);
205: }
206:
207: public String getRealPath(String path) {
208: return servletContext.getRealPath(path);
209: }
210:
211: public Set getResourcePaths(String path) {
212: return servletContext.getResourcePaths(path);
213: }
214:
215: public URL getResource(String path)
216: throws java.net.MalformedURLException {
217: if (path == null || !path.startsWith("/")) {
218: throw new MalformedURLException(
219: "path must start with a '/'");
220: }
221: return servletContext.getResource(path);
222: }
223:
224: public Object getAttribute(java.lang.String name) {
225: if (name == null) {
226: throw new IllegalArgumentException("Attribute name == null");
227: }
228:
229: return servletContext.getAttribute(name);
230: }
231:
232: public Enumeration getAttributeNames() {
233: return servletContext.getAttributeNames();
234: }
235:
236: public String getInitParameter(java.lang.String name) {
237: if (name == null) {
238: throw new IllegalArgumentException("Parameter name == null");
239: }
240:
241: return servletContext.getInitParameter(name);
242: }
243:
244: public Enumeration getInitParameterNames() {
245: return servletContext.getInitParameterNames();
246: }
247:
248: public void log(java.lang.String msg) {
249: servletContext.log(msg);
250: }
251:
252: public void log(java.lang.String message,
253: java.lang.Throwable throwable) {
254: servletContext.log(message, throwable);
255: }
256:
257: public void removeAttribute(java.lang.String name) {
258: if (name == null) {
259: throw new IllegalArgumentException("Attribute name == null");
260: }
261:
262: servletContext.removeAttribute(name);
263: }
264:
265: public void setAttribute(java.lang.String name,
266: java.lang.Object object) {
267: if (name == null) {
268: throw new IllegalArgumentException("Attribute name == null");
269: }
270:
271: servletContext.setAttribute(name, object);
272: }
273:
274: public String getPortletContextName() {
275: return servletContext.getServletContextName();
276: }
277:
278: // org.apache.pluto.core.InternalPortletContext Impl -----------------------
279:
280: public ServletContext getServletContext() {
281: return servletContext;
282: }
283:
284: public PortletAppDD getPortletApplicationDefinition() {
285: return portletAppDD;
286: }
287:
288: }
|