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.util.Hashtable; 024 import javax.naming.ldap.InitialLdapContext; 025 import javax.naming.ldap.LdapContext; 026 027 import org.apache.commons.cli.CommandLine; 028 import org.apache.commons.cli.Option; 029 import org.apache.commons.cli.Options; 030 import org.apache.directory.daemon.AvailablePortFinder; 031 import org.apache.directory.server.i18n.I18n; 032 import org.apache.directory.shared.ldap.message.extended.LaunchDiagnosticUiRequest; 033 034 035 /** 036 * A command to send an extened request which launches a diagnostic UI 037 * on the server's console. This may not work unless the display is set 038 * and access is granted to the display (via xhost +). This is especially 039 * the case when running the server in daemon mode. Usually when running 040 * the server in debug mode is when you want the diagnostics turned on. 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 * @version $Rev: 434420 $ 044 */ 045 public class DiagnosticCommand extends ToolCommand 046 { 047 public static final String PORT_RANGE = "(" + AvailablePortFinder.MIN_PORT_NUMBER + ", " 048 + AvailablePortFinder.MAX_PORT_NUMBER + ")"; 049 050 private int port = 10389; 051 private String host = "localhost"; 052 private String password = "secret"; 053 054 055 protected DiagnosticCommand() 056 { 057 super( "diagnostic" ); 058 } 059 060 061 public void execute( CommandLine cmd ) throws Exception 062 { 063 processOptions( cmd ); 064 065 if ( isDebugEnabled() ) 066 { 067 System.out.println( "Parameters for LaunchDiagnosticUI extended request:" ); 068 System.out.println( "port = " + port ); 069 System.out.println( "host = " + host ); 070 System.out.println( "password = " + password ); 071 } 072 073 Hashtable env = new Hashtable(); 074 env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" ); 075 env.put( "java.naming.provider.url", "ldap://" + host + ":" + port ); 076 env.put( "java.naming.security.principal", "uid=admin,ou=system" ); 077 env.put( "java.naming.security.credentials", password ); 078 env.put( "java.naming.security.authentication", "simple" ); 079 080 LdapContext ctx = new InitialLdapContext( env, null ); 081 if ( isDebugEnabled() ) 082 { 083 System.out.println( "Connection to the server established.\n" + "Sending extended request ... " ); 084 } 085 ctx.extendedOperation( new LaunchDiagnosticUiRequest( 3 ) ); 086 ctx.close(); 087 } 088 089 090 private void processOptions( CommandLine cmd ) 091 { 092 if ( isDebugEnabled() ) 093 { 094 System.out.println( "Processing options for launching diagnostic UI ..." ); 095 } 096 097 // ------------------------------------------------------------------- 098 // figure out and error check the port value 099 // ------------------------------------------------------------------- 100 101 if ( cmd.hasOption( 'p' ) ) // - user provided port w/ -p takes precedence 102 { 103 String val = cmd.getOptionValue( 'p' ); 104 try 105 { 106 port = Integer.parseInt( val ); 107 } 108 catch ( NumberFormatException e ) 109 { 110 System.err.println( I18n.err( I18n.ERR_193, val ) ); 111 System.exit( 1 ); 112 } 113 114 if ( port > AvailablePortFinder.MAX_PORT_NUMBER ) 115 { 116 System.err.println( I18n.err( I18n.ERR_194, val, AvailablePortFinder.MAX_PORT_NUMBER ) ); 117 System.exit( 1 ); 118 } 119 else if ( port < AvailablePortFinder.MIN_PORT_NUMBER ) 120 { 121 System.err.println( I18n.err( I18n.ERR_195, val, AvailablePortFinder.MIN_PORT_NUMBER ) ); 122 System.exit( 1 ); 123 } 124 125 if ( isDebugEnabled() ) 126 { 127 System.out.println( "port overriden by -p option: " + port ); 128 } 129 } 130 else if ( getApacheDS() != null ) 131 { 132 port = getApacheDS().getLdapServer().getPort(); 133 134 if ( isDebugEnabled() ) 135 { 136 System.out.println( "port overriden by server.xml configuration: " + port ); 137 } 138 } 139 else if ( isDebugEnabled() ) 140 { 141 System.out.println( "port set to default: " + port ); 142 } 143 144 // ------------------------------------------------------------------- 145 // figure out the host value 146 // ------------------------------------------------------------------- 147 148 if ( cmd.hasOption( 'h' ) ) 149 { 150 host = cmd.getOptionValue( 'h' ); 151 152 if ( isDebugEnabled() ) 153 { 154 System.out.println( "host overriden by -h option: " + host ); 155 } 156 } 157 else if ( isDebugEnabled() ) 158 { 159 System.out.println( "host set to default: " + host ); 160 } 161 162 // ------------------------------------------------------------------- 163 // figure out the password value 164 // ------------------------------------------------------------------- 165 166 if ( cmd.hasOption( 'w' ) ) 167 { 168 password = cmd.getOptionValue( 'w' ); 169 170 if ( isDebugEnabled() ) 171 { 172 System.out.println( "password overriden by -w option: " + password ); 173 } 174 } 175 else if ( isDebugEnabled() ) 176 { 177 System.out.println( "password set to default: " + password ); 178 } 179 } 180 181 182 public Options getOptions() 183 { 184 Options opts = new Options(); 185 Option op = new Option( "h", "host", true, "server host: defaults to localhost" ); 186 op.setRequired( false ); 187 opts.addOption( op ); 188 op = new Option( "p", "port", true, "server port: defaults to 10389 or server.xml specified port" ); 189 op.setRequired( false ); 190 opts.addOption( op ); 191 op = new Option( "w", "password", true, "the apacheds administrator's password: defaults to secret" ); 192 op.setRequired( false ); 193 opts.addOption( op ); 194 op = new Option( "i", "install-path", true, "path to apacheds installation directory" ); 195 op.setRequired( false ); 196 opts.addOption( op ); 197 return opts; 198 } 199 }