move shell thread from Bootstrap to JLineShellComponent
This commit is contained in:
@@ -10,7 +10,6 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Collection;
|
||||
@@ -18,11 +17,8 @@ import java.util.Date;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import org.springframework.roo.shell.event.AbstractShellStatusPublisher;
|
||||
import org.springframework.roo.shell.event.ShellStatus;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,35 +17,50 @@ import org.springframework.roo.shell.SimpleParser;
|
||||
public class JLineShellComponent extends JLineShell implements Lifecycle {
|
||||
|
||||
private volatile boolean running = false;
|
||||
|
||||
private Thread shellThread;
|
||||
|
||||
// Fields
|
||||
private ExecutionStrategy executionStrategy = new SimpleExecutionStrategy(); //ProcessManagerHostedExecutionStrategy is not what i think we need outside of Roo.
|
||||
private SimpleParser parser = new SimpleParser();
|
||||
|
||||
|
||||
// Dont' need this, used to get twitter status.
|
||||
//@Reference private UrlInputStreamService urlInputStreamService;
|
||||
//
|
||||
|
||||
|
||||
|
||||
public SimpleParser getSimpleParser() {
|
||||
return parser;
|
||||
}
|
||||
|
||||
|
||||
public void start() {
|
||||
running=true;
|
||||
}
|
||||
|
||||
|
||||
public void stop() {
|
||||
closeShell();
|
||||
running=false;
|
||||
|
||||
public void start() {
|
||||
shellThread = new Thread(this, "Spring Shell");
|
||||
shellThread.start();
|
||||
running = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void stop() {
|
||||
closeShell();
|
||||
running = false;
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
/**
|
||||
* wait the shell command to complete by typing "quit" or "exit"
|
||||
*
|
||||
*/
|
||||
public void waitForComplete() {
|
||||
try {
|
||||
shellThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<URL> findResources(final String path) {
|
||||
// For an OSGi bundle search, we add the root prefix to the given path
|
||||
|
||||
Reference in New Issue
Block a user