]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
Reduce debug verbosity
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionModulesManager.java
1 package org.argeo.slc.osgi;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.slc.SlcException;
11 import org.argeo.slc.core.execution.AbstractExecutionModulesManager;
12 import org.argeo.slc.core.execution.DefaultDescriptorConverter;
13 import org.argeo.slc.execution.ExecutionContext;
14 import org.argeo.slc.execution.ExecutionFlow;
15 import org.argeo.slc.execution.ExecutionFlowDescriptor;
16 import org.argeo.slc.execution.ExecutionFlowDescriptorConverter;
17 import org.argeo.slc.execution.ExecutionModuleDescriptor;
18 import org.argeo.slc.process.RealizedFlow;
19 import org.osgi.framework.InvalidSyntaxException;
20 import org.osgi.framework.ServiceReference;
21 import org.osgi.util.tracker.ServiceTracker;
22 import org.springframework.beans.factory.DisposableBean;
23 import org.springframework.beans.factory.InitializingBean;
24
25 public class OsgiExecutionModulesManager extends AbstractExecutionModulesManager implements
26 InitializingBean, DisposableBean {
27 private final static Log log = LogFactory.getLog(OsgiExecutionModulesManager.class);
28
29 private BundlesManager bundlesManager;
30 private ServiceTracker executionContexts;
31 private ExecutionFlowDescriptorConverter defaultDescriptorConverter = new DefaultDescriptorConverter();
32
33 public ExecutionModuleDescriptor getExecutionModuleDescriptor(
34 String moduleName, String version) {
35 return createDescriptor(moduleName, version, listFlows(moduleName,
36 version));
37 }
38
39 public List<ExecutionModuleDescriptor> listExecutionModules() {
40 List<ExecutionModuleDescriptor> descriptors = new ArrayList<ExecutionModuleDescriptor>();
41
42 ServiceReference[] srs = executionContexts.getServiceReferences();
43 for (ServiceReference sr : srs) {
44 String moduleName = sr.getBundle().getSymbolicName();
45 String moduleVersion = sr.getBundle().getHeaders().get(
46 "Bundle-Version").toString();
47 ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
48 md.setName(moduleName);
49 md.setVersion(moduleVersion);
50 descriptors.add(md);
51 }
52 return descriptors;
53 }
54
55 protected Map<String, ExecutionFlow> listFlows(String moduleName,
56 String moduleVersion) {
57 // TODO: use service trackers?
58 // String filter = OsgiFilterUtils.unifyFilter(ExecutionFlow.class,
59 // null);
60
61 String filter = "(Bundle-SymbolicName=" + moduleName + ")";
62 ServiceReference[] sfs;
63 try {
64 sfs = bundlesManager.getBundleContext().getServiceReferences(
65 ExecutionFlow.class.getName(), filter);
66 } catch (InvalidSyntaxException e) {
67 throw new SlcException(
68 "Cannot retrieve service reference for flow " + filter, e);
69 }
70
71 Map<String, ExecutionFlow> flows = new HashMap<String, ExecutionFlow>();
72 for (ServiceReference sf : sfs) {
73 ExecutionFlow flow = (ExecutionFlow) bundlesManager
74 .getBundleContext().getService(sf);
75 flows.put(flow.getName(), flow);
76 }
77 return flows;
78 }
79
80 public ExecutionFlow findExecutionFlow(String moduleName,
81 String moduleVersion, String flowName) {
82 String filter = "(&(Bundle-SymbolicName=" + moduleName
83 + ")(org.springframework.osgi.bean.name=" + flowName + "))";
84 return bundlesManager.getSingleServiceStrict(ExecutionFlow.class,
85 filter);
86 }
87
88 public ExecutionContext findExecutionContext(String moduleName,
89 String moduleVersion) {
90 String filter = "(&(Bundle-SymbolicName=" + moduleName
91 + ")(Bundle-Version=" + moduleVersion + "))";
92 return bundlesManager.getSingleServiceStrict(ExecutionContext.class,
93 filter);
94 }
95
96 public ExecutionFlowDescriptorConverter findExecutionFlowDescriptorConverter(
97 String moduleName, String moduleVersion) {
98 String filter = "(&(Bundle-SymbolicName=" + moduleName
99 + ")(Bundle-Version=" + moduleVersion + "))";
100 return bundlesManager.getSingleService(
101 ExecutionFlowDescriptorConverter.class, filter);
102 }
103
104 public void setBundlesManager(BundlesManager bundlesManager) {
105 this.bundlesManager = bundlesManager;
106 }
107
108 public void afterPropertiesSet() throws Exception {
109 executionContexts = bundlesManager.newTracker(ExecutionContext.class);
110 }
111
112 public void destroy() throws Exception {
113 if (executionContexts != null)
114 executionContexts.close();
115 }
116
117 /**
118 * Builds a minimal realized flow, based on the provided information
119 * (typically from the command line).
120 *
121 * @param module
122 * a bundle id, or a pattern contained in a bundle symbolic name
123 * @param module
124 * the execution flow name
125 * @return a minimal realized flow, to be used in an execution
126 */
127 public RealizedFlow findRealizedFlow(String module, String executionName) {
128 // First check whether we have a bundleId
129 Long bundleId = null;
130 try {
131 bundleId = Long.parseLong(module);
132 } catch (NumberFormatException e) {
133 // silent
134 }
135
136 // Look for bundle names containing pattern
137 OsgiBundle bundle = null;
138 if (bundleId != null) {
139 bundle = bundlesManager.getBundle(bundleId);
140 } else {
141 bundle = bundlesManager.findFromPattern(module);
142 }
143
144 if (bundle != null) {
145 RealizedFlow launch = new RealizedFlow();
146 launch.setModuleName(bundle.getName());
147 launch.setModuleVersion(bundle.getVersion());
148 ExecutionFlowDescriptor descriptor = new ExecutionFlowDescriptor();
149 descriptor.setName(executionName);
150 launch.setFlowDescriptor(descriptor);
151 return launch;
152 } else {
153 log
154 .warn("Could not find any execution module matching these requirements.");
155 return null;
156 }
157 }
158
159 public void updateAndExecute(RealizedFlow realizedFlow) {
160 OsgiBundle osgiBundle = new OsgiBundle(realizedFlow);
161 bundlesManager.upgradeSynchronous(osgiBundle);
162 execute(realizedFlow);
163 }
164
165 public void execute(RealizedFlow realizedFlow) {
166 if (log.isTraceEnabled())
167 log.trace("Executing " + realizedFlow);
168
169 String moduleName = realizedFlow.getModuleName();
170 String moduleVersion = realizedFlow.getModuleVersion();
171
172 ExecutionContext executionContext = findExecutionContext(moduleName,
173 moduleVersion);
174
175 // Check whether a descriptor converter is published by this module
176 ExecutionFlowDescriptorConverter descriptorConverter = findExecutionFlowDescriptorConverter(
177 moduleName, moduleVersion);
178
179 final Map<? extends String, ? extends Object> variablesToAdd;
180 if (descriptorConverter != null)
181 variablesToAdd = descriptorConverter.convertValues(realizedFlow
182 .getFlowDescriptor());
183 else
184 variablesToAdd = defaultDescriptorConverter
185 .convertValues(realizedFlow.getFlowDescriptor());
186
187 executionContext.addVariables(variablesToAdd);
188
189 ExecutionFlow flow = findExecutionFlow(moduleName, moduleVersion,
190 realizedFlow.getFlowDescriptor().getName());
191
192 //
193 // Actually runs the flow, IN THIS THREAD
194 //
195 flow.run();
196 //
197 //
198 //
199 }
200
201 }