KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > WebApplicationResourceLocator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.util;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.ServletContext JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * A ResourceLocator that can find resources relative to web application context.
35  *
36  * @author Andrus Adamchik
37  */

38 public class WebApplicationResourceLocator extends ResourceLocator {
39
40     private static Log logObj = LogFactory.getLog(WebApplicationResourceLocator.class);
41
42     protected ServletContext JavaDoc context;
43     protected List JavaDoc additionalContextPaths;
44
45     /**
46      * @since 1.2
47      */

48     public WebApplicationResourceLocator() {
49         this.additionalContextPaths = new ArrayList JavaDoc();
50         this.addFilesystemPath("/WEB-INF/");
51     }
52
53     /**
54      * Creates new WebApplicationResourceLocator with default lookup policy including user
55      * home directory, current directory and CLASSPATH.
56      */

57     public WebApplicationResourceLocator(ServletContext JavaDoc context) {
58         this();
59         setServletContext(context);
60     }
61
62     /**
63      * Sets the ServletContext used to locate resources.
64      */

65     public void setServletContext(ServletContext JavaDoc servletContext) {
66         this.context = servletContext;
67     }
68
69     /**
70      * Gets the ServletContext used to locate resources.
71      */

72     public ServletContext JavaDoc getServletContext() {
73         return this.context;
74     }
75
76     /**
77      * Looks for resources relative to /WEB-INF/ directory or any extra context paths
78      * configured. Internal ServletContext is used to find resources.
79      */

80     public URL JavaDoc findResource(String JavaDoc location) {
81         if (!additionalContextPaths.isEmpty() && getServletContext() != null) {
82
83             String JavaDoc suffix = location != null ? location : "";
84             if (suffix.startsWith("/")) {
85                 suffix = suffix.substring(1);
86             }
87
88             Iterator JavaDoc cpi = this.additionalContextPaths.iterator();
89             while (cpi.hasNext()) {
90                 String JavaDoc prefix = (String JavaDoc) cpi.next();
91
92                 if (!prefix.endsWith("/")) {
93                     prefix += "/";
94                 }
95
96                 String JavaDoc fullName = prefix + suffix;
97                 logObj.debug("searching for: " + fullName);
98                 try {
99                     URL JavaDoc url = getServletContext().getResource(fullName);
100                     if (url != null) {
101                         return url;
102                     }
103                 }
104                 catch (MalformedURLException JavaDoc ex) {
105                     // ignoring
106
logObj.debug("Malformed URL, ignoring.", ex);
107                 }
108             }
109         }
110
111         return super.findResource(location);
112     }
113
114     /**
115      * Override ResourceLocator.addFilesystemPath(String) to intercept context paths
116      * starting with "/WEB-INF/" to place in additionalContextPaths.
117      */

118     public void addFilesystemPath(String JavaDoc path) {
119         if (path != null) {
120             if (path.startsWith("/WEB-INF/")) {
121                 this.additionalContextPaths.add(path);
122             }
123             else {
124                 super.addFilesystemPath(path);
125             }
126         }
127         else {
128             throw new IllegalArgumentException JavaDoc("Path must not be null.");
129         }
130     }
131 }
132
Popular Tags