]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java
Introduce Eclipse Commons
[lgpl/argeo-commons.git] / eclipse / plugins / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / spring / SpringExtensionFactory.java
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
new file mode 100644 (file)
index 0000000..f92b423
--- /dev/null
@@ -0,0 +1,105 @@
+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
+ * The Spring Extension Factory builds a bridge between the Eclipse Extension\r
+ * Registry and the Spring Framework (especially Spring Dynamic Modules).\r
+ * \r
+ * It allows you to define your extension as a spring bean within the spring\r
+ * application context of your bundle. If you would like to use this bean as an\r
+ * instance of an extension (an Eclipse RCP view, for example) you define the\r
+ * extension with this spring extension factory as the class to be created.\r
+ * \r
+ * To let the spring extension factory pick the right bean from your application\r
+ * context you need to set the bean id to the same value as the id of the view\r
+ * within the view definition, for example. This is important if your extension\r
+ * definition contains more than one element, where each element has its own id.\r
+ * \r
+ * If the extension definition elements themselves have no id attribute the\r
+ * spring extension factory uses the id of the extension itself to identify the\r
+ * bean.\r
+ * \r
+ * @author Martin Lippert\r
+ */\r
+public class SpringExtensionFactory implements IExecutableExtensionFactory,\r
+               IExecutableExtension {\r
+\r
+       private Object bean;\r
+\r
+       public Object create() throws CoreException {\r
+               return bean;\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
+\r
+               if (beanName != null && appContext != null) {\r
+                       this.bean = appContext.getBean(beanName);\r
+                       if (this.bean instanceof IExecutableExtension) {\r
+                               ((IExecutableExtension) this.bean).setInitializationData(\r
+                                               config, propertyName, data);\r
+                       }\r
+               }\r
+       }\r
+\r
+       private String getBeanName(Object data, IConfigurationElement config) {\r
+               \r
+               // try the specific bean id the extension defines\r
+               if (data != null && data.toString().length() > 0) {\r
+                       return data.toString();\r
+               }\r
+\r
+               // try the id of the config element\r
+               if (config.getAttribute("id") != null) {\r
+                       return config.getAttribute("id");\r
+               }\r
+\r
+               // try the id of the extension element itself\r
+               if (config.getParent() != null\r
+                               && config.getParent() instanceof IExtension) {\r
+                       IExtension extensionDefinition = (IExtension) config.getParent();\r
+                       return extensionDefinition.getSimpleIdentifier();\r
+               }\r
+\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