1 17 18 package org.apache.avalon.repository.cli; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.util.Properties ; 24 import java.util.Locale ; 25 import java.util.ArrayList ; 26 import java.util.StringTokenizer ; 27 import java.net.URL ; 28 29 import javax.naming.directory.Attributes ; 30 31 import org.apache.avalon.repository.Artifact; 32 import org.apache.avalon.repository.provider.InitialContext; 33 import org.apache.avalon.repository.RepositoryException; 34 import org.apache.avalon.repository.main.DefaultInitialContextFactory; 35 import org.apache.avalon.repository.main.DefaultBuilder; 36 import org.apache.avalon.repository.meta.ArtifactDescriptor; 37 import org.apache.avalon.repository.util.RepositoryUtils; 38 39 import org.apache.avalon.util.env.Env; 40 import org.apache.avalon.util.exception.ExceptionHelper; 41 import org.apache.avalon.util.i18n.ResourceManager; 42 import org.apache.avalon.util.i18n.Resources; 43 44 import org.apache.commons.cli.BasicParser; 45 import org.apache.commons.cli.CommandLine; 46 import org.apache.commons.cli.CommandLineParser; 47 import org.apache.commons.cli.HelpFormatter; 48 import org.apache.commons.cli.Option; 49 import org.apache.commons.cli.OptionBuilder; 50 import org.apache.commons.cli.Options; 51 52 53 59 public class Main 60 { 61 65 private static Resources REZ = 66 ResourceManager.getPackageResources( Main.class ); 67 68 private static final File USER_HOME = 69 new File ( System.getProperty( "user.home" ) ); 70 71 private static final String AVALON_PROPERTIES = "avalon.properties"; 72 73 private static final String IMPLEMENTATION_KEY = "avalon.repository.implementation"; 74 75 private static Options CL_OPTIONS = buildCommandLineOptions(); 76 77 private static Options buildCommandLineOptions() 78 { 79 Options options = new Options(); 80 81 Option help = new Option( 82 "help", 83 REZ.getString( "cli-help-description" ) ); 84 85 Option verify = new Option( 86 "verify", 87 REZ.getString( "cli-verify-description" ) ); 88 89 Option version = new Option( 90 "version", 91 REZ.getString( "cli-version-description" ) ); 92 93 Option locale = OptionBuilder 94 .hasArg() 95 .withArgName( "code" ) 96 .withDescription( REZ.getString( "cli-language-description" ) ) 97 .create( "lang" ); 98 99 Option info = new Option( 100 "info", 101 REZ.getString( "cli-info-description" ) ); 102 103 Option implementation = OptionBuilder 104 .hasArg() 105 .withArgName( "artifact" ) 106 .withDescription( REZ.getString( "cli-implementation-description" ) ) 107 .create( "impl" ); 108 109 Option home = OptionBuilder 110 .hasArg() 111 .withArgName( REZ.getString( "directory" ) ) 112 .withDescription( REZ.getString( "cli-home-description" ) ) 113 .create( "home" ); 114 115 Option cache = OptionBuilder 116 .hasArg() 117 .withArgName( REZ.getString( "directory" ) ) 118 .withDescription( REZ.getString( "cli-cache-description" ) ) 119 .create( "cache" ); 120 121 Option hosts = OptionBuilder 122 .hasArg() 123 .withArgName( REZ.getString( "urls" ) ) 124 .withDescription( REZ.getString( "cli-hosts-description" ) ) 125 .create( "hosts" ); 126 127 Option install = OptionBuilder 128 .hasArg() 129 .withArgName( REZ.getString( "url" ) ) 130 .withDescription( REZ.getString( "cli-install-description" ) ) 131 .create( "install" ); 132 133 options.addOption( help ); 134 options.addOption( locale ); 135 options.addOption( version ); 136 options.addOption( info ); 137 options.addOption( implementation ); 138 options.addOption( install ); 139 options.addOption( home ); 140 options.addOption( cache ); 141 options.addOption( hosts ); 142 options.addOption( verify ); 143 return options; 144 } 145 146 private static Main MAIN = null; 147 148 152 public static void main( String [] args ) 153 { 154 try 155 { 156 160 CommandLineParser parser = new BasicParser(); 161 CommandLine line = parser.parse( CL_OPTIONS, args ); 162 163 File dir = getWorkingDirectory( line ); 164 File cache = getCacheDirectory( line ); 165 Artifact artifact = getDefaultImplementation( dir, line ); 166 167 if( line.hasOption( "version" ) ) 168 { 169 Main.printVersionInfo( cache, artifact ); 170 return; 171 } 172 else if( line.hasOption( "help" ) ) 173 { 174 doHelp( line ); 175 return; 176 } 177 else 178 { 179 183 ClassLoader parent = Main.class.getClassLoader(); 184 String [] hosts = getHostsPath( line ); 185 186 DefaultInitialContextFactory factory = 187 new DefaultInitialContextFactory( "avalon", dir ); 188 factory.setCacheDirectory( cache ); 189 factory.setHosts( hosts ); 190 191 InitialContext context = factory.createInitialContext(); 192 193 197 MAIN = new Main( context, line ); 198 } 199 } 200 catch( Throwable e ) 201 { 202 String msg = 203 ExceptionHelper.packException( e, true ); 204 System.err.println( msg ); 205 System.exit( -1 ); 206 } 207 } 208 209 private static void doHelp( CommandLine line ) 210 { 211 if( line.hasOption( "lang" ) ) 212 { 213 ResourceManager.clearResourceCache(); 214 String language = line.getOptionValue( "lang" ); 215 Locale locale = new Locale ( language, "" ); 216 Locale.setDefault( locale ); 217 REZ = ResourceManager.getPackageResources( Main.class ); 218 } 219 220 HelpFormatter formatter = new HelpFormatter(); 221 formatter.printHelp( 222 "repository [artifact]", 223 " ", 224 buildCommandLineOptions(), 225 "", 226 true ); 227 } 228 229 233 239 public Main( 240 InitialContext context, CommandLine line ) throws Exception 241 { 242 if( line.hasOption( "info" ) ) 243 { 244 StringBuffer buffer = 245 new StringBuffer ( InitialContext.LINE ); 246 buffer.append( "\nAvalon Repository" ); 247 buffer.append( InitialContext.LINE ); 248 prepareInfoListing( buffer, context ); 249 buffer.append( InitialContext.LINE ); 250 System.out.println( buffer.toString() ); 251 } 252 253 if( line.hasOption( "install" ) ) 254 { 255 doInstall( context, line ); 256 } 257 else if( line.hasOption( "verify" ) ) 258 { 259 doVerify( context ); 260 } 261 else 262 { 263 doHelp( line ); 264 } 265 } 266 267 private void prepareInfoListing( StringBuffer buffer, InitialContext context ) 268 { 269 buffer.append( "\n${avalon.repository.cache} = " ); 270 buffer.append( context.getInitialCacheDirectory() ); 271 buffer.append( "\n${avalon.dir} = " ); 272 buffer.append( context.getInitialWorkingDirectory() ); 273 String [] hosts = context.getInitialHosts(); 274 buffer.append( "\n${avalon.repository.hosts} = (" ); 275 buffer.append( hosts.length ); 276 buffer.append( ")" ); 277 for( int i=0; i<hosts.length; i++ ) 278 { 279 buffer.append( "\n " + hosts[i] ); 280 } 281 } 282 283 private void doInstall( InitialContext context, CommandLine line ) 284 throws Exception 285 { 286 final URL url = getInstallTarget( line ); 287 try 288 { 289 context.install( url ); 290 } 291 catch( Throwable e ) 292 { 293 final String error = 294 "Install failure: " 295 + url; 296 throw new RepositoryException( error, e ); 297 } 298 } 299 300 private void doVerify( InitialContext context ) 301 throws Exception 302 { 303 RepositoryVerifier verifier = 304 new RepositoryVerifier( context ); 305 verifier.verify(); 306 } 307 308 309 313 318 private static File getWorkingDirectory( CommandLine line ) throws Exception 319 { 320 if( line.hasOption( "home" ) ) 321 { 322 String dir = line.getOptionValue( "home" ); 323 return new File ( dir ).getCanonicalFile(); 324 } 325 else 326 { 327 return getBaseDirectory(); 328 } 329 } 330 331 private static String [] getHostsPath( CommandLine line ) 332 { 333 if( line.hasOption( "hosts" ) ) 334 { 335 String hosts = line.getOptionValue( "hosts" ); 336 return expandHosts( hosts ); 337 } 338 else 339 { 340 return null; 341 } 342 } 343 344 private static String [] expandHosts( String arg ) 345 { 346 ArrayList list = new ArrayList (); 347 StringTokenizer tokenizer = new StringTokenizer ( arg, "," ); 348 while( tokenizer.hasMoreTokens() ) 349 { 350 String next = tokenizer.nextToken(); 351 list.add( next ); 352 } 353 return (String []) list.toArray( new String [0] ); 354 } 355 356 361 private URL getInstallTarget( CommandLine line ) throws IOException 362 { 363 String spec = line.getOptionValue( "install" ); 364 return new URL ( spec ); 365 } 366 367 374 private static Artifact getDefaultImplementation( 375 File base, CommandLine line ) throws Exception 376 { 377 if( line.hasOption( "impl" ) ) 378 { 379 String spec = line.getOptionValue( "impl" ); 380 return Artifact.createArtifact( spec ); 381 } 382 else 383 { 384 return DefaultBuilder.createImplementationArtifact( 385 Main.class.getClassLoader(), 386 getAvalonHome(), 387 getBaseDirectory(), 388 AVALON_PROPERTIES, 389 IMPLEMENTATION_KEY ); 390 } 391 } 392 393 401 private static void printVersionInfo( File cache, Artifact artifact ) 402 { 403 try 404 { 405 Attributes attr = RepositoryUtils.getAttributes( cache, artifact ); 406 ArtifactDescriptor desc = new ArtifactDescriptor( attr ); 407 System.out.println( "\n Implementation: " 408 + artifact.getGroup() 409 + ":" + artifact.getName() 410 + ";" + artifact.getVersion() 411 + " (" + desc.getBuild() + ")" 412 ); 413 } 414 catch( Throwable e ) 415 { 416 System.out.println( "\nImplementation: " 417 + artifact.getGroup() 418 + ":" + artifact.getName() 419 + ";" + artifact.getVersion() ); 420 } 421 } 422 423 431 private static File getCacheDirectory( CommandLine line ) 432 { 433 if( line.hasOption( "cache" ) ) 434 { 435 String system = line.getOptionValue( "cache" ); 436 return new File ( system ); 437 } 438 else 439 { 440 return new File ( getAvalonHome( ), "repository" ); 441 } 442 } 443 444 448 private static File getAvalonHome() 449 { 450 return new File ( getAvalonHomePath() ); 451 } 452 453 457 private static String getAvalonHomePath() 458 { 459 return getHomePath( "AVALON_HOME", ".avalon" ); 460 } 461 462 466 private static String getHomePath( final String var, final String dir ) 467 { 468 try 469 { 470 String avalon = 471 System.getProperty( 472 "avalon.home", 473 Env.getEnvVariable( var ) ); 474 if( null != avalon ) return avalon; 475 return System.getProperty( "user.home" ) 476 + File.separator + dir; 477 } 478 catch( Throwable e ) 479 { 480 final String error = 481 "Internal error while attempting to access AVALON_HOME environment."; 482 final String message = 483 ExceptionHelper.packException( error, e, true ); 484 throw new RuntimeException ( message ); 485 } 486 } 487 488 496 private static File getBaseDirectory() 497 { 498 final String path = System.getProperty( "avalon.dir" ); 499 if( null != path ) 500 { 501 return new File ( path ); 502 } 503 final String base = System.getProperty( "basedir" ); 504 if( null != base ) 505 { 506 return new File ( base ); 507 } 508 return new File ( System.getProperty( "user.dir" ) ); 509 } 510 511 517 private static Properties getLocalProperties( 518 File dir, String filename ) 519 { 520 Properties properties = new Properties (); 521 if( null == dir ) return properties; 522 File file = new File ( dir, filename ); 523 if( !file.exists() ) return properties; 524 try 525 { 526 properties.load( new FileInputStream ( file ) ); 527 return properties; 528 } 529 catch( Throwable e ) 530 { 531 final String error = 532 "Unexpected exception while attempting to read properties from: " 533 + file + ". Cause: " + e.toString(); 534 throw new IllegalStateException ( error ); 535 } 536 } 537 } 538 | Popular Tags |