KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > ContextLoader


1 // ========================================================================
2
// $Id: ContextLoader.java,v 1.37 2006/01/09 07:26:12 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http;
17
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream 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.net.URLClassLoader JavaDoc;
25 import java.security.CodeSource JavaDoc;
26 import java.security.PermissionCollection JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.mortbay.log.LogFactory;
32 import org.mortbay.util.IO;
33 import org.mortbay.util.Resource;
34
35 /* ------------------------------------------------------------ */
36 /** ClassLoader for HttpContext.
37  * Specializes URLClassLoader with some utility and file mapping
38  * methods.
39  *
40  * This loader defaults to the 2.3 servlet spec behaviour where non
41  * system classes are loaded from the classpath in preference to the
42  * parent loader. Java2 compliant loading, where the parent loader
43  * always has priority, can be selected with the setJava2Complient method.
44  *
45  * @version $Id: ContextLoader.java,v 1.37 2006/01/09 07:26:12 gregwilkins Exp $
46  * @author Greg Wilkins (gregw)
47  */

48 public class ContextLoader extends URLClassLoader JavaDoc
49 {
50     private static Log log= LogFactory.getLog(ContextLoader.class);
51
52     private boolean _java2compliant= false;
53     private ClassLoader JavaDoc _parent;
54     private PermissionCollection JavaDoc _permissions;
55     private String JavaDoc _urlClassPath;
56     private HttpContext _context;
57     
58     /* ------------------------------------------------------------ */
59     /** Constructor.
60      * @param classPath Comma separated path of filenames or URLs
61      * pointing to directories or jar files. Directories should end
62      * with '/'.
63      * @exception IOException
64      */

65     public ContextLoader(HttpContext context, String JavaDoc classPath, ClassLoader JavaDoc parent, PermissionCollection JavaDoc permisions)
66         throws MalformedURLException JavaDoc, IOException JavaDoc
67     {
68         super(new URL JavaDoc[0], parent);
69         _context=context;
70         _permissions= permisions;
71         _parent= parent;
72         
73         if (_parent == null)
74             _parent= getSystemClassLoader();
75
76         if (classPath == null)
77         {
78             _urlClassPath= "";
79         }
80         else
81         {
82             StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(classPath, ",;");
83
84             while (tokenizer.hasMoreTokens())
85             {
86                 Resource resource= Resource.newResource(tokenizer.nextToken());
87                 if (log.isDebugEnabled())
88                     log.debug("Path resource=" + resource);
89
90                 // Resolve file path if possible
91
File JavaDoc file= resource.getFile();
92
93                 if (file != null)
94                 {
95                     URL JavaDoc url= resource.getURL();
96                     addURL(url);
97                     _urlClassPath= (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString());
98                 }
99                 else
100                 {
101                     // Add resource or expand jar/
102
if (!resource.isDirectory() && file == null)
103                     {
104                         InputStream JavaDoc in= resource.getInputStream();
105                         File JavaDoc lib= new File JavaDoc(context.getTempDirectory(), "lib");
106                         if (!lib.exists())
107                         {
108                             lib.mkdir();
109                             lib.deleteOnExit();
110                         }
111                         File JavaDoc jar= File.createTempFile("Jetty-", ".jar", lib);
112
113                         jar.deleteOnExit();
114                         if (log.isDebugEnabled())
115                             log.debug("Extract " + resource + " to " + jar);
116                         FileOutputStream JavaDoc out = null;
117                         try
118                         {
119                             out= new FileOutputStream JavaDoc(jar);
120                             IO.copy(in, out);
121                         }
122                         finally
123                         {
124                             IO.close(out);
125                         }
126
127                         URL JavaDoc url= jar.toURL();
128                         addURL(url);
129                         _urlClassPath=
130                             (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString());
131                     }
132                     else
133                     {
134                         URL JavaDoc url= resource.getURL();
135                         addURL(url);
136                         _urlClassPath=
137                             (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString());
138                     }
139                 }
140             }
141         }
142
143         if (log.isDebugEnabled())
144         {
145             if (log.isDebugEnabled())
146                 log.debug("ClassPath=" + _urlClassPath);
147             if (log.isDebugEnabled())
148                 log.debug("Permissions=" + _permissions);
149             if (log.isDebugEnabled())
150                 log.debug("URL=" + Arrays.asList(getURLs()));
151         }
152     }
153
154     /* ------------------------------------------------------------ */
155     /** Set Java2 compliant status.
156      * @param compliant
157      */

158     public void setJava2Compliant(boolean compliant)
159     {
160         _java2compliant= compliant;
161     }
162
163     /* ------------------------------------------------------------ */
164     public boolean isJava2Compliant()
165     {
166         return _java2compliant;
167     }
168
169     /* ------------------------------------------------------------ */
170     public String JavaDoc toString()
171     {
172         return "ContextLoader@" + hashCode() + "(" + _urlClassPath + ")\n --parent--> " + _parent.toString();
173     }
174
175     /* ------------------------------------------------------------ */
176     public PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc cs)
177     {
178         PermissionCollection JavaDoc pc= (_permissions == null) ? super.getPermissions(cs) : _permissions;
179         if (log.isDebugEnabled())
180             log.debug("loader.getPermissions(" + cs + ")=" + pc);
181         return pc;
182     }
183
184     /* ------------------------------------------------------------ */
185     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc
186     {
187         return loadClass(name, false);
188     }
189
190     /* ------------------------------------------------------------ */
191     protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc
192     {
193         Class JavaDoc c= findLoadedClass(name);
194         ClassNotFoundException JavaDoc ex= null;
195         boolean tried_parent= false;
196         if (c == null && (_java2compliant || isSystemPath(name)) && !isServerPath(name) && _parent!=null)
197         {
198             if (log.isTraceEnabled())
199                 log.trace("try loadClass " + name + " from " + _parent);
200             tried_parent= true;
201             try
202             {
203                 c= _parent.loadClass(name);
204                 if (log.isTraceEnabled())
205                     log.trace("p0 loaded " + c);
206             }
207             catch (ClassNotFoundException JavaDoc e)
208             {
209                 ex= e;
210             }
211         }
212
213         if (c == null)
214         {
215             if (log.isTraceEnabled())
216                 log.trace("try findClass " + name + " from " + _urlClassPath);
217             try
218             {
219                 c= this.findClass(name);
220                 if (log.isTraceEnabled())
221                     log.trace("cx loaded " + c);
222             }
223             catch (ClassNotFoundException JavaDoc e)
224             {
225                 ex= e;
226             }
227         }
228
229         if (c == null && !tried_parent && !isServerPath(name) && _parent!=null)
230         {
231             if (log.isTraceEnabled())
232                 log.trace("try loadClass " + name + " from " + _parent);
233             c= _parent.loadClass(name);
234             if (log.isTraceEnabled())
235                 log.trace("p1 loaded " + c);
236         }
237         
238         if (c == null)
239             throw ex;
240
241         if (resolve)
242             resolveClass(c);
243
244         return c;
245     }
246
247     /* ------------------------------------------------------------ */
248     public URL JavaDoc getResource(String JavaDoc name)
249     {
250         URL JavaDoc url= null;
251         boolean tried_parent= false;
252         if (_parent!=null &&(_java2compliant || isSystemPath(name)))
253         {
254             if (log.isTraceEnabled())
255                 log.trace("try getResource " + name + " from " + _parent);
256             tried_parent= true;
257             url= _parent.getResource(name);
258         }
259
260         if (url == null)
261         {
262             if (log.isTraceEnabled())
263                 log.trace("try findResource " + name + " from " + _urlClassPath);
264             url= this.findResource(name);
265
266             if (url == null && name.startsWith("/"))
267             {
268                 if (log.isDebugEnabled())
269                     log.debug("HACK leading / off " + name);
270                 url= this.findResource(name.substring(1));
271             }
272         }
273
274         if (_parent!=null && url == null && !tried_parent)
275         {
276             if (log.isTraceEnabled())
277                 log.trace("try getResource " + name + " from " + _parent);
278             url= _parent.getResource(name);
279         }
280
281         if (url != null)
282             if (log.isTraceEnabled())
283                 log.trace("found " + url);
284
285         return url;
286     }
287
288     /* ------------------------------------------------------------ */
289     public boolean isServerPath(String JavaDoc name)
290     {
291         name=name.replace('/','.');
292         while(name.startsWith("."))
293             name=name.substring(1);
294
295         String JavaDoc[] server_classes=_context.getServerClasses();
296         
297         if (server_classes!=null)
298         {
299             for (int i=0;i<server_classes.length;i++)
300             {
301                 boolean result=true;
302                 String JavaDoc c=server_classes[i];
303                 if (c.startsWith("-"))
304                 {
305                     c=c.substring(1);
306                     result=false;
307                 }
308                 
309                 if (c.endsWith("."))
310                 {
311                     if (name.startsWith(c))
312                         return result;
313                 }
314                 else if (name.equals(c))
315                 {
316                     return result;
317                 }
318             }
319         }
320         return false;
321     }
322
323     /* ------------------------------------------------------------ */
324     public boolean isSystemPath(String JavaDoc name)
325     {
326         name=name.replace('/','.');
327         while(name.startsWith("."))
328             name=name.substring(1);
329
330         String JavaDoc[] system_classes=_context.getSystemClasses();
331         if (system_classes!=null)
332         {
333             for (int i=0;i<system_classes.length;i++)
334             {
335                 boolean result=true;
336                 String JavaDoc c=system_classes[i];
337                 if (c.startsWith("-"))
338                 {
339                     c=c.substring(1);
340                     result=false;
341                 }
342                 
343                 if (c.endsWith("."))
344                 {
345                     if (name.startsWith(c))
346                         return result;
347                 }
348                 else if (name.equals(c))
349                     return result;
350             }
351         }
352         
353         return false;
354     }
355
356     /* ------------------------------------------------------------ */
357     public void destroy()
358     {
359         this._parent=null;
360         this._permissions=null;
361         this._urlClassPath=null;
362     }
363     
364     /* ------------------------------------------------------------ */
365     /**
366      * @return Returns the serverClasses.
367      */

368     String JavaDoc[] getServerClasses()
369     {
370         return _context.getServerClasses();
371     }
372     
373     /* ------------------------------------------------------------ */
374     /**
375      * @param serverClasses The serverClasses to set.
376      */

377     void setServerClasses(String JavaDoc[] serverClasses)
378     {
379         _context.setServerClasses(serverClasses);
380     }
381     
382     /* ------------------------------------------------------------ */
383     /**
384      * @return Returns the systemClasses.
385      */

386     String JavaDoc[] getSystemClasses()
387     {
388         return _context.getSystemClasses();
389     }
390     
391     /* ------------------------------------------------------------ */
392     /**
393      * @param systemClasses The systemClasses to set.
394      */

395     void setSystemClasses(String JavaDoc[] systemClasses)
396     {
397         _context.setSystemClasses(systemClasses);
398     }
399 }
400
Popular Tags