View Javadoc

1   package org.apache.commons.modeler.modules;
2   
3   import java.io.InputStream;
4   import java.io.ObjectInputStream;
5   import java.net.URL;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.apache.commons.modeler.ManagedBean;
12  import org.apache.commons.modeler.Registry;
13  
14  
15  public class MbeansDescriptorsSerSource extends ModelerSource
16  {
17      private static Log log = LogFactory.getLog(MbeansDescriptorsSerSource.class);
18      Registry registry;
19      String location;
20      String type;
21      Object source;
22      List mbeans=new ArrayList();
23  
24      public void setRegistry(Registry reg) {
25          this.registry=reg;
26      }
27  
28      public void setLocation( String loc ) {
29          this.location=loc;
30      }
31  
32      /*** Used if a single component is loaded
33       *
34       * @param type
35       */
36      public void setType( String type ) {
37         this.type=type;
38      }
39  
40      public void setSource( Object source ) {
41          this.source=source;
42      }
43  
44      public List loadDescriptors( Registry registry, String location,
45                                   String type, Object source)
46              throws Exception
47      {
48          setRegistry(registry);
49          setLocation(location);
50          setType(type);
51          setSource(source);
52          execute();
53          return mbeans;
54      }
55  
56      public void execute() throws Exception {
57          if( registry==null ) registry=Registry.getRegistry();
58          long t1=System.currentTimeMillis();
59          try {
60              InputStream stream=null;
61              if( source instanceof URL ) {
62                  stream=((URL)source).openStream();
63              }
64              if( source instanceof InputStream ) {
65                  stream=(InputStream)source;
66              }
67              if( stream==null ) {
68                  throw new Exception( "Can't process "+ source);
69              }
70              ObjectInputStream ois=new ObjectInputStream(stream);
71              Thread.currentThread().setContextClassLoader(ManagedBean.class.getClassLoader());
72              Object obj=ois.readObject();
73              //log.info("Reading " + obj);
74              ManagedBean beans[]=(ManagedBean[])obj;
75              // after all are read without error
76              for( int i=0; i<beans.length; i++ ) {
77                  mbeans.add(beans[i]);
78              }
79  
80          } catch( Exception ex ) {
81              log.error( "Error reading descriptors " + source + " " +  ex.toString(),
82                      ex);
83              throw ex;
84          }
85          long t2=System.currentTimeMillis();
86          log.info( "Reading descriptors ( ser ) " + (t2-t1));
87      }
88  }