View Javadoc

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