001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mock.web.portlet;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.util.Enumeration;
025: import java.util.HashSet;
026: import java.util.Hashtable;
027: import java.util.Properties;
028: import java.util.Set;
029:
030: import javax.portlet.PortletContext;
031: import javax.portlet.PortletRequestDispatcher;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import org.springframework.core.io.DefaultResourceLoader;
037: import org.springframework.core.io.Resource;
038: import org.springframework.core.io.ResourceLoader;
039: import org.springframework.util.Assert;
040: import org.springframework.web.util.WebUtils;
041:
042: /**
043: * Mock implementation of the {@link javax.portlet.PortletContext} interface.
044: *
045: * @author John A. Lewis
046: * @author Juergen Hoeller
047: * @since 2.0
048: */
049: public class MockPortletContext implements PortletContext {
050:
051: private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir";
052:
053: private final Log logger = LogFactory.getLog(getClass());
054:
055: private final String resourceBasePath;
056:
057: private final ResourceLoader resourceLoader;
058:
059: private final Hashtable attributes = new Hashtable();
060:
061: private final Properties initParameters = new Properties();
062:
063: private String portletContextName = "MockPortletContext";
064:
065: /**
066: * Create a new MockPortletContext with no base path and a
067: * DefaultResourceLoader (i.e. the classpath root as WAR root).
068: * @see org.springframework.core.io.DefaultResourceLoader
069: */
070: public MockPortletContext() {
071: this ("", null);
072: }
073:
074: /**
075: * Create a new MockPortletContext using a DefaultResourceLoader.
076: * @param resourceBasePath the WAR root directory (should not end with a slash)
077: * @see org.springframework.core.io.DefaultResourceLoader
078: */
079: public MockPortletContext(String resourceBasePath) {
080: this (resourceBasePath, null);
081: }
082:
083: /**
084: * Create a new MockPortletContext, using the specified ResourceLoader
085: * and no base path.
086: * @param resourceLoader the ResourceLoader to use (or null for the default)
087: */
088: public MockPortletContext(ResourceLoader resourceLoader) {
089: this ("", resourceLoader);
090: }
091:
092: /**
093: * Create a new MockPortletContext.
094: * @param resourceBasePath the WAR root directory (should not end with a slash)
095: * @param resourceLoader the ResourceLoader to use (or null for the default)
096: */
097: public MockPortletContext(String resourceBasePath,
098: ResourceLoader resourceLoader) {
099: this .resourceBasePath = (resourceBasePath != null ? resourceBasePath
100: : "");
101: this .resourceLoader = (resourceLoader != null ? resourceLoader
102: : new DefaultResourceLoader());
103:
104: // Use JVM temp dir as PortletContext temp dir.
105: String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY);
106: if (tempDir != null) {
107: this .attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE,
108: new File(tempDir));
109: }
110: }
111:
112: /**
113: * Build a full resource location for the given path,
114: * prepending the resource base path of this MockPortletContext.
115: * @param path the path as specified
116: * @return the full resource path
117: */
118: protected String getResourceLocation(String path) {
119: if (!path.startsWith("/")) {
120: path = "/" + path;
121: }
122: return this .resourceBasePath + path;
123: }
124:
125: public String getServerInfo() {
126: return "MockPortal/1.0";
127: }
128:
129: public PortletRequestDispatcher getRequestDispatcher(String path) {
130: if (!path.startsWith("/")) {
131: throw new IllegalArgumentException(
132: "PortletRequestDispatcher path at PortletContext level must start with '/'");
133: }
134: return new MockPortletRequestDispatcher(path);
135: }
136:
137: public PortletRequestDispatcher getNamedDispatcher(String path) {
138: return null;
139: }
140:
141: public InputStream getResourceAsStream(String path) {
142: Resource resource = this .resourceLoader
143: .getResource(getResourceLocation(path));
144: try {
145: return resource.getInputStream();
146: } catch (IOException ex) {
147: logger
148: .info("Couldn't open InputStream for " + resource,
149: ex);
150: return null;
151: }
152: }
153:
154: public int getMajorVersion() {
155: return 1;
156: }
157:
158: public int getMinorVersion() {
159: return 0;
160: }
161:
162: public String getMimeType(String filePath) {
163: return null;
164: }
165:
166: public String getRealPath(String path) {
167: Resource resource = this .resourceLoader
168: .getResource(getResourceLocation(path));
169: try {
170: return resource.getFile().getAbsolutePath();
171: } catch (IOException ex) {
172: logger.info("Couldn't determine real path of resource "
173: + resource, ex);
174: return null;
175: }
176: }
177:
178: public Set getResourcePaths(String path) {
179: Resource resource = this .resourceLoader
180: .getResource(getResourceLocation(path));
181: try {
182: File file = resource.getFile();
183: String[] fileList = file.list();
184: String prefix = (path.endsWith("/") ? path : path + "/");
185: Set resourcePaths = new HashSet(fileList.length);
186: for (int i = 0; i < fileList.length; i++) {
187: resourcePaths.add(prefix + fileList[i]);
188: }
189: return resourcePaths;
190: } catch (IOException ex) {
191: logger.info("Couldn't get resource paths for " + resource,
192: ex);
193: return null;
194: }
195: }
196:
197: public URL getResource(String path) throws MalformedURLException {
198: Resource resource = this .resourceLoader
199: .getResource(getResourceLocation(path));
200: try {
201: return resource.getURL();
202: } catch (IOException ex) {
203: logger.info("Couldn't get URL for " + resource, ex);
204: return null;
205: }
206: }
207:
208: public Object getAttribute(String name) {
209: return this .attributes.get(name);
210: }
211:
212: public Enumeration getAttributeNames() {
213: return this .attributes.keys();
214: }
215:
216: public void setAttribute(String name, Object value) {
217: if (value != null) {
218: this .attributes.put(name, value);
219: } else {
220: this .attributes.remove(name);
221: }
222: }
223:
224: public void removeAttribute(String name) {
225: this .attributes.remove(name);
226: }
227:
228: public void addInitParameter(String name, String value) {
229: Assert.notNull(name, "Parameter name must not be null");
230: this .initParameters.setProperty(name, value);
231: }
232:
233: public String getInitParameter(String name) {
234: Assert.notNull(name, "Parameter name must not be null");
235: return this .initParameters.getProperty(name);
236: }
237:
238: public Enumeration getInitParameterNames() {
239: return this .initParameters.keys();
240: }
241:
242: public void log(String message) {
243: logger.info(message);
244: }
245:
246: public void log(String message, Throwable t) {
247: logger.info(message, t);
248: }
249:
250: public void setPortletContextName(String portletContextName) {
251: this .portletContextName = portletContextName;
252: }
253:
254: public String getPortletContextName() {
255: return portletContextName;
256: }
257:
258: }
|