OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [FileSystemClassLoader.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* gnu.classpath.tools.FileSystemClassLoader
2
   Copyright (C) 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
02111-1307 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.classpath.tools;
39
 
40
import java.io.File;
41
import java.io.FileInputStream;
42
import java.io.FileNotFoundException;
43
import java.io.InputStream;
44
import java.io.IOException;
45
import java.io.StreamTokenizer;
46
import java.io.StringReader;
47
 
48
import java.net.MalformedURLException;
49
import java.net.URL;
50
 
51
import java.util.LinkedList;
52
import java.util.List;
53
import java.util.ArrayList;
54
import java.util.StringTokenizer;
55
 
56
import java.util.jar.JarEntry;
57
import java.util.jar.JarFile;
58
import java.util.jar.Manifest;
59
import java.util.jar.Attributes;
60
 
61
/**
62
 *  A <code>ClassLoader</code> implementation which looks for classes
63
 *  on the local filesystem given a standard search path.
64
 */
65
public class FileSystemClassLoader extends ClassLoader {
66
 
67
   private File[] pathComponents;
68
 
69
   /**
70
    *  Initialize the class loader with a normal path string. The path
71
    *  string should contain path components separated by {@link
72
    *  File.pathSeparator}. Each path component should either denote a
73
    *  directory or a .jar or .zip file.
74
    */
75
   public FileSystemClassLoader(String path)
76
   {
77
      List components = new ArrayList();
78
      for (StringTokenizer st = new StringTokenizer(path, File.pathSeparator); st.hasMoreTokens(); ) {
79
         File pathComponent = new File(st.nextToken());
80
         components.add(pathComponent);
81
         if (pathComponent.exists() && !pathComponent.isDirectory()) {
82
            List subComponents = tryGetJarFileClassPathComponents(pathComponent);
83
            if (null != subComponents) {
84
               components.addAll(subComponents);
85
            }
86
         }
87
      }
88
      File[] componentArray = new File[components.size()];
89
      this.pathComponents = (File[])components.toArray(componentArray);
90
   }
91
 
92
   /**
93
    *  Initialize the class loader with an array of path
94
    *  components. Each path component should either denote a
95
    *  directory or a .jar or .zip file.
96
    */
97
   public FileSystemClassLoader(File[] pathComponents)
98
   {
99
      this.pathComponents = pathComponents;
100
      for (int i = 0; i < pathComponents.length; ++i) {
101
         if (!pathComponents[i].exists()) {
102
            System.err.println("WARNING: Path component '" + pathComponents[i] + "' not found.");
103
         }
104
      }
105
   }
106
 
107
   public Class loadClass(String name)
108
      throws ClassNotFoundException {
109
 
110
      return super.loadClass(name);
111
   }
112
 
113
   public Class findClass(String name)
114
      throws ClassNotFoundException {
115
 
116
      byte[] b = loadClassData(name);
117
      return defineClass(name, b, 0, b.length);
118
   }
119
 
120
   public URL findResource(String name)
121
   {
122
      StreamInfo streamInfo = getResourceStream(name);
123
      if (null == streamInfo) {
124
         return super.findResource(name);
125
      }
126
      else {
127
         try {
128
            return streamInfo.getURL();
129
         }
130
         catch (MalformedURLException e) {
131
            System.err.println("WARNING: In FileSystemClassLoader: could not derive URL from file or jar entry: " + e.toString());
132
            return null;
133
         }
134
      }
135
   }
136
 
137
   private byte[] readFromStream(InputStream in, long size)
138
      throws IOException
139
   {
140
      byte[] result = new byte[(int)size];
141
      int nread = 0;
142
      int offset = 0;
143
      while (offset < size && (nread = in.read(result, offset, (int)(size - offset))) >= 0) {
144
         offset += nread;
145
      }
146
      in.close();
147
      return result;
148
   }
149
 
150
   private byte[] readFromStream(StreamInfo streamInfo)
151
      throws IOException
152
   {
153
      InputStream in = streamInfo.openStream();
154
      long size = streamInfo.getSize();
155
 
156
      byte[] result = new byte[(int)size];
157
      int nread = 0;
158
      int offset = 0;
159
      while (offset < size && (nread = in.read(result, offset, (int)(size - offset))) >= 0) {
160
         offset += nread;
161
      }
162
      in.close();
163
      return result;
164
   }
165
 
166
   private static interface StreamInfo
167
   {
168
      public InputStream openStream()
169
         throws IOException;
170
      public long getSize();
171
      public URL getURL()
172
         throws MalformedURLException;
173
   }
174
 
175
   private static class FileStreamInfo
176
      implements StreamInfo
177
   {
178
      File file;
179
 
180
      FileStreamInfo(File file)
181
      {
182
         this.file = file;
183
      }
184
 
185
      public InputStream openStream()
186
         throws IOException
187
      {
188
         return new FileInputStream(file);
189
      }
190
 
191
      public long getSize()
192
      {
193
         return file.length();
194
      }
195
 
196
      public URL getURL()
197
         throws MalformedURLException
198
      {
199
         return file.toURL();
200
      }
201
   }
202
 
203
   private static class JarStreamInfo
204
      implements StreamInfo
205
   {
206
      private File file;
207
      private JarFile jarFile;
208
      private JarEntry jarEntry;
209
 
210
      JarStreamInfo(File file, JarFile jarFile, JarEntry jarEntry)
211
      {
212
         this.file = file;
213
         this.jarFile = jarFile;
214
         this.jarEntry = jarEntry;
215
      }
216
 
217
      public InputStream openStream()
218
         throws IOException
219
      {
220
         return jarFile.getInputStream(jarEntry);
221
      }
222
 
223
      public long getSize()
224
      {
225
         return jarEntry.getSize();
226
      }
227
 
228
      public URL getURL()
229
         throws MalformedURLException
230
      {
231
         String urlString = "jar:" + file.toURL() + "!/" + jarEntry.getName();
232
         return new URL(urlString);
233
      }
234
   }
235
 
236
   private StreamInfo getResourceStream(String path)
237
   {
238
      for (int i = 0; i < pathComponents.length; ++i) {
239
         try {
240
            File parent = pathComponents[i];
241
            if (parent.isDirectory()) {
242
               File file = new File(parent, path);
243
               if (file.exists()) {
244
                  return new FileStreamInfo(file);
245
               }
246
            }
247
            else {
248
               JarFile jarFile = new JarFile(parent, false, JarFile.OPEN_READ);
249
               JarEntry jarEntry = jarFile.getJarEntry(path);
250
               if (null != jarEntry) {
251
                  return new JarStreamInfo(parent, jarFile, jarEntry);
252
               }
253
            }
254
         }
255
         catch (IOException ignore) {
256
         }
257
      }
258
      return null;
259
   }
260
 
261
   private byte[] loadClassData(String className)
262
      throws ClassNotFoundException
263
   {
264
      String classFileName = className.replace('.', File.separatorChar) + ".class";
265
      StreamInfo streamInfo = getResourceStream(classFileName);
266
 
267
      try {
268
         if (null != streamInfo) {
269
            return readFromStream(streamInfo);
270
         }
271
      }
272
      catch (IOException ignore) {
273
      }
274
 
275
      throw new ClassNotFoundException(className);
276
   }
277
 
278
   private static List tryGetJarFileClassPathComponents(File file)
279
   {
280
      try {
281
         JarFile jarFile = new JarFile(file, false, JarFile.OPEN_READ);
282
         Manifest manifest = jarFile.getManifest();
283
         if (null != manifest) {
284
            Attributes mainAttributes = manifest.getMainAttributes();
285
            if (null != mainAttributes) {
286
               String classPath = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
287
               if (null != classPath) {
288
                  List result = new LinkedList();
289
                  StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(classPath));
290
                  tokenizer.resetSyntax();
291
                  tokenizer.wordChars(0, Integer.MAX_VALUE);
292
                  tokenizer.whitespaceChars(9, 9);   // tab
293
                  tokenizer.whitespaceChars(10, 10); // lf
294
                  tokenizer.whitespaceChars(13, 13); // cr
295
                  tokenizer.whitespaceChars(32, 32); // space
296
                  tokenizer.quoteChar('"');
297
                  int token;
298
                  while ((token = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {
299
                     if (StreamTokenizer.TT_WORD == token) {
300
                        result.add(new File(file.getParentFile(), tokenizer.sval));
301
                     }
302
                  }
303
                  return result;
304
               }
305
            }
306
         }
307
      }
308
      catch (IOException ignore) {
309
      }
310
      return null;
311
   }
312
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.