| 1 |
package org.astrogrid.samp; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.lang.reflect.InvocationTargetException; |
| 5 |
import java.lang.reflect.Method; |
| 6 |
import java.lang.reflect.Modifier; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.List; |
| 11 |
import org.astrogrid.samp.client.ClientProfile; |
| 12 |
import org.astrogrid.samp.client.DefaultClientProfile; |
| 13 |
import org.astrogrid.samp.hub.Hub; |
| 14 |
import org.astrogrid.samp.web.WebClientProfile; |
| 15 |
import org.astrogrid.samp.xmlrpc.StandardClientProfile; |
| 16 |
import org.astrogrid.samp.xmlrpc.XmlRpcKit; |
| 17 |
|
| 18 |
/** |
| 19 |
* Convenience class for invoking JSAMP command-line applications. |
| 20 |
* |
| 21 |
* @author Mark Taylor |
| 22 |
* @since 23 Jul 2008 |
| 23 |
*/ |
| 24 |
public class JSamp { |
| 25 |
|
| 26 |
/** Known command class names. */ |
| 27 |
static final String[] COMMAND_CLASSES = new String[] { |
| 28 |
"org.astrogrid.samp.hub.Hub", |
| 29 |
"org.astrogrid.samp.gui.HubMonitor", |
| 30 |
"org.astrogrid.samp.util.SampLoad", |
| 31 |
"org.astrogrid.samp.test.Snooper", |
| 32 |
"org.astrogrid.samp.test.MessageSender", |
| 33 |
"org.astrogrid.samp.test.HubTester", |
| 34 |
"org.astrogrid.samp.test.CalcStorm", |
| 35 |
"org.astrogrid.samp.bridge.Bridge", |
| 36 |
}; |
| 37 |
|
| 38 |
/** |
| 39 |
* Private sole constructor prevents instantiation. |
| 40 |
*/ |
| 41 |
private JSamp() { |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Does the work for the main method. |
| 46 |
*/ |
| 47 |
public static int runMain( String[] args ) { |
| 48 |
|
| 49 |
// Assemble usage message. |
| 50 |
StringBuffer ubuf = new StringBuffer() |
| 51 |
.append( "\n Usage:" ) |
| 52 |
.append( "\n " ) |
| 53 |
.append( JSamp.class.getName() ) |
| 54 |
.append( " [-help]" ) |
| 55 |
.append( " [-version]" ) |
| 56 |
.append( " <command>" ) |
| 57 |
.append( " [-help]" ) |
| 58 |
.append( " <cmd-args>" ) |
| 59 |
.append( "\n " ) |
| 60 |
.append( "<command-class>" ) |
| 61 |
.append( " [-help]" ) |
| 62 |
.append( " <cmd-args>" ) |
| 63 |
.append( "\n" ) |
| 64 |
.append( "\n Commands (command-classes) are:" ); |
| 65 |
for ( int ic = 0; ic < COMMAND_CLASSES.length; ic++ ) { |
| 66 |
String className = COMMAND_CLASSES[ ic ]; |
| 67 |
ubuf.append( "\n " ) |
| 68 |
.append( abbrev( className ) ); |
| 69 |
int pad = 14 - abbrev( className ).length(); |
| 70 |
for ( int i = 0; i < pad; i++ ) { |
| 71 |
ubuf.append( ' ' ); |
| 72 |
} |
| 73 |
ubuf.append( " (" ) |
| 74 |
.append( className ) |
| 75 |
.append( ")" ); |
| 76 |
} |
| 77 |
ubuf.append( "\n" ) |
| 78 |
.append( "\n " ) |
| 79 |
.append( "Environment Variable:" ) |
| 80 |
.append( "\n " ) |
| 81 |
.append( DefaultClientProfile.HUBLOC_ENV ) |
| 82 |
.append( " = " ) |
| 83 |
.append( StandardClientProfile.STDPROFILE_HUB_PREFIX ) |
| 84 |
.append( "<url>" ) |
| 85 |
.append( "|" ) |
| 86 |
.append( WebClientProfile.WEBPROFILE_HUB_PREFIX ) |
| 87 |
.append( "<name>" ) |
| 88 |
.append( "\n " ) |
| 89 |
.append( "|" ) |
| 90 |
.append( DefaultClientProfile.HUBLOC_CLASS_PREFIX ) |
| 91 |
.append( "<clientprofile-class>" ) |
| 92 |
.append( "\n" ) |
| 93 |
.append( "\n " ) |
| 94 |
.append( "System Properties:" ) |
| 95 |
.append( "\n " ) |
| 96 |
.append( "jsamp.hub.profiles = " ) |
| 97 |
.append( "std|web|<hubprofile-class>[,...]" ) |
| 98 |
.append( "\n " ) |
| 99 |
.append( "jsamp.localhost = " ) |
| 100 |
.append( "\"[hostname]\"|\"[hostnumber]\"|<value>" ) |
| 101 |
.append( "\n " ) |
| 102 |
.append( "jsamp.server.port = " ) |
| 103 |
.append( "<port-number>" ) |
| 104 |
.append( "\n " ) |
| 105 |
.append( "jsamp.web.extrahosts = " ) |
| 106 |
.append( "<host>[,...]" ) |
| 107 |
.append( "\n " ) |
| 108 |
.append( "jsamp.xmlrpc.impl = " ) |
| 109 |
.append( formatImpls( XmlRpcKit.KNOWN_IMPLS, XmlRpcKit.class ) ) |
| 110 |
.append( "\n" ); |
| 111 |
String usage = ubuf.toString(); |
| 112 |
|
| 113 |
// Perform general tweaks. |
| 114 |
setDefaultProperty( "java.awt.Window.locationByPlatform", "true" ); |
| 115 |
|
| 116 |
// Process command line arguments. |
| 117 |
List argList = new ArrayList( Arrays.asList( args ) ); |
| 118 |
for ( Iterator it = argList.iterator(); it.hasNext(); ) { |
| 119 |
String arg = (String) it.next(); |
| 120 |
if ( arg.toLowerCase().equals( "hubrunner" ) ) { |
| 121 |
System.err.println( "\"hubrunner\" command is deprecated. " |
| 122 |
+ "Use \"hub\" instead." ); |
| 123 |
return 1; |
| 124 |
} |
| 125 |
for ( int ic = 0; ic < COMMAND_CLASSES.length; ic++ ) { |
| 126 |
String className = COMMAND_CLASSES[ ic ]; |
| 127 |
if ( arg.toLowerCase() |
| 128 |
.equals( abbrev( className ).toLowerCase() ) ) { |
| 129 |
it.remove(); |
| 130 |
return runCommand( className, |
| 131 |
(String[]) |
| 132 |
argList.toArray( new String[ 0 ] ) ); |
| 133 |
} |
| 134 |
} |
| 135 |
if ( arg.startsWith( "-h" ) ) { |
| 136 |
System.out.println( usage ); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
else if ( arg.startsWith( "-vers" ) ) { |
| 140 |
System.out.println(); |
| 141 |
System.out.println( getVersionText() ); |
| 142 |
System.out.println(); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
else { |
| 146 |
System.err.println( usage ); |
| 147 |
return 1; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// No arguments. |
| 152 |
assert argList.isEmpty(); |
| 153 |
System.err.println( JSamp.class.getName() + " invoked with no arguments" |
| 154 |
+ " - running hub" ); |
| 155 |
System.err.println( "Use \"-help\" flag for more options" ); |
| 156 |
System.err.println( "Use \"hub\" argument" |
| 157 |
+ " to suppress this message" ); |
| 158 |
return runCommand( Hub.class.getName(), new String[ 0 ] ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Runs a command. |
| 163 |
* |
| 164 |
* @param className name of a class with a <code>main(String[])</code> |
| 165 |
* method |
| 166 |
* @param args arguments as if passed from the command line |
| 167 |
*/ |
| 168 |
private static int runCommand( String className, String[] args ) { |
| 169 |
Class clazz; |
| 170 |
try { |
| 171 |
clazz = Class.forName( className ); |
| 172 |
} |
| 173 |
catch ( ClassNotFoundException e ) { |
| 174 |
System.err.println( "Class " + className + " not available" ); |
| 175 |
return 1; |
| 176 |
} |
| 177 |
Object statusObj; |
| 178 |
try { |
| 179 |
getMainMethod( clazz ).invoke( null, new Object[] { args } ); |
| 180 |
return 0; |
| 181 |
} |
| 182 |
catch ( InvocationTargetException e ) { |
| 183 |
e.getCause().printStackTrace(); |
| 184 |
return 1; |
| 185 |
} |
| 186 |
catch ( Exception e ) { |
| 187 |
e.printStackTrace(); |
| 188 |
return 1; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Returns the <code>main(String[])</code> method for a given class. |
| 194 |
*/ |
| 195 |
static Method getMainMethod( Class clazz ) { |
| 196 |
Method method; |
| 197 |
try { |
| 198 |
method = clazz.getMethod( "main", |
| 199 |
new Class[] { String[].class } ); |
| 200 |
} |
| 201 |
catch ( NoSuchMethodException e ) { |
| 202 |
throw (IllegalArgumentException) |
| 203 |
new AssertionError( "main() method missing for " |
| 204 |
+ clazz.getName() ) |
| 205 |
.initCause( e ); |
| 206 |
} |
| 207 |
int mods = method.getModifiers(); |
| 208 |
if ( Modifier.isStatic( mods ) && |
| 209 |
Modifier.isPublic( mods ) && |
| 210 |
method.getReturnType() == void.class ) { |
| 211 |
return method; |
| 212 |
} |
| 213 |
else { |
| 214 |
throw new IllegalArgumentException( "Wrong main() method signature" |
| 215 |
+ " for " + clazz.getName() ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Returns the abbreviated form of a given class name. |
| 221 |
* |
| 222 |
* @param className class name |
| 223 |
* @return abbreviation |
| 224 |
*/ |
| 225 |
private static String abbrev( String className ) { |
| 226 |
return className.substring( className.lastIndexOf( "." ) + 1 ) |
| 227 |
.toLowerCase(); |
| 228 |
} |
| 229 |
|
| 230 |
private static String formatImpls( Object[] options, Class clazz ) { |
| 231 |
StringBuffer sbuf = new StringBuffer(); |
| 232 |
if ( options != null ) { |
| 233 |
for ( int i = 0; i < options.length; i++ ) { |
| 234 |
if ( sbuf.length() > 0 ) { |
| 235 |
sbuf.append( '|' ); |
| 236 |
} |
| 237 |
sbuf.append( options[ i ] ); |
| 238 |
} |
| 239 |
} |
| 240 |
if ( clazz != null ) { |
| 241 |
if ( sbuf.length() > 0 ) { |
| 242 |
sbuf.append( '|' ); |
| 243 |
} |
| 244 |
sbuf.append( '<' ) |
| 245 |
.append( clazz.getName().replaceFirst( "^.*\\.", "" ) |
| 246 |
.toLowerCase() ) |
| 247 |
.append( "-class" ) |
| 248 |
.append( '>' ); |
| 249 |
} |
| 250 |
return sbuf.toString(); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Returns a string giving version details for this package. |
| 255 |
* |
| 256 |
* @return version string |
| 257 |
*/ |
| 258 |
private static String getVersionText() { |
| 259 |
return new StringBuffer() |
| 260 |
.append( " This is JSAMP.\n" ) |
| 261 |
.append( "\n " ) |
| 262 |
.append( "JSAMP toolkit version:" ) |
| 263 |
.append( "\n " ) |
| 264 |
.append( SampUtils.getSoftwareVersion() ) |
| 265 |
.append( "\n " ) |
| 266 |
.append( "SAMP standard version:" ) |
| 267 |
.append( "\n " ) |
| 268 |
.append( SampUtils.getSampVersion() ) |
| 269 |
.append( "\n " ) |
| 270 |
.append( "Author:" ) |
| 271 |
.append( "\n " ) |
| 272 |
.append( "Mark Taylor ([email protected])" ) |
| 273 |
.append( "\n " ) |
| 274 |
.append( "WWW:" ) |
| 275 |
.append( "\n " ) |
| 276 |
.append( "http://www.star.bristol.ac.uk/~mbt/jsamp" ) |
| 277 |
.toString(); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Sets a system property to a given value unless it has already been set. |
| 282 |
* If it has a prior value, that is undisturbed. |
| 283 |
* Potential security exceptions are caught and dealt with. |
| 284 |
* |
| 285 |
* @param key property name |
| 286 |
* @param value suggested property value |
| 287 |
*/ |
| 288 |
private static void setDefaultProperty( String key, String value ) { |
| 289 |
String existingVal = System.getProperty( key ); |
| 290 |
if ( existingVal == null || existingVal.trim().length() == 0 ) { |
| 291 |
try { |
| 292 |
System.setProperty( key, value ); |
| 293 |
} |
| 294 |
catch ( SecurityException e ) { |
| 295 |
// never mind. |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Main method. |
| 302 |
* Use -help flag for documentation. |
| 303 |
*/ |
| 304 |
public static void main( String[] args ) { |
| 305 |
int status = runMain( args ); |
| 306 |
if ( status != 0 ) { |
| 307 |
System.exit( status ); |
| 308 |
} |
| 309 |
} |
| 310 |
} |