001: package com.bostechcorp.cbesb.ui.util.ucm;
002:
003: import java.io.File;
004: import java.io.FileFilter;
005: import java.io.IOException;
006: import java.net.MalformedURLException;
007: import java.net.URL;
008: import java.net.URLClassLoader;
009: import java.util.HashMap;
010: import java.util.Vector;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
016:
017: public class UcmClassURLHelper {
018: private static URL[] runtimeUrls = null;
019: private static final Log log = LogFactory
020: .getLog(UcmClassURLHelper.class);
021: private static Vector<URL> runtimeUrlsList = new Vector<URL>();
022: public final static String SUFFIX = "cbesb-core-1.0";//$NON-NLS-1$
023: private static HashMap<String, Vector<URL>> saUrlsMap = new HashMap<String, Vector<URL>>();
024:
025: /*
026: * Extract the runtime URLs from the environment variable.
027: *
028: */
029: static {
030:
031: String home = EsbPathHelper.getCbesbHomeDir();
032: String libPath = "";
033: if (home != null) {
034: libPath = home + "/lib/ext/" + SUFFIX;
035:
036: File libDir = new File(libPath);//$NON-NLS-1$ //$NON-NLS-2$
037:
038: // Lists every modules in the lib dir
039: File[] jars = libDir.listFiles(new FileFilter() {
040: public boolean accept(File file) {
041: return (file.toString().endsWith(".jar"));//$NON-NLS-1$
042: }
043: });
044:
045: if (jars != null) {
046:
047: runtimeUrls = new URL[jars.length];
048:
049: for (int i = 0; i < jars.length; i++) {
050: File jarFile = jars[i];
051: try {
052: runtimeUrls[i] = jarFile.toURL();
053:
054: runtimeUrlsList.add(runtimeUrls[i]);
055: } catch (IOException ioe) {
056: //TODO handle Exception
057: Log log = LogFactory
058: .getLog(UcmClassURLHelper.class);
059: log
060: .error("environment variable CBESB is malformed: "
061: + ioe);
062: }
063:
064: }
065: }
066: }
067: }
068:
069: public static void addProjectLibExtToPath(String saName,
070: String saLocation) {
071: File libDir = new File(saLocation + "/lib/optional");//$NON-NLS-1$ //$NON-NLS-2$
072:
073: // Lists every modules in the lib dir
074: File[] jars = libDir.listFiles(new FileFilter() {
075: public boolean accept(File file) {
076: return (file.toString().endsWith(".jar"));//$NON-NLS-1$
077: }
078: });
079:
080: if (jars != null) {
081:
082: for (int i = 0; i < jars.length; i++) {
083: File jarFile = jars[i];
084: addToPath(saName, jarFile.getAbsolutePath());
085:
086: }
087: }
088: }
089:
090: public static void addUcmJarToPath(String saName, String saLocation) {
091:
092: File ucmJar = new File(saLocation
093: + "/lib/CBESB_ucm_" + saName + ".jar");//$NON-NLS-1$ //$NON-NLS-2$
094:
095: if (ucmJar.exists()) {
096: addToPath(saName, ucmJar.getAbsolutePath());
097: }
098:
099: }
100:
101: /*
102: * Add to the SA URL list
103: */
104: public static void addToPath(String saName, String newPath) {
105: log.debug("addToPath(" + saName + ", " + newPath + ")\n\n");
106:
107: try {
108: URL newUrl = null;
109: try {
110: newUrl = new URL(newPath);
111: } catch (MalformedURLException e) {
112: // if its malformed then try to evaluate it as a file
113: newUrl = new File(newPath).toURL();
114: }
115: Vector<URL> saList = saUrlsMap.get(saName);
116: if (saList == null) {
117: saList = new Vector<URL>();
118: saUrlsMap.put(saName, saList);
119: }
120: if (!saList.contains(newUrl))
121: saList.add(newUrl);
122: } catch (Exception e) {
123: Log log = LogFactory.getLog(UcmClassURLHelper.class);
124: log.error("error in RuntimeClassLoader addToPath("
125: + newPath + ") " + e);
126: }
127: }
128:
129: /*
130: * Contructs a URLClassLoader from the CBESB_CLASSPATH environment variable.
131: *
132: * @param parent An object to use as the source of the parent class loader
133: * @return a URLClassLoader to use for runtime objects such as translates and parsers.
134: */
135: public static URL[] getURL() {
136: // add the system class path to the runtime URL list
137: URLClassLoader systemLoader = (URLClassLoader) ClassLoader
138: .getSystemClassLoader();
139: URL[] sysUrls = systemLoader.getURLs();
140:
141: URL[] newUrls = new URL[runtimeUrlsList.size() + sysUrls.length];
142: runtimeUrlsList.toArray(newUrls);
143: int dest = runtimeUrlsList.size();
144: for (int i = 0; i < sysUrls.length; i++) {
145: newUrls[dest++] = sysUrls[i];
146: }
147: return newUrls;
148:
149: }
150:
151: /*
152: * Contructs a service assembly specific URLClassLoader from the CBESB_CLASSPATH environment
153: * variable and the SA URL list.
154: *
155: * @param saName The service assembly name
156: * @param parent An object to use as the source of the parent class loader
157: * @return a URLClassLoader to use for runtime objects such as translates and parsers.
158: */
159: public static URL[] getURL(String saName, String saLocation) {
160: if (saName == null)
161: return getURL();
162:
163: addUcmJarToPath(saName, saLocation);
164: addProjectLibExtToPath(saName, saLocation);
165: // calculate the total url list size
166: int urlCount = runtimeUrlsList.size();
167: Vector<URL> saUrlsList = saUrlsMap.get(saName);
168: if (saUrlsList != null)
169: urlCount += saUrlsList.size();
170: // add the system class path to the runtime URL list
171: URLClassLoader systemLoader = (URLClassLoader) ClassLoader
172: .getSystemClassLoader();
173: URL[] sysUrls = systemLoader.getURLs();
174: urlCount += sysUrls.length;
175:
176: URL[] newUrls = new URL[urlCount];
177: // populate the url list
178: runtimeUrlsList.toArray(newUrls);
179: int dest = runtimeUrlsList.size();
180: if (saUrlsList != null) {
181: int src = 0;
182: for (; src < saUrlsList.size(); src++)
183: newUrls[dest++] = saUrlsList.elementAt(src);
184: }
185: for (int i = 0; i < sysUrls.length; i++)
186: newUrls[dest++] = sysUrls[i];
187:
188: return newUrls;
189: }
190:
191: public static void clearSaPath(String saName) {
192: saUrlsMap.remove(saName);
193: }
194:
195: }
|