]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java
Deprecate TreeObject (not used anywhere anymore)
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / spring / SpringExtensionFactory.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.eclipse.spring;
18
19 import org.argeo.ArgeoException;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExecutableExtension;
23 import org.eclipse.core.runtime.IExecutableExtensionFactory;
24 import org.eclipse.core.runtime.IExtension;
25 import org.springframework.context.ApplicationContext;
26
27 /**
28 * The Spring Extension Factory builds a bridge between the Eclipse Extension
29 * Registry and the Spring Framework (especially Spring Dynamic Modules).
30 *
31 * It allows you to define your extension as a spring bean within the spring
32 * application context of your bundle. If you would like to use this bean as an
33 * instance of an extension (an Eclipse RCP view, for example) you define the
34 * extension with this spring extension factory as the class to be created.
35 *
36 * To let the spring extension factory pick the right bean from your application
37 * context you need to set the bean id to the same value as the id of the view
38 * within the view definition, for example. This is important if your extension
39 * definition contains more than one element, where each element has its own id.
40 *
41 * If the extension definition elements themselves have no id attribute the
42 * spring extension factory uses the id of the extension itself to identify the
43 * bean.
44 *
45 * original code from: <a href=
46 * "http://martinlippert.blogspot.com/2008/10/new-version-of-spring-extension-factory.html"
47 * >Blog entry</a>
48 *
49 * @author Martin Lippert
50 * @author mbaudier
51 */
52 public class SpringExtensionFactory implements IExecutableExtensionFactory,
53 IExecutableExtension {
54
55 private Object bean;
56
57 public Object create() throws CoreException {
58 if (bean == null)
59 throw new ArgeoException("No underlying bean for extension");
60 return bean;
61 }
62
63 public void setInitializationData(IConfigurationElement config,
64 String propertyName, Object data) throws CoreException {
65 String bundleSymbolicName = config.getContributor().getName();
66 ApplicationContext applicationContext = ApplicationContextTracker
67 .getApplicationContext(bundleSymbolicName);
68 if (applicationContext == null)
69 throw new ArgeoException(
70 "Cannot find application context for bundle "
71 + bundleSymbolicName);
72
73 String beanName = getBeanName(data, config);
74 if (beanName == null)
75 throw new ArgeoException("Cannot find bean name for extension "
76 + config);
77
78 if (!applicationContext.containsBean(beanName)) {
79 if (beanName.startsWith(bundleSymbolicName))
80 beanName = beanName.substring(bundleSymbolicName.length() + 1);
81 }
82
83 if (!applicationContext.containsBean(beanName))
84 throw new ArgeoException("No bean with name '" + beanName + "'");
85
86 this.bean = applicationContext.getBean(beanName);
87 if (this.bean instanceof IExecutableExtension) {
88 ((IExecutableExtension) this.bean).setInitializationData(config,
89 propertyName, data);
90 }
91 }
92
93 private String getBeanName(Object data, IConfigurationElement config) {
94
95 // try the specific bean id the extension defines
96 if (data != null && data.toString().length() > 0) {
97 return data.toString();
98 }
99
100 // try the id of the config element
101 if (config.getAttribute("id") != null) {
102 return config.getAttribute("id");
103 }
104
105 // try the id of the extension element itself
106 if (config.getParent() != null
107 && config.getParent() instanceof IExtension) {
108 IExtension extensionDefinition = (IExtension) config.getParent();
109 return extensionDefinition.getSimpleIdentifier();
110 }
111
112 return null;
113 }
114
115 }