001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2006 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution.lang;
010:
011: import java.util.Enumeration;
012: import j2me.io.File;
013: import j2me.util.zip.ZipFile;
014: import j2me.util.zip.ZipEntry;
015:
016: import javolution.util.StandardLog;
017:
018: /**
019: * <p> This utility class allows for initialization of all classes
020: * at startup to avoid initialization delays at an innapropriate time.</p>
021: *
022: * <p> Note: Users might want to disable logging when initializing run-time
023: * classes at start-up because of the presence of old classes (never used)
024: * in the jar files for which initialization fails. For example:[code]
025: * public static main(String[] args) {
026: * LogContext.enter(LogContext.NULL); // Temporarely disables logging errors and warnings.
027: * try {
028: * ClassInitializer.initializeAll(); // Initializes bootstrap, extensions and classpath classes.
029: * } finally {
030: * LogContext.exit(LogContext.NULL); // Goes back to default logging.
031: * }
032: * ...
033: * }[/code]</p>
034: *
035: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
036: * @version 3.6, November 6, 2005
037: */
038: public class ClassInitializer {
039:
040: /**
041: * Default constructor (private for utility class)
042: */
043: private ClassInitializer() {
044: }
045:
046: /**
047: * Initializes all runtime and classpath classes.
048: *
049: * @see #initializeRuntime()
050: * @see #initializeClassPath()
051: */
052: public static void initializeAll() {
053: initializeRuntime();
054: initializeClassPath();
055: }
056:
057: /**
058: * Initializes runtime classes (bootstrap classes in <code>
059: * System.getProperty("sun.boot.class.path"))</code> and the
060: * extension <code>.jar</code> in <code>lib/ext</code> directory).
061: */
062: public static void initializeRuntime() {
063: String bootPath = System.getProperty("sun.boot.class.path");
064: String pathSeparator = System.getProperty("path.separator");
065: if ((bootPath == null) || (pathSeparator == null)) {
066: StandardLog
067: .warning("Cannot initialize boot path through system properties");
068: return;
069: }
070: initialize(bootPath, pathSeparator);
071: String javaHome = System.getProperty("java.home");
072: String fileSeparator = System.getProperty("file.separator");
073: if ((javaHome == null) || (fileSeparator == null)) {
074: StandardLog
075: .warning("Cannot initialize extension library through system properties");
076: return;
077: }
078: File extDir = new File(javaHome + fileSeparator + "lib"
079: + fileSeparator + "ext");
080: if (!extDir.getClass().getName().equals("java.io.File")) {
081: StandardLog
082: .warning("Extension classes initialization not supported for J2ME build");
083: return;
084: }
085: if (extDir.isDirectory()) {
086: File[] files = extDir.listFiles();
087: for (int i = 0; i < files.length; i++) {
088: String path = files[i].getPath();
089: if (path.endsWith(".jar") || path.endsWith(".zip")) {
090: initializeJar(path);
091: }
092: }
093: } else {
094: StandardLog.warning(extDir + " is not a directory");
095: }
096: }
097:
098: /**
099: * Initializes all classes in current classpath.
100: */
101: public static void initializeClassPath() {
102: String classPath = System.getProperty("java.class.path");
103: String pathSeparator = System.getProperty("path.separator");
104: if ((classPath == null) || (pathSeparator == null)) {
105: StandardLog
106: .warning("Cannot initialize classpath through system properties");
107: return;
108: }
109: initialize(classPath, pathSeparator);
110: }
111:
112: private static void initialize(String classPath,
113: String pathSeparator) {
114: StandardLog.fine("Initialize classpath: " + classPath);
115: while (classPath.length() > 0) {
116: String name;
117: int index = classPath.indexOf(pathSeparator);
118: if (index < 0) {
119: name = classPath;
120: classPath = "";
121: } else {
122: name = classPath.substring(0, index);
123: classPath = classPath.substring(index
124: + pathSeparator.length());
125: }
126: if (name.endsWith(".jar") || name.endsWith(".zip")) {
127: initializeJar(name);
128: } else {
129: initializeDir(name);
130: }
131: }
132: }
133:
134: /**
135: * Initializes the specified class.
136: *
137: * @param cls the class to initialize.
138: */
139: public static void initialize(Class cls) {
140: try {
141: Reflection.getClass(cls.getName());
142: } catch (Throwable error) {
143: StandardLog.error(error);
144: }
145: }
146:
147: /**
148: * Initializes the class with the specified name.
149: *
150: * @param className the name of the class to initialize.
151: */
152: public static void initialize(String className) {
153: try {
154: Reflection.getClass(className);
155: } catch (ClassNotFoundException e) {
156: StandardLog.warning("Class + " + className + " not found");
157: } catch (Throwable error) {
158: StandardLog.error(error);
159: }
160: }
161:
162: /**
163: * Initializes all the classes in the specified jar file.
164: *
165: * @param jarName the jar filename.
166: */
167: public static void initializeJar(String jarName) {
168: try {
169: StandardLog.fine("Initialize Jar file: " + jarName);
170: ZipFile jarFile = new ZipFile(jarName);
171: if (!jarFile.getClass().getName().equals(
172: "java.util.zip.ZipFile")) {
173: StandardLog
174: .warning("Initialization of classes in jar file not supported for J2ME build");
175: return;
176: }
177: Enumeration e = jarFile.entries();
178: while (e.hasMoreElements()) {
179: ZipEntry entry = (ZipEntry) e.nextElement();
180: String entryName = entry.getName();
181: if (entryName.endsWith(".class")) {
182: String className = entryName.substring(0, entryName
183: .length() - 6);
184: className = className.replace('/', '.');
185: StandardLog.finer("Initialize " + className);
186: ClassInitializer.initialize(className);
187: }
188: }
189: } catch (Exception e) {
190: StandardLog.error(e);
191: }
192: }
193:
194: /**
195: * Initializes all the classes in the specified directory.
196: *
197: * @param dirName the name of the directory containing the classes to
198: * initialize.
199: */
200: public static void initializeDir(String dirName) {
201: StandardLog.fine("Initialize Directory: " + dirName);
202: File file = new File(dirName);
203: if (!file.getClass().getName().equals("java.io.File")) {
204: StandardLog
205: .warning("Initialization of classes in directory not supported for J2ME build");
206: return;
207: }
208: if (file.isDirectory()) {
209: File[] files = file.listFiles();
210: for (int i = 0; i < files.length; i++) {
211: initialize("", files[i]);
212: }
213: } else {
214: StandardLog.warning(dirName + " is not a directory");
215: }
216: }
217:
218: private static void initialize(String prefix, File file) {
219: String name = file.getName();
220: if (file.isDirectory()) {
221: File[] files = file.listFiles();
222: String newPrefix = (prefix.length() == 0) ? name : prefix
223: + "." + name;
224: for (int i = 0; i < files.length; i++) {
225: initialize(newPrefix, files[i]);
226: }
227: } else {
228: if (name.endsWith(".class")) {
229: String className = prefix + "."
230: + name.substring(0, name.length() - 6);
231: StandardLog.finer("Initialize " + className);
232: ClassInitializer.initialize(className);
233: }
234: }
235: }
236:
237: }
|