1 package org
.argeo
.slc
.equinox
;
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
.eclipse
.osgi
.framework
.console
.CommandInterpreter
;
9 import org
.eclipse
.osgi
.framework
.console
.CommandProvider
;
10 import org
.osgi
.framework
.Bundle
;
11 import org
.osgi
.framework
.BundleContext
;
12 import org
.osgi
.framework
.BundleException
;
13 import org
.osgi
.framework
.FrameworkEvent
;
14 import org
.osgi
.framework
.FrameworkListener
;
15 import org
.osgi
.framework
.InvalidSyntaxException
;
16 import org
.osgi
.framework
.ServiceReference
;
17 import org
.osgi
.service
.packageadmin
.PackageAdmin
;
18 import org
.springframework
.beans
.factory
.InitializingBean
;
19 import org
.springframework
.context
.ApplicationContext
;
20 import org
.springframework
.osgi
.context
.BundleContextAware
;
22 public class ExecutionCommandProvider
implements CommandProvider
,
23 BundleContextAware
, FrameworkListener
, InitializingBean
{
24 private final static Log log
= LogFactory
25 .getLog(ExecutionCommandProvider
.class);
27 private Long defaultTimeout
= 10000l;
29 private BundleContext bundleContext
;
31 private String lastModuleName
= null;
32 private String lastExecutionName
= null;
34 private final Object refreshedPackageSem
= new Object();
36 /** @deprecated Use slc command instead. */
37 public Object
_slc_exec(CommandInterpreter ci
) {
41 public Object
_slc(CommandInterpreter ci
) {
42 // TODO: check version
43 String firstArg
= ci
.nextArgument();
44 if (firstArg
== null) {
45 if (lastModuleName
!= null) {
46 String cmd
= "slc " + lastModuleName
+ " " + lastExecutionName
;
47 if (log
.isDebugEnabled())
48 log
.debug("Execute again last command: " + cmd
);
49 return ci
.execute(cmd
);
52 throw new SlcException("Command not properly formatted");
55 String executionName
= ci
.nextArgument();
57 String moduleName
= null;
59 // First check whether we have a bundleId
62 bundleId
= Long
.parseLong(firstArg
);
63 } catch (NumberFormatException e
) {
67 // Look for bundle names containing pattern
69 if (bundleId
!= null) {
70 bundle
= bundleContext
.getBundle(bundleId
);
72 for (Bundle b
: bundleContext
.getBundles()) {
73 if (b
.getSymbolicName().contains(firstArg
)) {
81 moduleName
= bundle
.getSymbolicName();
82 lastModuleName
= moduleName
;
83 lastExecutionName
= executionName
;
86 .warn("Could not find any execution module matching these requirements.");
91 ExecutionModule module
= null;
92 ServiceReference serviceRef
= null;
94 stopSynchronous(bundle
);
95 updateSynchronous(bundle
);
96 // Refresh in case there are fragments
97 refreshSynchronous(bundle
);
98 startSynchronous(bundle
);
100 String filter
= "(Bundle-SymbolicName=" + moduleName
+ ")";
101 // Wait for application context to be ready
102 getServiceRefSynchronous(ApplicationContext
.class.getName(), filter
);
104 if (log
.isDebugEnabled())
105 log
.debug("Bundle " + bundle
.getSymbolicName()
106 + " ready to be used at latest version.");
108 ServiceReference
[] sfs
= getServiceRefSynchronous(
109 ExecutionModule
.class.getName(), filter
);
113 .warn("More than one execution module service found in module "
116 if (sfs
.length
> 0) {
118 module
= (ExecutionModule
) bundleContext
.getService(serviceRef
);
121 if (module
!= null) {
122 ExecutionFlowDescriptor descriptor
= new ExecutionFlowDescriptor();
123 descriptor
.setName(executionName
);
124 module
.execute(descriptor
);
125 log
.info("Executed " + executionName
+ " from " + moduleName
);
128 } catch (Exception e
) {
129 throw new SlcException("Cannot find or update module.", e
);
131 if (serviceRef
!= null)
132 bundleContext
.ungetService(serviceRef
);
135 return "COMMAND COMPLETED";
138 public String
getHelp() {
139 StringBuffer buf
= new StringBuffer();
140 buf
.append("---SLC Execution Commands---\n");
142 .append("\tslc (<id>|<segment of bsn>) <execution bean> - execute an execution flow (without arg, execute last)\n");
143 return buf
.toString();
147 /** Updates bundle synchronously. */
148 protected void updateSynchronous(Bundle bundle
) throws BundleException
{
149 // int originalState = bundle.getState();
151 boolean waiting
= true;
153 long begin
= System
.currentTimeMillis();
155 int state
= bundle
.getState();
156 if (state
== Bundle
.INSTALLED
|| state
== Bundle
.ACTIVE
157 || state
== Bundle
.RESOLVED
)
161 if (System
.currentTimeMillis() - begin
> defaultTimeout
)
162 throw new SlcException("Update of bundle "
163 + bundle
.getSymbolicName()
164 + " timed out. Bundle state = " + bundle
.getState());
167 if (log
.isTraceEnabled())
168 log
.debug("Bundle " + bundle
.getSymbolicName() + " updated.");
171 /** Starts bundle synchronously. Does nothing if already started. */
172 protected void startSynchronous(Bundle bundle
) throws BundleException
{
173 int originalState
= bundle
.getState();
174 if (originalState
== Bundle
.ACTIVE
)
178 boolean waiting
= true;
180 long begin
= System
.currentTimeMillis();
182 if (bundle
.getState() == Bundle
.ACTIVE
)
186 if (System
.currentTimeMillis() - begin
> defaultTimeout
)
187 throw new SlcException("Start of bundle "
188 + bundle
.getSymbolicName()
189 + " timed out. Bundle state = " + bundle
.getState());
192 if (log
.isTraceEnabled())
193 log
.debug("Bundle " + bundle
.getSymbolicName() + " started.");
196 /** Stops bundle synchronously. Does nothing if already started. */
197 protected void stopSynchronous(Bundle bundle
) throws BundleException
{
198 int originalState
= bundle
.getState();
199 if (originalState
!= Bundle
.ACTIVE
)
203 boolean waiting
= true;
205 long begin
= System
.currentTimeMillis();
207 if (bundle
.getState() != Bundle
.ACTIVE
208 && bundle
.getState() != Bundle
.STOPPING
)
212 if (System
.currentTimeMillis() - begin
> defaultTimeout
)
213 throw new SlcException("Stop of bundle "
214 + bundle
.getSymbolicName()
215 + " timed out. Bundle state = " + bundle
.getState());
218 if (log
.isTraceEnabled())
219 log
.debug("Bundle " + bundle
.getSymbolicName() + " stopped.");
222 /** Refresh bundle synchronously. Does nothing if already started. */
223 protected void refreshSynchronous(Bundle bundle
) throws BundleException
{
224 ServiceReference packageAdminRef
= bundleContext
225 .getServiceReference(PackageAdmin
.class.getName());
226 PackageAdmin packageAdmin
= (PackageAdmin
) bundleContext
227 .getService(packageAdminRef
);
228 Bundle
[] bundles
= { bundle
};
229 packageAdmin
.refreshPackages(bundles
);
231 synchronized (refreshedPackageSem
) {
233 refreshedPackageSem
.wait(defaultTimeout
);
234 } catch (InterruptedException e
) {
239 if (log
.isTraceEnabled())
240 log
.debug("Bundle " + bundle
.getSymbolicName() + " refreshed.");
243 public void frameworkEvent(FrameworkEvent event
) {
244 if (event
.getType() == FrameworkEvent
.PACKAGES_REFRESHED
) {
245 synchronized (refreshedPackageSem
) {
246 refreshedPackageSem
.notifyAll();
251 protected ServiceReference
[] getServiceRefSynchronous(String clss
,
252 String filter
) throws InvalidSyntaxException
{
253 if (log
.isTraceEnabled())
254 log
.debug("Filter: '" + filter
+ "'");
255 ServiceReference
[] sfs
= null;
256 boolean waiting
= true;
257 long begin
= System
.currentTimeMillis();
259 sfs
= bundleContext
.getServiceReferences(clss
, filter
);
265 if (System
.currentTimeMillis() - begin
> defaultTimeout
)
266 throw new SlcException("Search of services " + clss
267 + " with filter " + filter
+ " timed out.");
273 protected void sleep(long ms
) {
276 } catch (InterruptedException e
) {
281 public void setBundleContext(BundleContext bundleContext
) {
282 this.bundleContext
= bundleContext
;
285 public void afterPropertiesSet() throws Exception
{
286 bundleContext
.addFrameworkListener(this);
289 public void setDefaultTimeout(Long defaultTimeout
) {
290 this.defaultTimeout
= defaultTimeout
;