1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37
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
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 }