KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > ant > RegistryTask


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.modeler.ant;
19
20
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.commons.modeler.ManagedBean;
29 import org.apache.commons.modeler.Registry;
30 import org.apache.tools.ant.BuildException;
31
32 /**
33  * Load descriptors into registry.
34  *
35  * @author Costin Manolache
36  */

37 public final class RegistryTask {
38     private static Log log = LogFactory.getLog(RegistryTask.class);
39
40     public RegistryTask() {
41     }
42
43     String JavaDoc resource;
44     String JavaDoc file;
45     String JavaDoc type="MbeansDescriptorsDOMSource";
46
47     /** Set the resource type that will be loaded
48      *
49      * @param type
50      */

51     public void setType( String JavaDoc type ) {
52         this.type=type;
53     }
54
55     public void setFile( String JavaDoc file ) {
56         this.file=file;
57     }
58
59     public void setResource( String JavaDoc res ) {
60         this.resource=res;
61     }
62
63     String JavaDoc outFile;
64
65     public void setOut( String JavaDoc outFile ) {
66         this.outFile=outFile;
67     }
68
69     public void execute() throws Exception JavaDoc {
70         URL JavaDoc url=null;
71
72         if( resource != null ) {
73             url=this.getClass().getClassLoader().getResource(resource);
74         } else if( file != null ) {
75             File JavaDoc f=new File JavaDoc(file);
76             url=new URL JavaDoc("file", null, f.getAbsolutePath());
77         } else {
78             throw new BuildException( "Resource or file attribute required");
79         }
80
81         Registry.getRegistry().loadDescriptors( type, url, null);
82
83         if( outFile !=null ) {
84             FileOutputStream JavaDoc fos=new FileOutputStream JavaDoc(outFile);
85             ObjectOutputStream JavaDoc oos=new ObjectOutputStream JavaDoc(fos);
86             Registry reg=Registry.getRegistry();
87             String JavaDoc beans[]=reg.findManagedBeans();
88             ManagedBean mbeans[]=new ManagedBean[beans.length];
89             for( int i=0; i<beans.length; i++ ) {
90                 mbeans[i]=reg.findManagedBean(beans[i]);
91             }
92             oos.writeObject( mbeans );
93             oos.flush();
94             oos.close();
95             fos.close();
96         }
97     }
98 }
99  
Popular Tags