]> git.argeo.org Git - lgpl/argeo-commons.git/blob - spring/SpringExtensionFactory.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExecutableExtension;
22 import org.eclipse.core.runtime.IExecutableExtensionFactory;
23 import org.eclipse.core.runtime.IExtension;
24 import org.springframework.context.ApplicationContext;
25
26 /**
27 * The Spring Extension Factory builds a bridge between the Eclipse Extension
28 * Registry and the Spring Framework (especially Spring Dynamic Modules).
29 *
30 * It allows you to define your extension as a spring bean within the spring
31 * application context of your bundle. If you would like to use this bean as an
32 * instance of an extension (an Eclipse RCP view, for example) you define the
33 * extension with this spring extension factory as the class to be created.
34 *
35 * To let the spring extension factory pick the right bean from your application
36 * context you need to set the bean id to the same value as the id of the view
37 * within the view definition, for example. This is important if your extension
38 * definition contains more than one element, where each element has its own id.
39 *
40 * If the extension definition elements themselves have no id attribute the
41 * spring extension factory uses the id of the extension itself to identify the
42 * bean.
43 *
44 * original code from: <a href="http://martinlippert.blogspot.com/2008/10/new-version-of-spring-extension-factory.html"
45 * >Blog entry</a>
46 *
47 * @author Martin Lippert
48 * @author mbaudier
49 */
50 public class SpringExtensionFactory implements IExecutableExtensionFactory,
51 IExecutableExtension {
52
53 private Object bean;
54
55 public Object create() throws CoreException {
56 return bean;
57 }
58
59 public void setInitializationData(IConfigurationElement config,
60 String propertyName, Object data) throws CoreException {
61 String beanName = getBeanName(data, config);
62 ApplicationContext appContext = ApplicationContextTracker
63 .getApplicationContext(config.getContributor().getName());
64
65 if (beanName != null && appContext != null) {
66 this.bean = appContext.getBean(beanName);
67 if (this.bean instanceof IExecutableExtension) {
68 ((IExecutableExtension) this.bean).setInitializationData(
69 config, propertyName, data);
70 }
71 }
72 }
73
74 private String getBeanName(Object data, IConfigurationElement config) {
75
76 // try the specific bean id the extension defines
77 if (data != null && data.toString().length() > 0) {
78 return data.toString();
79 }
80
81 // try the id of the config element
82 if (config.getAttribute("id") != null) {
83 return config.getAttribute("id");
84 }
85
86 // try the id of the extension element itself
87 if (config.getParent() != null
88 && config.getParent() instanceof IExtension) {
89 IExtension extensionDefinition = (IExtension) config.getParent();
90 return extensionDefinition.getSimpleIdentifier();
91 }
92
93 return null;
94 }
95
96 }