Source Code Cross Referenced for ClassInitializer.java in  » Development » Javolution » javolution » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Javolution » javolution.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.