KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > modules > MbeansDescriptorsSerSource


1 package org.apache.commons.modeler.modules;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.ObjectInputStream JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
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 JavaDoc location;
20     String JavaDoc type;
21     Object JavaDoc source;
22     List JavaDoc mbeans=new ArrayList JavaDoc();
23
24     public void setRegistry(Registry reg) {
25         this.registry=reg;
26     }
27
28     public void setLocation( String JavaDoc 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 JavaDoc type ) {
37        this.type=type;
38     }
39
40     public void setSource( Object JavaDoc source ) {
41         this.source=source;
42     }
43
44     public List JavaDoc loadDescriptors( Registry registry, String JavaDoc location,
45                                  String JavaDoc type, Object JavaDoc source)
46             throws Exception JavaDoc
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 JavaDoc {
57         if( registry==null ) registry=Registry.getRegistry();
58         long t1=System.currentTimeMillis();
59         try {
60             InputStream JavaDoc stream=null;
61             if( source instanceof URL JavaDoc ) {
62                 stream=((URL JavaDoc)source).openStream();
63             }
64             if( source instanceof InputStream JavaDoc ) {
65                 stream=(InputStream JavaDoc)source;
66             }
67             if( stream==null ) {
68                 throw new Exception JavaDoc( "Can't process "+ source);
69             }
70             ObjectInputStream JavaDoc ois=new ObjectInputStream JavaDoc(stream);
71             Thread.currentThread().setContextClassLoader(ManagedBean.class.getClassLoader());
72             Object JavaDoc 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 JavaDoc 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 }
89
Popular Tags