private void parseCmdLine(String[] args) { CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption("v", "version", false, "Q2's version"); options.addOption("d", "deploydir", true, "Deployment directory"); options.addOption("r", "recursive", false, "Deploy subdirectories recursively"); options.addOption("h", "help", false, "Usage information"); options.addOption("C", "config", true, "Configuration bundle"); options.addOption("e", "encrypt", true, "Encrypt configuration bundle"); options.addOption("i", "cli", false, "Command Line Interface"); options.addOption("c", "command", true, "Command to execute"); try { CommandLine line = parser.parse(options, args); if (line.hasOption("v")) { displayVersion(); System.exit(0); } if (line.hasOption("h")) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp("Q2", options); System.exit(0); } if (line.hasOption("c")) { cli = new CLI(this, line.getOptionValue("c"), line.hasOption("i")); } else if (line.hasOption("i")) cli = new CLI(this, null, true); String dir = DEFAULT_DEPLOY_DIR; if (line.hasOption("d")) { dir = line.getOptionValue("d"); } recursive = line.hasOption("r"); this.deployDir = new File(dir); if (line.hasOption("C")) deployBundle(new File(line.getOptionValue("C")), false); if (line.hasOption("e")) deployBundle(new File(line.getOptionValue("e")), true); } catch (MissingArgumentException e) { System.out.println("ERROR: " + e.getMessage()); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } }
public void run() { started = true; Thread.currentThread().setName("Q2-" + getInstanceId().toString()); try { /* * The following code determines whether a MBeanServer exists * already. If so then the first one in the list is used. * I have not yet find a way to interrogate the server for * information other than MBeans so to pick a specific one * would be difficult. */ ArrayList mbeanServerList = MBeanServerFactory.findMBeanServer(null); if (mbeanServerList.isEmpty()) { server = MBeanServerFactory.createMBeanServer(JMX_NAME); } else { server = (MBeanServer) mbeanServerList.get(0); } final ObjectName loaderName = new ObjectName(Q2_CLASS_LOADER); try { loader = (QClassLoader) java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { return new QClassLoader(server, libDir, loaderName, mainClassLoader); } }); server.registerMBean(loader, loaderName); loader = loader.scan(false); } catch (Throwable t) { if (log != null) log.error("initial-scan", t); else t.printStackTrace(); } factory = new QFactory(loaderName, this); initSystemLogger(); addShutdownHook(); q2Thread = Thread.currentThread(); q2Thread.setContextClassLoader(loader); if (cli != null) cli.start(); initConfigDecorator(); for (int i = 1; !shutdown; i++) { try { boolean forceNewClassLoader = scan(); QClassLoader oldClassLoader = loader; loader = loader.scan(forceNewClassLoader); if (loader != oldClassLoader) { oldClassLoader = null; // We want't this to be null so it gets GCed. System.gc(); // force a GC log.info( "new classloader [" + Integer.toString(loader.hashCode(), 16) + "] has been created"); } deploy(); checkModified(); relax(SCAN_INTERVAL); if (i % (3600000 / SCAN_INTERVAL) == 0) logVersion(); } catch (Throwable t) { log.error("start", t); relax(); } } undeploy(); try { server.unregisterMBean(loaderName); } catch (InstanceNotFoundException e) { log.error(e); } if (decorator != null) { decorator.uninitialize(); } if (exit && !shuttingDown) System.exit(0); } catch (Exception e) { if (log != null) log.error(e); else e.printStackTrace(); System.exit(1); } }