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