]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringCommandHandler.java
Deprecate TreeObject (not used anywhere anymore)
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / spring / SpringCommandHandler.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.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.argeo.ArgeoException;
22 import org.eclipse.core.commands.ExecutionEvent;
23 import org.eclipse.core.commands.ExecutionException;
24 import org.eclipse.core.commands.IHandler;
25 import org.eclipse.core.commands.IHandlerListener;
26 import org.springframework.context.ApplicationContext;
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 // TODO: make it more flexible and robust
47 ApplicationContext applicationContext = ApplicationContextTracker
48 .getApplicationContext(bundleSymbolicName);
49 if (applicationContext == null)
50 throw new ArgeoException("No application context found for "
51 + bundleSymbolicName);
52
53 // retrieve the command via its id
54 String beanName = event.getCommand().getId();
55
56 if (!applicationContext.containsBean(beanName)) {
57 if (beanName.startsWith(bundleSymbolicName))
58 beanName = beanName
59 .substring(bundleSymbolicName.length() + 1);
60 }
61
62 if (!applicationContext.containsBean(beanName))
63 throw new ExecutionException("No bean found with name "
64 + beanName + " in bundle " + bundleSymbolicName);
65 Object bean = applicationContext.getBean(beanName);
66
67 if (!(bean instanceof IHandler))
68 throw new ExecutionException("Bean with name " + beanName
69 + " and class " + bean.getClass()
70 + " does not implement the IHandler interface.");
71
72 IHandler handler = (IHandler) bean;
73 return handler.execute(event);
74 } catch (Exception e) {
75 // TODO: use eclipse error management
76 // log.error(e);
77 throw new ExecutionException("Cannot execute Spring command "
78 + commandId + " in bundle " + bundleSymbolicName, e);
79 }
80 }
81
82 public boolean isEnabled() {
83 return true;
84 }
85
86 public boolean isHandled() {
87 return true;
88 }
89
90 public void removeHandlerListener(IHandlerListener handlerListener) {
91 }
92
93 }