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.util.Hashtable;
24 import javax.naming.ldap.InitialLdapContext;
25 import javax.naming.ldap.LdapContext;
26
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.Option;
29 import org.apache.commons.cli.Options;
30 import org.apache.directory.daemon.AvailablePortFinder;
31 import org.apache.directory.shared.ldap.message.extended.LaunchDiagnosticUiRequest;
32
33
34
35
36
37
38
39
40
41
42
43
44 public class DiagnosticCommand extends ToolCommand
45 {
46 public static final String PORT_RANGE = "(" + AvailablePortFinder.MIN_PORT_NUMBER + ", "
47 + AvailablePortFinder.MAX_PORT_NUMBER + ")";
48
49 private int port = 10389;
50 private String host = "localhost";
51 private String password = "secret";
52
53
54 protected DiagnosticCommand()
55 {
56 super( "diagnostic" );
57 }
58
59
60 public void execute( CommandLine cmd ) throws Exception
61 {
62 processOptions( cmd );
63
64 if ( isDebugEnabled() )
65 {
66 System.out.println( "Parameters for LaunchDiagnosticUI extended request:" );
67 System.out.println( "port = " + port );
68 System.out.println( "host = " + host );
69 System.out.println( "password = " + password );
70 }
71
72 Hashtable env = new Hashtable();
73 env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
74 env.put( "java.naming.provider.url", "ldap://" + host + ":" + port );
75 env.put( "java.naming.security.principal", "uid=admin,ou=system" );
76 env.put( "java.naming.security.credentials", password );
77 env.put( "java.naming.security.authentication", "simple" );
78
79 LdapContext ctx = new InitialLdapContext( env, null );
80 if ( isDebugEnabled() )
81 {
82 System.out.println( "Connection to the server established.\n" + "Sending extended request ... " );
83 }
84 ctx.extendedOperation( new LaunchDiagnosticUiRequest( 3 ) );
85 ctx.close();
86 }
87
88
89 private void processOptions( CommandLine cmd )
90 {
91 if ( isDebugEnabled() )
92 {
93 System.out.println( "Processing options for launching diagnostic UI ..." );
94 }
95
96
97
98
99
100 if ( cmd.hasOption( 'p' ) )
101 {
102 String val = cmd.getOptionValue( 'p' );
103 try
104 {
105 port = Integer.parseInt( val );
106 }
107 catch ( NumberFormatException e )
108 {
109 System.err.println( "port value of '" + val + "' is not a number" );
110 System.exit( 1 );
111 }
112
113 if ( port > AvailablePortFinder.MAX_PORT_NUMBER )
114 {
115 System.err.println( "port value of '" + val + "' is larger than max port number: "
116 + AvailablePortFinder.MAX_PORT_NUMBER );
117 System.exit( 1 );
118 }
119 else if ( port < AvailablePortFinder.MIN_PORT_NUMBER )
120 {
121 System.err.println( "port value of '" + val + "' is smaller than the minimum port number: "
122 + AvailablePortFinder.MIN_PORT_NUMBER );
123 System.exit( 1 );
124 }
125
126 if ( isDebugEnabled() )
127 {
128 System.out.println( "port overriden by -p option: " + port );
129 }
130 }
131 else if ( getApacheDS() != null )
132 {
133 port = getApacheDS().getLdapService().getIpPort();
134
135 if ( isDebugEnabled() )
136 {
137 System.out.println( "port overriden by server.xml configuration: " + port );
138 }
139 }
140 else if ( isDebugEnabled() )
141 {
142 System.out.println( "port set to default: " + port );
143 }
144
145
146
147
148
149 if ( cmd.hasOption( 'h' ) )
150 {
151 host = cmd.getOptionValue( 'h' );
152
153 if ( isDebugEnabled() )
154 {
155 System.out.println( "host overriden by -h option: " + host );
156 }
157 }
158 else if ( isDebugEnabled() )
159 {
160 System.out.println( "host set to default: " + host );
161 }
162
163
164
165
166
167 if ( cmd.hasOption( 'w' ) )
168 {
169 password = cmd.getOptionValue( 'w' );
170
171 if ( isDebugEnabled() )
172 {
173 System.out.println( "password overriden by -w option: " + password );
174 }
175 }
176 else if ( isDebugEnabled() )
177 {
178 System.out.println( "password set to default: " + password );
179 }
180 }
181
182
183 public Options getOptions()
184 {
185 Options opts = new Options();
186 Option op = new Option( "h", "host", true, "server host: defaults to localhost" );
187 op.setRequired( false );
188 opts.addOption( op );
189 op = new Option( "p", "port", true, "server port: defaults to 10389 or server.xml specified port" );
190 op.setRequired( false );
191 opts.addOption( op );
192 op = new Option( "w", "password", true, "the apacheds administrator's password: defaults to secret" );
193 op.setRequired( false );
194 opts.addOption( op );
195 op = new Option( "i", "install-path", true, "path to apacheds installation directory" );
196 op.setRequired( false );
197 opts.addOption( op );
198 return opts;
199 }
200 }