1 19 23 24 package org.netbeans.modules.j2ee.sun.api; 25 26 27 import java.io.File ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Collections ; 33 34 import org.openide.modules.InstalledFileLocator; 35 import javax.enterprise.deploy.spi.factories.DeploymentFactory ; 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 INSTALL_ROOT_PROP_NAME = "com.sun.aas.installRoot"; private static final String JAR_BRIGDES_DEFINITION_LAYER="/J2EE/SunAppServer/Bridge"; private static Map ServerLocationAndClassLoaderMap = Collections.synchronizedMap((Map )new HashMap (2,1)); 46 47 private static void updatePluginLoader(File platformLocation, ExtendedClassLoader loader) throws Exception { 48 try { 49 java.io.File f = platformLocation; 50 if (null == f || !f.exists()){ 51 return; 52 } 53 String installRoot = f.getAbsolutePath(); 54 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 location= (String )ch[i].getAttribute("jar.location"); 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); 77 } 78 79 f = new File (installRoot+"/lib/appserv-admin.jar"); loader.addURL(f); 81 f = new File (installRoot+"/lib/appserv-ext.jar"); loader.addURL(f); 83 f = new File (installRoot+"/lib/appserv-rt.jar"); loader.addURL(f); 85 f = new File (installRoot+"/lib/appserv-cmp.jar"); loader.addURL(f); 87 f = new File (installRoot+"/lib/commons-logging.jar"); loader.addURL(f); 89 f = new File (installRoot+"/lib/admin-cli.jar"); loader.addURL(f); 91 f = new File (installRoot+"/lib/common-laucher.jar"); loader.addURL(f); 93 f = new File (installRoot+"/lib/j2ee.jar"); loader.addURL(f); 95 f = new File (installRoot+"/lib/install/applications/jmsra/imqjmsra.jar"); loader.addURL(f); 97 98 f = new File (installRoot+"/lib/jaxrpc-api.jar"); loader.addURL(f); 108 f = new File (installRoot+"/lib/jaxrpc-impl.jar"); loader.addURL(f); 110 111 112 } catch (Exception ex2) { 113 throw new Exception (ex2.getLocalizedMessage()); 114 } 115 } 116 117 118 119 120 126 127 static public File getLatestPlatformLocation(){ 128 Iterator i = ServerLocationAndClassLoaderMap.entrySet().iterator(); 129 File ret =null; 130 while (i.hasNext()){ 131 Map.Entry e = (Map.Entry )i.next(); 132 String loc = (String )e.getKey(); 133 File possibleOne = new File (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 149 static class Empty{ 150 151 } 152 153 154 public static ClassLoader getServerOnlyClassLoader(File platformLocation){ 155 CacheData data =(CacheData) ServerLocationAndClassLoaderMap.get(platformLocation.getAbsolutePath()); 156 if (data==null){ 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 getDeploymentFactory(File platformLocation) { 167 CacheData data =(CacheData) ServerLocationAndClassLoaderMap.get(platformLocation.getAbsolutePath()); 168 if (data==null){ 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 getNetBeansAndServerClassLoader(File 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 ) data.cachedClassLoader.loadClass("com.sun.enterprise.deployapi.SunDeploymentFactory").newInstance(); data.serverOnlyClassLoader = new ExtendedClassLoader(); 198 updatePluginLoader(platformLocation, data.serverOnlyClassLoader); 199 } catch (Exception 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 fileColl = new java.util.ArrayList (); 209 210 static { 211 fileColl.add("bin"); fileColl.add("lib"); fileColl.add("config"); } 215 216 public static boolean isGlassFish(File candidate){ 217 File as9 = new File (candidate.getAbsolutePath()+ 219 "/lib/dtds/sun-web-app_2_5-0.dtd"); return as9.exists(); 221 } 222 224 public static boolean isJavaDBPresent(File installdir){ 225 File derbyInstall = new File (installdir,"derby"); if (!derbyInstall.exists()){ 228 derbyInstall = new File (installdir,"javadb"); } 230 return derbyInstall.exists(); 231 } 232 public static boolean isGoodAppServerLocation(File candidate){ 234 if (null == candidate || !candidate.exists() || !candidate.canRead() || 235 !candidate.isDirectory() || !hasRequiredChildren(candidate, fileColl)) { 236 237 return false; 238 } 239 File f = new File (candidate.getAbsolutePath()+"/lib/appserv-rt.jar"); if (!f.exists()){ 241 return false; 242 } 243 244 if(isGlassFish(candidate)){ 246 return true; } 248 249 return true; 253 254 } 255 256 257 258 259 260 private static void loadLocaleSpecificJars(File file, ExtendedClassLoader loader) { 261 File parentDir = file.getParentFile(); 262 File localeDir = new File (parentDir, "locale"); if(localeDir.exists()){ 265 File [] localeFiles = localeDir.listFiles(); 266 File localeFile; String localeFileName; String fileName = file.getName(); 269 fileName = getFileNameWithoutExt(fileName); 270 assert(fileName.length() > 0); 272 for(int i=0; i<localeFiles.length; i++){ 273 localeFile = localeFiles[i]; 274 localeFileName = localeFile.getName(); 275 assert(localeFileName.length() > 0); 277 if(localeFileName.startsWith(fileName)){ 278 try{ 279 loader.addURL(localeFile); 280 }catch (Exception ex2) { 281 System.out.println(ex2.getLocalizedMessage()); 282 } 283 } 284 } 285 } 286 } 287 288 private static String getFileNameWithoutExt(String fileName){ 289 int index = fileName.lastIndexOf("."); if(index != -1){ 291 fileName = fileName.substring(0, index); 292 } 293 return fileName; 294 } 295 296 private static boolean hasRequiredChildren(File candidate, Collection requiredChildren) { 297 if (null == candidate){ 298 return false; 299 } 300 String [] children = candidate.list(); 301 if (null == children){ 302 return false; 303 } 304 if (null == requiredChildren){ 305 return true; 306 } 307 java.util.List 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 deploymentFactory; 319 320 } 321 } 322 | Popular Tags |