1 9 package javolution.lang; 10 11 import java.util.Enumeration ; 12 import j2me.io.File; 13 import j2me.util.zip.ZipFile; 14 import j2me.util.zip.ZipEntry; 15 16 import javolution.util.StandardLog; 17 18 36 public class ClassInitializer { 37 38 41 private ClassInitializer() { 42 } 43 44 50 public static void initializeAll() { 51 initializeRuntime(); 52 initializeClassPath(); 53 } 54 55 60 public static void initializeRuntime() { 61 String bootPath = System.getProperty("sun.boot.class.path"); 62 String pathSeparator = System.getProperty("path.separator"); 63 if ((bootPath == null) || (pathSeparator == null)) { 64 StandardLog 65 .warning("Cannot initialize boot path through system properties"); 66 return; 67 } 68 initialize(bootPath, pathSeparator); 69 String javaHome = System.getProperty("java.home"); 70 String fileSeparator = System.getProperty("file.separator"); 71 if ((javaHome == null) || (fileSeparator == null)) { 72 StandardLog 73 .warning("Cannot initialize extension library through system properties"); 74 return; 75 } 76 File extDir = new File(javaHome + fileSeparator + "lib" + fileSeparator 77 + "ext"); 78 if (!extDir.getClass().getName().equals("java.io.File")) { 79 StandardLog 80 .warning("Extension classes initialization not supported for J2ME build"); 81 return; 82 } 83 if (extDir.isDirectory()) { 84 File[] files = extDir.listFiles(); 85 for (int i = 0; i < files.length; i++) { 86 String path = files[i].getPath(); 87 if (path.endsWith(".jar") || path.endsWith(".zip")) { 88 initializeJar(path); 89 } 90 } 91 } else { 92 StandardLog.warning(extDir + " is not a directory"); 93 } 94 } 95 96 99 public static void initializeClassPath() { 100 String classPath = System.getProperty("java.class.path"); 101 String pathSeparator = System.getProperty("path.separator"); 102 if ((classPath == null) || (pathSeparator == null)) { 103 StandardLog 104 .warning("Cannot initialize classpath through system properties"); 105 return; 106 } 107 initialize(classPath, pathSeparator); 108 } 109 110 private static void initialize(String classPath, String pathSeparator) { 111 StandardLog.fine("Initialize classpath: " + classPath); 112 while (classPath.length() > 0) { 113 String name; 114 int index = classPath.indexOf(pathSeparator); 115 if (index < 0) { 116 name = classPath; 117 classPath = ""; 118 } else { 119 name = classPath.substring(0, index); 120 classPath = classPath.substring(index + pathSeparator.length()); 121 } 122 if (name.endsWith(".jar") || name.endsWith(".zip")) { 123 initializeJar(name); 124 } else { 125 initializeDir(name); 126 } 127 } 128 } 129 130 135 public static void initialize(Class cls) { 136 try { 137 Reflection.getClass(cls.getName()); 138 } catch (Throwable error) { 139 StandardLog.error(error); 140 } 141 } 142 143 148 public static void initialize(String className) { 149 try { 150 Reflection.getClass(className); 151 } catch (ClassNotFoundException e) { 152 StandardLog.warning("Class + " + className + " not found"); 153 } catch (Throwable error) { 154 StandardLog.error(error); 155 } 156 } 157 158 163 public static void initializeJar(String jarName) { 164 try { 165 StandardLog.fine("Initialize Jar file: " + jarName); 166 ZipFile jarFile = new ZipFile(jarName); 167 if (!jarFile.getClass().getName().equals("java.util.zip.ZipFile")) { 168 StandardLog 169 .warning("Initialization of classes in jar file not supported for J2ME build"); 170 return; 171 } 172 Enumeration e = jarFile.entries(); 173 while (e.hasMoreElements()) { 174 ZipEntry entry = (ZipEntry) e.nextElement(); 175 String entryName = entry.getName(); 176 if (entryName.endsWith(".class")) { 177 String className = entryName.substring(0, entryName 178 .length() - 6); 179 className = className.replace('/', '.'); 180 StandardLog.finer("Initialize " + className); 181 ClassInitializer.initialize(className); 182 } 183 } 184 } catch (Exception e) { 185 StandardLog.error(e); 186 } 187 } 188 189 195 public static void initializeDir(String dirName) { 196 StandardLog.fine("Initialize Directory: " + dirName); 197 File file = new File(dirName); 198 if (!file.getClass().getName().equals("java.io.File")) { 199 StandardLog 200 .warning("Initialization of classes in directory not supported for J2ME build"); 201 return; 202 } 203 if (file.isDirectory()) { 204 File[] files = file.listFiles(); 205 for (int i = 0; i < files.length; i++) { 206 initialize("", files[i]); 207 } 208 } else { 209 StandardLog.warning(dirName + " is not a directory"); 210 } 211 } 212 213 private static void initialize(String prefix, File file) { 214 String name = file.getName(); 215 if (file.isDirectory()) { 216 File[] files = file.listFiles(); 217 String newPrefix = (prefix.length() == 0) ? name : prefix + "." 218 + name; 219 for (int i = 0; i < files.length; i++) { 220 initialize(newPrefix, files[i]); 221 } 222 } else { 223 if (name.endsWith(".class")) { 224 String className = prefix + "." 225 + name.substring(0, name.length() - 6); 226 StandardLog.finer("Initialize " + className); 227 ClassInitializer.initialize(className); 228 } 229 } 230 } 231 232 } | Popular Tags |