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 // We assume all entries are zip or jar files. 46 URLStreamHandler urlStreamHandler = 47 new ClassPathURLStreamHandler(bootClassPathEntry); 48 urlStreamHandlers.add(urlStreamHandler); 49 } catch (IOException e) { 50 // Skip it 51 System.logE("Unable to open boot classpath entry: " + bootClassPathEntry, e); 52 } 53 } 54 return urlStreamHandlers.toArray(new ClassPathURLStreamHandler[urlStreamHandlers.size()]); 55 } 56 57 /** 58 * Get a resource from a file in the bootstrap class path. 59 * 60 * We assume that the bootclasspath can't change once the VM has started. 61 * This assumption seems to be supported by the spec. 62 */ getResource(String name)63 static URL getResource(String name) { 64 for (ClassPathURLStreamHandler urlHandler : bootClassPathUrlHandlers) { 65 URL url = urlHandler.getEntryUrlOrNull(name); 66 if (url != null) { 67 return url; 68 } 69 } 70 return null; 71 } 72 73 /* 74 * Get an enumeration with all matching resources. 75 */ getResources(String name)76 static List<URL> getResources(String name) { 77 ArrayList<URL> list = new ArrayList<URL>(); 78 for (ClassPathURLStreamHandler urlHandler : bootClassPathUrlHandlers) { 79 URL url = urlHandler.getEntryUrlOrNull(name); 80 if (url != null) { 81 list.add(url); 82 } 83 } 84 return list; 85 } 86 87 @FastNative findLoadedClass(ClassLoader cl, String name)88 native static Class findLoadedClass(ClassLoader cl, String name); 89 90 /** 91 * Boot class path manipulation, for getResources(). 92 */ getBootClassPathEntries()93 native private static String[] getBootClassPathEntries(); 94 95 } 96