From: Mathieu Baudier Date: Mon, 2 Aug 2010 13:06:22 +0000 (+0000) Subject: Improve Spring Integration X-Git-Tag: argeo-commons-2.1.30~1580 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=dcaa55aad26d7fd945a713af5854741f82dd13f3;p=lgpl%2Fargeo-commons.git Improve Spring Integration git-svn-id: https://svn.argeo.org/commons/trunk@3741 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF b/eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF index f1109799e..2e7f5b89b 100644 --- a/eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF +++ b/eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF @@ -9,7 +9,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime Bundle-RequiredExecutionEnvironment: J2SE-1.5 Bundle-ActivationPolicy: lazy -Import-Package: org.springframework.beans.factory;version="2.5.6.SEC01", +Import-Package: org.apache.commons.logging;version="1.1.1", + org.springframework.beans.factory;version="2.5.6.SEC01", org.springframework.context;version="2.5.6.SEC01", org.springframework.core.io.support;version="2.5.6.SEC01" Export-Package: org.argeo.eclipse.spring, diff --git a/eclipse/plugins/org.argeo.eclipse.ui/pom.xml b/eclipse/plugins/org.argeo.eclipse.ui/pom.xml index b03db8139..7c67d3cc4 100644 --- a/eclipse/plugins/org.argeo.eclipse.ui/pom.xml +++ b/eclipse/plugins/org.argeo.eclipse.ui/pom.xml @@ -27,5 +27,9 @@ org.springframework org.springframework.context + + org.slf4j + com.springsource.slf4j.org.apache.commons.logging + diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/AbstractSpringUiPlugin.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/AbstractSpringUiPlugin.java new file mode 100644 index 000000000..8b4ed7bd4 --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/AbstractSpringUiPlugin.java @@ -0,0 +1,25 @@ +package org.argeo.eclipse.spring; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; +import org.springframework.context.ApplicationContext; + +public abstract class AbstractSpringUiPlugin extends AbstractUIPlugin { + private BundleContext bundleContext; + + @Override + public void start(BundleContext context) throws Exception { + super.start(context); + this.bundleContext = context; + } + + @Override + public void stop(BundleContext context) throws Exception { + super.stop(context); + } + + public ApplicationContext getApplicationContext() { + return ApplicationContextTracker.getApplicationContext(bundleContext + .getBundle()); + } +} diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java index be53a97fc..3ab629fd0 100644 --- a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java @@ -2,8 +2,13 @@ package org.argeo.eclipse.spring; import static java.text.MessageFormat.format; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.eclipse.ui.ArgeoUiPlugin; +import org.eclipse.core.runtime.Platform; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.util.tracker.ServiceTracker; @@ -11,8 +16,11 @@ import org.springframework.context.ApplicationContext; /** * @author Heiko Seeberger + * @author Mathieu Baudier */ -public class ApplicationContextTracker { +class ApplicationContextTracker { + private final static Log log = LogFactory + .getLog(ApplicationContextTracker.class); private static final String FILTER = "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$ + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$ @@ -21,18 +29,21 @@ public class ApplicationContextTracker { /** * @param contributorBundle - * OSGi bundle for which the Spring application context is to be - * tracked. Must not be null! + * OSGi bundle for which the Spring application context is to be + * tracked. Must not be null! * @param factoryBundleContext - * BundleContext object which can be used to track services + * BundleContext object which can be used to track services * @throws IllegalArgumentException - * if the given bundle is null. + * if the given bundle is null. */ - public ApplicationContextTracker(final Bundle contributorBundle, final BundleContext factoryBundleContext) { - final String filter = format(FILTER, contributorBundle.getSymbolicName()); + public ApplicationContextTracker(final Bundle contributorBundle, + final BundleContext factoryBundleContext) { + final String filter = format(FILTER, contributorBundle + .getSymbolicName()); try { applicationContextServiceTracker = new ServiceTracker( - factoryBundleContext, FrameworkUtil.createFilter(filter), null); + factoryBundleContext, FrameworkUtil.createFilter(filter), + null); applicationContextServiceTracker.open(); } catch (final InvalidSyntaxException e) { e.printStackTrace(); @@ -63,4 +74,39 @@ public class ApplicationContextTracker { close(); super.finalize(); } + + static ApplicationContext getApplicationContext(String bundleSymbolicName) { + Bundle contributorBundle = Platform.getBundle(bundleSymbolicName); + return getApplicationContext(contributorBundle); + } + + static ApplicationContext getApplicationContext(Bundle contributorBundle) { + if (log.isTraceEnabled()) + log + .trace("Get application context for bundle " + + contributorBundle); + + if (contributorBundle.getState() != Bundle.ACTIVE + && contributorBundle.getState() != Bundle.STARTING) { + try { + log.info("Starting bundle: " + + contributorBundle.getSymbolicName()); + contributorBundle.start(); + } catch (BundleException e) { + e.printStackTrace(); + } + } + + final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker( + contributorBundle, ArgeoUiPlugin.getDefault() + .getBundleContext()); + ApplicationContext applicationContext = null; + try { + applicationContext = applicationContextTracker + .getApplicationContext(); + } finally { + applicationContextTracker.close(); + } + return applicationContext; + } } diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringCommandHandler.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringCommandHandler.java new file mode 100644 index 000000000..9f3161c2e --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringCommandHandler.java @@ -0,0 +1,87 @@ +package org.argeo.eclipse.spring; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.commands.IHandlerListener; +import org.springframework.context.ApplicationContext; + +public class SpringCommandHandler implements IHandler { + private final static Log log = LogFactory + .getLog(SpringCommandHandler.class); + + public void addHandlerListener(IHandlerListener handlerListener) { + } + + public void dispose() { + } + + public Object execute(ExecutionEvent event) throws ExecutionException { + try { + if (log.isDebugEnabled()) + log.debug("Execute " + event + " via spring command handler " + + this); + + // Find the application context of the bundle defining the command + // extension + // ICommandService commandService = + // (ICommandService)ArgeoUiPlugin.getDefault().getWorkbench().getService(ICommandService.class); + // + // Command command = + // commandService.getCommand(event.getCommand().getId()); + // log.debug("command=" + command); + // log.debug("Command id " + command.getId()); + // + // Platform.getExtensionRegistry().getE + // + // IExtension extension = + // Platform.getExtensionRegistry().getExtension( + // "org.eclipse.ui.commands", command.getId()); + // log.debug("extension=" + extension); + // String bundleSymbolicName = extension.getContributor().getName(); + + // Assume that the defining bundle name is the first part of the + // command + // id + // TODO: make it more flexible and robust + String commandId = event.getCommand().getId(); + String bundleSymbolicName = commandId.substring(0, commandId + .lastIndexOf('.')); + ApplicationContext applicationContext = ApplicationContextTracker + .getApplicationContext(bundleSymbolicName); + + // retrieve the command via its id + String beanName = event.getCommand().getId(); + if (!applicationContext.containsBean(beanName)) + throw new ExecutionException("No bean found with name " + + beanName + " in bundle " + bundleSymbolicName); + Object bean = applicationContext.getBean(beanName); + + if (!(bean instanceof IHandler)) + throw new ExecutionException("Bean with name " + beanName + + " and class " + bean.getClass() + + " does not implement the IHandler interface."); + + IHandler handler = (IHandler) bean; + return handler.execute(event); + } catch (Exception e) { + // TODO: use eclipse error management + log.error(e); + return null; + } + } + + public boolean isEnabled() { + return true; + } + + public boolean isHandled() { + return true; + } + + public void removeHandlerListener(IHandlerListener handlerListener) { + } + +} diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java index f92b423e9..dc35c7d7f 100644 --- a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java @@ -1,14 +1,10 @@ package org.argeo.eclipse.spring; -import org.argeo.eclipse.ui.ArgeoUiPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExecutableExtension; import org.eclipse.core.runtime.IExecutableExtensionFactory; import org.eclipse.core.runtime.IExtension; -import org.eclipse.core.runtime.Platform; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleException; import org.springframework.context.ApplicationContext; /** @@ -43,7 +39,8 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory, public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException { String beanName = getBeanName(data, config); - ApplicationContext appContext = getApplicationContext(config); + ApplicationContext appContext = ApplicationContextTracker + .getApplicationContext(config.getContributor().getName()); if (beanName != null && appContext != null) { this.bean = appContext.getBean(beanName); @@ -55,7 +52,7 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory, } private String getBeanName(Object data, IConfigurationElement config) { - + // try the specific bean id the extension defines if (data != null && data.toString().length() > 0) { return data.toString(); @@ -76,30 +73,4 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory, return null; } - private ApplicationContext getApplicationContext( - IConfigurationElement config) { - String contributorName = config.getContributor().getName(); - Bundle contributorBundle = Platform.getBundle(contributorName); - - if (contributorBundle.getState() != Bundle.ACTIVE && contributorBundle.getState() != Bundle.STARTING) { - try { - System.out.println("starting bundle: " + contributorBundle.getSymbolicName()); - contributorBundle.start(); - } catch (BundleException e) { - e.printStackTrace(); - } - } - - final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker( - contributorBundle, ArgeoUiPlugin.getDefault().getBundleContext()); - ApplicationContext applicationContext = null; - try { - applicationContext = applicationContextTracker - .getApplicationContext(); - } finally { - applicationContextTracker.close(); - } - return applicationContext; - } - }