1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package java.lang;
18 
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.net.URLStreamHandler;
23 import java.util.ArrayList;
24 import java.util.List;
25 import libcore.io.ClassPathURLStreamHandler;
26 
27 class VMClassLoader {
28 
29     private static final ClassPathURLStreamHandler[] bootClassPathUrlHandlers;
30     static {
31         bootClassPathUrlHandlers = createBootClassPathUrlHandlers();
32     }
33 
34     /**
35      * Creates an array of ClassPathURLStreamHandler objects for handling resource loading from
36      * the boot classpath.
37      */
createBootClassPathUrlHandlers()38     private static ClassPathURLStreamHandler[] createBootClassPathUrlHandlers() {
39         String[] bootClassPathEntries = getBootClassPathEntries();
40         ArrayList<String> zipFileUris = new ArrayList<String>(bootClassPathEntries.length);
41         ArrayList<URLStreamHandler> urlStreamHandlers =
42                 new ArrayList<URLStreamHandler>(bootClassPathEntries.length);
43         for (String bootClassPathEntry : bootClassPathEntries) {
44             try {
45                 String entryUri = new File(bootClassPathEntry).toURI().toString();
46 
47                 // We assume all entries are zip or jar files.
48                 URLStreamHandler urlStreamHandler =
49                         new ClassPathURLStreamHandler(bootClassPathEntry);
50                 zipFileUris.add(entryUri);
51                 urlStreamHandlers.add(urlStreamHandler);
52             } catch (IOException e) {
53                 // Skip it
54                 System.logE("Unable to open boot classpath entry: " + bootClassPathEntry, e);
55             }
56         }
57         return urlStreamHandlers.toArray(new ClassPathURLStreamHandler[urlStreamHandlers.size()]);
58     }
59 
60     /**
61      * Get a resource from a file in the bootstrap class path.
62      *
63      * We assume that the bootclasspath can't change once the VM has started.
64      * This assumption seems to be supported by the spec.
65      */
getResource(String name)66     static URL getResource(String name) {
67         for (ClassPathURLStreamHandler urlHandler : bootClassPathUrlHandlers) {
68             URL url = urlHandler.getEntryUrlOrNull(name);
69             if (url != null) {
70                 return url;
71             }
72         }
73         return null;
74     }
75 
76     /*
77      * Get an enumeration with all matching resources.
78      */
getResources(String name)79     static List<URL> getResources(String name) {
80         ArrayList<URL> list = new ArrayList<URL>();
81         for (ClassPathURLStreamHandler urlHandler : bootClassPathUrlHandlers) {
82             URL url = urlHandler.getEntryUrlOrNull(name);
83             if (url != null) {
84                 list.add(url);
85             }
86         }
87         return list;
88     }
89 
findLoadedClass(ClassLoader cl, String name)90     native static Class findLoadedClass(ClassLoader cl, String name);
91 
92     /**
93      * Boot class path manipulation, for getResources().
94      */
getBootClassPathEntries()95     native private static String[] getBootClassPathEntries();
96 
97 }
98