001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.tools;
021    
022    
023    import java.io.File;
024    import java.io.IOException;
025    import java.util.Properties;
026    
027    import org.apache.commons.cli.CommandLine;
028    import org.apache.directory.server.i18n.I18n;
029    
030    
031    /**
032     * The main() application which executes command targets.
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev: 921363 $
036     */
037    public class ApachedsTools
038    {
039        public static void main( String[] args ) throws Exception
040        {
041            BaseCommand tools = getInstance();
042    
043            if ( !BaseCommand.hasBannerOption( args ) )
044            {
045                tools.printBanner();
046            }
047    
048            if ( args.length == 0 )
049            {
050                System.err.println( I18n.err( I18n.ERR_184, tools.getProductCommand() ) );
051                System.exit( 1 );
052            }
053    
054            // help is a special command 
055            String command = args[0].toLowerCase();
056            if ( "help".equals( command ) )
057            {
058                CommandLine cmdline = tools.getCommandLine( command, args );
059                if ( cmdline.getArgs().length > 1 )
060                {
061                    tools.helpOnCommand( cmdline.getArgs()[1] );
062                    System.exit( 0 );
063                }
064                else
065                {
066                    tools.printUsage();
067                    System.exit( 0 );
068                }
069            }
070            else if ( command.equals( "-version" ) )
071            {
072                System.out.println( tools.getProductCommand() + " version " + tools.getProductVersion() );
073                System.exit( 0 );
074            }
075    
076            ToolCommand cmd = ( ToolCommand ) tools.getCommands().get( command );
077            if ( cmd == null )
078            {
079                System.err.println( I18n.err( I18n.ERR_185, args[0] ) );
080                System.err.println( I18n.err( I18n.ERR_184, tools.getProductCommand() ) );
081                System.exit( 1 );
082            }
083    
084            CommandLine cmdline = tools.getCommandLine( command, args );
085            if ( cmdline.hasOption( 'd' ) )
086            {
087                cmd.setDebugEnabled( true );
088                BaseCommand.dumpArgs( "raw command line arguments: ", args );
089                BaseCommand.dumpArgs( "parsed arguments: ", cmdline.getArgs() );
090            }
091    
092            cmd.setQuietEnabled( cmdline.hasOption( 'q' ) );
093            cmd.setDebugEnabled( cmdline.hasOption( 'd' ) );
094            cmd.setVerboseEnabled( cmdline.hasOption( 'v' ) );
095            cmd.setVersion( tools.getProductVersion() );
096            
097            if ( cmdline.getOptionValue( 'i' ) != null )
098            {
099                cmd.setLayout( cmdline.getOptionValue( 'i' ) );
100            }
101            else if ( cmdline.hasOption( 'c' ) )
102            {
103                System.err.println( I18n.err( I18n.ERR_186 ) );
104                System.exit( 1 );
105            }
106    
107            if ( cmdline.getOptionValue( 'z' ) != null )
108            {
109                cmd.setInstanceLayout( new InstanceLayout( new File( cmdline.getOptionValue( 'z' ) ) ) );
110            }
111            
112            cmd.execute( cmdline );
113        }
114    
115    
116        public static BaseCommand getInstance() throws InstantiationException, IllegalAccessException,
117            ClassNotFoundException
118        {
119            Properties props = new Properties();
120            try
121            {
122                props.load( BaseCommand.class.getResourceAsStream( "product.properties" ) );
123            }
124            catch ( IOException e )
125            {
126                e.printStackTrace();
127            }
128    
129            String productVersion = props.getProperty( "product.version", "UNKNOWN" );
130            String productUrl = props.getProperty( "product.url", "http://directory.apache.org" );
131            String productDisplayName = props.getProperty( "product.display.name", "Apache Directory Server" );
132            String productCommand = props.getProperty( "product.command", "apacheds-tools" );
133            String productBanner = props.getProperty( "product.banner", BaseCommand.BANNER );
134            String productClass = props.getProperty( "product.class", "org.apache.directory.server.tools.BaseCommand" );
135    
136            BaseCommand baseCommand = ( BaseCommand ) Class.forName( productClass ).newInstance();
137            baseCommand.setProductBanner( productBanner );
138            baseCommand.setProductDisplayName( productDisplayName );
139            baseCommand.setProductUrl( productUrl );
140            baseCommand.setProductVersion( productVersion );
141            baseCommand.setProductCommand( productCommand );
142            return baseCommand;
143        }
144    }