Improve Spring Integration
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 2 Aug 2010 13:06:22 +0000 (13:06 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 2 Aug 2010 13:06:22 +0000 (13:06 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@3741 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF
eclipse/plugins/org.argeo.eclipse.ui/pom.xml
eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/AbstractSpringUiPlugin.java [new file with mode: 0644]
eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java
eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringCommandHandler.java [new file with mode: 0644]
eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java

index f1109799ee74a05e8b07ddf4fb1d3c777ffecac8..2e7f5b89b044a3572c5704f082c98265e6f7fbd0 100644 (file)
@@ -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,
index b03db813974c0fe583019b55a5e08fb5325be0fc..7c67d3cc4e746df68ce825e103a059118cc921f8 100644 (file)
@@ -27,5 +27,9 @@
                        <groupId>org.springframework</groupId>
                        <artifactId>org.springframework.context</artifactId>
                </dependency>
+               <dependency>
+                       <groupId>org.slf4j</groupId>
+                       <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
+               </dependency>
        </dependencies>
 </project>
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 (file)
index 0000000..8b4ed7b
--- /dev/null
@@ -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());
+       }
+}
index be53a97fcba764029ce9534753d0cce31b122891..3ab629fd033bb6166ba8ddbef0ea0ae9d1295334 100644 (file)
@@ -2,8 +2,13 @@ package org.argeo.eclipse.spring;
 \r
 import static java.text.MessageFormat.format;\r
 \r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.argeo.eclipse.ui.ArgeoUiPlugin;\r
+import org.eclipse.core.runtime.Platform;\r
 import org.osgi.framework.Bundle;\r
 import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.BundleException;\r
 import org.osgi.framework.FrameworkUtil;\r
 import org.osgi.framework.InvalidSyntaxException;\r
 import org.osgi.util.tracker.ServiceTracker;\r
@@ -11,8 +16,11 @@ import org.springframework.context.ApplicationContext;
 \r
 /**\r
  * @author Heiko Seeberger\r
+ * @author Mathieu Baudier\r
  */\r
-public class ApplicationContextTracker {\r
+class ApplicationContextTracker {\r
+       private final static Log log = LogFactory\r
+                       .getLog(ApplicationContextTracker.class);\r
 \r
        private static final String FILTER = "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$\r
                        + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$\r
@@ -21,18 +29,21 @@ public class ApplicationContextTracker {
 \r
        /**\r
         * @param contributorBundle\r
-        *              OSGi bundle for which the Spring application context is to be\r
-        *              tracked. Must not be null!\r
+        *            OSGi bundle for which the Spring application context is to be\r
+        *            tracked. Must not be null!\r
         * @param factoryBundleContext\r
-        *      BundleContext object which can be used to track services\r
+        *            BundleContext object which can be used to track services\r
         * @throws IllegalArgumentException\r
-        *              if the given bundle is null.\r
+        *             if the given bundle is null.\r
         */\r
-       public ApplicationContextTracker(final Bundle contributorBundle, final BundleContext factoryBundleContext) {\r
-               final String filter = format(FILTER, contributorBundle.getSymbolicName());\r
+       public ApplicationContextTracker(final Bundle contributorBundle,\r
+                       final BundleContext factoryBundleContext) {\r
+               final String filter = format(FILTER, contributorBundle\r
+                               .getSymbolicName());\r
                try {\r
                        applicationContextServiceTracker = new ServiceTracker(\r
-                                       factoryBundleContext, FrameworkUtil.createFilter(filter), null);\r
+                                       factoryBundleContext, FrameworkUtil.createFilter(filter),\r
+                                       null);\r
                        applicationContextServiceTracker.open();\r
                } catch (final InvalidSyntaxException e) {\r
                        e.printStackTrace();\r
@@ -63,4 +74,39 @@ public class ApplicationContextTracker {
                close();\r
                super.finalize();\r
        }\r
+\r
+       static ApplicationContext getApplicationContext(String bundleSymbolicName) {\r
+               Bundle contributorBundle = Platform.getBundle(bundleSymbolicName);\r
+               return getApplicationContext(contributorBundle);\r
+       }\r
+\r
+       static ApplicationContext getApplicationContext(Bundle contributorBundle) {\r
+               if (log.isTraceEnabled())\r
+                       log\r
+                                       .trace("Get application context for bundle "\r
+                                                       + contributorBundle);\r
+\r
+               if (contributorBundle.getState() != Bundle.ACTIVE\r
+                               && contributorBundle.getState() != Bundle.STARTING) {\r
+                       try {\r
+                               log.info("Starting bundle: "\r
+                                               + contributorBundle.getSymbolicName());\r
+                               contributorBundle.start();\r
+                       } catch (BundleException e) {\r
+                               e.printStackTrace();\r
+                       }\r
+               }\r
+\r
+               final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker(\r
+                               contributorBundle, ArgeoUiPlugin.getDefault()\r
+                                               .getBundleContext());\r
+               ApplicationContext applicationContext = null;\r
+               try {\r
+                       applicationContext = applicationContextTracker\r
+                                       .getApplicationContext();\r
+               } finally {\r
+                       applicationContextTracker.close();\r
+               }\r
+               return applicationContext;\r
+       }\r
 }\r
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 (file)
index 0000000..9f3161c
--- /dev/null
@@ -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) {
+       }
+
+}
index f92b423e9799e3f7e510301ee93999d508f3d089..dc35c7d7f003fd365f14462267053a1b0d47cf9e 100644 (file)
@@ -1,14 +1,10 @@
 package org.argeo.eclipse.spring;\r
 \r
-import org.argeo.eclipse.ui.ArgeoUiPlugin;\r
 import org.eclipse.core.runtime.CoreException;\r
 import org.eclipse.core.runtime.IConfigurationElement;\r
 import org.eclipse.core.runtime.IExecutableExtension;\r
 import org.eclipse.core.runtime.IExecutableExtensionFactory;\r
 import org.eclipse.core.runtime.IExtension;\r
-import org.eclipse.core.runtime.Platform;\r
-import org.osgi.framework.Bundle;\r
-import org.osgi.framework.BundleException;\r
 import org.springframework.context.ApplicationContext;\r
 \r
 /**\r
@@ -43,7 +39,8 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory,
        public void setInitializationData(IConfigurationElement config,\r
                        String propertyName, Object data) throws CoreException {\r
                String beanName = getBeanName(data, config);\r
-               ApplicationContext appContext = getApplicationContext(config);\r
+               ApplicationContext appContext = ApplicationContextTracker\r
+                               .getApplicationContext(config.getContributor().getName());\r
 \r
                if (beanName != null && appContext != null) {\r
                        this.bean = appContext.getBean(beanName);\r
@@ -55,7 +52,7 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory,
        }\r
 \r
        private String getBeanName(Object data, IConfigurationElement config) {\r
-               \r
+\r
                // try the specific bean id the extension defines\r
                if (data != null && data.toString().length() > 0) {\r
                        return data.toString();\r
@@ -76,30 +73,4 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory,
                return null;\r
        }\r
 \r
-       private ApplicationContext getApplicationContext(\r
-                       IConfigurationElement config) {\r
-               String contributorName = config.getContributor().getName();\r
-               Bundle contributorBundle = Platform.getBundle(contributorName);\r
-\r
-               if (contributorBundle.getState() != Bundle.ACTIVE && contributorBundle.getState() != Bundle.STARTING) {\r
-                       try {\r
-                               System.out.println("starting bundle: " + contributorBundle.getSymbolicName());\r
-                               contributorBundle.start();\r
-                       } catch (BundleException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-               }\r
-\r
-               final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker(\r
-                               contributorBundle, ArgeoUiPlugin.getDefault().getBundleContext());\r
-               ApplicationContext applicationContext = null;\r
-               try {\r
-                       applicationContext = applicationContextTracker\r
-                                       .getApplicationContext();\r
-               } finally {\r
-                       applicationContextTracker.close();\r
-               }\r
-               return applicationContext;\r
-       }\r
-\r
 }\r