KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > context > PortletContextResourcePatternResolver


1 /*
2  * Copyright 2002-2005 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.web.portlet.context;
18
19 import java.io.IOException JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.portlet.PortletContext;
25
26 import org.springframework.core.io.Resource;
27 import org.springframework.core.io.ResourceLoader;
28 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
29 import org.springframework.util.StringUtils;
30
31 /**
32  * PortletContext-aware subclass of PathMatchingResourcePatternResolver,
33  * able to find matching resources below the web application root directory
34  * via Portlet API's <code>PortletContext.getResourcePaths</code>.
35  * Falls back to the superclass' file system checking for other resources.
36  *
37  * <p>The advantage of using <code>PortletContext.getResourcePaths</code> to
38  * find matching files is that it will work in a WAR file which has not been
39  * expanded too.
40  *
41  * @author Juergen Hoeller
42  * @author John A. Lewis
43  * @since 2.0
44  */

45 public class PortletContextResourcePatternResolver extends PathMatchingResourcePatternResolver {
46
47     /**
48      * Create a new PortletContextResourcePatternResolver.
49      * @param portletContext the PortletContext to load resources with
50      * @see PortletContextResourceLoader#PortletContextResourceLoader(javax.portlet.PortletContext)
51      */

52     public PortletContextResourcePatternResolver(PortletContext portletContext) {
53         super(new PortletContextResourceLoader(portletContext));
54     }
55
56     /**
57      * Create a new PortletContextResourcePatternResolver.
58      * @param resourceLoader the ResourceLoader to load root directories and
59      * actual resources with
60      */

61     public PortletContextResourcePatternResolver(ResourceLoader resourceLoader) {
62         super(resourceLoader);
63     }
64
65     /**
66      * Overridden version which checks for PortletContextResource
67      * and uses <code>PortletContext.getResourcePaths</code> to find
68      * matching resources below the web application root directory.
69      * In case of other resources, delegates to the superclass version.
70      * @see #doRetrieveMatchingPortletContextResources
71      * @see org.springframework.web.portlet.context.PortletContextResource
72      * @see javax.portlet.PortletContext#getResourcePaths
73      */

74     protected Set JavaDoc doFindPathMatchingFileResources(Resource rootDirResource, String JavaDoc subPattern) throws IOException JavaDoc {
75         if (rootDirResource instanceof PortletContextResource) {
76             PortletContextResource pcResource = (PortletContextResource) rootDirResource;
77             PortletContext pc = pcResource.getPortletContext();
78             String JavaDoc fullPattern = pcResource.getPath() + subPattern;
79             Set JavaDoc result = new HashSet JavaDoc();
80             doRetrieveMatchingPortletContextResources(pc, fullPattern, pcResource.getPath(), result);
81             return result;
82         }
83         return super.doFindPathMatchingFileResources(rootDirResource, subPattern);
84     }
85
86     /**
87      * Recursively retrieve PortletContextResources that match the given pattern,
88      * adding them to the given result set.
89      * @param portletContext the PortletContext to work on
90      * @param fullPattern the pattern to match against,
91      * with preprended root directory path
92      * @param dir the current directory
93      * @param result the Set of matching Resources to add to
94      * @throws IOException if directory contents could not be retrieved
95      * @see org.springframework.web.portlet.context.PortletContextResource
96      * @see javax.portlet.PortletContext#getResourcePaths
97      */

98     protected void doRetrieveMatchingPortletContextResources(
99             PortletContext portletContext, String JavaDoc fullPattern, String JavaDoc dir, Set JavaDoc result) throws IOException JavaDoc {
100
101         Set JavaDoc candidates = portletContext.getResourcePaths(dir);
102         if (candidates != null) {
103             boolean dirDepthNotFixed = (fullPattern.indexOf("**") != -1);
104             for (Iterator JavaDoc it = candidates.iterator(); it.hasNext();) {
105                 String JavaDoc currPath = (String JavaDoc) it.next();
106                 if (currPath.endsWith("/") &&
107                         (dirDepthNotFixed ||
108                         StringUtils.countOccurrencesOf(currPath, "/") < StringUtils.countOccurrencesOf(fullPattern, "/"))) {
109                     doRetrieveMatchingPortletContextResources(portletContext, fullPattern, currPath, result);
110                 }
111                 if (getPathMatcher().match(fullPattern, currPath)) {
112                     result.add(new PortletContextResource(portletContext, currPath));
113                 }
114             }
115         }
116     }
117
118 }
119
Popular Tags