home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / jmx.jar / javax / management / loading / MLet.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-10-28  |  14.5 KB  |  517 lines

  1. package javax.management.loading;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.OutputStream;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.net.URLClassLoader;
  17. import java.net.URLStreamHandlerFactory;
  18. import java.util.Arrays;
  19. import java.util.HashSet;
  20. import java.util.List;
  21. import java.util.Set;
  22. import java.util.StringTokenizer;
  23. import javax.management.MBeanRegistration;
  24. import javax.management.MBeanServer;
  25. import javax.management.MBeanServerFactory;
  26. import javax.management.ObjectInstance;
  27. import javax.management.ObjectName;
  28. import javax.management.ServiceNotFoundException;
  29. import mx4j.loading.MLetParseException;
  30. import mx4j.loading.MLetParser;
  31. import mx4j.loading.MLetTag;
  32. import mx4j.log.Log;
  33. import mx4j.log.Logger;
  34.  
  35. public class MLet extends URLClassLoader implements MLetMBean, MBeanRegistration {
  36.    private String m_libraryDirectory;
  37.    private ObjectName m_objectName;
  38.    private MBeanServer m_server;
  39.    private String m_defaultCodeBase;
  40.    private ThreadLocal m_loadingOnlyLocally;
  41.    private ThreadLocal m_loadingWithRepository;
  42.    private static ThreadLocal m_loadClassOriginator = new ThreadLocal();
  43.  
  44.    public MLet() {
  45.       this(new URL[0]);
  46.    }
  47.  
  48.    public MLet(URL[] urls) {
  49.       super(urls);
  50.       this.m_loadingOnlyLocally = new ThreadLocal();
  51.       this.m_loadingWithRepository = new ThreadLocal();
  52.       this.m_loadingWithRepository.set(Boolean.FALSE);
  53.       this.m_loadingOnlyLocally.set(Boolean.FALSE);
  54.    }
  55.  
  56.    public MLet(URL[] urls, ClassLoader parent) {
  57.       super(urls, parent);
  58.       this.m_loadingOnlyLocally = new ThreadLocal();
  59.       this.m_loadingWithRepository = new ThreadLocal();
  60.       this.m_loadingWithRepository.set(Boolean.FALSE);
  61.       this.m_loadingOnlyLocally.set(Boolean.FALSE);
  62.    }
  63.  
  64.    public MLet(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
  65.       super(urls, parent, factory);
  66.       this.m_loadingOnlyLocally = new ThreadLocal();
  67.       this.m_loadingWithRepository = new ThreadLocal();
  68.       this.m_loadingWithRepository.set(Boolean.FALSE);
  69.       this.m_loadingOnlyLocally.set(Boolean.FALSE);
  70.    }
  71.  
  72.    public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
  73.       this.m_server = server;
  74.       this.m_objectName = name == null ? new ObjectName(this.m_server.getDefaultDomain(), "service", "MLet") : name;
  75.       Logger logger = this.getLogger();
  76.       if (logger.isEnabledFor(0)) {
  77.          logger.trace("MLet service " + this.m_objectName + " preRegistered successfully");
  78.       }
  79.  
  80.       return this.m_objectName;
  81.    }
  82.  
  83.    public void postRegister(Boolean registrationDone) {
  84.       Logger logger = this.getLogger();
  85.       boolean done = registrationDone;
  86.       if (!done) {
  87.          this.m_server = null;
  88.          logger.warn("MLet service " + this.m_objectName + " was not registered");
  89.       } else if (logger.isEnabledFor(0)) {
  90.          logger.trace("MLet service " + this.m_objectName + " postRegistered successfully");
  91.       }
  92.  
  93.    }
  94.  
  95.    public void preDeregister() throws Exception {
  96.       Logger logger = this.getLogger();
  97.       if (logger.isEnabledFor(0)) {
  98.          logger.debug("MLet service " + this.m_objectName + " preDeregistered successfully");
  99.       }
  100.  
  101.    }
  102.  
  103.    public void postDeregister() {
  104.       Logger logger = this.getLogger();
  105.       if (logger.isEnabledFor(0)) {
  106.          logger.debug("MLet service " + this.m_objectName + " postDeregistered successfully");
  107.       }
  108.  
  109.    }
  110.  
  111.    public void addURL(String url) throws ServiceNotFoundException {
  112.       this.addURL(this.createURL(url));
  113.    }
  114.  
  115.    public void addURL(URL url) {
  116.       Logger logger = this.getLogger();
  117.       if (!Arrays.asList(this.getURLs()).contains(url)) {
  118.          if (logger.isEnabledFor(20)) {
  119.             logger.info("Adding URL to this MLet (" + this.m_objectName + ") classpath: " + url);
  120.          }
  121.  
  122.          super.addURL(url);
  123.       } else if (logger.isEnabledFor(20)) {
  124.          logger.info("URL already present in this MLet (" + this.m_objectName + ") classpath: " + url);
  125.       }
  126.  
  127.    }
  128.  
  129.    public Class loadClass(String name, ClassLoaderRepository repository) throws ClassNotFoundException {
  130.       if (repository == null) {
  131.          Class cls = this.loadClassLocally(name);
  132.          return cls;
  133.       } else {
  134.          try {
  135.             Class cls = this.loadClassLocally(name);
  136.             return cls;
  137.          } catch (ClassNotFoundException var5) {
  138.             Class cls = this.findClassInRepository(name, repository);
  139.             return cls;
  140.          }
  141.       }
  142.    }
  143.  
  144.    private Class loadClassLocally(String name) throws ClassNotFoundException {
  145.       Class var2;
  146.       try {
  147.          this.m_loadingOnlyLocally.set(Boolean.TRUE);
  148.          var2 = this.loadClass(name);
  149.       } finally {
  150.          this.m_loadingOnlyLocally.set(Boolean.FALSE);
  151.       }
  152.  
  153.       return var2;
  154.    }
  155.  
  156.    protected Class findClass(String name) throws ClassNotFoundException {
  157.       if (this.m_loadingWithRepository.get() == Boolean.TRUE) {
  158.          throw new ClassNotFoundException(name);
  159.       } else {
  160.          MLet originator = (MLet)m_loadClassOriginator.get();
  161.          if (originator == null) {
  162.             originator = this;
  163.             m_loadClassOriginator.set(this);
  164.          }
  165.  
  166.          Class cls;
  167.          try {
  168.             Logger logger = this.getLogger();
  169.             if (logger.isEnabledFor(20)) {
  170.                logger.info("Finding class " + name + "...");
  171.             }
  172.  
  173.             try {
  174.                Class cls = this.findClassLocally(name);
  175.                if (logger.isEnabledFor(20)) {
  176.                   logger.info("Class " + name + " found in this MLet (" + this.m_objectName + ") classpath: " + this);
  177.                }
  178.  
  179.                Class var32 = cls;
  180.                return var32;
  181.             } catch (ClassNotFoundException x) {
  182.                if (this.m_loadingOnlyLocally.get() == Boolean.TRUE) {
  183.                   throw x;
  184.                }
  185.  
  186.                if (logger.isEnabledFor(10)) {
  187.                   logger.debug("Class " + name + " not found in this MLet (" + this.m_objectName + ") classpath: " + this + ", trying the ClassLoaderRepository...", x);
  188.                }
  189.             }
  190.  
  191.             try {
  192.                this.m_loadingWithRepository.set(Boolean.TRUE);
  193.                ClassLoaderRepository repository = MBeanServerFactory.getClassLoaderRepository(this.m_server);
  194.                Class cls = this.findClassInRepository(name, repository);
  195.                if (logger.isEnabledFor(20)) {
  196.                   logger.info("Class " + name + " found with the ClassLoaderRepository " + repository);
  197.                }
  198.  
  199.                cls = cls;
  200.             } catch (ClassNotFoundException var27) {
  201.                ClassNotFoundException xx = var27;
  202.                if (logger.isEnabledFor(10)) {
  203.                   logger.debug("Class " + name + " not found in the ClassLoaderRepository, trying the DefaultLoaderRepository...", var27);
  204.                }
  205.  
  206.                try {
  207.                   ClassLoaderRepository repository = MBeanServerFactory.getClassLoaderRepository((MBeanServer)null);
  208.                   cls = this.findClassInRepository(name, repository);
  209.                   if (logger.isEnabledFor(20)) {
  210.                      logger.info("Class " + name + " found with the DefaultLoaderRepository " + repository);
  211.                   }
  212.  
  213.                   Class var8 = cls;
  214.                   return var8;
  215.                } catch (ClassNotFoundException xxx) {
  216.                   if (logger.isEnabledFor(10)) {
  217.                      logger.debug("Class " + name + " not found in the DefaultLoaderRepository, giving up", xxx);
  218.                   }
  219.  
  220.                   throw new MLetClassNotFoundError((1)null);
  221.                }
  222.             } finally {
  223.                this.m_loadingWithRepository.set(Boolean.FALSE);
  224.             }
  225.          } catch (MLetClassNotFoundError x) {
  226.             if (originator == this) {
  227.                throw new ClassNotFoundException(name);
  228.             }
  229.  
  230.             throw x;
  231.          } finally {
  232.             if (originator == this) {
  233.                m_loadClassOriginator.set((Object)null);
  234.             }
  235.  
  236.          }
  237.  
  238.          return cls;
  239.       }
  240.    }
  241.  
  242.    private Class findClassLocally(String name) throws ClassNotFoundException {
  243.       return super.findClass(name);
  244.    }
  245.  
  246.    private Class findClassInRepository(String name, ClassLoaderRepository repository) throws ClassNotFoundException {
  247.       return repository.loadClassWithout(this, name);
  248.    }
  249.  
  250.    public Set getMBeansFromURL(String url) throws ServiceNotFoundException {
  251.       return this.getMBeansFromURL(this.createURL(url));
  252.    }
  253.  
  254.    public Set getMBeansFromURL(URL url) throws ServiceNotFoundException {
  255.       if (url == null) {
  256.          throw new ServiceNotFoundException("Cannot load MBeans from null URL");
  257.       } else {
  258.          Logger logger = this.getLogger();
  259.          String urlString = url.toString();
  260.          this.m_defaultCodeBase = urlString.substring(0, urlString.lastIndexOf(47) + 1);
  261.          InputStream is = null;
  262.  
  263.          Set var5;
  264.          try {
  265.             is = url.openStream();
  266.             if (logger.isEnabledFor(20)) {
  267.                logger.info("MLet " + this.m_objectName + ", parsing MLET URL " + url);
  268.             }
  269.  
  270.             var5 = this.getMBeansFromURL(is);
  271.          } catch (IOException var14) {
  272.             if (logger.isEnabledFor(10)) {
  273.                logger.debug("MLet " + this.m_objectName + ", cannot open MLET URL", var14);
  274.             }
  275.  
  276.             throw new ServiceNotFoundException(var14.toString());
  277.          } finally {
  278.             if (is != null) {
  279.                try {
  280.                   is.close();
  281.                } catch (IOException var13) {
  282.                }
  283.             }
  284.  
  285.          }
  286.  
  287.          return var5;
  288.       }
  289.    }
  290.  
  291.    private Logger getLogger() {
  292.       return Log.getLogger(this.getClass().getName());
  293.    }
  294.  
  295.    private String getDefaultCodeBase() {
  296.       if (this.m_defaultCodeBase == null) {
  297.          String userDir = System.getProperty("user.dir");
  298.          File f = new File(userDir);
  299.  
  300.          try {
  301.             String url = f.toURL().toString();
  302.             this.m_defaultCodeBase = url.substring(0, url.lastIndexOf(47) + 1);
  303.          } catch (MalformedURLException var4) {
  304.             this.m_defaultCodeBase = "";
  305.          }
  306.       }
  307.  
  308.       return this.m_defaultCodeBase;
  309.    }
  310.  
  311.    private Set getMBeansFromURL(InputStream is) throws ServiceNotFoundException {
  312.       Logger logger = this.getLogger();
  313.       ByteArrayOutputStream baos = new ByteArrayOutputStream();
  314.       BufferedOutputStream os = new BufferedOutputStream(baos);
  315.  
  316.       try {
  317.          this.readFromAndWriteTo(is, os);
  318.       } catch (IOException x) {
  319.          logger.error("Cannot read input stream", x);
  320.          throw new ServiceNotFoundException(x.toString());
  321.       } finally {
  322.          try {
  323.             os.close();
  324.          } catch (IOException var18) {
  325.          }
  326.  
  327.       }
  328.  
  329.       try {
  330.          HashSet mbeans = new HashSet();
  331.          MLetParser parser = new MLetParser();
  332.          List tags = parser.parse(new String(baos.toByteArray()));
  333.  
  334.          for(int i = 0; i < tags.size(); ++i) {
  335.             MLetTag tag = (MLetTag)tags.get(i);
  336.             URL[] urls = tag.getURLs(this.getDefaultCodeBase());
  337.  
  338.             for(int j = 0; j < urls.length; ++j) {
  339.                this.addURL(urls[j]);
  340.             }
  341.  
  342.             Object obj = this.createMBean(tag);
  343.             mbeans.add(obj);
  344.          }
  345.  
  346.          return mbeans;
  347.       } catch (MLetParseException x) {
  348.          throw new ServiceNotFoundException(x.toString());
  349.       }
  350.    }
  351.  
  352.    protected String findLibrary(String libraryName) {
  353.       String sysLibraryName = System.mapLibraryName(libraryName);
  354.       InputStream is = this.getResourceAsStream(sysLibraryName.replace(File.separatorChar, '/'));
  355.       String path = this.loadResource(sysLibraryName, is);
  356.       if (path != null) {
  357.          return path;
  358.       } else {
  359.          StringBuffer sysBuff = new StringBuffer();
  360.          sysBuff.append(this.removeWhiteSpace(System.getProperty("os.name").trim())).append(File.separator);
  361.          sysBuff.append(this.removeWhiteSpace(System.getProperty("os.arch").trim())).append(File.separator);
  362.          sysBuff.append(this.removeWhiteSpace(System.getProperty("os.version").trim())).append(File.separator);
  363.          sysBuff.append("lib").append(File.separator).append(sysLibraryName);
  364.          path = this.loadResource(sysBuff.toString(), is);
  365.          if (path != null) {
  366.             return path;
  367.          } else {
  368.             StringTokenizer tokenizer = new StringTokenizer(System.getProperty("java.library.path").trim(), File.pathSeparator);
  369.  
  370.             while(tokenizer.hasMoreTokens()) {
  371.                String filePath = tokenizer.nextToken().trim() + File.separator + sysLibraryName;
  372.  
  373.                try {
  374.                   path = this.loadResource(filePath, new FileInputStream(filePath));
  375.                   if (path != null) {
  376.                      return path;
  377.                   }
  378.                } catch (FileNotFoundException var9) {
  379.                }
  380.             }
  381.  
  382.             return null;
  383.          }
  384.       }
  385.    }
  386.  
  387.    private String loadResource(String sysLibraryName, InputStream is) {
  388.       if (is != null) {
  389.          try {
  390.             File file = null;
  391.             if (this.getLibraryDirectory() == null) {
  392.                file = new File(sysLibraryName);
  393.             } else {
  394.                file = new File(this.getLibraryDirectory(), sysLibraryName);
  395.             }
  396.  
  397.             OutputStream os = null;
  398.  
  399.             String var5;
  400.             try {
  401.                os = new BufferedOutputStream(new FileOutputStream(file));
  402.                is = new BufferedInputStream(is);
  403.                if (file.exists()) {
  404.                   file.delete();
  405.                }
  406.  
  407.                this.readFromAndWriteTo(is, os);
  408.                var5 = file.getCanonicalPath();
  409.             } finally {
  410.                if (is != null) {
  411.                   is.close();
  412.                }
  413.  
  414.                if (os != null) {
  415.                   os.close();
  416.                }
  417.  
  418.             }
  419.  
  420.             return var5;
  421.          } catch (IOException var10) {
  422.          }
  423.       }
  424.  
  425.       return null;
  426.    }
  427.  
  428.    private void readFromAndWriteTo(InputStream is, OutputStream os) throws IOException {
  429.       byte[] buffer = new byte[65536];
  430.       int read = -1;
  431.  
  432.       while((read = is.read(buffer)) >= 0) {
  433.          os.write(buffer, 0, read);
  434.       }
  435.  
  436.       Object var5 = null;
  437.    }
  438.  
  439.    private String removeWhiteSpace(String word) {
  440.       int j = word.indexOf(32);
  441.       if (j < 0) {
  442.          return word;
  443.       } else {
  444.          StringBuffer temp = new StringBuffer();
  445.  
  446.          for(int i = 0; j >= 0; i = j + 1) {
  447.             word = word.substring(i);
  448.             j = word.indexOf(32);
  449.             if (j >= 0) {
  450.                temp = temp.append(word.substring(0, j));
  451.             } else {
  452.                temp = temp.append(word.substring(0));
  453.             }
  454.          }
  455.  
  456.          return temp.toString();
  457.       }
  458.    }
  459.  
  460.    public String getLibraryDirectory() {
  461.       return this.m_libraryDirectory;
  462.    }
  463.  
  464.    public void setLibraryDirectory(String libdir) {
  465.       this.m_libraryDirectory = libdir;
  466.    }
  467.  
  468.    private Object createMBean(MLetTag tag) throws ServiceNotFoundException {
  469.       if (this.m_server == null) {
  470.          throw new ServiceNotFoundException("MLet not registered on the MBeanServer");
  471.       } else {
  472.          Logger logger = this.getLogger();
  473.          if (logger.isEnabledFor(20)) {
  474.             logger.info("MLet " + this.m_objectName + ", creating MBean from\n" + tag);
  475.          }
  476.  
  477.          try {
  478.             Object mbean = null;
  479.             if (tag.getObject() != null) {
  480.                String name = tag.getObject();
  481.                InputStream is = this.getResourceAsStream(name);
  482.                if (is == null) {
  483.                   throw new ServiceNotFoundException("Cannot find MBean " + name + " in this MLet (" + this.m_objectName + ") classpath");
  484.                }
  485.  
  486.                InputStream bis = new BufferedInputStream(is);
  487.                ByteArrayOutputStream baos = new ByteArrayOutputStream();
  488.                OutputStream os = new BufferedOutputStream(baos);
  489.                this.readFromAndWriteTo(bis, os);
  490.                ObjectInputStream ois = this.m_server.deserialize(this.m_objectName, baos.toByteArray());
  491.                mbean = ois.readObject();
  492.             } else {
  493.                String clsName = tag.getCode();
  494.                Object[] args = tag.getArguments();
  495.                String[] params = tag.getSignature();
  496.                mbean = this.m_server.instantiate(clsName, this.m_objectName, args, params);
  497.             }
  498.  
  499.             ObjectName objectName = tag.getObjectName();
  500.             ObjectInstance instance = this.m_server.registerMBean(mbean, objectName);
  501.             return instance;
  502.          } catch (Throwable t) {
  503.             return t;
  504.          }
  505.       }
  506.    }
  507.  
  508.    private URL createURL(String urlString) throws ServiceNotFoundException {
  509.       try {
  510.          URL url = new URL(urlString);
  511.          return url;
  512.       } catch (MalformedURLException x) {
  513.          throw new ServiceNotFoundException(x.toString());
  514.       }
  515.    }
  516. }
  517.