move shell thread from Bootstrap to JLineShellComponent
This commit is contained in:
@@ -30,90 +30,79 @@ import org.springframework.util.StopWatch;
|
||||
*/
|
||||
public class Bootstrap {
|
||||
|
||||
private static Bootstrap bootstrap;
|
||||
private JLineShellComponent shell;
|
||||
private Thread shellThread;
|
||||
private ConfigurableApplicationContext ctx;
|
||||
private static StopWatch sw = new StopWatch("Spring Sehll");
|
||||
private static SimpleShellCommandLineOptions options;
|
||||
private static Bootstrap bootstrap;
|
||||
private JLineShellComponent shell;
|
||||
private ConfigurableApplicationContext ctx;
|
||||
private static StopWatch sw = new StopWatch("Spring Sehll");
|
||||
private static SimpleShellCommandLineOptions options;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
sw.start();
|
||||
options = SimpleShellCommandLineOptions.parseCommandLine(args);
|
||||
public static void main(String[] args) throws IOException {
|
||||
sw.start();
|
||||
options = SimpleShellCommandLineOptions.parseCommandLine(args);
|
||||
|
||||
for (Map.Entry<String, String> entry : options.extraSystemProperties.entrySet()) {
|
||||
System.setProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
ExitShellRequest exitShellRequest;
|
||||
try {
|
||||
bootstrap = new Bootstrap(options.applicationContextLocation);
|
||||
exitShellRequest = bootstrap.run(options.executeThenQuit);
|
||||
} catch (RuntimeException t) {
|
||||
throw t;
|
||||
} finally {
|
||||
HandlerUtils.flushAllHandlers(Logger.getLogger(""));
|
||||
}
|
||||
for (Map.Entry<String, String> entry : options.extraSystemProperties.entrySet()) {
|
||||
System.setProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
ExitShellRequest exitShellRequest;
|
||||
try {
|
||||
bootstrap = new Bootstrap(options.applicationContextLocation);
|
||||
exitShellRequest = bootstrap.run(options.executeThenQuit);
|
||||
} catch (RuntimeException t) {
|
||||
throw t;
|
||||
} finally {
|
||||
HandlerUtils.flushAllHandlers(Logger.getLogger(""));
|
||||
}
|
||||
|
||||
System.exit(exitShellRequest.getExitCode());
|
||||
}
|
||||
System.exit(exitShellRequest.getExitCode());
|
||||
}
|
||||
|
||||
public Bootstrap(String applicationContextLocation) throws IOException {
|
||||
public Bootstrap(String applicationContextLocation) throws IOException {
|
||||
//setupLogging();
|
||||
Assert.hasText(applicationContextLocation, "Application context location required");
|
||||
createApplicationContext(applicationContextLocation);
|
||||
|
||||
//setupLogging();
|
||||
shell = ctx.getBean("shell", JLineShellComponent.class);
|
||||
shell.setApplicationContext(ctx);
|
||||
if (options.executeThenQuit != null) {
|
||||
shell.setPrintBanner(false);
|
||||
}
|
||||
|
||||
Assert.hasText(applicationContextLocation, "Application context location required");
|
||||
Map<String, CommandMarker> commands = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx, CommandMarker.class);
|
||||
for (CommandMarker command : commands.values()) {
|
||||
//System.out.println("Registering command " + command);
|
||||
shell.getSimpleParser().add(command);
|
||||
}
|
||||
|
||||
createApplicationContext(applicationContextLocation);
|
||||
Map<String, Converter> converters = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx, Converter.class);
|
||||
for (Converter converter : converters.values()) {
|
||||
//System.out.println("Registering converter " + converter);
|
||||
shell.getSimpleParser().add(converter);
|
||||
}
|
||||
|
||||
|
||||
//shell = new JLineShellComponent();
|
||||
shell = ctx.getBean("shell", JLineShellComponent.class);
|
||||
shell.setApplicationContext(ctx);
|
||||
if(options.executeThenQuit != null){
|
||||
shell.setPrintBanner(false);
|
||||
}
|
||||
|
||||
Map<String, CommandMarker> commands = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx,CommandMarker.class);
|
||||
|
||||
shell.start();
|
||||
|
||||
for (CommandMarker command : commands.values()) {
|
||||
//System.out.println("Registering command " + command);
|
||||
shell.getSimpleParser().add(command);
|
||||
}
|
||||
|
||||
Map<String, Converter> converters = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx,Converter.class);
|
||||
|
||||
for (Converter converter : converters.values()) {
|
||||
//System.out.println("Registering converter " + converter);
|
||||
shell.getSimpleParser().add(converter);
|
||||
}
|
||||
|
||||
|
||||
shellThread = new Thread(shell,"Spring Shell");
|
||||
shellThread.start();
|
||||
shell.start();
|
||||
|
||||
//TODO use listener and latch..
|
||||
while(true) {
|
||||
//System.out.println("shell status = " + shell.getShellStatus().getStatus());
|
||||
if (shell.getShellStatus().getStatus().equals(ShellStatus.Status.USER_INPUT)) {
|
||||
break;
|
||||
} else {
|
||||
try {
|
||||
//TODO use listener and latch..
|
||||
while (true) {
|
||||
if (shell.getShellStatus().getStatus().equals(ShellStatus.Status.USER_INPUT)) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createApplicationContext(String applicationContextLocation) {
|
||||
AnnotationConfigApplicationContext annctx = new AnnotationConfigApplicationContext();
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.StringConverter.class);
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.AvailableCommandsConverter.class);
|
||||
}
|
||||
|
||||
private void createApplicationContext(String applicationContextLocation) {
|
||||
AnnotationConfigApplicationContext annctx = new AnnotationConfigApplicationContext();
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.StringConverter.class);
|
||||
createAndRegisterBeanDefinition(annctx,
|
||||
org.springframework.roo.shell.converters.AvailableCommandsConverter.class);
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.BigDecimalConverter.class);
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.BigIntegerConverter.class);
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.BooleanConverter.class);
|
||||
@@ -129,15 +118,15 @@ public class Bootstrap {
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.StaticFieldConverterImpl.class);
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.shell.JLineShellComponent.class, "shell");
|
||||
createAndRegisterBeanDefinition(annctx, org.springframework.shell.converters.SimpleFileConverter.class);
|
||||
|
||||
|
||||
annctx.scan("org.springframework.shell.commands");
|
||||
annctx.scan("org.springframework.shell.converters");
|
||||
annctx.scan("org.springframework.shell.plugin.support");
|
||||
annctx.refresh();
|
||||
|
||||
|
||||
ctx = initPluginApplicationContext(annctx);
|
||||
ctx.refresh();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -148,67 +137,63 @@ public class Bootstrap {
|
||||
* @return new ApplicationContext in the plugin with core spring shell's context as parent
|
||||
*/
|
||||
private ConfigurableApplicationContext initPluginApplicationContext(AnnotationConfigApplicationContext annctx) {
|
||||
ClassPathXmlApplicationContext subContext = new ClassPathXmlApplicationContext("classpath*:/META-INF/spring/spring-shell-plugin.xml");
|
||||
ClassPathXmlApplicationContext subContext = new ClassPathXmlApplicationContext(
|
||||
"classpath*:/META-INF/spring/spring-shell-plugin.xml");
|
||||
subContext.setParent(annctx);
|
||||
return subContext;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void createAndRegisterBeanDefinition(AnnotationConfigApplicationContext annctx, Class clazz){
|
||||
createAndRegisterBeanDefinition(annctx,clazz,null);
|
||||
|
||||
|
||||
|
||||
protected void createAndRegisterBeanDefinition(AnnotationConfigApplicationContext annctx, Class clazz) {
|
||||
createAndRegisterBeanDefinition(annctx, clazz, null);
|
||||
}
|
||||
|
||||
protected void createAndRegisterBeanDefinition(AnnotationConfigApplicationContext annctx, Class clazz,String name) {
|
||||
|
||||
protected void createAndRegisterBeanDefinition(AnnotationConfigApplicationContext annctx, Class clazz, String name) {
|
||||
RootBeanDefinition rbd = new RootBeanDefinition();
|
||||
rbd.setBeanClass(clazz);
|
||||
if(name != null){
|
||||
if (name != null) {
|
||||
annctx.registerBeanDefinition(name, rbd);
|
||||
}
|
||||
else{
|
||||
else {
|
||||
annctx.registerBeanDefinition(clazz.getSimpleName(), rbd);
|
||||
}
|
||||
}
|
||||
|
||||
protected ExitShellRequest run(String[] executeThenQuit) {
|
||||
|
||||
ExitShellRequest exitShellRequest;
|
||||
|
||||
if (null != executeThenQuit) {
|
||||
boolean successful = false;
|
||||
exitShellRequest = ExitShellRequest.FATAL_EXIT;
|
||||
protected ExitShellRequest run(String[] executeThenQuit) {
|
||||
|
||||
for(String cmd : executeThenQuit) {
|
||||
successful = shell.executeCommand(cmd);
|
||||
if(!successful)
|
||||
break;
|
||||
}
|
||||
ExitShellRequest exitShellRequest;
|
||||
|
||||
//if all commands were successful, set the normal exit status
|
||||
if (successful) {
|
||||
exitShellRequest = ExitShellRequest.NORMAL_EXIT;
|
||||
}
|
||||
} else {
|
||||
exitShellRequest = shell.getExitShellRequest();
|
||||
if (exitShellRequest == null) {
|
||||
// shouldn't really happen, but we'll fallback to this anyway
|
||||
exitShellRequest = ExitShellRequest.NORMAL_EXIT;
|
||||
}
|
||||
|
||||
try {
|
||||
shellThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ctx.close();
|
||||
sw.stop();
|
||||
if (shell.isDevelopmentMode()) {
|
||||
System.out.println("Total execution time: " + sw.getLastTaskTimeMillis() + " ms");
|
||||
}
|
||||
return exitShellRequest;
|
||||
}
|
||||
if (null != executeThenQuit) {
|
||||
boolean successful = false;
|
||||
exitShellRequest = ExitShellRequest.FATAL_EXIT;
|
||||
|
||||
for (String cmd : executeThenQuit) {
|
||||
successful = shell.executeCommand(cmd);
|
||||
if (!successful)
|
||||
break;
|
||||
}
|
||||
|
||||
//if all commands were successful, set the normal exit status
|
||||
if (successful) {
|
||||
exitShellRequest = ExitShellRequest.NORMAL_EXIT;
|
||||
}
|
||||
}
|
||||
else {
|
||||
exitShellRequest = shell.getExitShellRequest();
|
||||
if (exitShellRequest == null) {
|
||||
// shouldn't really happen, but we'll fallback to this anyway
|
||||
exitShellRequest = ExitShellRequest.NORMAL_EXIT;
|
||||
}
|
||||
shell.waitForComplete();
|
||||
}
|
||||
|
||||
ctx.close();
|
||||
sw.stop();
|
||||
if (shell.isDevelopmentMode()) {
|
||||
System.out.println("Total execution time: " + sw.getLastTaskTimeMillis() + " ms");
|
||||
}
|
||||
return exitShellRequest;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user