1 23 24 package com.sun.appserv.server.util; 25 26 import java.io.File ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.List ; 30 import java.util.logging.*; 31 import com.sun.enterprise.config.ConfigContext; 32 import com.sun.enterprise.config.ConfigException; 33 import com.sun.enterprise.config.serverbeans.Applications; 34 import com.sun.enterprise.config.serverbeans.Domain; 35 import com.sun.enterprise.config.serverbeans.EjbModule; 36 import com.sun.enterprise.config.serverbeans.J2eeApplication; 37 import com.sun.enterprise.config.serverbeans.WebModule; 38 import com.sun.enterprise.server.ApplicationServer; 39 import com.sun.enterprise.server.PELaunch; 40 import com.sun.enterprise.server.ServerContext; 41 import com.sun.enterprise.util.SystemPropertyConstants; 42 43 public class ASClassLoaderUtil { 44 private static Logger _logger = Logger.getAnonymousLogger(); 45 46 47 private static ClassLoader sharedCL; 48 49 57 public static String getWebModuleClassPath(String moduleId) { 58 if (_logger.isLoggable(Level.FINE)) { 59 _logger.log(Level.FINE, "module Id : " + moduleId); 60 } 61 62 StringBuilder classpath = new StringBuilder (); 63 if (Boolean.getBoolean(PELaunch.USE_NEW_CLASSLOADER_PROPERTY)) { 64 List <String > cp = PELaunch.getSharedClasspath(); 67 for(String s:cp){ 68 classpath.append(s); 69 classpath.append(File.pathSeparatorChar); 70 } 71 } else { 72 classpath.append(System.getProperty("java.class.path")); 73 } 74 75 if (moduleId != null) { 76 String specifiedLibraries = getLibrariesForWebModule(moduleId); 77 URL [] libs = getLibraries(specifiedLibraries); 78 if (libs == null) { 79 if (_logger.isLoggable(Level.FINE)) { 80 _logger.log(Level.FINE, "classpath: " + classpath.toString()); 81 } 82 return classpath.toString(); 83 } 84 85 for (URL u : libs) { 86 classpath.append(u + File.pathSeparator); 87 } 88 } 89 90 if (_logger.isLoggable(Level.FINE)) { 91 _logger.log(Level.FINE, "Final classpath: " + classpath.toString()); 92 } 93 return classpath.toString(); 94 } 95 96 102 public static String getLibrariesForJ2EEApplication(String moduleId) { 103 J2eeApplication app = null; 104 try { 105 app = getApplications().getJ2eeApplicationByName(moduleId); 106 if(app == null) return null; 107 } catch(ConfigException malEx) { 108 _logger.log(Level.WARNING, "loader.cannot_convert_classpath_into_url", 109 moduleId); 110 _logger.log(Level.WARNING,"loader.exception", malEx); 111 } 112 return app.getLibraries(); 113 } 114 115 121 public static String getLibrariesForWebModule(String moduleId) { 122 WebModule app = null; 123 try { 124 app = getApplications().getWebModuleByName(moduleId); 125 if(app == null) return null; 126 } catch(ConfigException malEx) { 127 _logger.log(Level.WARNING, "loader.cannot_convert_classpath_into_url", 128 moduleId); 129 _logger.log(Level.WARNING,"loader.exception", malEx); 130 } 131 132 String librariesStr = app.getLibraries(); 133 if (_logger.isLoggable(Level.FINE)) { 134 _logger.log(Level.FINE, "app = " + app + " library = " + librariesStr); 135 } 136 return librariesStr; 137 } 138 139 145 public static String getLibrariesForEJBJars(String moduleId) { 146 EjbModule app = null; 147 try { 148 app = getApplications().getEjbModuleByName(moduleId); 149 if(app == null) return null; 150 } catch(ConfigException malEx) { 151 _logger.log(Level.WARNING, "loader.cannot_convert_classpath_into_url", 152 moduleId); 153 _logger.log(Level.WARNING,"loader.exception", malEx); 154 } 155 return app.getLibraries(); 156 } 157 158 private static Applications getApplications() throws ConfigException { 160 ConfigContext serverConfigCtx = ApplicationServer.getServerContext().getConfigContext(); 161 Domain domain = ((Domain)serverConfigCtx.getRootConfigBean()); 162 return domain.getApplications(); 163 } 164 165 174 public static URL [] getLibraries(String librariesStr) { 175 if(librariesStr == null) 176 return null; 177 178 String [] librariesStrArray = librariesStr.split(","); 179 if(librariesStrArray == null) 180 return null; 181 182 URL [] urls = new URL [librariesStrArray.length]; 183 String appLibsDir = System.getProperty( 186 SystemPropertyConstants.INSTANCE_ROOT_PROPERTY) 187 + File.separator + "lib" 188 + File.separator + "applibs"; 189 190 int i=0; 191 for(String libraryStr:librariesStrArray){ 192 try { 193 File f = new File (libraryStr); 194 if(!f.isAbsolute()) 195 f = new File (appLibsDir, libraryStr); 196 URL url = f.toURL(); 197 urls[i++] = url; 198 } catch (MalformedURLException malEx) { 199 _logger.log(Level.WARNING, 200 "loader.cannot_convert_classpath_into_url", 201 libraryStr); 202 _logger.log(Level.WARNING,"loader.exception", malEx); 203 } 204 } 205 return urls; 206 } 207 208 212 public static ClassLoader getSharedClassLoader() { 213 if(sharedCL == null) { 214 ServerContext sc = ApplicationServer.getServerContext(); 215 sharedCL = sc.getSharedClassLoader(); 216 } 217 return sharedCL; 218 } 219 } 220 | Popular Tags |