1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.argeo.eclipse.spring;
17  
18  import org.argeo.eclipse.ui.EclipseUiException;
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  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  public class SpringExtensionFactory implements IExecutableExtensionFactory,
52  		IExecutableExtension {
53  
54  	private Object bean;
55  
56  	public Object create() throws CoreException {
57  		if (bean == null)
58  			throw new EclipseUiException("No underlying bean for extension");
59  		return bean;
60  	}
61  
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 EclipseUiException(
69  					"Cannot find application context for bundle "
70  							+ bundleSymbolicName);
71  
72  		String beanName = getBeanName(data, config);
73  		if (beanName == null)
74  			throw new EclipseUiException("Cannot find bean name for extension "
75  					+ config);
76  
77  		if (!applicationContext.containsBean(beanName)) {
78  			if (beanName.startsWith(bundleSymbolicName))
79  				beanName = beanName.substring(bundleSymbolicName.length() + 1);
80  		}
81  
82  		if (!applicationContext.containsBean(beanName))
83  			throw new EclipseUiException("No bean with name '" + beanName + "'");
84  
85  		this.bean = applicationContext.getBean(beanName);
86  		if (this.bean instanceof IExecutableExtension) {
87  			((IExecutableExtension) this.bean).setInitializationData(config,
88  					propertyName, data);
89  		}
90  	}
91  
92  	private String getBeanName(Object data, IConfigurationElement config) {
93  
94  		
95  		if (data != null && data.toString().length() > 0) {
96  			return data.toString();
97  		}
98  
99  		
100 		if (config.getAttribute("id") != null) {
101 			return config.getAttribute("id");
102 		}
103 
104 		
105 		if (config.getParent() != null
106 				&& config.getParent() instanceof IExtension) {
107 			IExtension extensionDefinition = (IExtension) config.getParent();
108 			return extensionDefinition.getSimpleIdentifier();
109 		}
110 
111 		return null;
112 	}
113 }