KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > Loader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.loader;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.log.Log;
33 import com.caucho.vfs.Path;
34
35 import java.io.InputStream JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.security.CodeSource JavaDoc;
38 import java.security.cert.Certificate JavaDoc;
39 import java.util.Vector JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43
44 /**
45  * Loads resources.
46  */

47 abstract public class Loader {
48   protected static final Logger JavaDoc log = Log.open(Loader.class);
49   
50   private DynamicClassLoader _loader;
51
52   /**
53    * Sets the owning class loader.
54    */

55   public void setLoader(DynamicClassLoader loader)
56   {
57     _loader = loader;
58   }
59
60   /**
61    * Gets the owning class loader.
62    */

63   public DynamicClassLoader getLoader()
64   {
65     return _loader;
66   }
67
68   /**
69    * Validates the loader.
70    */

71   public void validate()
72     throws ConfigException
73   {
74   }
75   
76   /**
77    * Returns the class entry.
78    *
79    * @param name name of the class
80    */

81   protected ClassEntry getClassEntry(String JavaDoc name)
82     throws ClassNotFoundException JavaDoc
83   {
84     String JavaDoc pathName = name.replace('.', '/') + ".class";
85
86     // Find the path corresponding to the class
87
Path path = getPath(pathName);
88
89     if (path != null && path.getLength() > 0)
90       return new ClassEntry(_loader, name, path, path,
91                 getCodeSource(path));
92     else
93       return null;
94   }
95   
96   /**
97    * Returns the resource
98    *
99    * @param name name of the resource
100    */

101   public URL JavaDoc getResource(String JavaDoc name)
102   {
103     Path path;
104
105     if (name.startsWith("/"))
106       path = getPath("." + name);
107     else
108       path = getPath(name);
109
110     if (path != null && path.exists()) {
111       try {
112         return new URL JavaDoc(path.getURL());
113       } catch (Exception JavaDoc e) {
114     e.printStackTrace();
115     log.log(Level.FINER, e.toString(), e);
116       }
117     }
118
119     return null;
120   }
121   
122   /**
123    * Returns the resource
124    *
125    * @param name name of the resource
126    */

127   public void getResources(Vector JavaDoc<URL JavaDoc> resources, String JavaDoc name)
128   {
129     Path path;
130
131     if (name.startsWith("/"))
132       path = getPath("." + name);
133     else
134       path = getPath(name);
135
136     if (path != null && path.canRead()) {
137       try {
138         resources.add(new URL JavaDoc(path.getURL()));
139       } catch (Exception JavaDoc e) {
140       }
141     }
142   }
143   
144   /**
145    * Opens the stream to the resource.
146    *
147    * @param name name of the resource
148    */

149   public InputStream JavaDoc getResourceAsStream(String JavaDoc name)
150   {
151     Path path;
152
153     if (name.startsWith("/"))
154       path = getPath("." + name);
155     else
156       path = getPath(name);
157
158     if (path != null && path.canRead()) {
159       try {
160         return path.openRead();
161       } catch (Exception JavaDoc e) {
162       }
163     }
164
165     return null;
166   }
167   
168   /**
169    * Returns a path for the given name.
170    */

171   public Path getPath(String JavaDoc name)
172   {
173     return null;
174   }
175   
176   /**
177    * Returns the code source for the path.
178    */

179   protected CodeSource JavaDoc getCodeSource(Path path)
180   {
181     try {
182       return new CodeSource JavaDoc(new URL JavaDoc(path.getURL()),
183                 (Certificate JavaDoc []) path.getCertificates());
184     } catch (Exception JavaDoc e) {
185       log.log(Level.WARNING, e.toString(), e);
186
187       return null;
188     }
189   }
190   
191   /**
192    * Adds the classpath of this loader.
193    */

194   protected String JavaDoc getClassPath(String JavaDoc head)
195   {
196     return head;
197   }
198   
199   /**
200    * Adds the sourcepath of this loader.
201    */

202   protected String JavaDoc getSourcePath(String JavaDoc head)
203   {
204     return getClassPath(head);
205   }
206
207   /**
208    * Destroys the loader.
209    */

210   protected void destroy()
211   {
212   }
213 }
214
Popular Tags