KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > lang > ClassInitializer


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.lang;
10
11 import java.util.Enumeration JavaDoc;
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 /**
19  * <p> This utility class allows for initialization of all Java(tm) classes
20  * at startup to avoid initialization delays at an innapropriate time.</p>
21  * <p> Initialization logs are available through
22  * {@link javolution.context.LogContext LogContext}. For example:[code]
23  * public static main(String[] args) {
24  * LogContext.enter(LogContext.NULL); // Temporarely disables logging errors and warnings.
25  * try {
26  * ClassInitializer.initializeAll(); // Initializes bootstrap, extensions and classpath classes.
27  * } finally {
28  * LogContext.exit(LogContext.NULL);
29  * }
30  * ...
31  * }[/code]</p>
32  *
33  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
34  * @version 3.6, November 6, 2005
35  */

36 public class ClassInitializer {
37
38     /**
39      * Default constructor (private for utility class)
40      */

41     private ClassInitializer() {
42     }
43
44     /**
45      * Initializes all runtime and classpath classes.
46      *
47      * @see #initializeRuntime()
48      * @see #initializeClassPath()
49      */

50     public static void initializeAll() {
51         initializeRuntime();
52         initializeClassPath();
53     }
54
55     /**
56      * Initializes runtime classes (bootstrap classes in <code>
57      * System.getProperty("sun.boot.class.path"))</code> and the
58      * extension <code>.jar</code> in <code>lib/ext</code> directory).
59      */

60     public static void initializeRuntime() {
61         String JavaDoc bootPath = System.getProperty("sun.boot.class.path");
62         String JavaDoc 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 JavaDoc javaHome = System.getProperty("java.home");
70         String JavaDoc 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 JavaDoc 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     /**
97      * Initializes all classes in current classpath.
98      */

99     public static void initializeClassPath() {
100         String JavaDoc classPath = System.getProperty("java.class.path");
101         String JavaDoc 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 JavaDoc classPath, String JavaDoc pathSeparator) {
111         StandardLog.fine("Initialize classpath: " + classPath);
112         while (classPath.length() > 0) {
113             String JavaDoc 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     /**
131      * Initializes the specified class.
132      *
133      * @param cls the class to initialize.
134      */

135     public static void initialize(Class JavaDoc cls) {
136         try {
137             Reflection.getClass(cls.getName());
138         } catch (Throwable JavaDoc error) {
139             StandardLog.error(error);
140         }
141     }
142
143     /**
144      * Initializes the class with the specified name.
145      *
146      * @param className the name of the class to initialize.
147      */

148     public static void initialize(String JavaDoc className) {
149         try {
150             Reflection.getClass(className);
151         } catch (ClassNotFoundException JavaDoc e) {
152             StandardLog.warning("Class + " + className + " not found");
153         } catch (Throwable JavaDoc error) {
154             StandardLog.error(error);
155         }
156     }
157
158     /**
159      * Initializes all the classes in the specified jar file.
160      *
161      * @param jarName the jar filename.
162      */

163     public static void initializeJar(String JavaDoc 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 JavaDoc e = jarFile.entries();
173             while (e.hasMoreElements()) {
174                 ZipEntry entry = (ZipEntry) e.nextElement();
175                 String JavaDoc entryName = entry.getName();
176                 if (entryName.endsWith(".class")) {
177                     String JavaDoc 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 JavaDoc e) {
185             StandardLog.error(e);
186         }
187     }
188
189     /**
190      * Initializes all the classes in the specified directory.
191      *
192      * @param dirName the name of the directory containing the classes to
193      * initialize.
194      */

195     public static void initializeDir(String JavaDoc 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 JavaDoc prefix, File file) {
214         String JavaDoc name = file.getName();
215         if (file.isDirectory()) {
216             File[] files = file.listFiles();
217             String JavaDoc 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 JavaDoc 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