1 package org
.argeo
.slc
.osgi
;
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
;
14 public class OsgiExecutionLauncher
implements BundleContextAware
{
15 private final static Log log
= LogFactory
16 .getLog(OsgiExecutionLauncher
.class);
18 private BundleContext bundleContext
;
19 private BundlesManager bundlesManager
;
21 public Launch
findLaunch(String firstArg
, String executionName
) {
22 Launch launch
= new Launch();
24 // String moduleName = null;
26 // First check whether we have a bundleId
29 bundleId
= Long
.parseLong(firstArg
);
30 } catch (NumberFormatException e
) {
34 // Look for bundle names containing pattern
36 if (bundleId
!= null) {
37 bundle
= bundleContext
.getBundle(bundleId
);
39 for (Bundle b
: bundleContext
.getBundles()) {
40 if (b
.getSymbolicName().contains(firstArg
)) {
48 launch
.setBundleId(bundle
.getBundleId());
49 launch
.setModuleName(bundle
.getSymbolicName());
50 launch
.setExecutionName(executionName
);
54 .warn("Could not find any execution module matching these requirements.");
60 public void launch(Launch launch
) {
61 Bundle bundle
= bundleContext
.getBundle(launch
.getBundleId());
64 ExecutionModule module
= null;
65 ServiceReference serviceRef
= null;
67 bundlesManager
.stopSynchronous(bundle
);
68 bundlesManager
.updateSynchronous(bundle
);
69 // Refresh in case there are fragments
70 bundlesManager
.refreshSynchronous(bundle
);
71 bundlesManager
.startSynchronous(bundle
);
73 String filter
= "(Bundle-SymbolicName=" + launch
.getModuleName()
75 // Wait for application context to be ready
76 bundlesManager
.getServiceRefSynchronous(ApplicationContext
.class
79 if (log
.isDebugEnabled())
80 log
.debug("Bundle " + bundle
.getSymbolicName()
81 + " ready to be used at latest version.");
83 ServiceReference
[] sfs
= bundlesManager
.getServiceRefSynchronous(
84 ExecutionModule
.class.getName(), filter
);
88 .warn("More than one execution module service found in module "
89 + launch
.getModuleName());
93 module
= (ExecutionModule
) bundleContext
.getService(serviceRef
);
97 ExecutionFlowDescriptor descriptor
= new ExecutionFlowDescriptor();
98 descriptor
.setName(launch
.getExecutionName());
99 module
.execute(descriptor
);
100 log
.info("Executed " + launch
.getExecutionName() + " from "
101 + launch
.getExecutionName());
104 } catch (Exception e
) {
105 throw new SlcException("Cannot launch " + launch
, e
);
107 if (serviceRef
!= null)
108 bundleContext
.ungetService(serviceRef
);
112 public void setBundleContext(BundleContext bundleContext
) {
113 this.bundleContext
= bundleContext
;
116 public void setBundlesManager(BundlesManager bundlesManager
) {
117 this.bundlesManager
= bundlesManager
;
120 public static class Launch
{
121 private Long bundleId
;
122 private String moduleName
;
123 private String executionName
;
125 public Long
getBundleId() {
129 public void setBundleId(Long bundleId
) {
130 this.bundleId
= bundleId
;
133 public String
getModuleName() {
137 public void setModuleName(String moduleName
) {
138 this.moduleName
= moduleName
;
141 public String
getExecutionName() {
142 return executionName
;
145 public void setExecutionName(String executionName
) {
146 this.executionName
= executionName
;
150 public String
toString() {
151 return moduleName
+ " " + executionName
;