]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.equinox/src/main/java/org/argeo/slc/osgi/OsgiExecutionLauncher.java
1b911269be19f674e5146af28a9784b566dc63a6
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.equinox / src / main / java / org / argeo / slc / osgi / OsgiExecutionLauncher.java
1 package org.argeo.slc.osgi;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argeo.slc.SlcException;
6 import org.argeo.slc.execution.ExecutionFlowDescriptor;
7 import org.argeo.slc.execution.ExecutionModule;
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.BundleContext;
10 import org.osgi.framework.ServiceReference;
11 import org.springframework.context.ApplicationContext;
12 import org.springframework.osgi.context.BundleContextAware;
13
14 public class OsgiExecutionLauncher implements BundleContextAware {
15 private final static Log log = LogFactory
16 .getLog(OsgiExecutionLauncher.class);
17
18 private BundleContext bundleContext;
19 private BundlesManager bundlesManager;
20
21 public Launch findLaunch(String firstArg, String executionName) {
22 Launch launch = new Launch();
23
24 // String moduleName = null;
25
26 // First check whether we have a bundleId
27 Long bundleId = null;
28 try {
29 bundleId = Long.parseLong(firstArg);
30 } catch (NumberFormatException e) {
31 // silent
32 }
33
34 // Look for bundle names containing pattern
35 Bundle bundle = null;
36 if (bundleId != null) {
37 bundle = bundleContext.getBundle(bundleId);
38 } else {
39 for (Bundle b : bundleContext.getBundles()) {
40 if (b.getSymbolicName().contains(firstArg)) {
41 bundle = b;
42 break;
43 }
44 }
45 }
46
47 if (bundle != null) {
48 launch.setBundleId(bundle.getBundleId());
49 launch.setModuleName(bundle.getSymbolicName());
50 launch.setExecutionName(executionName);
51 return launch;
52 } else {
53 log
54 .warn("Could not find any execution module matching these requirements.");
55 return null;
56 }
57
58 }
59
60 public void launch(Launch launch) {
61 Bundle bundle = bundleContext.getBundle(launch.getBundleId());
62
63 // Find module
64 ExecutionModule module = null;
65 ServiceReference serviceRef = null;
66 try {
67 bundlesManager.stopSynchronous(bundle);
68 bundlesManager.updateSynchronous(bundle);
69 // Refresh in case there are fragments
70 bundlesManager.refreshSynchronous(bundle);
71 bundlesManager.startSynchronous(bundle);
72
73 String filter = "(Bundle-SymbolicName=" + launch.getModuleName()
74 + ")";
75 // Wait for application context to be ready
76 bundlesManager.getServiceRefSynchronous(ApplicationContext.class
77 .getName(), filter);
78
79 if (log.isDebugEnabled())
80 log.debug("Bundle " + bundle.getSymbolicName()
81 + " ready to be used at latest version.");
82
83 ServiceReference[] sfs = bundlesManager.getServiceRefSynchronous(
84 ExecutionModule.class.getName(), filter);
85
86 if (sfs.length > 1)
87 log
88 .warn("More than one execution module service found in module "
89 + launch.getModuleName());
90
91 if (sfs.length > 0) {
92 serviceRef = sfs[0];
93 module = (ExecutionModule) bundleContext.getService(serviceRef);
94 }
95
96 if (module != null) {
97 ExecutionFlowDescriptor descriptor = new ExecutionFlowDescriptor();
98 descriptor.setName(launch.getExecutionName());
99 module.execute(descriptor);
100 log.info("Executed " + launch.getExecutionName() + " from "
101 + launch.getModuleName());
102 }
103
104 } catch (Exception e) {
105 throw new SlcException("Cannot launch " + launch, e);
106 } finally {
107 if (serviceRef != null)
108 bundleContext.ungetService(serviceRef);
109 }
110 }
111
112 public void setBundleContext(BundleContext bundleContext) {
113 this.bundleContext = bundleContext;
114 }
115
116 public void setBundlesManager(BundlesManager bundlesManager) {
117 this.bundlesManager = bundlesManager;
118 }
119
120 public static class Launch {
121 private Long bundleId;
122 private String moduleName;
123 private String executionName;
124
125 public Long getBundleId() {
126 return bundleId;
127 }
128
129 public void setBundleId(Long bundleId) {
130 this.bundleId = bundleId;
131 }
132
133 public String getModuleName() {
134 return moduleName;
135 }
136
137 public void setModuleName(String moduleName) {
138 this.moduleName = moduleName;
139 }
140
141 public String getExecutionName() {
142 return executionName;
143 }
144
145 public void setExecutionName(String executionName) {
146 this.executionName = executionName;
147 }
148
149 @Override
150 public String toString() {
151 return moduleName + " " + executionName;
152 }
153
154 }
155 }