TouchDex.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 17package dalvik.system; 18 19import java.io.BufferedReader; 20import java.io.InputStreamReader; 21import java.io.IOException; 22import java.io.File; 23import java.io.FilenameFilter; 24 25/** 26 * Induce optimization/verification of a set of DEX files. 27 * 28 * TODO: This class is public, so SystemServer can access it. This is NOT 29 * the correct long-term solution; once we have a real installer and/or 30 * dalvik-cache manager, this class should be removed. 31 */ 32public class TouchDex { 33 34 /** 35 * Fork a process, make sure the DEX files are prepped, and return 36 * when everything is finished. 37 * 38 * The filenames must be the same as will be used when the files are 39 * actually opened, because the dalvik-cache filename is based upon 40 * this filename. (The absolute path to the jar/apk should work.) 41 * 42 * @param dexFiles Colon-separated list of DEX files. 43 * @return zero on success 44 */ 45 public static int start(String dexFiles) { 46 return trampoline(dexFiles, System.getProperty("java.boot.class.path")); 47 } 48 49 /** 50 * This calls fork() and then, in the child, calls cont(dexFiles). 51 * 52 * @param dexFiles Colon-separated list of DEX files. 53 * @return zero on success 54 */ 55 native private static int trampoline(String dexFiles, String bcp); 56 57 /** 58 * We continue here in the child process. args[0] can be a colon-separated 59 * path list, or "-" to read from stdin. 60 * 61 * Alternatively, if we're invoked directly from the command line we 62 * just start here (skipping the fork/exec stuff). 63 * 64 * @param args command line args 65 */ 66 public static void main(String[] args) { 67 68 if ("-".equals(args[0])) { 69 BufferedReader in = new BufferedReader( 70 new InputStreamReader(System.in), 256); 71 72 String line; 73 try { 74 while ((line = in.readLine()) != null) { 75 prepFiles(line); 76 } 77 } catch (IOException ex) { 78 throw new RuntimeException ("Error processing stdin"); 79 } 80 } else { 81 prepFiles(args[0]); 82 } 83 84 System.out.println(" Prep complete"); 85 } 86 87 88 private static String expandDirectories(String dexPath) { 89 String[] parts = dexPath.split(":"); 90 StringBuilder outPath = new StringBuilder(dexPath.length()); 91 92 // A filename filter accepting *.jar and *.apk 93 FilenameFilter filter = new FilenameFilter() { 94 public boolean accept(File dir, String name) { 95 return name.endsWith(".jar") || name.endsWith(".apk"); 96 } 97 }; 98 99 for (String part: parts) { 100 File f = new File(part); 101 102 if (f.isFile()) { 103 outPath.append(part); 104 outPath.append(':'); 105 } else if (f.isDirectory()) { 106 String[] filenames = f.list(filter); 107 108 if (filenames == null) { 109 System.err.println("I/O error with directory: " + part); 110 continue; 111 } 112 113 for (String filename: filenames) { 114 outPath.append(part); 115 outPath.append(File.separatorChar); 116 outPath.append(filename); 117 outPath.append(':'); 118 } 119 } else { 120 System.err.println("File not found: " + part); 121 } 122 } 123 124 125 return outPath.toString(); 126 } 127 128 private static void prepFiles(String dexPath) { 129 130 System.out.println(" Prepping: " + dexPath); 131 132 TouchDexLoader loader 133 = new TouchDexLoader(expandDirectories(dexPath), null); 134 135 try { 136 /* By looking for a nonexistent class, we'll trick TouchDexLoader 137 * into trying to load something from every file on dexPath, 138 * optimizing all of them as a side-effect. 139 * 140 * The optimization happens implicitly in the VM the first time 141 * someone tries to load a class from an unoptimized dex file. 142 */ 143 loader.loadClass("com.google.NonexistentClassNeverFound"); 144 throw new RuntimeException("nonexistent class loaded?!"); 145 } catch (ClassNotFoundException cnfe) { 146 //System.out.println("got expected dnfe"); 147 } 148 } 149} 150 151