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,
<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>
--- /dev/null
+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());
+ }
+}
\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
\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
\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
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
--- /dev/null
+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) {
+ }
+
+}
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
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
}\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
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