View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.tools;
21  
22  
23  import java.io.IOException;
24  import java.net.URL;
25  import java.util.Properties;
26  
27  import org.apache.commons.cli.CommandLine;
28  import org.apache.directory.server.configuration.ApacheDS;
29  import org.springframework.context.ApplicationContext;
30  import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
31  
32  
33  /**
34   * The main() application which executes command targets.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 648932 $
38   */
39  public class ApachedsTools
40  {
41      public static void main( String[] args ) throws Exception
42      {
43          BaseCommand tools = getInstance();
44  
45          if ( !BaseCommand.hasBannerOption( args ) )
46          {
47              tools.printBanner();
48          }
49  
50          if ( args.length == 0 )
51          {
52              System.err.println( "Type " + tools.getProductCommand() + " help for usage." );
53              System.exit( 1 );
54          }
55  
56          // help is a special command 
57          String command = args[0].toLowerCase();
58          if ( "help".equals( command ) )
59          {
60              CommandLine cmdline = tools.getCommandLine( command, args );
61              if ( cmdline.getArgs().length > 1 )
62              {
63                  tools.helpOnCommand( cmdline.getArgs()[1] );
64                  System.exit( 0 );
65              }
66              else
67              {
68                  tools.printUsage();
69                  System.exit( 0 );
70              }
71          }
72          else if ( command.equals( "-version" ) )
73          {
74              System.out.println( tools.getProductCommand() + " version " + tools.getProductVersion() );
75              System.exit( 0 );
76          }
77  
78          ToolCommand cmd = ( ToolCommand ) tools.getCommands().get( command );
79          if ( cmd == null )
80          {
81              System.err.println( "Unknown command: " + args[0] );
82              System.err.println( "Type " + tools.getProductCommand() + " help for usage." );
83              System.exit( 1 );
84          }
85  
86          CommandLine cmdline = tools.getCommandLine( command, args );
87          if ( cmdline.hasOption( 'd' ) )
88          {
89              cmd.setDebugEnabled( true );
90              BaseCommand.dumpArgs( "raw command line arguments: ", args );
91              BaseCommand.dumpArgs( "parsed arguments: ", cmdline.getArgs() );
92          }
93  
94          cmd.setQuietEnabled( cmdline.hasOption( 'q' ) );
95          cmd.setDebugEnabled( cmdline.hasOption( 'd' ) );
96          cmd.setVerboseEnabled( cmdline.hasOption( 'v' ) );
97          cmd.setVersion( tools.getProductVersion() );
98          if ( cmdline.getOptionValue( 'i' ) != null )
99          {
100             cmd.setLayout( cmdline.getOptionValue( 'i' ) );
101             if ( !cmd.isQuietEnabled() )
102             {
103                 System.out.println( "loading settings from: " + cmd.getLayout().getConfigurationFile() );
104             }
105             ApplicationContext factory = null;
106             URL configUrl = cmd.getLayout().getConfigurationFile().toURL();
107             factory = new FileSystemXmlApplicationContext( configUrl.toString() );
108             cmd.setApacheDS( ( ApacheDS ) factory.getBean( "apacheDS" ) );
109         }
110         else if ( cmdline.hasOption( 'c' ) )
111         {
112             System.err.println( "forced configuration load (-c) requires the -i option" );
113             System.exit( 1 );
114         }
115 
116         cmd.execute( cmdline );
117     }
118 
119 
120     public static BaseCommand getInstance() throws InstantiationException, IllegalAccessException,
121         ClassNotFoundException
122     {
123         Properties props = new Properties();
124         try
125         {
126             props.load( BaseCommand.class.getResourceAsStream( "product.properties" ) );
127         }
128         catch ( IOException e )
129         {
130             e.printStackTrace();
131         }
132 
133         String productVersion = props.getProperty( "product.version", "UNKNOWN" );
134         String productUrl = props.getProperty( "product.url", "http://directory.apache.org" );
135         String productDisplayName = props.getProperty( "product.display.name", "Apache Directory Server" );
136         String productCommand = props.getProperty( "product.command", "apacheds-tools" );
137         String productBanner = props.getProperty( "product.banner", BaseCommand.BANNER );
138         String productClass = props.getProperty( "product.class", "org.apache.directory.server.tools.BaseCommand" );
139 
140         BaseCommand baseCommand = ( BaseCommand ) Class.forName( productClass ).newInstance();
141         baseCommand.setProductBanner( productBanner );
142         baseCommand.setProductDisplayName( productDisplayName );
143         baseCommand.setProductUrl( productUrl );
144         baseCommand.setProductVersion( productVersion );
145         baseCommand.setProductCommand( productCommand );
146         return baseCommand;
147     }
148 }