001: /*
002: * Created on Aug 22, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.core.manager;
008:
009: /**
010: * @author AYegorov
011: *
012: * To change the template for this generated type comment go to
013: * Window>Preferences>Java>Code Generation>Code and Comments
014: */
015: import java.util.*;
016: import java.io.*;
017: import java.lang.reflect.Method;
018: import java.net.*;
019:
020: import org.apache.log4j.Level;
021: import org.xdev.base.core.BASE;
022: import org.xdev.base.log.LoggerWriter;
023:
024: public class PluginManager extends ClassLoader {
025: private static PluginManager loader = null;
026:
027: private HashMap map = new HashMap();
028:
029: private ArrayList urlList = new ArrayList();
030:
031: /**
032: *
033: */
034: public PluginManager() {
035: super ();
036: // TODO Auto-generated constructor stub
037: }
038:
039: /**
040: * @param parent
041: */
042: public PluginManager(ClassLoader parent) {
043: super (parent);
044: // TODO Auto-generated constructor stub
045: }
046:
047: public static PluginManager getInstance(ClassLoader parent) {
048: if (loader == null) {
049: if (parent == null) {
050: loader = new PluginManager();
051: } else {
052: loader = new PluginManager(parent);
053: }
054: }
055:
056: return loader;
057: }
058:
059: public static PluginManager getInstance() {
060: return getInstance(null);
061: }
062:
063: public boolean isClassLoaded(String id) {
064: return map.containsKey(id);
065: }
066:
067: public void addClass(String id, Class cls) {
068: this .map.put(id, cls);
069: }
070:
071: public Class loadClass(String id) {
072: Class cls = null;
073:
074: try {
075: cls = (Class) this .map.get(id);
076:
077: if (cls == null) {
078: cls = Class.forName(id);
079: }
080: } catch (Exception ex) {
081: LoggerWriter.log(BASE.getValueFromObject(ex),
082: Level.DEBUG_INT, this .getClass());
083: }
084:
085: return cls;
086: }
087:
088: public void addUrl(File f) throws Exception {
089: urlList.add(f.toURL());
090: }
091:
092: public URL[] getUrlList() {
093: int size = this .urlList.size();
094: java.net.URL[] urlList = new java.net.URL[size];
095: for (int i = 0; i < size; i++) {
096: urlList[i] = (URL) this .urlList.get(i);
097: }
098:
099: return urlList;
100: }
101:
102: public URLClassLoader getUrlClassLoader() {
103: return URLClassLoader.newInstance(this .getUrlList(),
104: PluginManager.class.getClassLoader());
105: }
106:
107: }
|