KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > util > Resource


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14 package com.sun.facelets.util;
15
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLConnection JavaDoc;
22 import java.net.URLStreamHandler JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import javax.faces.context.ExternalContext;
27 import javax.faces.context.FacesContext;
28 import javax.servlet.ServletContext JavaDoc;
29
30 /**
31  * @author Roland Huss
32  *
33  */

34 public final class Resource {
35
36     protected final static Logger JavaDoc log = Logger.getLogger("facelets.factory");
37
38     /**
39      * Get an URL of an internal resource. First,
40      * {@link javax.faces.context.ExternalContext#getResource(String)} is
41      * checked for an non-null URL return value. In the case of a null return
42      * value (as it is the case for Weblogic 8.1 for a packed war), a URL with a
43      * special URL handler is constructed, which can be used for
44      * <em>opening</em> a serlvet resource later. Internally, this special URL
45      * handler will call {@link ServletContext#getResourceAsStream(String)} when
46      * an inputstream is requested. This works even on Weblogic 8.1
47      *
48      * @param ctx
49      * the faces context from which to retrieve the resource
50      * @param path
51      * an URL path
52      *
53      * @return an url representing the URL and on which getInputStream() can be
54      * called to get the resource
55      * @throws MalformedURLException
56      */

57     public static URL JavaDoc getResourceUrl(FacesContext ctx, String JavaDoc path)
58             throws MalformedURLException JavaDoc {
59         final ExternalContext externalContext = ctx.getExternalContext();
60         URL JavaDoc url = externalContext.getResource(path);
61         if (log.isLoggable(Level.FINE)) {
62             log.fine("Resource-Url from external context: " + url);
63         }
64         if (url == null) {
65             // This might happen on Servlet container which doesnot return
66
// anything
67
// for getResource() (like weblogic 8.1 for packaged wars) we
68
// are trying
69
// to use an own URL protocol in order to use
70
// ServletContext.getResourceAsStream()
71
// when opening the url
72
if (resourceExist(externalContext, path)) {
73                 url = getUrlForResourceAsStream(externalContext, path);
74             }
75         }
76         return url;
77     }
78
79     // This method could be used above to provide a 'fail fast' if a
80
// resource
81
// doesnt exist. Otherwise, the URL will fail on the first access.
82
private static boolean resourceExist(ExternalContext externalContext,
83             String JavaDoc path) {
84         if ("/".equals(path)) {
85             // The root context exists always
86
return true;
87         }
88         Object JavaDoc ctx = externalContext.getContext();
89         if (ctx instanceof ServletContext JavaDoc) {
90             ServletContext JavaDoc servletContext = (ServletContext JavaDoc) ctx;
91             InputStream JavaDoc stream = servletContext.getResourceAsStream(path);
92             if (stream != null) {
93                 try {
94                     stream.close();
95                 } catch (IOException JavaDoc e) {
96                     // Ignore here, since we donnot wanted to read from this
97
// resource anyway
98
}
99                 return true;
100             }
101         }
102         return false;
103     }
104
105     // Construct URL with special URLStreamHandler for proxying
106
// ServletContext.getResourceAsStream()
107
private static URL JavaDoc getUrlForResourceAsStream(
108             final ExternalContext externalContext, String JavaDoc path)
109             throws MalformedURLException JavaDoc {
110         URLStreamHandler JavaDoc handler = new URLStreamHandler JavaDoc() {
111             protected URLConnection JavaDoc openConnection(URL JavaDoc u) throws IOException JavaDoc {
112                 final String JavaDoc file = u.getFile();
113                 return new URLConnection JavaDoc(u) {
114                     public void connect() throws IOException JavaDoc {
115                     }
116
117                     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
118                         if (log.isLoggable(Level.FINE)) {
119                             log.fine("Opening internal url to " + file);
120                         }
121                         Object JavaDoc ctx = externalContext.getContext();
122                         // Or maybe fetch the external context afresh ?
123
// Object ctx =
124
// FacesContext.getCurrentInstance().getExternalContext().getContext();
125

126                         if (ctx instanceof ServletContext JavaDoc) {
127                             ServletContext JavaDoc servletContext = (ServletContext JavaDoc) ctx;
128                             InputStream JavaDoc stream = servletContext
129                                     .getResourceAsStream(file);
130                             if (stream == null) {
131                                 throw new FileNotFoundException JavaDoc(
132                                         "Cannot open resource " + file);
133                             }
134                             return stream;
135                         } else {
136                             throw new IOException JavaDoc(
137                                     "Cannot open resource for an context of "
138                                             + (ctx != null ? ctx.getClass()
139                                                     : null));
140                         }
141                     }
142                 };
143             }
144         };
145         return new URL JavaDoc("internal", null, 0, path, handler);
146     }
147 }
148
Popular Tags