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 dalvik.annotation.optimization.FastNative; 20 import java.io.File; 21 import java.io.IOException; 22 import java.net.URL; 23 import java.net.URLStreamHandler; 24 import java.util.ArrayList; 25 import java.util.List; 26 import libcore.io.ClassPathURLStreamHandler; 27 28 class VMClassLoader { 29 30 private static final ClassPathURLStreamHandler[] bootClassPathUrlHandlers; 31 static { 32 bootClassPathUrlHandlers = createBootClassPathUrlHandlers(); 33 } 34 35 /** 36 * Creates an array of ClassPathURLStreamHandler objects for handling resource loading from 37 * the boot classpath. 38 */ createBootClassPathUrlHandlers()39 private static ClassPathURLStreamHandler[] createBootClassPathUrlHandlers() { 40 String[] bootClassPathEntries = getBootClassPathEntries(); 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 urlStreamHandlers.add(urlStreamHandler); 51 } catch (IOException e) { 52 // Skip it 53 System.logE("Unable to open boot classpath entry: " + bootClassPathEntry, e); 54 } 55 } 56 return urlStreamHandlers.toArray(new ClassPathURLStreamHandler[urlStreamHandlers.size()]); 57 } 58 59 /** 60 * Get a resource from a file in the bootstrap class path. 61 * 62 * We assume that the bootclasspath can't change once the VM has started. 63 * This assumption seems to be supported by the spec. 64 */ getResource(String name)65 static URL getResource(String name) { 66 for (ClassPathURLStreamHandler urlHandler : bootClassPathUrlHandlers) { 67 URL url = urlHandler.getEntryUrlOrNull(name); 68 if (url != null) { 69 return url; 70 } 71 } 72 return null; 73 } 74 75 /* 76 * Get an enumeration with all matching resources. 77 */ getResources(String name)78 static List<URL> getResources(String name) { 79 ArrayList<URL> list = new ArrayList<URL>(); 80 for (ClassPathURLStreamHandler urlHandler : bootClassPathUrlHandlers) { 81 URL url = urlHandler.getEntryUrlOrNull(name); 82 if (url != null) { 83 list.add(url); 84 } 85 } 86 return list; 87 } 88 89 @FastNative 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