1 4 package com.tc.admin; 5 6 import org.apache.commons.cli.CommandLine; 7 import org.apache.commons.cli.GnuParser; 8 import org.apache.commons.cli.HelpFormatter; 9 import org.apache.commons.cli.Option; 10 import org.apache.commons.cli.Options; 11 import org.apache.commons.cli.UnrecognizedOptionException; 12 import org.apache.commons.lang.ArrayUtils; 13 14 import com.tc.config.schema.NewCommonL2Config; 15 import com.tc.config.schema.setup.FatalIllegalConfigurationChangeHandler; 16 import com.tc.config.schema.setup.L2TVSConfigurationSetupManager; 17 import com.tc.config.schema.setup.StandardTVSConfigurationSetupManagerFactory; 18 import com.tc.config.schema.setup.TVSConfigurationSetupManagerFactory; 19 import com.tc.management.TerracottaManagement; 20 import com.tc.management.beans.L2MBeanNames; 21 import com.tc.management.beans.TCServerInfoMBean; 22 23 import java.io.File ; 24 import java.lang.reflect.Method ; 25 import java.net.ConnectException ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.HashMap ; 29 30 import javax.management.MBeanServerConnection ; 31 import javax.management.remote.JMXConnector ; 32 import javax.management.remote.JMXConnectorFactory ; 33 import javax.management.remote.JMXServiceURL ; 34 35 public class TCStop { 36 37 private String m_host; 38 private int m_port; 39 private String m_userName; 40 41 public static final String DEFAULT_HOST = "localhost"; 42 public static final int DEFAULT_PORT = 9520; 43 44 public static final void main(String [] args) throws Exception { 45 Options options = StandardTVSConfigurationSetupManagerFactory.createOptions(true); 46 CommandLine commandLine = null; 47 48 Option userNameOption = new Option("u", "username", true, "user name"); 49 userNameOption.setType(String .class); 50 userNameOption.setRequired(false); 51 options.addOption(userNameOption); 52 53 Option helpOption = new Option("h", "help"); 54 helpOption.setType(String .class); 55 helpOption.setRequired(false); 56 options.addOption(helpOption); 57 58 try { 59 commandLine = new GnuParser().parse(options, args); 60 } catch(UnrecognizedOptionException e) { 61 System.err.println(e.getMessage()); 62 usageAndDie(options); 63 } 64 65 if(commandLine == null || commandLine.getArgs().length > 2) { 66 usageAndDie(options); 67 } 68 69 String host = null; 70 int port = -1; 71 72 if (commandLine.hasOption("h")) { 73 new HelpFormatter().printHelp("java " + TCStop.class.getName(), options); 74 System.exit(1); 75 } 76 77 String defaultName = StandardTVSConfigurationSetupManagerFactory.DEFAULT_CONFIG_SPEC; 78 File configFile = new File (System.getProperty("user.dir"), defaultName); 79 boolean configSpecified = commandLine.hasOption('f'); 80 boolean nameSpecified = commandLine.hasOption('n'); 81 boolean userNameSpecified = commandLine.hasOption('u'); 82 83 String userName = null; 84 if(userNameSpecified) { 85 userName = commandLine.getOptionValue('u'); 86 } 87 88 if (configSpecified || System.getProperty("tc.config") != null || configFile.exists()) { 89 if (!configSpecified && System.getProperty("tc.config") == null) { 90 ArrayList tmpArgs = new ArrayList (Arrays.asList(args)); 91 92 tmpArgs.add("-f"); 93 tmpArgs.add(configFile.getAbsolutePath()); 94 args = (String [])tmpArgs.toArray(new String [tmpArgs.size()]); 95 } 96 97 FatalIllegalConfigurationChangeHandler changeHandler = new FatalIllegalConfigurationChangeHandler(); 98 TVSConfigurationSetupManagerFactory factory = new StandardTVSConfigurationSetupManagerFactory(args, true, changeHandler); 99 100 String name = null; 101 if (nameSpecified) { 102 name = commandLine.getOptionValue('n'); 103 } 104 105 L2TVSConfigurationSetupManager manager = factory.createL2TVSConfigurationSetupManager(name); 106 String [] servers = manager.allCurrentlyKnownServers(); 107 108 if (nameSpecified && !Arrays.asList(servers).contains(name)) { 109 System.err.println("The specified Terracotta server configuration '"+name+"' does not exist; exiting."); 110 System.exit(1); 111 } 112 113 if (name == null && servers != null && servers.length == 1) { 114 name = servers[0]; 115 System.err.println("There is only one Terracotta server in this configuration file ("+name+"); stopping it."); 116 } else if (name == null && servers != null && servers.length > 1) { 117 System.err.println("There are multiple Terracotta servers defined in this configuration file; please specify " 118 + "which one you want to stop, using the '-n' command-line option. Available servers are:\n" 119 + " " + ArrayUtils.toString(servers)); 120 System.exit(1); 121 } 122 123 NewCommonL2Config serverConfig = manager.commonL2ConfigFor(name); 124 125 host = serverConfig.host().getString(); 126 if (host == null) host = name; 127 if (host == null) host = DEFAULT_HOST; 128 port = serverConfig.jmxPort().getInt(); 129 System.err.println("Host: " + host + ", port: " + port); 130 } else { 131 if (commandLine.getArgs().length == 0) { 132 host = DEFAULT_HOST; 133 port = DEFAULT_PORT; 134 System.err 135 .println("No host or port provided. Stopping the Terracotta server at '" + host + "', port " + port + " by default."); 136 } else if (commandLine.getArgs().length == 1) { 137 host = DEFAULT_HOST; 138 port = Integer.parseInt(commandLine.getArgs()[0]); 139 } else { 140 host = commandLine.getArgs()[0]; 141 port = Integer.parseInt(commandLine.getArgs()[1]); 142 } 143 } 144 145 try { 146 new TCStop(host, port, userName).stop(); 147 } catch (ConnectException ce) { 148 System.err.println("Unable to connect to host '" + host + "', port " + port 149 + ". Are you sure there is a Terracotta server running there?"); 150 } catch(Exception e) { 151 System.err.println(e.getMessage()); 152 } 153 } 154 155 private static void usageAndDie(Options options) throws Exception { 156 new HelpFormatter().printHelp("java " + TCStop.class.getName(), options); 157 System.exit(1); 158 } 159 160 public TCStop(String host, int port) { 161 m_host = host; 162 m_port = port; 163 } 164 165 public TCStop(String host, int port, String userName) { 166 this(host, port); 167 m_userName = userName; 168 } 169 170 public void stop() throws Exception { 171 JMXConnector jmxc = getJMXConnector(); 172 MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); 173 174 if (mbsc != null) { 175 TCServerInfoMBean tcServerInfo = (TCServerInfoMBean) TerracottaManagement.findMBean(L2MBeanNames.TC_SERVER_INFO, 176 TCServerInfoMBean.class, mbsc); 177 try { 178 tcServerInfo.shutdown(); 179 } catch (Exception e) { 180 jmxc.close(); 181 throw e; 182 } finally { 183 jmxc.close(); 184 } 185 } 186 } 187 188 private static String getPassword() { 189 try { 190 Method m = System .class.getMethod("console", new Class []{}); 191 Object console = m.invoke(null, null); 192 if(console != null) { 193 m = console.getClass().getMethod("readPassword", new Class [] {String .class, Object [].class}); 194 if(m != null) { 195 byte[] pw = (byte[])m.invoke(console, new Object [] {"[%s]", "[console] Enter Password: "}); 196 return new String (pw); 197 } 198 } 199 } catch(Exception e) {} 200 201 try { 202 System.out.print("Enter password: "); 203 return new jline.ConsoleReader().readLine(new Character ('*')); 204 } catch(Exception e) {} 205 206 return null; 207 } 208 209 private JMXConnector getJMXConnector() throws Exception { 210 String uri = "service:jmx:rmi:///jndi/rmi://" + m_host + ":" + m_port + "/jmxrmi"; 211 JMXServiceURL url = new JMXServiceURL (uri); 212 HashMap env = new HashMap (); 213 214 if(m_userName != null) { 215 String [] creds = {m_userName, getPassword()}; 216 env.put("jmx.remote.credentials", creds); 217 } 218 219 return JMXConnectorFactory.connect(url, env); 220 } 221 } 222 | Popular Tags |