KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > util > ClasspathEntityResolver


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.jsfext.util;
24
25 import org.xml.sax.ext.EntityResolver2 JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.net.URL JavaDoc;
31
32 import java.lang.reflect.Method JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34
35
36 /**
37  * <p> This <code>EntityResolver</code> looks for files that are included as
38  * <code>SYSTEM</code> entities in the java class-path. If the file is
39  * not found in the class path the resolver returns null, allowing
40  * default mechanism to search for the file on the file system.</p>
41  *
42  * @author Ken Paulsen (ken.paulsen@sun.com)
43  */

44 public class ClasspathEntityResolver implements EntityResolver2 JavaDoc {
45
46     /**
47      * <p> This method implements the old-style resolveEntity method. This
48      * is not recommended as it does not provide the baseURI and may fail
49      * when it shouldn't.</p>
50      */

51     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
52     return resolveEntity("", publicId, "", systemId);
53     }
54
55     /**
56      * <p> This method returns null.</p>
57      */

58     public InputSource JavaDoc getExternalSubset(String JavaDoc name, String JavaDoc baseURI) {
59     return null;
60     }
61
62     /**
63      *
64      */

65     public InputSource JavaDoc resolveEntity(String JavaDoc name, String JavaDoc publicId, String JavaDoc baseURI, String JavaDoc systemId) {
66     if (systemId != null) {
67         if (baseURI != null) {
68         if (systemId.startsWith(baseURI)) {
69             systemId = systemId.substring(baseURI.length());
70         }
71         }
72         if (systemId.startsWith("file:/")) {
73         systemId = systemId.substring(6); // remove "file:/"
74
}
75
76         // Remove any extra leading /'s
77
while (systemId.startsWith("/")) {
78         // String of leading '/'
79
systemId = systemId.substring(1);
80         }
81
82         // We should first check to see if it is in the context root,
83
// avoid using Servlet API's so this code can work outside a
84
// Servlet container.
85
InputStream JavaDoc stream = null;
86         URL JavaDoc url = null;
87         if (FACES_CONTEXT != null) {
88         try {
89             // The following will work w/ ServletContext/PortletContext
90
// Get the FacesContext...
91
Method JavaDoc meth = FACES_CONTEXT.getMethod(
92                 "getCurrentInstance", (Class JavaDoc []) null);
93             Object JavaDoc ctx = meth.invoke((Object JavaDoc) null, (Object JavaDoc []) null);
94
95             if (ctx != null) {
96             // Get the ExternalContext...
97
meth = ctx.getClass().getMethod(
98                 "getExternalContext", (Class JavaDoc []) null);
99             ctx = meth.invoke(ctx, (Object JavaDoc []) null);
100
101             // Get actual underlying external context...
102
meth = ctx.getClass().getMethod(
103                 "getContext", (Class JavaDoc []) null);
104             ctx = meth.invoke(ctx, (Object JavaDoc []) null);
105
106             // Get the resource using the ServletContext/PortletContext
107
meth = ctx.getClass().getMethod("getResource", STRING_ARG);
108             // The path must start w/ a '/'
109
url = (URL JavaDoc) meth.invoke(
110                 ctx, new Object JavaDoc [] {"/" + systemId});
111             if (url != null) {
112                 stream = url.openStream();
113             }
114             }
115         } catch (NoSuchMethodException JavaDoc ex) {
116             throw new RuntimeException JavaDoc(ex);
117         } catch (IllegalAccessException JavaDoc ex) {
118             throw new RuntimeException JavaDoc(ex);
119         } catch (InvocationTargetException JavaDoc ex) {
120             throw new RuntimeException JavaDoc(ex);
121         } catch (IOException JavaDoc ex) {
122             // Ignore... we will check other places
123
}
124         }
125
126         // First check to see if we can load this via a file:/// path,
127
// even though this may work via the default... depending on how
128
// the uri is constructed, it may not be able to locate it
129
// correctly this way. We will give higher priority to
130
// file:/// than finding it in the ClassPath.
131
if (stream == null) {
132         try {
133             stream = new URL JavaDoc(baseURI + "/" + systemId).openStream();
134         } catch (IOException JavaDoc ex) {
135             // Ignore... we will check the ClassPath
136
}
137
138         if (stream == null) {
139             // Ok, we tried... now check the Classpath...
140
// Get the ClassLoader
141
ClassLoader JavaDoc loader = Util.getClassLoader(systemId);
142
143             // Attempt to find the resource via the ClassPath
144
stream = loader.getResourceAsStream(systemId);
145             if (stream == null) {
146             // Try adding a '/'
147
stream = loader.getResourceAsStream("/" + systemId);
148             if (stream == null) {
149                 // Try in the META-INF directory
150
stream = loader.getResourceAsStream(
151                     "META-INF/" + systemId);
152             }
153             }
154         }
155         }
156
157         if (stream != null) {
158         // Found, return an InputSource to the resource
159
return new InputSource JavaDoc(stream);
160         }
161         if (LogUtil.configEnabled(LOGGER_NAME)) {
162         LogUtil.config((Object JavaDoc) LOGGER_NAME,
163             "Unable to resolve entity."
164             + "\n\tsystemId: '" + systemId
165             + "'\n\tbaseURI: '" + baseURI
166             + "'\n\tpublicId: '" + publicId
167             + "'\n\tname: '" + name + "'");
168         }
169     }
170
171     // use the default behaviour
172
return null;
173     }
174
175     public static final String JavaDoc LOGGER_NAME = "javax.enterpise.system.tools.admin.guiframework";
176
177     private static final Class JavaDoc [] STRING_ARG = new Class JavaDoc[] {String JavaDoc.class};
178     private static Class JavaDoc FACES_CONTEXT;
179
180     static {
181     try {
182         FACES_CONTEXT = Class.forName("javax.faces.context.FacesContext");
183     } catch (Exception JavaDoc ex) {
184         // Ignore, this just means we're not in a JSF environment
185
FACES_CONTEXT = null;
186     }
187     }
188 }
189
Popular Tags