001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id:$
023: */
024: package com.bostechcorp.cbesb.ui.util.ucm;
025:
026: import java.io.File;
027: import java.io.FileFilter;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.net.URLClassLoader;
033: import java.util.ArrayList;
034: import java.util.Enumeration;
035: import java.util.HashMap;
036: import java.util.List;
037: import java.util.Vector;
038: import java.util.zip.ZipEntry;
039: import java.util.zip.ZipFile;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.eclipse.ui.PlatformUI;
044:
045: import com.bostechcorp.cbesb.common.i18n.Message;
046: import com.bostechcorp.cbesb.common.i18n.Messages;
047: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
048: import com.bostechcorp.cbesb.common.util.custcomponent.ICustComponent;
049: import com.bostechcorp.cbesb.ui.util.MessageDetailDialog;
050:
051: public class CustCompClassLoader extends URLClassLoader {
052:
053: private static URL[] runtimeUrls = null;
054:
055: private static final Log log = LogFactory
056: .getLog(CustCompClassLoader.class);
057:
058: private static Vector<URL> runtimeUrlsList = new Vector<URL>();
059:
060: private static String customCompPath = "/customComp";
061:
062: private static HashMap<String, Vector<URL>> saUrlsMap = new HashMap<String, Vector<URL>>();
063:
064: private String saName;
065:
066: private String saLocation;
067:
068: public static void addSACustCompToPath(String saName,
069: String saLocation) {
070: File libDir = new File(saLocation + "/src/customComp");//$NON-NLS-1$ //$NON-NLS-2$
071:
072: // Lists every modules in the lib dir
073: File[] jars = libDir.listFiles(new FileFilter() {
074: public boolean accept(File file) {
075: return (file.toString().endsWith(".jar"));//$NON-NLS-1$
076: }
077: });
078:
079: if (jars != null) {
080:
081: for (int i = 0; i < jars.length; i++) {
082: File jarFile = jars[i];
083: addToPath(saName, jarFile.getAbsolutePath());
084:
085: }
086: }
087:
088: }
089:
090: /*
091: * Add to the SA URL list
092: */
093: private static void addToPath(String saName, String newPath) {
094: log.debug("addToPath(" + saName + ", " + newPath + ")\n\n");
095:
096: try {
097: URL newUrl = null;
098: try {
099: newUrl = new URL(newPath);
100: } catch (MalformedURLException e) {
101: // if its malformed then try to evaluate it as a file
102: newUrl = new File(newPath).toURL();
103: }
104: Vector<URL> saList = saUrlsMap.get(saName);
105: if (saList == null) {
106: saList = new Vector<URL>();
107: saUrlsMap.put(saName, saList);
108: }
109: if (!saList.contains(newUrl))
110: saList.add(newUrl);
111: } catch (Exception e) {
112: //TODO
113: Log log = LogFactory.getLog(UcmClassURLHelper.class);
114: log.error("error in RuntimeClassLoader addToPath("
115: + newPath + ") " + e);
116: }
117: }
118:
119: public static URL[] getURL() {
120: refreshList();
121: URL[] newUrls = new URL[runtimeUrlsList.size()];
122: runtimeUrlsList.toArray(newUrls);
123: return newUrls;
124: }
125:
126: public static URL[] getURL(String saName, String saLocation) {
127: if (saName == null)
128: return getURL();
129: refreshList();
130: addSACustCompToPath(saName, saLocation);
131: // calculate the total url list size
132: int urlCount = runtimeUrlsList.size();
133: Vector<URL> saUrlsList = saUrlsMap.get(saName);
134: if (saUrlsList != null)
135: urlCount += saUrlsList.size();
136:
137: URL[] newUrls = new URL[urlCount];
138: // populate the url list
139: runtimeUrlsList.toArray(newUrls);
140: int dest = runtimeUrlsList.size();
141: if (saUrlsList != null) {
142: int src = 0;
143: for (; src < saUrlsList.size(); src++)
144: newUrls[dest++] = saUrlsList.elementAt(src);
145: }
146: return newUrls;
147: }
148:
149: public static CustCompClassLoader getInstance(ClassLoader parent,
150: String saName, String saLocation) {
151:
152: CustCompClassLoader classLoader = new CustCompClassLoader(
153: getURL(saName, saLocation), parent);
154: classLoader.saName = saName;
155: classLoader.saLocation = saLocation;
156: return classLoader;
157: }
158:
159: public CustCompClassLoader(URL[] urls, ClassLoader parent) {
160: super (urls, parent);
161:
162: }
163:
164: public List<ICustComponent> getComponentlist() {
165:
166: List<ICustComponent> result = new Vector<ICustComponent>();
167:
168: ArrayList<String> errorList = new ArrayList<String>();
169: URL[] urls = getURL(this .saName, this .saLocation);
170:
171: for (int i = 0; i < urls.length; i++) {
172: ZipFile zipFile = null;
173: try {
174: zipFile = new ZipFile(urls[i].getPath());
175: } catch (Exception ex) {
176: //TODO
177: continue;
178: }
179:
180: for (Enumeration entries = zipFile.entries(); entries
181: .hasMoreElements();) {
182: ZipEntry entry = (ZipEntry) entries.nextElement();
183: if (!entry.isDirectory()) {
184: if (entry.getName().endsWith(".class")) {
185: Class userClass = null;
186: String className = UCMClassUtil
187: .handleEntryName(entry.getName());
188: try {
189: userClass = Class.forName(className, true,
190: this );
191: if (userClass != null) {
192: Object object = userClass.newInstance();
193: if (object instanceof com.bostechcorp.cbesb.common.util.custcomponent.ICustComponent)
194:
195: result
196: .add((com.bostechcorp.cbesb.common.util.custcomponent.ICustComponent) object);
197: }
198: } catch (Exception ex) {
199: errorList.add("Load class " + className
200: + "error:" + ex.getMessage());
201: } catch (Throwable t) {
202: // MsgUtil.warningMsg(t.toString());
203: errorList.add(t.toString());
204:
205: }
206: }
207: }
208: }
209: try {
210: if (zipFile != null)
211: zipFile.close();
212: } catch (Exception ex) {
213: //TODO
214: }
215:
216: }
217: if (errorList.size() > 0) {
218: String title = new Message(Messages.CLASS_LOAD_ERROR,
219: errorList.size()).getMessage();
220: MessageDetailDialog showError = new MessageDetailDialog(
221: PlatformUI.getWorkbench()
222: .getActiveWorkbenchWindow().getShell(),
223: errorList, title);
224: if (!showError.run()) {
225: return result;
226: }
227: }
228: return result;
229: }
230:
231: public ICustComponent getComponentInstance(String componentName) {
232:
233: ArrayList<String> errorList = new ArrayList<String>();
234: ICustComponent result = null;
235: URL[] urls = getURL(this .saName, this .saLocation);
236:
237: for (int i = 0; i < urls.length; i++) {
238: ZipFile zipFile = null;
239: try {
240: zipFile = new ZipFile(urls[i].getPath());
241: } catch (Exception ex) {
242: //TODO
243: continue;
244: }
245:
246: for (Enumeration entries = zipFile.entries(); entries
247: .hasMoreElements();) {
248: ZipEntry entry = (ZipEntry) entries.nextElement();
249: if (!entry.isDirectory()) {
250: if (entry.getName().endsWith(".class")) {
251: Class userClass = null;
252: String className = UCMClassUtil
253: .handleEntryName(entry.getName());
254: try {
255: userClass = Class.forName(className, true,
256: this );
257: if (userClass != null) {
258: Object object = userClass.newInstance();
259: if (object instanceof ICustComponent) {
260: ICustComponent custComponent = (ICustComponent) object;
261: if (custComponent.getName().equals(
262: componentName)) {
263: result = custComponent;
264: }
265: }
266:
267: }
268: } catch (Exception ex) {
269:
270: errorList.add("Load class " + className
271: + "error:" + ex.getMessage());
272: }
273:
274: }
275:
276: }
277: }
278: try {
279: if (zipFile != null)
280: zipFile.close();
281: } catch (Exception ex) {
282: //TODO
283: }
284:
285: }
286: if (errorList.size() > 0) {
287: String title = new Message(Messages.CLASS_LOAD_ERROR,
288: errorList.size()).getMessage();
289: MessageDetailDialog showError = new MessageDetailDialog(
290: PlatformUI.getWorkbench()
291: .getActiveWorkbenchWindow().getShell(),
292: errorList, title);
293: showError.run();
294:
295: }
296: return result;
297: }
298:
299: /*
300: * Try to get a resource InputStream based on the classpath elements. Use
301: * this to read files located in our classpath (things like common
302: * groovyscript).
303: *
304: * @param resource a resource (file) name @return an input stream. @throws
305: * IOException if the resource can not be located
306: */
307: public InputStream getInputStream(String resourceName)
308: throws IOException {
309: return this .getResourceAsStream(resourceName);
310:
311: }
312:
313: public static void refreshList() {
314: /*
315: * Extract the runtime URLs from the environment variable.
316: *
317: */
318: String home = EsbPathHelper.getCbesbHomeDir();
319: String libPath = "";
320: if (home != null) {
321: libPath = home + customCompPath;
322: File libDir = new File(libPath);//$NON-NLS-1$ //$NON-NLS-2$
323: // Lists every modules in the lib dir
324: File[] jars = libDir.listFiles(new FileFilter() {
325: public boolean accept(File file) {
326: return (file.toString().endsWith(".jar"));//$NON-NLS-1$
327: }
328: });
329:
330: if (jars != null) {
331: runtimeUrls = new URL[jars.length];
332: runtimeUrlsList.clear(); // yes clear
333: for (int i = 0; i < jars.length; i++) {
334: File jarFile = jars[i];
335: try {
336: runtimeUrls[i] = jarFile.toURL();
337: runtimeUrlsList.add(runtimeUrls[i]);
338: } catch (IOException ioe) {
339: //TODO handle Exception
340: Log log = LogFactory
341: .getLog(CustCompClassLoader.class);
342: log
343: .error("environment variable CBESB is malformed: "
344: + ioe);
345: }
346:
347: }
348: }
349: }
350: }
351: }
|