1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.modeler.ant;
20
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.ObjectOutputStream;
25 import java.net.URL;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.modeler.ManagedBean;
30 import org.apache.commons.modeler.Registry;
31 import org.apache.tools.ant.BuildException;
32
33 /***
34 * Load descriptors into registry.
35 *
36 * @author Costin Manolache
37 */
38 public final class RegistryTask {
39 private static Log log = LogFactory.getLog(RegistryTask.class);
40
41 public RegistryTask() {
42 }
43
44 String resource;
45 String file;
46 String type="MbeansDescriptorsDOMSource";
47
48 /*** Set the resource type that will be loaded
49 *
50 * @param type
51 */
52 public void setType( String type ) {
53 this.type=type;
54 }
55
56 public void setFile( String file ) {
57 this.file=file;
58 }
59
60 public void setResource( String res ) {
61 this.resource=res;
62 }
63
64 String outFile;
65
66 public void setOut( String outFile ) {
67 this.outFile=outFile;
68 }
69
70 public void execute() throws Exception {
71 URL url=null;
72
73 if( resource != null ) {
74 url=this.getClass().getClassLoader().getResource(resource);
75 } else if( file != null ) {
76 File f=new File(file);
77 url=new URL("file", null, f.getAbsolutePath());
78 } else {
79 throw new BuildException( "Resource or file attribute required");
80 }
81
82 Registry.getRegistry().loadDescriptors( type, url, null);
83
84 if( outFile !=null ) {
85 FileOutputStream fos=new FileOutputStream(outFile);
86 ObjectOutputStream oos=new ObjectOutputStream(fos);
87 Registry reg=Registry.getRegistry();
88 String beans[]=reg.findManagedBeans();
89 ManagedBean mbeans[]=new ManagedBean[beans.length];
90 for( int i=0; i<beans.length; i++ ) {
91 mbeans[i]=reg.findManagedBean(beans[i]);
92 }
93 oos.writeObject( mbeans );
94 oos.flush();
95 oos.close();
96 fos.close();
97 }
98 }
99 }
100