1 9 10 package org.nanocontainer; 11 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.net.URL ; 15 16 import org.apache.commons.cli.CommandLine; 17 import org.apache.commons.cli.CommandLineParser; 18 import org.apache.commons.cli.Options; 19 import org.apache.commons.cli.ParseException; 20 import org.apache.commons.cli.PosixParser; 21 import org.nanocontainer.script.ScriptedContainerBuilderFactory; 22 import org.picocontainer.defaults.ObjectReference; 23 import org.picocontainer.defaults.SimpleReference; 24 25 36 public class Standalone { 37 38 private static final char HELP_OPT = 'h'; 39 private static final char VERSION_OPT = 'v'; 40 private static final char COMPOSITION_OPT = 'c'; 41 private static final char RESOURCE_OPT = 'r'; 42 private static final char QUIET_OPT = 'q'; 43 private static final char NOWAIT_OPT = 'n'; 44 45 private static final String DEFAULT_COMPOSITION_FILE = "composition.groovy"; 46 47 static final Options createOptions() { 48 Options options = new Options(); 49 options.addOption(String.valueOf(HELP_OPT), "help", false, 50 "print this message and exit"); 51 options.addOption(String.valueOf(VERSION_OPT), "version", false, 52 "print the version information and exit"); 53 options.addOption(String.valueOf(COMPOSITION_OPT), "composition", true, 54 "specify the composition file"); 55 options.addOption(String.valueOf(RESOURCE_OPT), "resource", true, 56 "specify the composition file (as a resource read from classpath - like inside a jar)"); 57 options.addOption(String.valueOf(QUIET_OPT), "quiet", false, 58 "forces ScriptedContainerBuilderFactory to be quiet"); 59 options.addOption(String.valueOf(NOWAIT_OPT), "nowait", false, 60 "forces ScriptedContainerBuilderFactory to exit after start"); 61 return options; 62 } 63 64 public static void main(String [] args) throws IOException , ClassNotFoundException { 65 new Standalone(args); 66 } 67 68 public Standalone(String [] args) throws IOException , ClassNotFoundException { 69 File defaultCompositionFile = new File (DEFAULT_COMPOSITION_FILE); 70 CommandLine cl = null; 71 Options options = createOptions(); 72 if (args.length == 0 && !defaultCompositionFile.exists()) { 73 printUsage(options); 74 System.exit(-1); 75 } 76 try { 77 cl = getCommandLine(args, options); 78 } catch (ParseException e) { 79 System.out.println("NanoContainer Standalone: Error in parsing arguments: "); 80 e.printStackTrace(); 81 System.exit(-1); 82 } 83 84 if (cl.hasOption(HELP_OPT)) { 85 printUsage(options); 86 System.exit(0); 87 } 88 if (cl.hasOption(VERSION_OPT)) { 89 printVersion(); 90 System.exit(0); 91 } 92 93 boolean quiet = cl.hasOption(QUIET_OPT); 94 boolean nowait = cl.hasOption(NOWAIT_OPT); 95 try { 96 String compositionFile = cl.getOptionValue(COMPOSITION_OPT); 97 String compositionResource = cl.getOptionValue(RESOURCE_OPT); 98 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); 99 if (compositionFile != null) { 100 buildAndStartContainer(new File (compositionFile), quiet, nowait); 101 } else if (compositionResource != null) { 102 buildAndStartContainer(Standalone.class.getResource(compositionResource), quiet, nowait); 103 } else { 104 if (defaultCompositionFile.exists()) { 105 buildAndStartContainer(defaultCompositionFile, quiet, nowait); 106 } else { 107 printUsage(options); 108 System.exit(10); 109 } 110 } 111 } catch (RuntimeException e) { 112 System.err.println("NanoContainer Standalone: Failed to start application. Cause : " + e.getMessage()); 113 e.printStackTrace(); 114 throw e; 115 } catch (ClassNotFoundException e) { 116 System.err.println("NanoContainer Standalone: Failed to start application. A Class was not found. Exception message : " + e.getMessage()); 117 e.printStackTrace(); 118 throw e; 119 } 120 if (!quiet) { 121 System.out.println("NanoContainer Standalone: Exiting main method."); 122 } 123 } 124 125 126 146 private static void buildAndStartContainer(URL composition, final boolean quiet, boolean nowait) throws ClassNotFoundException { 147 final ScriptedContainerBuilderFactory scriptedContainerBuilderFactory = new ScriptedContainerBuilderFactory(composition); 148 buildContainer(scriptedContainerBuilderFactory, nowait, quiet); 149 } 150 151 private static void buildAndStartContainer(File composition, boolean quiet, boolean nowait) throws IOException , ClassNotFoundException { 152 final ScriptedContainerBuilderFactory scriptedContainerBuilderFactory = new ScriptedContainerBuilderFactory(composition); 153 buildContainer(scriptedContainerBuilderFactory, nowait, quiet); 154 } 155 156 157 private static void buildContainer(final ScriptedContainerBuilderFactory scriptedContainerBuilderFactory, boolean nowait, final boolean quiet) { 158 final ObjectReference containerRef = new SimpleReference(); 159 scriptedContainerBuilderFactory.getContainerBuilder().buildContainer(containerRef, null, null, true); 160 161 if (nowait == false) { 162 setShutdownHook(quiet, scriptedContainerBuilderFactory, containerRef); 163 } else { 164 } 166 } 167 168 private static void setShutdownHook(final boolean quiet, final ScriptedContainerBuilderFactory scriptedContainerBuilderFactory, final ObjectReference containerRef) { 169 Runnable shutdownHook = new Runnable () { 171 public void run() { 172 shuttingDown(quiet, scriptedContainerBuilderFactory, containerRef); 173 } 174 }; 175 Runtime.getRuntime().addShutdownHook(new Thread (shutdownHook)); 176 } 177 178 private static void shuttingDown(final boolean quiet, final ScriptedContainerBuilderFactory scriptedContainerBuilderFactory, final ObjectReference containerRef) { 179 try { 180 scriptedContainerBuilderFactory.getContainerBuilder().killContainer(containerRef); 181 } catch (RuntimeException e) { 182 e.printStackTrace(); 183 } finally { 184 if (!quiet) { 185 System.out.println("NanoContainer Standalone: Exiting Virtual Machine"); 186 } 187 } 188 } 189 190 191 static CommandLine getCommandLine(String [] args, Options options) throws ParseException { 192 CommandLineParser parser = new PosixParser(); 193 return parser.parse(options, args); 194 } 195 196 private static void printUsage(Options options) { 197 final String lineSeparator = System.getProperty("line.separator"); 198 199 final StringBuffer usage = new StringBuffer (); 200 usage.append(lineSeparator); 201 usage.append("NanoContainer Standalone: -c <composition-file> [-q|-n|-h|-v]"); 202 usage.append(options.getOptions()); 203 System.out.println(usage.toString()); 204 } 205 206 private static void printVersion() { 207 System.out.println("1.1"); 208 } 209 210 211 } 212 213 214 | Popular Tags |