From 044fc81a18c40788b5ac7451b8b0063729027555 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 25 Nov 2013 23:25:45 +0100 Subject: [PATCH] [New] Added a MainStarter application to start Strolch from main classes --- pom.xml | 4 + .../strolch/runtime/agent/StrolchAgent.java | 8 +- .../PostInitializer.java} | 16 +- .../component/SimplePostInitializer.java | 38 +++++ .../li/strolch/runtime/main/MainStarter.java | 144 ++++++++++++++++++ .../java/li/strolch/runtime/QueryTest.java | 2 +- 6 files changed, 197 insertions(+), 15 deletions(-) rename src/main/java/li/strolch/runtime/{main/Main.java => component/PostInitializer.java} (76%) create mode 100644 src/main/java/li/strolch/runtime/component/SimplePostInitializer.java create mode 100644 src/main/java/li/strolch/runtime/main/MainStarter.java diff --git a/pom.xml b/pom.xml index 64000ee0f..daae11db3 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,10 @@ ch.eitchnet ch.eitchnet.privilege + + commons-cli + commons-cli + diff --git a/src/main/java/li/strolch/runtime/agent/StrolchAgent.java b/src/main/java/li/strolch/runtime/agent/StrolchAgent.java index d56cbe64e..a67c87c91 100644 --- a/src/main/java/li/strolch/runtime/agent/StrolchAgent.java +++ b/src/main/java/li/strolch/runtime/agent/StrolchAgent.java @@ -53,7 +53,7 @@ public class StrolchAgent { public StrolchConfiguration getStrolchConfiguration() { return this.strolchConfiguration; } - + /** * @return the container */ @@ -61,6 +61,10 @@ public class StrolchAgent { return this.container; } + public String getApplicationName() { + return this.strolchConfiguration.getRuntimeConfiguration().getApplicationName(); + } + public void initialize() { this.container.initialize(this.strolchConfiguration); } @@ -93,7 +97,7 @@ public class StrolchAgent { for (ComponentConfiguration configuration : componentConfigurations) { this.strolchConfiguration.addConfiguration(configuration.getName(), configuration); } - + ComponentContainer container = new ComponentContainer(); this.container = container; diff --git a/src/main/java/li/strolch/runtime/main/Main.java b/src/main/java/li/strolch/runtime/component/PostInitializer.java similarity index 76% rename from src/main/java/li/strolch/runtime/main/Main.java rename to src/main/java/li/strolch/runtime/component/PostInitializer.java index 2e7be1115..24a3875b6 100644 --- a/src/main/java/li/strolch/runtime/main/Main.java +++ b/src/main/java/li/strolch/runtime/component/PostInitializer.java @@ -19,21 +19,13 @@ * along with XXX. If not, see * . */ -package li.strolch.runtime.main; - -import java.io.File; - -import li.strolch.runtime.agent.StrolchAgent; +package li.strolch.runtime.component; /** * @author Robert von Burg - * + * */ -public class Main { +public interface PostInitializer { - public static void main(String[] args) { - - StrolchAgent agent = new StrolchAgent(); - agent.setup(new File(".")); - } + // marker interface } diff --git a/src/main/java/li/strolch/runtime/component/SimplePostInitializer.java b/src/main/java/li/strolch/runtime/component/SimplePostInitializer.java new file mode 100644 index 000000000..c907953af --- /dev/null +++ b/src/main/java/li/strolch/runtime/component/SimplePostInitializer.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package li.strolch.runtime.component; + +/** + * @author Robert von Burg + * + */ +public class SimplePostInitializer extends StrolchComponent implements PostInitializer { + + /** + * @param container + * @param componentName + */ + public SimplePostInitializer(ComponentContainer container, String componentName) { + super(container, componentName); + } + +} diff --git a/src/main/java/li/strolch/runtime/main/MainStarter.java b/src/main/java/li/strolch/runtime/main/MainStarter.java new file mode 100644 index 000000000..b2a1b29d6 --- /dev/null +++ b/src/main/java/li/strolch/runtime/main/MainStarter.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2012, Robert von Burg + * + * All rights reserved. + * + * This file is part of the XXX. + * + * XXX is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * XXX is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XXX. If not, see + * . + */ +package li.strolch.runtime.main; + +import java.io.File; +import java.text.MessageFormat; +import java.util.concurrent.atomic.AtomicBoolean; + +import li.strolch.runtime.agent.StrolchAgent; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.GnuParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Robert von Burg + * + */ +@SuppressWarnings("nls") +public class MainStarter { + + private static final Logger logger = LoggerFactory.getLogger(MainStarter.class); + private Options options; + private StrolchAgent agent; + + public MainStarter() { + Options op = new Options(); + Option rootPathOption = new Option("p", "root-path", true, "root path to strolch runtime"); + rootPathOption.setOptionalArg(false); + op.addOption(rootPathOption); + this.options = op; + } + + public int start(String[] args) { + + // create the parser + CommandLineParser parser = new GnuParser(); + CommandLine line; + try { + // parse the command line arguments + line = parser.parse(this.options, args); + } catch (ParseException exp) { + // oops, something went wrong + logger.error("Parsing failed. Reason: " + exp.getMessage()); + printUsage(); + return 1; + } + + String pathS = line.getOptionValue("root-path"); + File pathF = new File(pathS); + if (!pathF.exists()) { + logger.info(MessageFormat.format("Path parameter does not exist at: {0}", pathS)); + printUsage(); + return 1; + } + + logger.info("Starting Agent..."); + this.setAgent(new StrolchAgent()); + this.getAgent().setup(pathF); + this.getAgent().initialize(); + this.getAgent().start(); + + final AtomicBoolean atomicBoolean = new AtomicBoolean(); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + synchronized (MainStarter.class) { + System.out.println("VM Shutting down. Stopping Strolch..."); + System.out.println(""); + System.out.println("Strolch application " + MainStarter.this.getAgent().getApplicationName() + + " shutting down..."); + MainStarter.this.getAgent().stop(); + MainStarter.this.getAgent().destroy(); + System.out.println("Strolch application " + MainStarter.this.getAgent().getApplicationName() + + " shut down. Exiting VM."); + + atomicBoolean.set(true); + MainStarter.class.notify(); + } + } + }); + + logger.info(""); + logger.info("Strolch application " + this.getAgent().getApplicationName() + " started "); + while (!atomicBoolean.get()) { + synchronized (MainStarter.class) { + try { + MainStarter.class.wait(); + } catch (InterruptedException e) { + logger.error("Interrupted."); + return 1; + } + } + } + return 0; + } + + private void printUsage() { + + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("java -classpath lib/ -jar .jar ", this.options); + } + + /** + * @return the agent + */ + public StrolchAgent getAgent() { + return this.agent; + } + + /** + * @param agent + * the agent to set + */ + public void setAgent(StrolchAgent agent) { + this.agent = agent; + } +} diff --git a/src/test/java/li/strolch/runtime/QueryTest.java b/src/test/java/li/strolch/runtime/QueryTest.java index df9b4affa..83fc0a981 100644 --- a/src/test/java/li/strolch/runtime/QueryTest.java +++ b/src/test/java/li/strolch/runtime/QueryTest.java @@ -28,7 +28,7 @@ import org.junit.Test; * @author Robert von Burg * */ -@SuppressWarnings("nls") +//@SuppressWarnings("nls") public class QueryTest { @Test