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.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.eclipse.ui.EclipseUiException;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.core.commands.IHandler;
24 import org.eclipse.core.commands.IHandlerListener;
25 import org.springframework.context.ApplicationContext;
26
27
28 public class SpringCommandHandler implements IHandler {
29 private final static Log log = LogFactory
30 .getLog(SpringCommandHandler.class);
31
32 public void addHandlerListener(IHandlerListener handlerListener) {
33 }
34
35 public void dispose() {
36 }
37
38 public Object execute(ExecutionEvent event) throws ExecutionException {
39 String commandId = event.getCommand().getId();
40 String bundleSymbolicName = commandId.substring(0,
41 commandId.lastIndexOf('.'));
42 try {
43 if (log.isTraceEnabled())
44 log.trace("Execute " + event + " via spring command handler "
45 + this);
46
47 ApplicationContext applicationContext = ApplicationContextTracker
48 .getApplicationContext(bundleSymbolicName);
49 if (applicationContext == null)
50 throw new EclipseUiException(
51 "No application context found for "
52 + bundleSymbolicName);
53
54
55 String beanName = event.getCommand().getId();
56
57 if (!applicationContext.containsBean(beanName)) {
58 if (beanName.startsWith(bundleSymbolicName))
59 beanName = beanName
60 .substring(bundleSymbolicName.length() + 1);
61 }
62
63 if (!applicationContext.containsBean(beanName))
64 throw new ExecutionException("No bean found with name "
65 + beanName + " in bundle " + bundleSymbolicName);
66 Object bean = applicationContext.getBean(beanName);
67
68 if (!(bean instanceof IHandler))
69 throw new ExecutionException("Bean with name " + beanName
70 + " and class " + bean.getClass()
71 + " does not implement the IHandler interface.");
72
73 IHandler handler = (IHandler) bean;
74 return handler.execute(event);
75 } catch (Exception e) {
76
77
78 throw new ExecutionException("Cannot execute Spring command "
79 + commandId + " in bundle " + bundleSymbolicName, e);
80 }
81 }
82
83 public boolean isEnabled() {
84 return true;
85 }
86
87 public boolean isHandled() {
88 return true;
89 }
90
91 public void removeHandlerListener(IHandlerListener handlerListener) {
92 }
93 }