]> 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 security UI RAP
[lgpl/argeo-commons.git] / eclipse / plugins / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / spring / SpringExtensionFactory.java
index f92b423e9799e3f7e510301ee93999d508f3d089..49a226d39339bf15d5a7dffb3884f79709896897 100644 (file)
@@ -1,14 +1,27 @@
+/*\r
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\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
+\r
 package org.argeo.eclipse.spring;\r
 \r
-import org.argeo.eclipse.ui.ArgeoUiPlugin;\r
+import org.argeo.ArgeoException;\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
@@ -29,7 +42,12 @@ import org.springframework.context.ApplicationContext;
  * 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
@@ -37,25 +55,35 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory,
        private Object bean;\r
 \r
        public Object create() throws CoreException {\r
+               if (bean == null)\r
+                       throw new ArgeoException("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 beanName = getBeanName(data, config);\r
-               ApplicationContext appContext = getApplicationContext(config);\r
+               if (beanName == null)\r
+                       throw new ArgeoException("Cannot find bean name for extension "\r
+                                       + 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
+               String bundleSymbolicName = config.getContributor().getName();\r
+               ApplicationContext appContext = ApplicationContextTracker\r
+                               .getApplicationContext(bundleSymbolicName);\r
+               if (appContext == null)\r
+                       throw new ArgeoException(\r
+                                       "Cannot find application context for bundle "\r
+                                                       + bundleSymbolicName);\r
+\r
+               this.bean = appContext.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
+\r
                // try the specific bean id the extension defines\r
                if (data != null && data.toString().length() > 0) {\r
                        return data.toString();\r
@@ -76,30 +104,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