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.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   * A command to send an extened request which launches a diagnostic UI 
36   * on the server's console.  This may not work unless the display is set 
37   * and access is granted to the display (via xhost +).  This is especially
38   * the case when running the server in daemon mode.  Usually when running
39   * the server in debug mode is when you want the diagnostics turned on.
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev: 434420 $
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          // figure out and error check the port value
98          // -------------------------------------------------------------------
99  
100         if ( cmd.hasOption( 'p' ) ) // - user provided port w/ -p takes precedence
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         // figure out the host value
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         // figure out the password value
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 }