home *** CD-ROM | disk | FTP | other *** search
- package javax.management.loading;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream;
- import java.io.OutputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.net.URLStreamHandlerFactory;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.StringTokenizer;
- import javax.management.MBeanRegistration;
- import javax.management.MBeanServer;
- import javax.management.MBeanServerFactory;
- import javax.management.ObjectInstance;
- import javax.management.ObjectName;
- import javax.management.ServiceNotFoundException;
- import mx4j.loading.MLetParseException;
- import mx4j.loading.MLetParser;
- import mx4j.loading.MLetTag;
- import mx4j.log.Log;
- import mx4j.log.Logger;
-
- public class MLet extends URLClassLoader implements MLetMBean, MBeanRegistration {
- private String m_libraryDirectory;
- private ObjectName m_objectName;
- private MBeanServer m_server;
- private String m_defaultCodeBase;
- private ThreadLocal m_loadingOnlyLocally;
- private ThreadLocal m_loadingWithRepository;
- private static ThreadLocal m_loadClassOriginator = new ThreadLocal();
-
- public MLet() {
- this(new URL[0]);
- }
-
- public MLet(URL[] urls) {
- super(urls);
- this.m_loadingOnlyLocally = new ThreadLocal();
- this.m_loadingWithRepository = new ThreadLocal();
- this.m_loadingWithRepository.set(Boolean.FALSE);
- this.m_loadingOnlyLocally.set(Boolean.FALSE);
- }
-
- public MLet(URL[] urls, ClassLoader parent) {
- super(urls, parent);
- this.m_loadingOnlyLocally = new ThreadLocal();
- this.m_loadingWithRepository = new ThreadLocal();
- this.m_loadingWithRepository.set(Boolean.FALSE);
- this.m_loadingOnlyLocally.set(Boolean.FALSE);
- }
-
- public MLet(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
- super(urls, parent, factory);
- this.m_loadingOnlyLocally = new ThreadLocal();
- this.m_loadingWithRepository = new ThreadLocal();
- this.m_loadingWithRepository.set(Boolean.FALSE);
- this.m_loadingOnlyLocally.set(Boolean.FALSE);
- }
-
- public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
- this.m_server = server;
- this.m_objectName = name == null ? new ObjectName(this.m_server.getDefaultDomain(), "service", "MLet") : name;
- Logger logger = this.getLogger();
- if (logger.isEnabledFor(0)) {
- logger.trace("MLet service " + this.m_objectName + " preRegistered successfully");
- }
-
- return this.m_objectName;
- }
-
- public void postRegister(Boolean registrationDone) {
- Logger logger = this.getLogger();
- boolean done = registrationDone;
- if (!done) {
- this.m_server = null;
- logger.warn("MLet service " + this.m_objectName + " was not registered");
- } else if (logger.isEnabledFor(0)) {
- logger.trace("MLet service " + this.m_objectName + " postRegistered successfully");
- }
-
- }
-
- public void preDeregister() throws Exception {
- Logger logger = this.getLogger();
- if (logger.isEnabledFor(0)) {
- logger.debug("MLet service " + this.m_objectName + " preDeregistered successfully");
- }
-
- }
-
- public void postDeregister() {
- Logger logger = this.getLogger();
- if (logger.isEnabledFor(0)) {
- logger.debug("MLet service " + this.m_objectName + " postDeregistered successfully");
- }
-
- }
-
- public void addURL(String url) throws ServiceNotFoundException {
- this.addURL(this.createURL(url));
- }
-
- public void addURL(URL url) {
- Logger logger = this.getLogger();
- if (!Arrays.asList(this.getURLs()).contains(url)) {
- if (logger.isEnabledFor(20)) {
- logger.info("Adding URL to this MLet (" + this.m_objectName + ") classpath: " + url);
- }
-
- super.addURL(url);
- } else if (logger.isEnabledFor(20)) {
- logger.info("URL already present in this MLet (" + this.m_objectName + ") classpath: " + url);
- }
-
- }
-
- public Class loadClass(String name, ClassLoaderRepository repository) throws ClassNotFoundException {
- if (repository == null) {
- Class cls = this.loadClassLocally(name);
- return cls;
- } else {
- try {
- Class cls = this.loadClassLocally(name);
- return cls;
- } catch (ClassNotFoundException var5) {
- Class cls = this.findClassInRepository(name, repository);
- return cls;
- }
- }
- }
-
- private Class loadClassLocally(String name) throws ClassNotFoundException {
- Class var2;
- try {
- this.m_loadingOnlyLocally.set(Boolean.TRUE);
- var2 = this.loadClass(name);
- } finally {
- this.m_loadingOnlyLocally.set(Boolean.FALSE);
- }
-
- return var2;
- }
-
- protected Class findClass(String name) throws ClassNotFoundException {
- if (this.m_loadingWithRepository.get() == Boolean.TRUE) {
- throw new ClassNotFoundException(name);
- } else {
- MLet originator = (MLet)m_loadClassOriginator.get();
- if (originator == null) {
- originator = this;
- m_loadClassOriginator.set(this);
- }
-
- Class cls;
- try {
- Logger logger = this.getLogger();
- if (logger.isEnabledFor(20)) {
- logger.info("Finding class " + name + "...");
- }
-
- try {
- Class cls = this.findClassLocally(name);
- if (logger.isEnabledFor(20)) {
- logger.info("Class " + name + " found in this MLet (" + this.m_objectName + ") classpath: " + this);
- }
-
- Class var32 = cls;
- return var32;
- } catch (ClassNotFoundException x) {
- if (this.m_loadingOnlyLocally.get() == Boolean.TRUE) {
- throw x;
- }
-
- if (logger.isEnabledFor(10)) {
- logger.debug("Class " + name + " not found in this MLet (" + this.m_objectName + ") classpath: " + this + ", trying the ClassLoaderRepository...", x);
- }
- }
-
- try {
- this.m_loadingWithRepository.set(Boolean.TRUE);
- ClassLoaderRepository repository = MBeanServerFactory.getClassLoaderRepository(this.m_server);
- Class cls = this.findClassInRepository(name, repository);
- if (logger.isEnabledFor(20)) {
- logger.info("Class " + name + " found with the ClassLoaderRepository " + repository);
- }
-
- cls = cls;
- } catch (ClassNotFoundException var27) {
- ClassNotFoundException xx = var27;
- if (logger.isEnabledFor(10)) {
- logger.debug("Class " + name + " not found in the ClassLoaderRepository, trying the DefaultLoaderRepository...", var27);
- }
-
- try {
- ClassLoaderRepository repository = MBeanServerFactory.getClassLoaderRepository((MBeanServer)null);
- cls = this.findClassInRepository(name, repository);
- if (logger.isEnabledFor(20)) {
- logger.info("Class " + name + " found with the DefaultLoaderRepository " + repository);
- }
-
- Class var8 = cls;
- return var8;
- } catch (ClassNotFoundException xxx) {
- if (logger.isEnabledFor(10)) {
- logger.debug("Class " + name + " not found in the DefaultLoaderRepository, giving up", xxx);
- }
-
- throw new MLetClassNotFoundError((1)null);
- }
- } finally {
- this.m_loadingWithRepository.set(Boolean.FALSE);
- }
- } catch (MLetClassNotFoundError x) {
- if (originator == this) {
- throw new ClassNotFoundException(name);
- }
-
- throw x;
- } finally {
- if (originator == this) {
- m_loadClassOriginator.set((Object)null);
- }
-
- }
-
- return cls;
- }
- }
-
- private Class findClassLocally(String name) throws ClassNotFoundException {
- return super.findClass(name);
- }
-
- private Class findClassInRepository(String name, ClassLoaderRepository repository) throws ClassNotFoundException {
- return repository.loadClassWithout(this, name);
- }
-
- public Set getMBeansFromURL(String url) throws ServiceNotFoundException {
- return this.getMBeansFromURL(this.createURL(url));
- }
-
- public Set getMBeansFromURL(URL url) throws ServiceNotFoundException {
- if (url == null) {
- throw new ServiceNotFoundException("Cannot load MBeans from null URL");
- } else {
- Logger logger = this.getLogger();
- String urlString = url.toString();
- this.m_defaultCodeBase = urlString.substring(0, urlString.lastIndexOf(47) + 1);
- InputStream is = null;
-
- Set var5;
- try {
- is = url.openStream();
- if (logger.isEnabledFor(20)) {
- logger.info("MLet " + this.m_objectName + ", parsing MLET URL " + url);
- }
-
- var5 = this.getMBeansFromURL(is);
- } catch (IOException var14) {
- if (logger.isEnabledFor(10)) {
- logger.debug("MLet " + this.m_objectName + ", cannot open MLET URL", var14);
- }
-
- throw new ServiceNotFoundException(var14.toString());
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException var13) {
- }
- }
-
- }
-
- return var5;
- }
- }
-
- private Logger getLogger() {
- return Log.getLogger(this.getClass().getName());
- }
-
- private String getDefaultCodeBase() {
- if (this.m_defaultCodeBase == null) {
- String userDir = System.getProperty("user.dir");
- File f = new File(userDir);
-
- try {
- String url = f.toURL().toString();
- this.m_defaultCodeBase = url.substring(0, url.lastIndexOf(47) + 1);
- } catch (MalformedURLException var4) {
- this.m_defaultCodeBase = "";
- }
- }
-
- return this.m_defaultCodeBase;
- }
-
- private Set getMBeansFromURL(InputStream is) throws ServiceNotFoundException {
- Logger logger = this.getLogger();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- BufferedOutputStream os = new BufferedOutputStream(baos);
-
- try {
- this.readFromAndWriteTo(is, os);
- } catch (IOException x) {
- logger.error("Cannot read input stream", x);
- throw new ServiceNotFoundException(x.toString());
- } finally {
- try {
- os.close();
- } catch (IOException var18) {
- }
-
- }
-
- try {
- HashSet mbeans = new HashSet();
- MLetParser parser = new MLetParser();
- List tags = parser.parse(new String(baos.toByteArray()));
-
- for(int i = 0; i < tags.size(); ++i) {
- MLetTag tag = (MLetTag)tags.get(i);
- URL[] urls = tag.getURLs(this.getDefaultCodeBase());
-
- for(int j = 0; j < urls.length; ++j) {
- this.addURL(urls[j]);
- }
-
- Object obj = this.createMBean(tag);
- mbeans.add(obj);
- }
-
- return mbeans;
- } catch (MLetParseException x) {
- throw new ServiceNotFoundException(x.toString());
- }
- }
-
- protected String findLibrary(String libraryName) {
- String sysLibraryName = System.mapLibraryName(libraryName);
- InputStream is = this.getResourceAsStream(sysLibraryName.replace(File.separatorChar, '/'));
- String path = this.loadResource(sysLibraryName, is);
- if (path != null) {
- return path;
- } else {
- StringBuffer sysBuff = new StringBuffer();
- sysBuff.append(this.removeWhiteSpace(System.getProperty("os.name").trim())).append(File.separator);
- sysBuff.append(this.removeWhiteSpace(System.getProperty("os.arch").trim())).append(File.separator);
- sysBuff.append(this.removeWhiteSpace(System.getProperty("os.version").trim())).append(File.separator);
- sysBuff.append("lib").append(File.separator).append(sysLibraryName);
- path = this.loadResource(sysBuff.toString(), is);
- if (path != null) {
- return path;
- } else {
- StringTokenizer tokenizer = new StringTokenizer(System.getProperty("java.library.path").trim(), File.pathSeparator);
-
- while(tokenizer.hasMoreTokens()) {
- String filePath = tokenizer.nextToken().trim() + File.separator + sysLibraryName;
-
- try {
- path = this.loadResource(filePath, new FileInputStream(filePath));
- if (path != null) {
- return path;
- }
- } catch (FileNotFoundException var9) {
- }
- }
-
- return null;
- }
- }
- }
-
- private String loadResource(String sysLibraryName, InputStream is) {
- if (is != null) {
- try {
- File file = null;
- if (this.getLibraryDirectory() == null) {
- file = new File(sysLibraryName);
- } else {
- file = new File(this.getLibraryDirectory(), sysLibraryName);
- }
-
- OutputStream os = null;
-
- String var5;
- try {
- os = new BufferedOutputStream(new FileOutputStream(file));
- is = new BufferedInputStream(is);
- if (file.exists()) {
- file.delete();
- }
-
- this.readFromAndWriteTo(is, os);
- var5 = file.getCanonicalPath();
- } finally {
- if (is != null) {
- is.close();
- }
-
- if (os != null) {
- os.close();
- }
-
- }
-
- return var5;
- } catch (IOException var10) {
- }
- }
-
- return null;
- }
-
- private void readFromAndWriteTo(InputStream is, OutputStream os) throws IOException {
- byte[] buffer = new byte[65536];
- int read = -1;
-
- while((read = is.read(buffer)) >= 0) {
- os.write(buffer, 0, read);
- }
-
- Object var5 = null;
- }
-
- private String removeWhiteSpace(String word) {
- int j = word.indexOf(32);
- if (j < 0) {
- return word;
- } else {
- StringBuffer temp = new StringBuffer();
-
- for(int i = 0; j >= 0; i = j + 1) {
- word = word.substring(i);
- j = word.indexOf(32);
- if (j >= 0) {
- temp = temp.append(word.substring(0, j));
- } else {
- temp = temp.append(word.substring(0));
- }
- }
-
- return temp.toString();
- }
- }
-
- public String getLibraryDirectory() {
- return this.m_libraryDirectory;
- }
-
- public void setLibraryDirectory(String libdir) {
- this.m_libraryDirectory = libdir;
- }
-
- private Object createMBean(MLetTag tag) throws ServiceNotFoundException {
- if (this.m_server == null) {
- throw new ServiceNotFoundException("MLet not registered on the MBeanServer");
- } else {
- Logger logger = this.getLogger();
- if (logger.isEnabledFor(20)) {
- logger.info("MLet " + this.m_objectName + ", creating MBean from\n" + tag);
- }
-
- try {
- Object mbean = null;
- if (tag.getObject() != null) {
- String name = tag.getObject();
- InputStream is = this.getResourceAsStream(name);
- if (is == null) {
- throw new ServiceNotFoundException("Cannot find MBean " + name + " in this MLet (" + this.m_objectName + ") classpath");
- }
-
- InputStream bis = new BufferedInputStream(is);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- OutputStream os = new BufferedOutputStream(baos);
- this.readFromAndWriteTo(bis, os);
- ObjectInputStream ois = this.m_server.deserialize(this.m_objectName, baos.toByteArray());
- mbean = ois.readObject();
- } else {
- String clsName = tag.getCode();
- Object[] args = tag.getArguments();
- String[] params = tag.getSignature();
- mbean = this.m_server.instantiate(clsName, this.m_objectName, args, params);
- }
-
- ObjectName objectName = tag.getObjectName();
- ObjectInstance instance = this.m_server.registerMBean(mbean, objectName);
- return instance;
- } catch (Throwable t) {
- return t;
- }
- }
- }
-
- private URL createURL(String urlString) throws ServiceNotFoundException {
- try {
- URL url = new URL(urlString);
- return url;
- } catch (MalformedURLException x) {
- throw new ServiceNotFoundException(x.toString());
- }
- }
- }
-