Move Eclipse 3 components to legacy.
[lgpl/argeo-commons.git] / legacy / org.argeo.cms.ui.workbench / src / org / argeo / eclipse / spring / SpringExtensionFactory.java
diff --git a/legacy/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java b/legacy/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java
new file mode 100644 (file)
index 0000000..ab1e8ca
--- /dev/null
@@ -0,0 +1,113 @@
+/*\r
+ * Copyright (C) 2007-2012 Argeo GmbH\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.argeo.eclipse.spring;\r
+\r
+import org.argeo.eclipse.ui.EclipseUiException;\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.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
+ * original code from: <a href=\r
+ * "http://martinlippert.blogspot.com/2008/10/new-version-of-spring-extension-factory.html"\r
+ * >Blog entry</a>\r
+ * \r
+ * @author Martin Lippert\r
+ * @author mbaudier\r
+ */\r
+public class SpringExtensionFactory implements IExecutableExtensionFactory,\r
+               IExecutableExtension {\r
+\r
+       private Object bean;\r
+\r
+       public Object create() throws CoreException {\r
+               if (bean == null)\r
+                       throw new EclipseUiException("No underlying bean for extension");\r
+               return bean;\r
+       }\r
+\r
+       public void setInitializationData(IConfigurationElement config,\r
+                       String propertyName, Object data) throws CoreException {\r
+               String bundleSymbolicName = config.getContributor().getName();\r
+               ApplicationContext applicationContext = ApplicationContextTracker\r
+                               .getApplicationContext(bundleSymbolicName);\r
+               if (applicationContext == null)\r
+                       throw new EclipseUiException(\r
+                                       "Cannot find application context for bundle "\r
+                                                       + bundleSymbolicName);\r
+\r
+               String beanName = getBeanName(data, config);\r
+               if (beanName == null)\r
+                       throw new EclipseUiException("Cannot find bean name for extension "\r
+                                       + config);\r
+\r
+               if (!applicationContext.containsBean(beanName)) {\r
+                       if (beanName.startsWith(bundleSymbolicName))\r
+                               beanName = beanName.substring(bundleSymbolicName.length() + 1);\r
+               }\r
+\r
+               if (!applicationContext.containsBean(beanName))\r
+                       throw new EclipseUiException("No bean with name '" + beanName + "'");\r
+\r
+               this.bean = applicationContext.getBean(beanName);\r
+               if (this.bean instanceof IExecutableExtension) {\r
+                       ((IExecutableExtension) this.bean).setInitializationData(config,\r
+                                       propertyName, data);\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