KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > WSClassLoader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.websphere6;
20
21 import java.io.*;
22 import java.net.*;
23 import java.util.*;
24
25 import org.netbeans.modules.j2ee.websphere6.util.WSDebug;
26
27 /**
28  * The singleton classloader that is used for loading the WebSphere classes.
29  * This loader has exactly one instance per server domain and is set as the
30  * thread's context classloader before any operation on WS classes is called.
31  *
32  * @author Kirill Sorokin
33  */

34 public class WSClassLoader extends URLClassLoader {
35
36     /**
37      * A <code>HashMap</code> used to store all registered instances of the
38      * loader
39      */

40     private static Map instances = new HashMap();
41     
42     /**
43      * A factory method.
44      * It is responsible for maintaining a single instance of
45      * <code>WSClassLoader</code> for each profile root.
46      *
47      * @param serverRoot path to the server installation directory
48      * @param domainRoot path to the profile root directory
49      */

50     public static WSClassLoader getInstance(String JavaDoc serverRoot,
51             String JavaDoc domainRoot) {
52         if (WSDebug.isEnabled()) // debug output
53
WSDebug.notify(WSClassLoader.class, "getInstance(" + // NOI18N
54
serverRoot + ", " + domainRoot + ")"); // NOI18N
55

56         // check whether such instance is already registered
57
WSClassLoader instance = (WSClassLoader) instances.get(domainRoot);
58         
59         // if it's not, create a new one and register
60
if (instance == null) {
61             instance = new WSClassLoader(serverRoot, domainRoot);
62             instances.put(domainRoot, instance);
63         }
64         
65         // return
66
return instance;
67     }
68     
69     /**
70      * Path to the server installation directory
71      */

72     private String JavaDoc serverRoot;
73     
74     /**
75      * Path to the profile root directory
76      */

77     private String JavaDoc domainRoot;
78     
79     /**
80      * Constructs an instance of the <code>WSClassLoader</code> with the
81      * specified server installation directory and the profile root directory.
82      *
83      * @param serverRoot path to the server installation directory
84      * @param domainRoot path to the profile root directory
85      */

86     private WSClassLoader(String JavaDoc serverRoot, String JavaDoc domainRoot) {
87         // we have to isolate the loader from the netbeans main loader in order
88
// to avoid conflicts with SOAP classes implementations
89
super(new URL[0], Thread.currentThread().getContextClassLoader());
90         
91         // save the instance variables
92
this.serverRoot = serverRoot;
93         this.domainRoot = domainRoot;
94         
95         // add the required directories to the class path
96
File[] directories = new File[] {
97             new File(serverRoot + "/lib/"), // NOI18N
98
new File(serverRoot + "/java/jre/lib/"), // NOI18N
99
new File(serverRoot + "/java/jre/lib/ext/"), // NOI18N
100
new File(serverRoot + "/lib/WMQ/java/lib/"),
101             new File(serverRoot + "/cloudscape/lib/"),
102             new File(serverRoot + "/cloudscape/lib/locales/"),
103             new File(serverRoot + "/cloudscape/lib/otherjars/"),
104             new File(serverRoot + "/deploytool/itp/"),
105             new File(serverRoot + "/deploytool/itp/plugins/"),
106             new File(serverRoot + "/installedChannels/"),
107             new File(serverRoot + "/etc/"),
108             new File(serverRoot + "/optionalLibraries/Apache/Struts/1.1/")
109         };
110         
111         // for each directory add all the .jar files to the class path
112
// and finally add the directory itself
113
for (int i = 0; i < directories.length; i++) {
114             File directory = directories[i];
115             if (directory.exists() && directory.isDirectory()) {
116                 File[] children = directory.listFiles(new JarFileFilter());
117                 for (int j = 0; j < children.length; j++) {
118                     try {
119                         addURL(children[j].toURL());
120                     } catch (MalformedURLException e) {
121                         // do nothing just skip this jar file
122
}
123                 }
124             }
125             try {
126                 addURL(directory.toURL());
127             } catch (MalformedURLException e) {
128                 // do nothing just skip this directory
129
}
130         }
131     }
132     
133     /**
134      * Handle for the clasloader that was the context loader for the current
135      * thread before update
136      */

137     private ClassLoader JavaDoc oldLoader;
138     
139     /**
140      * Updates the context classloader of the current thread.
141      * The old loader is saved so that a restore operation is possible.
142      */

143     public void updateLoader() {
144         if (WSDebug.isEnabled()) // debug output
145
WSDebug.notify(getClass(), "updateLoader()"); // NOI18N
146

147         // set the system properties that are required for correct functioning
148
// of WebSphere
149
System.setProperty("websphere.home", serverRoot); // NOI18N
150
System.setProperty("was.install.root", serverRoot); // NOI18N
151
System.setProperty("was.repository.root", domainRoot + // NOI18N
152
File.separator + "config"); // NOI18N
153

154         // if debugging is enabled set the system property pointing to the WS
155
// debug properties file
156
if (WSDebug.isEnabled())
157             System.setProperty("traceSettingsFile", // NOI18N
158
"TraceSettings.properties"); // NOI18N
159

160         // save the current context loader and update the thread if we are not
161
// already the context loader
162
if (!Thread.currentThread().getContextClassLoader().equals(this)) {
163             oldLoader = Thread.currentThread().getContextClassLoader();
164             Thread.currentThread().setContextClassLoader(this);
165         }
166     }
167     
168     /**
169      * Restores the thread's context classloader.
170      * Set the current thread's context loader to the classloader stored in
171      * <code>oldLoader</code> variable.
172      */

173     public void restoreLoader() {
174         if (WSDebug.isEnabled()) // debug output
175
WSDebug.notify(getClass(), "restoreLoader()"); // NOI18N
176

177         // restore the loader if it's not null
178
if (oldLoader != null) {
179             Thread.currentThread().setContextClassLoader(oldLoader);
180             oldLoader = null;
181         }
182     }
183     
184     /**
185      * File filter that accepts only .jar files.
186      *
187      * @author Kirill Sorokin
188      */

189     private static class JarFileFilter implements FileFilter {
190         /**
191          * Checks whether the supplied file complies with the filter
192          * requirements.
193          *
194          * @return whether the file complies with the requirements
195          */

196         public boolean accept(File file) {
197             // check the file's extension, if it's '.jar' then the file is ok
198
if (file.getName().endsWith(".jar")) { // NOI18N
199
return true;
200             } else {
201                 return false;
202             }
203         }
204     }
205 }
206
Popular Tags