1 17 18 package org.apache.geronimo.system.main; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.geronimo.common.GeronimoEnvironment; 23 import org.apache.geronimo.gbean.AbstractName; 24 import org.apache.geronimo.gbean.AbstractNameQuery; 25 import org.apache.geronimo.kernel.Kernel; 26 import org.apache.geronimo.kernel.KernelFactory; 27 import org.apache.geronimo.kernel.config.ConfigurationManager; 28 import org.apache.geronimo.kernel.config.ConfigurationUtil; 29 import org.apache.geronimo.kernel.config.PersistentConfigurationList; 30 import org.apache.geronimo.kernel.log.GeronimoLogging; 31 import org.apache.geronimo.kernel.repository.Artifact; 32 import org.apache.geronimo.system.serverinfo.DirectoryUtils; 33 34 import java.io.File ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.PrintStream ; 38 import java.util.ArrayList ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Set ; 42 43 46 public class Daemon { 47 private final static String ARGUMENT_NO_PROGRESS = "--quiet"; 48 private final static String ARGUMENT_LONG_PROGRESS = "--long"; 49 private final static String ARGUMENT_VERBOSE_SHORTFORM = "-v"; 50 private final static String ARGUMENT_VERBOSE = "--verbose"; 51 private final static String ARGUMENT_MORE_VERBOSE_SHORTFORM = "-vv"; 52 private final static String ARGUMENT_MORE_VERBOSE = "--veryverbose"; 53 private final static String ARGUMENT_MODULE_OVERRIDE = "--override"; 54 private static boolean started = false; 55 private static Log log; 56 private StartupMonitor monitor; 57 private List configs = new ArrayList (); 58 private String verboseArg = null; 59 private String noProgressArg = null; 60 private String longProgressArg = null; 61 62 private Daemon(String [] args) { 63 long start = System.currentTimeMillis(); 65 if(processArguments(args)) { 67 System.out.println("Booting Geronimo Kernel (in Java " + System.getProperty("java.version") + ")..."); 68 System.out.flush(); 69 70 initializeSystem(); 72 73 monitor.systemStarting(start); 74 doStartup(); 75 } else { 76 System.exit(1); 77 throw new AssertionError (); 78 } 79 } 80 81 private void printHelp(PrintStream out) { 82 out.println(); 83 out.println("Syntax: java -jar bin/server.jar [options]"); 84 out.println(); 85 out.println("Available options are: "); 86 out.println(" "+ARGUMENT_NO_PROGRESS); 87 out.println(" Suppress the normal startup progress bar. This is typically\n" + 88 " used when redirecting console output to a file, or starting\n" + 89 " the server from an IDE or other tool."); 90 out.println(" "+ARGUMENT_LONG_PROGRESS); 91 out.println(" Write startup progress to the console in a format that is\n" + 92 " suitable for redirecting console output to a file, or starting\n" + 93 " the server from an IDE or other tool (doesn't use linefeeds to\n" + 94 " update the progress information that is used by default if you\n" + 95 " don't specify " +ARGUMENT_NO_PROGRESS +" or "+ARGUMENT_LONG_PROGRESS+").\n"); 96 out.println(" "+ARGUMENT_VERBOSE_SHORTFORM +" " +ARGUMENT_VERBOSE); 97 out.println(" Reduces the console log level to DEBUG, resulting in more\n" + 98 " console output than is normally present."); 99 out.println(" "+ARGUMENT_MORE_VERBOSE_SHORTFORM +" " +ARGUMENT_MORE_VERBOSE); 100 out.println(" Reduces the console log level to TRACE, resulting in still\n" + 101 " more console output."); 102 out.println(); 103 out.println(" "+ARGUMENT_MODULE_OVERRIDE+" [moduleId] [moduleId] ..."); 104 out.println(" USE WITH CAUTION! Overrides the modules in\n" + 105 " var/config/config.xml such that only the modules listed on\n" + 106 " the command line will be started. Note that many J2EE\n" + 107 " features depend on certain modules being started, so you\n" + 108 " should be very careful what you omit. Any arguments after\n" + 109 " this are assumed to be module names."); 110 out.println(); 111 out.println("In addition you may specify a replacement for var/config/config.xml using by setting the property\n" + 112 "-Dorg.apache.geronimo.config.file=var/config/<my-config.xml>\n" + 113 "This is resolved relative to the geronimo base directory."); 114 out.println(); 115 } 116 117 121 private boolean processArguments(String [] args) { 122 boolean override = false; 123 boolean help = false; 124 for (int i = 0; i < args.length; i++) { 125 if(override) { 126 configs.add(Artifact.create(args[i])); 127 } else if (args[i].equals(ARGUMENT_NO_PROGRESS)) { 128 noProgressArg = ARGUMENT_NO_PROGRESS; 129 } else if (args[i].equals(ARGUMENT_LONG_PROGRESS)) { 130 longProgressArg = ARGUMENT_LONG_PROGRESS; 131 } else if (args[i].equals(ARGUMENT_VERBOSE_SHORTFORM) || 132 args[i].equals(ARGUMENT_VERBOSE)) { 133 if (verboseArg == null) { 134 verboseArg = ARGUMENT_VERBOSE; 135 } 136 } else if (args[i].equals(ARGUMENT_MORE_VERBOSE_SHORTFORM) || 137 args[i].equals(ARGUMENT_MORE_VERBOSE)) { 138 if (verboseArg == null) { 139 verboseArg = ARGUMENT_MORE_VERBOSE; 140 } 141 } else if (args[i].equals(ARGUMENT_MODULE_OVERRIDE)) { 142 override = true; 143 } else if(args[i].equalsIgnoreCase("-help") || args[i].equalsIgnoreCase("--help") || 144 args[i].equalsIgnoreCase("-h") || args[i].equalsIgnoreCase("/?")) { 145 help = true; 146 } else { 147 System.out.println("Unrecognized argument: "+args[i]); 148 help = true; 149 } 150 } 151 if(help) { 152 printHelp(System.out); 153 } 154 return !help; 155 } 156 157 private void initializeSystem() { 158 if (!started) { 159 started = true; 160 161 GeronimoEnvironment.init(); 163 164 GeronimoLogging.initialize(verboseArg == null || verboseArg.equals(ARGUMENT_VERBOSE) ? GeronimoLogging.WARN : GeronimoLogging.DEBUG); 167 GeronimoLogging.setConsoleLogLevel(verboseArg == null ? GeronimoLogging.INFO : verboseArg.equals(ARGUMENT_VERBOSE) ? GeronimoLogging.DEBUG : GeronimoLogging.TRACE); 169 log = LogFactory.getLog(Daemon.class.getName()); 170 } 171 172 if (verboseArg != null || noProgressArg != null) { 173 monitor = new SilentStartupMonitor(); 174 } else { 175 if (longProgressArg != null) 176 monitor = new LongStartupMonitor(); 177 else 178 monitor = new ProgressBarStartupMonitor(); 179 } 180 181 } 183 184 private void JVMCheck() { 185 String jvmVersion = System.getProperty("java.specification.version"); 186 if (! jvmVersion.equals("1.4")) 187 log.warn("\n====================================== Warning =======================================\n" + 188 " Geronimo is currently only certified on version 1.4 of the Java Virtual Machine.\n" + 189 " Use of version " + jvmVersion + " is not currently supported. Use at your own risk.\n" + 190 " Check http://geronimo.apache.org for current information on JDK certification level.\n" + 191 "====================================== Warning ======================================="); 192 } 193 194 private void doStartup() { 195 try { 196 String tmpDir = System.getProperty("java.io.tmpdir"); 200 if (tmpDir == null || (!(new File (tmpDir)).exists()) || 201 (!(new File (tmpDir)).isDirectory())) { 202 System.err.println("The java.io.tmpdir system property specifies the "+ 203 "non-existent directory " +tmpDir); 204 System.exit(1); 205 throw new AssertionError (); 206 } 207 208 File geronimoInstallDirectory = DirectoryUtils.getGeronimoInstallDirectory(); 210 if (geronimoInstallDirectory == null) { 211 System.err.println("Could not determine geronimo installation directory"); 212 System.exit(1); 213 throw new AssertionError (); 214 } 215 216 ClassLoader classLoader = Daemon.class.getClassLoader(); 217 218 final Kernel kernel = KernelFactory.newInstance().createKernel("geronimo"); 220 221 try { 223 kernel.boot(); 224 } catch (Exception e) { 225 e.printStackTrace(); 226 System.exit(1); 227 throw new AssertionError (); 228 } 229 230 Runtime.getRuntime().addShutdownHook(new Thread ("Geronimo shutdown thread") { 232 public void run() { 233 System.out.println("\rServer shutdown begun "); 234 kernel.shutdown(); 235 System.out.println("Server shutdown completed"); 236 } 237 }); 238 239 InputStream in = classLoader.getResourceAsStream("META-INF/config.ser"); 241 try { 242 ConfigurationUtil.loadBootstrapConfiguration(kernel, in, classLoader); 243 } finally { 244 if (in != null) { 245 try { 246 in.close(); 247 } catch (IOException ignored) { 248 } 250 } 251 } 252 253 monitor.systemStarted(kernel); 254 255 AbstractNameQuery query = new AbstractNameQuery(PersistentConfigurationList.class.getName()); 256 257 if (configs.isEmpty()) { 258 Set configLists = kernel.listGBeans(query); 260 for (Iterator i = configLists.iterator(); i.hasNext();) { 261 AbstractName configListName = (AbstractName) i.next(); 262 try { 263 configs.addAll((List) kernel.invoke(configListName, "restore")); 264 } catch (IOException e) { 265 System.err.println("Unable to restore last known configurations"); 266 e.printStackTrace(); 267 kernel.shutdown(); 268 System.exit(1); 269 throw new AssertionError (); 270 } 271 } 272 } 273 274 monitor.foundModules((Artifact[]) configs.toArray(new Artifact[configs.size()])); 275 276 try { 278 ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel); 279 try { 280 for (Iterator i = configs.iterator(); i.hasNext();) { 281 Artifact configID = (Artifact) i.next(); 282 monitor.moduleLoading(configID); 283 configurationManager.loadConfiguration(configID); 284 monitor.moduleLoaded(configID); 285 monitor.moduleStarting(configID); 286 configurationManager.startConfiguration(configID); 287 monitor.moduleStarted(configID); 288 } 289 } finally { 290 ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager); 291 } 292 } catch (Exception e) { 293 monitor.serverStartFailed(e); 295 try { 296 kernel.shutdown(); 297 } catch (Exception e1) { 298 System.err.println("Exception caught during kernel shutdown"); 299 e1.printStackTrace(); 300 } 301 System.exit(1); 302 throw new AssertionError (); 303 } 304 305 Set configLists = kernel.listGBeans(query); 307 for (Iterator i = configLists.iterator(); i.hasNext();) { 308 AbstractName configListName = (AbstractName) i.next(); 309 kernel.setAttribute(configListName, "kernelFullyStarted", Boolean.TRUE); 310 } 311 312 monitor.startupFinished(); 314 monitor = null; 315 316 while (kernel.isRunning()) { 318 try { 319 synchronized (kernel) { 320 kernel.wait(); 321 } 322 } catch (InterruptedException e) { 323 } 325 } 326 } catch (Exception e) { 327 if (monitor != null) { 328 monitor.serverStartFailed(e); 329 } 330 e.printStackTrace(); 331 System.exit(1); 332 throw new AssertionError (); 333 } 334 } 335 336 private void AddToSystemProperty(String propertyName, List dirsFromManifest, File geronimoInstallDirectory) { 337 String dirs = System.getProperty(propertyName, ""); 338 for (Iterator iterator = dirsFromManifest.iterator(); iterator.hasNext();) { 339 String directoryName = (String ) iterator.next(); 340 File directory = new File (directoryName); 341 if (!directory.isAbsolute()) { 342 directory = new File (geronimoInstallDirectory, directoryName); 343 } 344 345 if (dirs.length() > 0) { 346 dirs += File.pathSeparatorChar; 347 } 348 dirs += directory.getAbsolutePath(); 349 } 350 if (dirs.length() > 0) { 351 System.setProperty(propertyName, dirs); 352 } 353 log.debug(propertyName + "=" + System.getProperty(propertyName)); 354 } 355 356 365 public static void main(String [] args) { 366 new Daemon(args); 367 } 368 369 } 370 | Popular Tags |