KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > portlet > MockPortletContext


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mock.web.portlet;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.portlet.PortletContext;
31 import javax.portlet.PortletRequestDispatcher;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import org.springframework.core.io.DefaultResourceLoader;
37 import org.springframework.core.io.Resource;
38 import org.springframework.core.io.ResourceLoader;
39 import org.springframework.util.Assert;
40 import org.springframework.web.util.WebUtils;
41
42 /**
43  * Mock implementation of the {@link javax.portlet.PortletContext} interface.
44  *
45  * @author John A. Lewis
46  * @author Juergen Hoeller
47  * @since 2.0
48  */

49 public class MockPortletContext implements PortletContext {
50
51     private static final String JavaDoc TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir";
52
53
54     private final Log logger = LogFactory.getLog(getClass());
55
56     private final String JavaDoc resourceBasePath;
57     
58     private final ResourceLoader resourceLoader;
59
60     private final Hashtable JavaDoc attributes = new Hashtable JavaDoc();
61
62     private final Properties JavaDoc initParameters = new Properties JavaDoc();
63
64     private String JavaDoc portletContextName = "MockPortletContext";
65
66
67     /**
68      * Create a new MockPortletContext with no base path and a
69      * DefaultResourceLoader (i.e. the classpath root as WAR root).
70      * @see org.springframework.core.io.DefaultResourceLoader
71      */

72     public MockPortletContext() {
73         this("", null);
74     }
75
76     /**
77      * Create a new MockPortletContext using a DefaultResourceLoader.
78      * @param resourceBasePath the WAR root directory (should not end with a slash)
79      * @see org.springframework.core.io.DefaultResourceLoader
80      */

81     public MockPortletContext(String JavaDoc resourceBasePath) {
82         this(resourceBasePath, null);
83     }
84
85     /**
86      * Create a new MockPortletContext, using the specified ResourceLoader
87      * and no base path.
88      * @param resourceLoader the ResourceLoader to use (or null for the default)
89      */

90     public MockPortletContext(ResourceLoader resourceLoader) {
91         this("", resourceLoader);
92     }
93
94     /**
95      * Create a new MockPortletContext.
96      * @param resourceBasePath the WAR root directory (should not end with a slash)
97      * @param resourceLoader the ResourceLoader to use (or null for the default)
98      */

99     public MockPortletContext(String JavaDoc resourceBasePath, ResourceLoader resourceLoader) {
100         this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : "");
101         this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
102
103         // Use JVM temp dir as PortletContext temp dir.
104
String JavaDoc tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY);
105         if (tempDir != null) {
106             this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File JavaDoc(tempDir));
107         }
108     }
109
110     /**
111      * Build a full resource location for the given path,
112      * prepending the resource base path of this MockPortletContext.
113      * @param path the path as specified
114      * @return the full resource path
115      */

116     protected String JavaDoc getResourceLocation(String JavaDoc path) {
117         if (!path.startsWith("/")) {
118             path = "/" + path;
119         }
120         return this.resourceBasePath + path;
121     }
122
123     
124     public String JavaDoc getServerInfo() {
125         return "MockPortal/1.0";
126     }
127
128     public PortletRequestDispatcher getRequestDispatcher(String JavaDoc path) {
129         if (!path.startsWith("/")) {
130             throw new IllegalArgumentException JavaDoc(
131                     "PortletRequestDispatcher path at PortletContext level must start with '/'");
132         }
133         return new MockPortletRequestDispatcher(path);
134     }
135
136     public PortletRequestDispatcher getNamedDispatcher(String JavaDoc path) {
137         return null;
138     }
139
140     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
141         Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
142         try {
143             return resource.getInputStream();
144         }
145         catch (IOException JavaDoc ex) {
146             logger.info("Couldn't open InputStream for " + resource, ex);
147             return null;
148         }
149     }
150
151     public int getMajorVersion() {
152         return 1;
153     }
154
155     public int getMinorVersion() {
156         return 0;
157     }
158     
159     public String JavaDoc getMimeType(String JavaDoc filePath) {
160         return null;
161     }
162
163     public String JavaDoc getRealPath(String JavaDoc path) {
164         Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
165         try {
166             return resource.getFile().getAbsolutePath();
167         }
168         catch (IOException JavaDoc ex) {
169             logger.info("Couldn't determine real path of resource " + resource, ex);
170             return null;
171         }
172     }
173
174     public Set JavaDoc getResourcePaths(String JavaDoc path) {
175         Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
176         try {
177             File JavaDoc file = resource.getFile();
178             String JavaDoc[] fileList = file.list();
179             String JavaDoc prefix = (path.endsWith("/") ? path : path + "/");
180             Set JavaDoc resourcePaths = new HashSet JavaDoc(fileList.length);
181             for (int i = 0; i < fileList.length; i++) {
182                 resourcePaths.add(prefix + fileList[i]);
183             }
184             return resourcePaths;
185         }
186         catch (IOException JavaDoc ex) {
187             logger.info("Couldn't get resource paths for " + resource, ex);
188             return null;
189         }
190     }
191
192     public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc {
193         Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
194         try {
195             return resource.getURL();
196         }
197         catch (IOException JavaDoc ex) {
198             logger.info("Couldn't get URL for " + resource, ex);
199             return null;
200         }
201     }
202
203     public Object JavaDoc getAttribute(String JavaDoc name) {
204         return this.attributes.get(name);
205     }
206
207     public Enumeration JavaDoc getAttributeNames() {
208         return this.attributes.keys();
209     }
210
211     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
212         if (value != null) {
213             this.attributes.put(name, value);
214         }
215         else {
216             this.attributes.remove(name);
217         }
218     }
219
220     public void removeAttribute(String JavaDoc name) {
221         this.attributes.remove(name);
222     }
223
224     public void addInitParameter(String JavaDoc name, String JavaDoc value) {
225         Assert.notNull(name, "Parameter name must not be null");
226         this.initParameters.setProperty(name, value);
227     }
228
229     public String JavaDoc getInitParameter(String JavaDoc name) {
230         Assert.notNull(name, "Parameter name must not be null");
231         return this.initParameters.getProperty(name);
232     }
233
234     public Enumeration JavaDoc getInitParameterNames() {
235         return this.initParameters.keys();
236     }
237
238     public void log(String JavaDoc message) {
239         logger.info(message);
240     }
241
242     public void log(String JavaDoc message, Throwable JavaDoc t) {
243         logger.info(message, t);
244     }
245
246     public void setPortletContextName(String JavaDoc portletContextName) {
247         this.portletContextName = portletContextName;
248     }
249
250     public String JavaDoc getPortletContextName() {
251         return portletContextName;
252     }
253
254 }
255
Popular Tags