librariesStr,
ServerEnvironment env) {
if(librariesStr == null)
return null;
final URL [] urls = new URL[librariesStr.size()];
String [] librariesStrArray = new String[librariesStr.size()];
librariesStrArray = librariesStr.toArray(librariesStrArray);
return getDeployParamLibrariesAsURLs(env, librariesStrArray, urls);
}
/**
* converts libraries specified via the --libraries deployment option to
* URL[]. The library JAR files are specified by either relative or
* absolute paths. The relative path is relative to
* instance-root/lib/applibs. The libraries are made available to
* the application in the order specified.
*
* @param librariesStr is a comma-separated list of library JAR files
* @param env the server environment
* @return array of URL
*/
public static URL[] getDeployParamLibrariesAsURLs(String librariesStr,
ServerEnvironment env) {
if(librariesStr == null)
return null;
String [] librariesStrArray = librariesStr.split(",");
final URL [] urls = new URL[librariesStrArray.length];
return getDeployParamLibrariesAsURLs(env, librariesStrArray, urls);
}
private static URL[] getDeployParamLibrariesAsURLs(ServerEnvironment env, String[] librariesStrArray,
URL[] urls) {
final String appLibsDir = env.getLibPath()
+ File.separator + "applibs";
int i=0;
for(final String libraryStr:librariesStrArray){
try {
File f = new File(libraryStr);
if(!f.isAbsolute())
f = new File(appLibsDir, libraryStr);
URL url =f.toURI().toURL();
urls[i++] = url;
} catch (MalformedURLException malEx) {
_logger.log(Level.WARNING, "Cannot convert classpath to URL",
libraryStr);
_logger.log(Level.WARNING, malEx.getMessage(), malEx);
}
}
return urls;
}
private static synchronized String getModulesClasspath(Habitat habitat) {
synchronized (ASClassLoaderUtil.class) {
if (modulesClassPath == null) {
final StringBuilder tmpString = new StringBuilder();
ModulesRegistry mr = habitat.getComponent(ModulesRegistry.class);
if (mr != null) {
for (Module module : mr.getModules()) {
for (URI uri : module.getModuleDefinition().getLocations()) {
tmpString.append(uri.getPath());
tmpString.append(File.pathSeparator);
}
}
}
//set shared classpath for module so that it doesn't need to be
//recomputed for every other invocation
modulesClassPath = tmpString.toString();
}
}
return modulesClassPath;
}
/**
* Returns an array of urls that contains ..
*
* i. all the valid directories from the given directory (dirs) array
* ii. all jar files from the given directory (jarDirs) array
* iii. all zip files from the given directory (jarDirs) array if
* not ignoring zip file (ignoreZip is false).
*
*
* @param dirs array of directory path names
* @param jarDirs array of path name to directories that contains
* JAR & ZIP files.
* @param ignoreZip whether to ignore zip files
* @return an array of urls that contains all the valid dirs,
* *.jar & *.zip
*
* @throws IOException if an i/o error while constructing the urls
*/
public static URL[] getURLs(File[] dirs, File[] jarDirs,
boolean ignoreZip) throws IOException {
return convertURLListToArray(getURLsAsList(dirs, jarDirs, ignoreZip));
}
/**
* Returns a list of urls that contains ..
*
* i. all the valid directories from the given directory (dirs) array
* ii. all jar files from the given directory (jarDirs) array
* iii. all zip files from the given directory (jarDirs) array if
* not ignoring zip file (ignoreZip is false).
*
*
* @param dirs array of directory path names
* @param jarDirs array of path name to directories that contains
* JAR & ZIP files.
* @param ignoreZip whether to ignore zip files
* @return an array of urls that contains all the valid dirs,
* *.jar & *.zip
*
* @throws IOException if an i/o error while constructing the urls
*/
public static List getURLsAsList(File[] dirs, File[] jarDirs,
boolean ignoreZip) throws IOException {
List list = new ArrayList();
// adds all directories
if (dirs != null) {
for (int i=0; i list) {
// converts the list to an array
URL[] urls = new URL[0];
if (list != null && list.size() > 0) {
urls = new URL[list.size()];
urls = (URL[]) list.toArray(urls);
}
return urls;
}
/**
* get URL list from classpath
* @param classpath classpath string containing the classpaths
* @param delimiter delimiter to separate the classpath components
* in the classpath string
* @param rootPath root path of the classpath if the paths are relative
* @return urlList URL list from the given classpath
*/
public static List getURLsFromClasspath(String classpath,
String delimiter, String rootPath) {
final List urls = new ArrayList();
if (classpath == null || classpath.length() == 0) {
return urls;
}
// tokenize classpath
final StringTokenizer st = new StringTokenizer(classpath, delimiter);
while (st.hasMoreTokens()) {
try {
String path = st.nextToken();
try {
// try to see if the path is absolute
URL url = new URL(path);
URI uri = url.toURI();
if (uri.isAbsolute()) {
urls.add(uri.toURL());
continue;
}
} catch (Exception ie) {
// ignore
}
if (rootPath != null && rootPath.length() != 0) {
path = rootPath + File.separator + path;
}
File f = new File(path);
urls.add(f.toURI().toURL());
} catch(Exception e) {
_logger.log(Level.WARNING,
"unexpected error in getting urls",e);
}
}
return urls;
}
/**
* Returns the manifest file for the given root path.
*
*
* Example:
* |--repository/
* | |--applications/
* | |--converter/
* | |--ejb-jar-ic_jar/ <---- rootPath
* | |--META-INF/
* | |--MANIFEST.MF
*
*
* @param rootPath absolute path to the module
*
* @return the manifest file for the given module
*/
public static Manifest getManifest(String rootPath) {
InputStream in = null;
Manifest mf = null;
// gets the input stream to the MANIFEST.MF file
try {
in = new FileInputStream(rootPath+File.separator+MANIFEST_ENTRY);
if (in != null) {
mf = new Manifest(in);
}
} catch (IOException ioe) {
// ignore
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
// Ignore
}
}
}
return mf;
}
/**
* Returns the class path (if any) from the given manifest file as an
* URL list.
*
* @param manifest manifest file of an archive
* @param rootPath root path to the module
*
* @return a list of URLs
* an empty list if given manifest is null
*/
public static List getManifestClassPathAsURLs(Manifest manifest,
String rootPath) {
List urlList = new ArrayList();
if (manifest != null) {
Attributes mainAttributes = manifest.getMainAttributes();
for (Iterator itr=mainAttributes.keySet().iterator();
itr.hasNext();) {
Attributes.Name next = (Attributes.Name) itr.next();
if (next.equals(Attributes.Name.CLASS_PATH)) {
String classpathString = (String) mainAttributes.get(next);
urlList = getURLsFromClasspath(classpathString, " ",
rootPath);
}
}
}
return urlList;
}
/**
* add all the libraries packaged in the application library directory
*
* @param appRoot the application root
* @param appLibDir the Application library directory
* @param compatibilityProp the version of the release that we need to
* maintain backward compatibility
* @return an array of URL
*/
public static URL[] getAppLibDirLibraries(File appRoot, String appLibDir,
String compatibilityProp)
throws IOException {
return convertURLListToArray(
getAppLibDirLibrariesAsList(appRoot, appLibDir, compatibilityProp));
}
public static List getAppLibDirLibrariesAsList(File appRoot, String appLibDir, String compatibilityProp)
throws IOException {
URL[] libDirLibraries = new URL[0];
// get all the app lib dir libraries
if (appLibDir != null) {
String libPath = appLibDir.replace('/', File.separatorChar);
libDirLibraries = getURLs(null,
new File[] {new File(appRoot, libPath)}, true);
}
List allLibDirLibraries = new ArrayList();
for (URL url : libDirLibraries) {
allLibDirLibraries.add(url);
}
// if the compatibility property is set to "v2", we should add all the
// jars under the application root to maintain backward compatibility
// of v2 jar visibility
if (compatibilityProp != null && compatibilityProp.equals("v2")) {
List appRootLibraries = getURLsAsList(null, new File[] {appRoot}, true);
allLibDirLibraries.addAll(appRootLibraries);
}
return allLibDirLibraries;
}
}