]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringCommandHandler.java
Update license headers
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / spring / SpringCommandHandler.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.eclipse.spring;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.ArgeoException;
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 public class SpringCommandHandler implements IHandler {
28 private final static Log log = LogFactory
29 .getLog(SpringCommandHandler.class);
30
31 public void addHandlerListener(IHandlerListener handlerListener) {
32 }
33
34 public void dispose() {
35 }
36
37 public Object execute(ExecutionEvent event) throws ExecutionException {
38 String commandId = event.getCommand().getId();
39 String bundleSymbolicName = commandId.substring(0,
40 commandId.lastIndexOf('.'));
41 try {
42 if (log.isTraceEnabled())
43 log.trace("Execute " + event + " via spring command handler "
44 + this);
45 // TODO: make it more flexible and robust
46 ApplicationContext applicationContext = ApplicationContextTracker
47 .getApplicationContext(bundleSymbolicName);
48 if (applicationContext == null)
49 throw new ArgeoException("No application context found for "
50 + bundleSymbolicName);
51
52 // retrieve the command via its id
53 String beanName = event.getCommand().getId();
54
55 if (!applicationContext.containsBean(beanName)) {
56 if (beanName.startsWith(bundleSymbolicName))
57 beanName = beanName
58 .substring(bundleSymbolicName.length() + 1);
59 }
60
61 if (!applicationContext.containsBean(beanName))
62 throw new ExecutionException("No bean found with name "
63 + beanName + " in bundle " + bundleSymbolicName);
64 Object bean = applicationContext.getBean(beanName);
65
66 if (!(bean instanceof IHandler))
67 throw new ExecutionException("Bean with name " + beanName
68 + " and class " + bean.getClass()
69 + " does not implement the IHandler interface.");
70
71 IHandler handler = (IHandler) bean;
72 return handler.execute(event);
73 } catch (Exception e) {
74 // TODO: use eclipse error management
75 // log.error(e);
76 throw new ExecutionException("Cannot execute Spring command "
77 + commandId + " in bundle " + bundleSymbolicName, e);
78 }
79 }
80
81 public boolean isEnabled() {
82 return true;
83 }
84
85 public boolean isHandled() {
86 return true;
87 }
88
89 public void removeHandlerListener(IHandlerListener handlerListener) {
90 }
91
92 }