diff --git a/pom.xml b/pom.xml index 5b9414b4..f7879d8a 100644 --- a/pom.xml +++ b/pom.xml @@ -85,41 +85,7 @@ 1.5 1.5 - - - org.apache.maven.plugins - maven-shade-plugin - - - package - - shade - - - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - - - - - ${jar.mainclass} - - - - - - - + diff --git a/samples/helloworld/lib/spring-shell-1.0.0-SNAPSHOT.jar b/samples/helloworld/lib/spring-shell-1.0.0-SNAPSHOT.jar deleted file mode 100644 index 34e094d8..00000000 Binary files a/samples/helloworld/lib/spring-shell-1.0.0-SNAPSHOT.jar and /dev/null differ diff --git a/samples/helloworld/pom.xml b/samples/helloworld/pom.xml index d80c68a1..9aa9bdf2 100644 --- a/samples/helloworld/pom.xml +++ b/samples/helloworld/pom.xml @@ -37,38 +37,36 @@ org.apache.maven.plugins - maven-shade-plugin + maven-dependency-plugin - package + copy-dependencies + prepare-package - shade + copy-dependencies - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - - - - - ${jar.mainclass} - - - + ${project.build.directory}/lib + false + false + true + + org.apache.maven.plugins + maven-jar-plugin + + + + true + lib/ + ${jar.mainclass} + + + + diff --git a/samples/helloworld/readme.txt b/samples/helloworld/readme.txt index 40de4e2f..3be80f78 100644 --- a/samples/helloworld/readme.txt +++ b/samples/helloworld/readme.txt @@ -8,7 +8,7 @@ Maven: -Gradle: +Gradle (not supported yet): 1.Build the project $>../../gradlew build diff --git a/samples/helloworld/src/main/resources/META-INF/spring/spring-shell-plugin.xml b/samples/helloworld/src/main/resources/META-INF/spring/spring-shell-plugin.xml deleted file mode 100644 index fbbbf616..00000000 --- a/samples/helloworld/src/main/resources/META-INF/spring/spring-shell-plugin.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - org.springframework.shell.samples.helloworld.HelloWorldPlugin - - \ No newline at end of file diff --git a/samples/helloworld/src/main/resources/spring-shell-beans.xml b/samples/helloworld/src/main/resources/spring-shell-beans.xml new file mode 100644 index 00000000..c8d367a0 --- /dev/null +++ b/samples/helloworld/src/main/resources/spring-shell-beans.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/org/springframework/shell/Bootstrap.java b/src/main/java/org/springframework/shell/Bootstrap.java index 2ccbf456..1a01d915 100644 --- a/src/main/java/org/springframework/shell/Bootstrap.java +++ b/src/main/java/org/springframework/shell/Bootstrap.java @@ -4,43 +4,29 @@ package org.springframework.shell; import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.List; import java.util.Map; -import java.util.logging.Level; import java.util.logging.Logger; -//import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.roo.shell.AbstractShell; import org.springframework.roo.shell.CommandMarker; import org.springframework.roo.shell.Converter; import org.springframework.roo.shell.ExitShellRequest; -import org.springframework.roo.shell.Shell; -import org.springframework.roo.shell.converters.StringConverter; import org.springframework.roo.shell.event.ShellStatus; import org.springframework.roo.support.logging.HandlerUtils; import org.springframework.roo.support.util.Assert; -import org.springframework.shell.plugin.PluginConfigurationReader; -import org.springframework.shell.plugin.PluginInfo; -import org.springframework.util.ClassUtils; import org.springframework.util.StopWatch; -//import ch.qos.logback.classic.LoggerContext; -//import ch.qos.logback.classic.joran.JoranConfigurator; -//import ch.qos.logback.core.util.StatusPrinter; + /** * Main class, needs some cleanup * * @author vnagaraja + * @author Jarred Li */ public class Bootstrap { @@ -83,17 +69,18 @@ public class Bootstrap { shell = ctx.getBean("shell", JLineShellComponent.class); - Map commands = ctx.getBeansOfType(CommandMarker.class); + Map commands = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx,CommandMarker.class); + for (CommandMarker command : commands.values()) { - System.out.println("Registering command " + command); + //System.out.println("Registering command " + command); shell.getSimpleParser().add(command); } - Map converters = ctx.getBeansOfType(Converter.class); + Map converters = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx,Converter.class); for (Converter converter : converters.values()) { - System.out.println("Registering converter " + converter); + //System.out.println("Registering converter " + converter); shell.getSimpleParser().add(converter); } @@ -113,15 +100,10 @@ public class Bootstrap { } } } - - - } - private void createApplicationContext(String applicationContextLocation) { - //ctx = new ClassPathXmlApplicationContext(applicationContextLocation); - - AnnotationConfigApplicationContext annctx = new AnnotationConfigApplicationContext(); + 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); @@ -140,28 +122,28 @@ public class Bootstrap { createAndRegisterBeanDefinition(annctx, org.springframework.shell.JLineShellComponent.class, "shell"); createAndRegisterBeanDefinition(annctx, org.springframework.shell.converters.SimpleFileConverter.class); - ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); - PluginConfigurationReader configReader = new PluginConfigurationReader(resourcePatternResolver); - PluginInfo[] pluginInfos = configReader.readPluginInfos("classpath*:/META-INF/spring/spring-shell-plugin.xml"); - for (int i = 0; i < pluginInfos.length; i++) { - List configClassNames = pluginInfos[i].getConfigClassNames(); - for (String configClassName : configClassNames) { - try { - annctx.register(ClassUtils.forName(configClassName, ClassUtils.getDefaultClassLoader())); - } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (LinkageError e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } annctx.scan("org.springframework.shell.commands"); annctx.scan("org.springframework.shell.converters"); annctx.refresh(); - ctx = annctx; + + ctx = initPluginApplicationContext(annctx); + ctx.refresh(); + } + + /** + * init plugin ApplicactionContext + * + * @param annctx parent ApplicationContext in core spring shell + * @return new ApplicationContext in the plugin with core spring shell's context as parent + */ + private ConfigurableApplicationContext initPluginApplicationContext(AnnotationConfigApplicationContext annctx) { + ClassPathXmlApplicationContext subContext = new ClassPathXmlApplicationContext("spring-shell-beans.xml"); + subContext.setParent(annctx); + return subContext; + } + + protected void createAndRegisterBeanDefinition(AnnotationConfigApplicationContext annctx, Class clazz){ createAndRegisterBeanDefinition(annctx,clazz,null); diff --git a/src/main/java/org/springframework/shell/plugin/PluginConfigurationReader.java b/src/main/java/org/springframework/shell/plugin/PluginConfigurationReader.java deleted file mode 100644 index 74217882..00000000 --- a/src/main/java/org/springframework/shell/plugin/PluginConfigurationReader.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.springframework.shell.plugin; - -import java.io.IOException; -import java.io.InputStream; -import java.util.LinkedList; -import java.util.List; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.springframework.util.Assert; -import org.springframework.util.StringUtils; -import org.springframework.util.xml.DomUtils; -import org.springframework.util.xml.SimpleSaxErrorHandler; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.ResourcePatternResolver; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; - -public class PluginConfigurationReader { - - - private static final String CONFIGURATION = "configuration"; - - private static final String CONFIGURATION_CLASS_NAME = "class"; - - private final Log logger = LogFactory.getLog(getClass()); - - private final ResourcePatternResolver resourcePatternResolver; - - public PluginConfigurationReader(ResourcePatternResolver resourcePatternResolver) { - Assert.notNull(resourcePatternResolver, "ResourceLoader must not be null"); - this.resourcePatternResolver = resourcePatternResolver; - } - - - public PluginInfo[] readPluginInfos(String... pluginInfoXmlLocations) { - ErrorHandler handler = new SimpleSaxErrorHandler(logger); - List infos = new LinkedList(); - String resourceLocation = null; - try { - for (String location : pluginInfoXmlLocations) { - Resource[] resources = this.resourcePatternResolver.getResources(location); - for (Resource resource : resources) { - resourceLocation = resource.toString(); - InputStream stream = resource.getInputStream(); - try { - Document document = buildDocument(handler, stream); - parseDocument(resource, document, infos); - } - finally { - stream.close(); - } - } - } - } - catch (IOException ex) { - throw new IllegalArgumentException("Cannot parse persistence unit from " + resourceLocation, ex); - } - catch (SAXException ex) { - throw new IllegalArgumentException("Invalid XML in persistence unit from " + resourceLocation, ex); - } - catch (ParserConfigurationException ex) { - throw new IllegalArgumentException("Internal error parsing persistence unit from " + resourceLocation); - } - - return infos.toArray(new PluginInfo[infos.size()]); - } - - - /** - * Parse the validated document and add entries to the given unit info list. - */ - protected List parseDocument( - Resource resource, Document document, List infos) throws IOException { - - Element persistence = document.getDocumentElement(); - List configurations = DomUtils.getChildElementsByTagName(persistence, CONFIGURATION); - for (Element configuration : configurations) { - PluginInfo info = parsePluginInfo(configuration); - infos.add(info); - - } - - return infos; - } - - /** - * Parse the plugin DOM element. - */ - protected PluginInfo parsePluginInfo(Element configuration) throws IOException { - PluginInfo pluginInfo = new PluginInfo(); - parseClass(configuration, pluginInfo); - return pluginInfo; - } - - - /** - * Parse the class XML elements. - */ - @SuppressWarnings("unchecked") - protected void parseClass(Element configuration, PluginInfo pluginInfo) { - List classes = DomUtils.getChildElementsByTagName(configuration, CONFIGURATION_CLASS_NAME); - for (Element element : classes) { - String value = DomUtils.getTextValue(element).trim(); - if (StringUtils.hasText(value)) - pluginInfo.addConfigurationClassName(value); - } - } - - /** - * Validate the given stream and return a valid DOM document for parsing. - */ - protected Document buildDocument(ErrorHandler handler, InputStream stream) - throws ParserConfigurationException, SAXException, IOException { - - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - dbf.setNamespaceAware(true); - DocumentBuilder parser = dbf.newDocumentBuilder(); - parser.setErrorHandler(handler); - return parser.parse(stream); - } -} diff --git a/src/main/java/org/springframework/shell/plugin/PluginInfo.java b/src/main/java/org/springframework/shell/plugin/PluginInfo.java deleted file mode 100644 index 52bb8dce..00000000 --- a/src/main/java/org/springframework/shell/plugin/PluginInfo.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.springframework.shell.plugin; - -import java.util.LinkedList; -import java.util.List; - -public class PluginInfo { - - private List configClassNames = new LinkedList(); - - public void addConfigurationClassName(String configClassName) { - this.configClassNames.add(configClassName); - - } - - public List getConfigClassNames() { - return this.configClassNames; - } - -}