2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.spring
;
18 import org
.argeo
.ArgeoException
;
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
;
27 * The Spring Extension Factory builds a bridge between the Eclipse Extension
28 * Registry and the Spring Framework (especially Spring Dynamic Modules).
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.
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.
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
44 * original code from: <a href=
45 * "http://martinlippert.blogspot.com/2008/10/new-version-of-spring-extension-factory.html"
48 * @author Martin Lippert
51 public class SpringExtensionFactory
implements IExecutableExtensionFactory
,
52 IExecutableExtension
{
56 public Object
create() throws CoreException
{
58 throw new ArgeoException("No underlying bean for extension");
62 public void setInitializationData(IConfigurationElement config
,
63 String propertyName
, Object data
) throws CoreException
{
64 String bundleSymbolicName
= config
.getContributor().getName();
65 ApplicationContext applicationContext
= ApplicationContextTracker
66 .getApplicationContext(bundleSymbolicName
);
67 if (applicationContext
== null)
68 throw new ArgeoException(
69 "Cannot find application context for bundle "
70 + bundleSymbolicName
);
72 String beanName
= getBeanName(data
, config
);
74 throw new ArgeoException("Cannot find bean name for extension "
77 if (!applicationContext
.containsBean(beanName
)) {
78 if (beanName
.startsWith(bundleSymbolicName
))
79 beanName
= beanName
.substring(bundleSymbolicName
.length() + 1);
82 if (!applicationContext
.containsBean(beanName
))
83 throw new ArgeoException("No bean with name '" + beanName
+ "'");
85 this.bean
= applicationContext
.getBean(beanName
);
86 if (this.bean
instanceof IExecutableExtension
) {
87 ((IExecutableExtension
) this.bean
).setInitializationData(config
,
92 private String
getBeanName(Object data
, IConfigurationElement config
) {
94 // try the specific bean id the extension defines
95 if (data
!= null && data
.toString().length() > 0) {
96 return data
.toString();
99 // try the id of the config element
100 if (config
.getAttribute("id") != null) {
101 return config
.getAttribute("id");
104 // try the id of the extension element itself
105 if (config
.getParent() != null
106 && config
.getParent() instanceof IExtension
) {
107 IExtension extensionDefinition
= (IExtension
) config
.getParent();
108 return extensionDefinition
.getSimpleIdentifier();