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;
20  
21  
22  import java.io.File;
23  import java.net.URL;
24  import java.util.List;
25  
26  import org.apache.commons.modeler.util.IntrospectionUtils;
27  
28  
29  /***
30   *  Small main that loads mbeans.
31   *
32   *  Requires: commons-logging-api.jar, jaxp ( including DOM ), jmx
33   *
34   *  Arguments:
35   *   -file FILE
36   *       Will load mbeans from the file
37   *
38   * @author Costin Manolache
39   */
40  
41  public class Main
42  {
43      String file;
44      String home;
45  
46      public void setFile( String f ) {
47          this.file=f;
48      }
49  
50      // shortcut
51      public void setF( String f ) {
52          this.file=f;
53      }
54  
55      public void execute( )
56          throws Exception
57      {
58          if( home==null ) {
59              home=IntrospectionUtils.guessInstall("install.dir", "home.dir",
60                  "commons-modeler.jar", "org.apache.commons.modeler.Main");
61          }
62  
63          if( file==null ) throw new Exception( "No file, use -file file.xml");
64  
65          Registry reg=Registry.getRegistry();
66          File fileF=new File( file );
67          URL url=new URL("file", null, fileF.getAbsolutePath());
68  
69          // Load the mbeans defined in the file and set all
70          // attributes
71          List mbeans=reg.loadMBeans( url, null);
72          reg.invoke(mbeans, "init", false);
73          reg.invoke(mbeans, "start", false);
74      }
75  
76      public static void main( String args[] ) {
77          try {
78              Main main=new Main();
79              IntrospectionUtils.processArgs(main, args);
80  
81              main.execute();
82          } catch( Exception ex ) {
83              ex.printStackTrace();
84          }
85  
86      }
87  }