User: juhalindfors
  Date: 02/01/29 19:06:18

  Modified:    src/main/javax/management/loading MLet.java
  Log:
  MLet text file parsing
  
  Revision  Changes    Path
  1.2       +277 -40   jmx/src/main/javax/management/loading/MLet.java
  
  Index: MLet.java
  ===================================================================
  RCS file: /cvsroot/jboss/jmx/src/main/javax/management/loading/MLet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MLet.java 2001/12/03 02:08:48     1.1
  +++ MLet.java 2002/01/30 03:06:17     1.2
  @@ -1,5 +1,8 @@
   /*
  - * LGPL
  + * JBoss, the OpenSource J2EE webOS
  + *
  + * Distributable under LGPL license.
  + * See terms of license at gnu.org.
    */
   package javax.management.loading;
   
  @@ -14,87 +17,321 @@
   import java.net.MalformedURLException;
   
   import java.io.InputStream;
  -
  -public class MLet extends URLClassLoader implements MLetMBean, MBeanRegistration {
  -
  -   public MLet() {
  -      super(null);
  -   }
  -
  -
  -   public MLet(URL[] urls) {
  -      super(urls);
  +import java.io.InputStreamReader;
  +import java.io.BufferedReader;
  +import java.io.IOException;
  +
  +import java.util.StringTokenizer;
  +import java.util.Set;
  +import java.util.HashSet;
  +import java.util.Iterator;
  +import java.util.NoSuchElementException;
  +
  +/**
  + *
  + * @author  <a href="mailto:[EMAIL PROTECTED]";>Juha Lindfors</a>.
  + * @version $Revision: 1.2 $  
  + */
  +public class MLet 
  +   extends URLClassLoader 
  +   implements MLetMBean, MBeanRegistration
  +{
  +
  +   // Attributes ----------------------------------------------------
  +   private MBeanServer server    = null;
  +   private ObjectName objectName = null;
  +   
  +   // Constructors --------------------------------------------------
  +   public MLet()
  +   {
  +      super(new URL[0], Thread.currentThread().getContextClassLoader());
  +   }
  +
  +   public MLet(URL[] urls)
  +   {
  +      super(urls, Thread.currentThread().getContextClassLoader());
      }
   
  -   public MLet(URL[] urls, ClassLoader parent) {
  +   public MLet(URL[] urls, ClassLoader parent)
  +   {
         super(urls, parent);
      }
   
  -   public MLet(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
  +   public MLet(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory)
  +   {
         super(urls, parent, factory);
      }
   
  -
  -   public ObjectName preRegister(MBeanServer server, ObjectName name) throws  
Exception {
  -
  +   // MBeanRegistration implementation ------------------------------
  +   public ObjectName preRegister(MBeanServer server, ObjectName name) throws  
Exception
  +   {
         if (name == null)
  -         return new ObjectName(":type=MLet");
  +         name = new ObjectName(":type=MLet");
   
  +      this.objectName = name;
  +      this.server     = server;
  +      
         return name;
      }
   
  -   public void postRegister(java.lang.Boolean registrationDone) {}
  +   public void postRegister(Boolean registrationDone)
  +   {}
   
  -   public void preDeregister() throws java.lang.Exception {}
  +   public void preDeregister() throws Exception
  +   {}
   
  -   public void postDeregister() {}
  +   public void postDeregister()
  +   {}
   
  -   public java.util.Set getMBeansFromURL(String url) throws 
ServiceNotFoundException {
  -      return null;
  +   // Public --------------------------------------------------------
  +   public Set getMBeansFromURL(String url) throws ServiceNotFoundException
  +   {
  +      try
  +      {
  +         return getMBeansFromURL(new URL(url));
  +      }
  +      catch (MalformedURLException e) 
  +      {
  +         throw new ServiceNotFoundException("Malformed URL:" + url);
  +      }
      }
   
  -   public java.util.Set getMBeansFromURL(URL url) throws ServiceNotFoundException {
  -      return null;
  +   public Set getMBeansFromURL(URL url) throws ServiceNotFoundException
  +   {
  +      if (server == null)
  +         throw new ServiceNotFoundException("MLet must be registered to the server 
before loading the MBeans.");
  +
  +      HashSet mlets         = new HashSet(); 
  +      HashSet mbeans        = new HashSet();
  +      MLetEntry entry       = null;
  +      try
  +      {         
  +         BufferedReader reader = new BufferedReader(new 
InputStreamReader(url.openStream()));
  +         int c                 = -1;
  +      
  +         while((c = reader.read()) != -1)
  +         {
  +            if (c == '<')
  +            {            
  +               StringBuffer buf = new StringBuffer(1000);
  +               boolean readMore = true;
  +               
  +               while(readMore)
  +               {
  +                  c = reader.read();
  +                  
  +                  if (c == -1)
  +                     throw new IOException("Unexpected end of file. Tag was not 
closed: " + buf.toString());
  +                  
  +                  if (c == '>')
  +                  {
  +                     readMore = false;
  +                     break;
  +                  }
  +                  
  +                  buf.append((char)c);
  +               }
  +
  +               StringTokenizer tokenizer = new StringTokenizer(buf.toString(), "= 
\n\t\r");
  +               String tagName = null, attributeName = null, attributeValue = null;
  +               
  +               if (tokenizer.hasMoreTokens())
  +                  tagName = tokenizer.nextToken().trim();
  +               
  +               if (tagName.equals("MLET"))
  +               {
  +                  entry = new MLetEntry();
  +                  
  +                  while(tokenizer.hasMoreTokens())
  +                  {
  +                     try
  +                     {
  +                        attributeName = tokenizer.nextToken("= \n\t\r").trim();
  +                        attributeValue = tokenizer.nextToken(" \n\t\r").trim();
  +                     
  +                        if (attributeValue.equals("="))
  +                           attributeValue = tokenizer.nextToken();
  +                        else
  +                           attributeValue = attributeValue.substring(1, 
attributeValue.length());
  +                        
  +                        if (attributeName.equals("CODE"))
  +                           entry.code = trimQuotes(attributeValue);
  +                        else if (attributeName.equals("OBJECT"))
  +                           entry.object = trimQuotes(attributeValue);
  +                        else if (attributeName.equals("ARCHIVE"))
  +                           // FIXME: "archivelist" must be in quotes
  +                           entry.archive = trimQuotes(attributeValue);
  +                        else if (attributeName.equals("CODEBASE"))
  +                           entry.codebase = trimQuotes(attributeValue);
  +                        else if (attributeName.equals("NAME"))
  +                           entry.name = trimQuotes(attributeValue);
  +                        else if (attributeName.equals("VERSION"))
  +                           entry.version = trimQuotes(attributeValue);
  +                     }
  +                     catch (NoSuchElementException e)
  +                     {
  +                        // couldn't find a valid attribute, value pair
  +                        // ignore and move one
  +                        
  +                        // FIXME: log this somewhere
  +                        System.out.println("No value found for attribute '" + 
attributeName);
  +                     }
  +                  }
  +                  
  +                  if (entry.code == null && entry.object == null)
  +                     throw new ServiceNotFoundException("<" + buf.toString() + "> 
is missing mandatory CODE | OBJECT attribute");
  +                  if (entry.archive == null)
  +                     throw new ServiceNotFoundException("<" + buf.toString() + "> 
is missing mandatory ARCHIVE attribute");
  +                     
  +                  mlets.add(entry);
  +               }
  +                  
  +               else if (tagName.equals("/MLET"))
  +               {
  +                  entry = null;
  +               }
  +               
  +               else if (tagName.equals("ARG"))
  +               {
  +                  try
  +                  {
  +                     if (!tokenizer.nextToken().equals("TYPE"))
  +                        continue;
  +                        
  +                     String name = tokenizer.nextToken();
  +                     
  +                     if (!tokenizer.nextToken().equals("VALUE"))
  +                        continue;
  +                        
  +                     String value = tokenizer.nextToken(" \n\t\r");
  +                     
  +                     if (entry != null)
  +                        entry.args.add(new String[] { name, value });
  +                  }
  +                  catch (NoSuchElementException e)
  +                  {
  +                     // malformed ARG tag means the MBean can't be instantiated
  +                     // bogus class name will fail in createMBean
  +                     entry.args.add(new String[] { "MalformedARG", "" });
  +                     
  +                     // FIXME: log this
  +                     System.out.println("Malformed element: <" + buf.toString() + 
">");
  +                  }
  +               }               
  +            }  // end of if (c == '<')
  +         }  // while((c = reader.read()) != -1) 
  +         
  +         if (mlets.size() == 0)
  +            throw new ServiceNotFoundException("The specified URL '" + url + "' 
does not contain MLET tags.");
  +            
  +         Iterator it = mlets.iterator();
  +         while(it.hasNext())
  +         {
  +            entry = (MLetEntry)it.next();
  +            StringTokenizer tokenizer = new StringTokenizer(entry.archive, ", ");
  +            
  +            // if no codebase is specified then the url of the mlet text file is 
used
  +            if (entry.codebase == null)
  +               entry.codebase = url.toString();
  +               
  +            while(tokenizer.hasMoreElements())
  +               addURL(new URL(entry.codebase + ((entry.codebase.endsWith("/")) ? "" 
: "/") + tokenizer.nextToken()));
  +               
  +            try
  +            {
  +               mbeans.add(server.createMBean(entry.code, (entry.name != null) ? new 
ObjectName(entry.name) : null, objectName));
  +            }
  +            catch (Throwable t)
  +            {
  +               // if mbean can't be created, throwable is added to the return set
  +               mbeans.add(t);
  +               
  +               // FIXME: log it
  +               System.out.println(t.toString());
  +            }            
  +         }
  +      }
  +      catch (IOException e) 
  +      {
  +         throw new ServiceNotFoundException(e.toString());
  +      }
  +         
  +      return mbeans;
      }
   
  -   public void addURL(URL url) {
  +   public void addURL(URL url)
  +   {
         super.addURL(url);
      }
   
  -   public void addURL(String url) throws ServiceNotFoundException {
  -      try {
  +   public void addURL(String url) throws ServiceNotFoundException
  +   {
  +      try
  +      {
            super.addURL(new URL(url));
  -      } catch (MalformedURLException e) {
  +      }
  +      catch (MalformedURLException e)
  +      {
            throw new ServiceNotFoundException("Malformed URL: " + url);
         }
      }
   
  -   public URL[] getURLs() {
  +   public URL[] getURLs()
  +   {
         return super.getURLs();
      }
   
  -   public URL getResource(String name) {
  +   public URL getResource(String name)
  +   {
         return super.getResource(name);
      }
   
  -   public InputStream getResourceAsStream(String name) {
  +   public InputStream getResourceAsStream(String name)
  +   {
         return super.getResourceAsStream(name);
      }
   
   
  -   public String getLibraryDirectory() {
  +   public String getLibraryDirectory()
  +   {
         throw new Error("NYI");
      }
   
  -   public void setLibraryDirectory(String libdir) {
  +   public void setLibraryDirectory(String libdir)
  +   {
         throw new Error("NYI");
      }
  -
  -
  -
  -
  -
  -
  -
  +   
  +   // Private -------------------------------------------------------
  +   private String trimQuotes(String str) 
  +   {
  +      if (str.startsWith("\"") && str.endsWith("\""))
  +         return str.substring(1, str.length() - 1);
  +      else
  +         return str;
  +   }
  +
  +   // Inner classes -------------------------------------------------
  +   private class MLetEntry
  +   {
  +      String code       = null;
  +      String object     = null;
  +      String archive    = null;
  +      String codebase   = null;
  +      String name       = null;
  +      String version    = null;
  +      Set    args       = new HashSet();
  +      
  +      public String toString()
  +      {
  +         return "MLET:\n" +
  +                  "   CODE    =" + code + "\n" +
  +                  "   OBJECT  =" + object + "\n" +
  +                  "   ARCHIVE =" + archive + "\n" +
  +                  "   CODEBASE=" + codebase + "\n" +
  +                  "   NAME    =" + name + "\n" +
  +                  "   VERSION =" + version + "\n";
  +      }
  +   }
   }
   
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to