View Javadoc

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;
22  import java.io.FileOutputStream;
23  import java.io.ObjectOutputStream;
24  import java.net.URL;
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 resource;
44      String file;
45      String type="MbeansDescriptorsDOMSource";
46  
47      /*** Set the resource type that will be loaded
48       *
49       * @param type
50       */
51      public void setType( String type ) {
52          this.type=type;
53      }
54  
55      public void setFile( String file ) {
56          this.file=file;
57      }
58  
59      public void setResource( String res ) {
60          this.resource=res;
61      }
62  
63      String outFile;
64  
65      public void setOut( String outFile ) {
66          this.outFile=outFile;
67      }
68  
69      public void execute() throws Exception {
70          URL url=null;
71  
72          if( resource != null ) {
73              url=this.getClass().getClassLoader().getResource(resource);
74          } else if( file != null ) {
75              File f=new File(file);
76              url=new URL("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 fos=new FileOutputStream(outFile);
85              ObjectOutputStream oos=new ObjectOutputStream(fos);
86              Registry reg=Registry.getRegistry();
87              String 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