KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > api > ServerLocationManager


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 /*
20  * ServerLocationManager
21  *
22  */

23
24 package org.netbeans.modules.j2ee.sun.api;
25
26
27 import java.io.File JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Collections JavaDoc;
33
34 import org.openide.modules.InstalledFileLocator;
35 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.Repository;
38 import org.openide.util.Lookup;
39
40
41 public class ServerLocationManager {
42
43     public static final String JavaDoc INSTALL_ROOT_PROP_NAME = "com.sun.aas.installRoot"; //NOI18N
44
private static final String JavaDoc JAR_BRIGDES_DEFINITION_LAYER="/J2EE/SunAppServer/Bridge"; //NOI18N
45
private static Map JavaDoc ServerLocationAndClassLoaderMap = Collections.synchronizedMap((Map JavaDoc)new HashMap JavaDoc(2,1));
46     
47     private static void updatePluginLoader(File JavaDoc platformLocation, ExtendedClassLoader loader) throws Exception JavaDoc{
48         try {
49             java.io.File JavaDoc f = platformLocation;
50             if (null == f || !f.exists()){
51                 return;
52             }
53             String JavaDoc installRoot = f.getAbsolutePath();
54             //if we are only 8.1 set the necessary property there:
55
if(!isGlassFish(f)){
56                 System.setProperty(INSTALL_ROOT_PROP_NAME, installRoot);
57             }
58             
59             Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class);
60             FileObject bridgesDir = rep.getDefaultFileSystem().findResource(JAR_BRIGDES_DEFINITION_LAYER);
61             FileObject[] ch =new FileObject[0];
62             if(bridgesDir!=null){
63                 ch = bridgesDir.getChildren();
64             }
65             
66             for(int i = 0; i < ch.length; i++) {
67                 String JavaDoc location= (String JavaDoc)ch[i].getAttribute("jar.location");//NOI18N
68
//System.out.println("Location is "+location + platformLocation);
69
InstalledFileLocator fff= InstalledFileLocator.getDefault();
70                 f = fff.locate(location, null, true);
71                 if (f!=null){
72                     loader.addURL(f);
73                     loadLocaleSpecificJars(f, loader);
74                 } else
75                     System.out.println("cannot locate file "+location);//NOI18N
76

77             }
78             
79             f = new File JavaDoc(installRoot+"/lib/appserv-admin.jar");//NOI18N
80
loader.addURL(f);
81         f = new File JavaDoc(installRoot+"/lib/appserv-ext.jar");//NOI18N
82
loader.addURL(f);
83         f = new File JavaDoc(installRoot+"/lib/appserv-rt.jar");//NOI18N
84
loader.addURL(f);
85         f = new File JavaDoc(installRoot+"/lib/appserv-cmp.jar");//NOI18N
86
loader.addURL(f);
87         f = new File JavaDoc(installRoot+"/lib/commons-logging.jar");//NOI18N
88
loader.addURL(f);
89         f = new File JavaDoc(installRoot+"/lib/admin-cli.jar");//NOI18N
90
loader.addURL(f);
91         f = new File JavaDoc(installRoot+"/lib/common-laucher.jar");//NOI18N
92
loader.addURL(f);
93         f = new File JavaDoc(installRoot+"/lib/j2ee.jar");//NOI18N
94
loader.addURL(f);
95         f = new File JavaDoc(installRoot+"/lib/install/applications/jmsra/imqjmsra.jar");//NOI18N
96
loader.addURL(f);
97         
98         //for AS 8.1: no more endorsed dir!!!
99
////// f = new File(installRoot+"/lib/xercesImpl.jar");
100
////// loader.addURL(f);
101
////// f = new File(installRoot+"/lib/dom.jar");
102
////// loader.addURL(f);
103
////// f = new File(installRoot+"/lib/xalan.jar");
104
// loader.addURL(f);
105
//for AS 8.1:
106
f = new File JavaDoc(installRoot+"/lib/jaxrpc-api.jar");//NOI18N
107
loader.addURL(f);
108         f = new File JavaDoc(installRoot+"/lib/jaxrpc-impl.jar");//NOI18N
109
loader.addURL(f);
110         
111         
112     } catch (Exception JavaDoc ex2) {
113         throw new Exception JavaDoc(ex2.getLocalizedMessage());
114     }
115     }
116     
117     
118
119     
120     /* return the latest available platform, i.e if 9 and 8.1 are regsitered,
121      ** this will return the location of the AS 9 server
122      ** this way we can access the latest DTDs, etc that cover also the 8.1 server
123      ** because of the backward compatibility requirement.
124      * may return null if no platform is available...
125      **/

126     
127     static public File JavaDoc getLatestPlatformLocation(){
128         Iterator JavaDoc i = ServerLocationAndClassLoaderMap.entrySet().iterator();
129         File JavaDoc ret =null;
130         while (i.hasNext()){
131             Map.Entry JavaDoc e = (Map.Entry JavaDoc)i.next();
132             String JavaDoc loc = (String JavaDoc)e.getKey();
133             File JavaDoc possibleOne = new File JavaDoc(loc);
134             if (ret==null){
135                 ret =possibleOne;
136             }
137             if (isGlassFish(possibleOne)){
138                 ret =possibleOne;
139             }
140         }
141         return ret;
142         
143     }
144     
145     /*
146      *used to get the netbeans classload of this class.
147      *
148      **/

149     static class Empty{
150     
151     }
152     
153     
154     public static ClassLoader JavaDoc getServerOnlyClassLoader(File JavaDoc platformLocation){
155     CacheData data =(CacheData) ServerLocationAndClassLoaderMap.get(platformLocation.getAbsolutePath());
156     if (data==null){// try to initialize it
157
getNetBeansAndServerClassLoader(platformLocation);
158         data =(CacheData) ServerLocationAndClassLoaderMap.get(platformLocation.getAbsolutePath());
159             if (data==null){
160                 return null;
161             }
162     }
163             return data.serverOnlyClassLoader;
164
165     }
166     public synchronized static DeploymentFactory JavaDoc getDeploymentFactory(File JavaDoc platformLocation) {
167     CacheData data =(CacheData) ServerLocationAndClassLoaderMap.get(platformLocation.getAbsolutePath());
168     if (data==null){// try to initialize it
169
getNetBeansAndServerClassLoader(platformLocation);
170         data =(CacheData) ServerLocationAndClassLoaderMap.get(platformLocation.getAbsolutePath());
171             if (data==null){
172                 return null;
173             }
174     }
175     return data.deploymentFactory;
176     
177     }
178     
179     public synchronized static ClassLoader JavaDoc getNetBeansAndServerClassLoader(File JavaDoc platformLocation) {
180     CacheData data =(CacheData) ServerLocationAndClassLoaderMap.get(platformLocation.getAbsolutePath());
181     if (data==null){
182         if (!isGoodAppServerLocation(platformLocation)){
183         return null;
184             }
185             data = new CacheData();
186         ServerLocationAndClassLoaderMap.put(platformLocation.getAbsolutePath(), data);
187         
188     }
189     if(data.cachedClassLoader==null){
190         if (!isGoodAppServerLocation(platformLocation)){
191         return null;
192             }
193         try {
194         data.cachedClassLoader =new ExtendedClassLoader( new Empty().getClass().getClassLoader());
195         updatePluginLoader( platformLocation, data.cachedClassLoader);
196         data.deploymentFactory = (DeploymentFactory JavaDoc) data.cachedClassLoader.loadClass("com.sun.enterprise.deployapi.SunDeploymentFactory").newInstance();//NOI18N
197
data.serverOnlyClassLoader = new ExtendedClassLoader();
198                 updatePluginLoader(platformLocation, data.serverOnlyClassLoader);
199         } catch (Exception JavaDoc ex2) {
200         org.openide.ErrorManager.getDefault().notify(ex2);
201         System.out.println(ex2);
202         }}
203     
204     return data.cachedClassLoader;
205     }
206     
207     
208     private static Collection JavaDoc fileColl = new java.util.ArrayList JavaDoc();
209     
210     static {
211     fileColl.add("bin"); //NOI18N
212
fileColl.add("lib"); //NOI18N
213
fileColl.add("config"); //NOI18N
214
}
215     
216     public static boolean isGlassFish(File JavaDoc candidate){
217     //now test for AS 9 (J2EE 5.0) which should work for this plugin
218
File JavaDoc as9 = new File JavaDoc(candidate.getAbsolutePath()+
219                 "/lib/dtds/sun-web-app_2_5-0.dtd"); //NOI18N
220
return as9.exists();
221     }
222     /* return true if derby/javaDB is installed with the server
223      **/

224     public static boolean isJavaDBPresent(File JavaDoc installdir){
225     //check for both names, derby or jaadb
226
File JavaDoc derbyInstall = new File JavaDoc(installdir,"derby");//NOI18N
227
if (!derbyInstall.exists()){
228              derbyInstall = new File JavaDoc(installdir,"javadb");//NOI18N for latest Glassfish
229
}
230     return derbyInstall.exists();
231     }
232     // TODO remove isGoodAppServerLocation from PluginProperties.java???
233
public static boolean isGoodAppServerLocation(File JavaDoc candidate){
234     if (null == candidate || !candidate.exists() || !candidate.canRead() ||
235         !candidate.isDirectory() || !hasRequiredChildren(candidate, fileColl)) {
236         
237         return false;
238     }
239         File JavaDoc f = new File JavaDoc(candidate.getAbsolutePath()+"/lib/appserv-rt.jar");//NOI18N
240
if (!f.exists()){
241             return false;
242         }
243         
244     //now test for AS 9 (J2EE 5.0) which should work for this plugin
245
if(isGlassFish(candidate)){
246         return true;//we are as9
247
}
248     
249     //one extra test to detect 8.0 versus 8.1: dom.jar has to be in lib not endorsed anymore:
250
//// File f = new File(candidate.getAbsolutePath()+"/lib/dom.jar");
251
//// return f.exists();
252
return true;
253     
254     }
255     
256     
257     
258     
259     
260     private static void loadLocaleSpecificJars(File JavaDoc file, ExtendedClassLoader loader) {
261     File JavaDoc parentDir = file.getParentFile();
262     //System.out.println("parentDir: " + parentDir);
263
File JavaDoc localeDir = new File JavaDoc(parentDir, "locale"); //NOI18N
264
if(localeDir.exists()){
265         File JavaDoc[] localeFiles = localeDir.listFiles();
266         File JavaDoc localeFile; // = null;
267
String JavaDoc localeFileName;// = null;
268
String JavaDoc fileName = file.getName();
269         fileName = getFileNameWithoutExt(fileName);
270         //System.out.println("fineName: " + fileName);
271
assert(fileName.length() > 0);
272         for(int i=0; i<localeFiles.length; i++){
273         localeFile = localeFiles[i];
274         localeFileName = localeFile.getName();
275         //System.out.println("localeFileName: " + localeFileName);
276
assert(localeFileName.length() > 0);
277         if(localeFileName.startsWith(fileName)){
278             try{
279             loader.addURL(localeFile);
280             }catch (Exception JavaDoc ex2) {
281             System.out.println(ex2.getLocalizedMessage());
282             }
283         }
284         }
285     }
286     }
287     
288     private static String JavaDoc getFileNameWithoutExt(String JavaDoc fileName){
289     int index = fileName.lastIndexOf("."); //NOI18N
290
if(index != -1){
291         fileName = fileName.substring(0, index);
292     }
293     return fileName;
294     }
295
296     private static boolean hasRequiredChildren(File JavaDoc candidate, Collection JavaDoc requiredChildren) {
297         if (null == candidate){
298             return false;
299         }
300         String JavaDoc[] children = candidate.list();
301         if (null == children){
302             return false;
303         }
304         if (null == requiredChildren){
305             return true;
306         }
307         java.util.List JavaDoc kidsList = java.util.Arrays.asList(children);
308         return kidsList.containsAll(requiredChildren);
309     }
310     
311     static class CacheData{
312     public CacheData(){
313         
314     }
315     public ExtendedClassLoader cachedClassLoader;
316     public ExtendedClassLoader serverOnlyClassLoader;
317         
318     public DeploymentFactory JavaDoc deploymentFactory;
319     
320     }
321 }
322
Popular Tags