2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.osgi
;
19 import java
.lang
.management
.ManagementFactory
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashMap
;
22 import java
.util
.HashSet
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
28 import javax
.management
.MBeanServer
;
29 import javax
.management
.ObjectName
;
30 import javax
.management
.StandardMBean
;
32 import org
.apache
.commons
.logging
.Log
;
33 import org
.apache
.commons
.logging
.LogFactory
;
34 import org
.argeo
.slc
.BasicNameVersion
;
35 import org
.argeo
.slc
.NameVersion
;
36 import org
.argeo
.slc
.SlcException
;
37 import org
.argeo
.slc
.core
.execution
.AbstractExecutionModulesManager
;
38 import org
.argeo
.slc
.core
.execution
.DefaultExecutionFlowDescriptorConverter
;
39 import org
.argeo
.slc
.deploy
.Module
;
40 import org
.argeo
.slc
.deploy
.ModuleDescriptor
;
41 import org
.argeo
.slc
.execution
.ExecutionContext
;
42 import org
.argeo
.slc
.execution
.ExecutionFlow
;
43 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
44 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptorConverter
;
45 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
46 import org
.argeo
.slc
.execution
.ExecutionModulesListener
;
47 import org
.argeo
.slc
.process
.RealizedFlow
;
48 import org
.osgi
.framework
.Bundle
;
49 import org
.osgi
.framework
.BundleException
;
50 import org
.osgi
.framework
.Constants
;
51 import org
.springframework
.osgi
.service
.importer
.OsgiServiceLifecycleListener
;
53 /** Execution modules manager implementation based on an OSGi runtime. */
54 public class OsgiExecutionModulesManager
extends
55 AbstractExecutionModulesManager
implements OsgiServiceLifecycleListener
{
57 private final static Log log
= LogFactory
58 .getLog(OsgiExecutionModulesManager
.class);
60 private BundlesManager bundlesManager
;
61 private Map
<OsgiBundle
, ExecutionContext
> executionContexts
= new HashMap
<OsgiBundle
, ExecutionContext
>();
62 private Map
<OsgiBundle
, ExecutionFlowDescriptorConverter
> executionFlowDescriptorConverters
= new HashMap
<OsgiBundle
, ExecutionFlowDescriptorConverter
>();
63 private Map
<OsgiBundle
, Set
<ExecutionFlow
>> executionFlows
= new HashMap
<OsgiBundle
, Set
<ExecutionFlow
>>();
64 private ExecutionFlowDescriptorConverter defaultDescriptorConverter
= new DefaultExecutionFlowDescriptorConverter();
66 private List
<ExecutionModulesListener
> executionModulesListeners
= new ArrayList
<ExecutionModulesListener
>();
68 private Boolean registerFlowsToJmx
= true;
70 public synchronized ExecutionModuleDescriptor
getExecutionModuleDescriptor(
71 String moduleName
, String version
) {
72 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
73 OsgiBundle osgiBundle
= null;
74 BasicNameVersion nameVersion
= new BasicNameVersion(moduleName
, version
);
75 bundles
: for (Iterator
<OsgiBundle
> iterator
= executionContexts
76 .keySet().iterator(); iterator
.hasNext();) {
77 OsgiBundle ob
= iterator
.next();
78 if (ob
.equals(nameVersion
)) {
83 if (osgiBundle
== null)
84 throw new SlcException("No execution module registered for "
86 md
.setName(osgiBundle
.getName());
87 md
.setVersion(osgiBundle
.getVersion());
88 md
.setTitle(osgiBundle
.getTitle());
89 md
.setDescription(osgiBundle
.getDescription());
91 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= getExecutionFlowDescriptorConverter(
93 if (executionFlowDescriptorConverter
== null)
94 throw new SlcException("No flow converter found.");
95 executionFlowDescriptorConverter
.addFlowsToDescriptor(md
,
96 listFlows(moduleName
, version
));
100 public synchronized List
<ExecutionModuleDescriptor
> listExecutionModules() {
101 List
<ExecutionModuleDescriptor
> descriptors
= new ArrayList
<ExecutionModuleDescriptor
>();
103 for (Iterator
<OsgiBundle
> iterator
= executionContexts
.keySet()
104 .iterator(); iterator
.hasNext();) {
105 OsgiBundle osgiBundle
= iterator
.next();
106 ExecutionModuleDescriptor md
= new ExecutionModuleDescriptor();
107 setMetadataFromBundle(md
,
108 bundlesManager
.findRelatedBundle(osgiBundle
));
114 protected synchronized Map
<String
, ExecutionFlow
> listFlows(
115 String moduleName
, String moduleVersion
) {
117 Map
<String
, ExecutionFlow
> flows
= new HashMap
<String
, ExecutionFlow
>();
118 OsgiBundle key
= new OsgiBundle(moduleName
, moduleVersion
);
119 if (!executionFlows
.containsKey(key
))
121 Set
<ExecutionFlow
> flowsT
= executionFlows
.get(key
);
122 for (ExecutionFlow flow
: flowsT
)
123 flows
.put(flow
.getName(), flow
);
127 protected ExecutionFlow
findExecutionFlow(String moduleName
,
128 String moduleVersion
, String flowName
) {
129 String filter
= "(&(Bundle-SymbolicName=" + moduleName
130 + ")(org.springframework.osgi.bean.name=" + flowName
+ "))";
131 return bundlesManager
.getSingleServiceStrict(ExecutionFlow
.class,
135 protected ExecutionContext
findExecutionContext(String moduleName
,
136 String moduleVersion
) {
137 String filter
= "(&(Bundle-SymbolicName=" + moduleName
138 + ")(Bundle-Version=" + moduleVersion
+ "))";
139 return bundlesManager
.getSingleServiceStrict(ExecutionContext
.class,
143 protected ExecutionFlowDescriptorConverter
findExecutionFlowDescriptorConverter(
144 String moduleName
, String moduleVersion
) {
146 String filter
= "(&(Bundle-SymbolicName=" + moduleName
147 + ")(Bundle-Version=" + moduleVersion
+ "))";
148 return bundlesManager
.getSingleService(
149 ExecutionFlowDescriptorConverter
.class, filter
);
153 * Builds a minimal realized flow, based on the provided information
154 * (typically from the command line).
157 * a bundle id, or a pattern contained in a bundle symbolic name
159 * the execution flow name
160 * @return a minimal realized flow, to be used in an execution
162 public RealizedFlow
findRealizedFlow(String module
, String executionName
) {
163 // First check whether we have a bundleId
164 Long bundleId
= null;
166 bundleId
= Long
.parseLong(module
);
167 } catch (NumberFormatException e
) {
171 // Look for bundle names containing pattern
172 OsgiBundle bundle
= null;
173 if (bundleId
!= null) {
174 bundle
= bundlesManager
.getBundle(bundleId
);
176 bundle
= bundlesManager
.findFromPattern(module
);
179 if (bundle
!= null) {
180 RealizedFlow launch
= new RealizedFlow();
181 launch
.setModuleName(bundle
.getName());
182 launch
.setModuleVersion(bundle
.getVersion());
183 ExecutionFlowDescriptor descriptor
= new ExecutionFlowDescriptor();
184 descriptor
.setName(executionName
);
185 launch
.setFlowDescriptor(descriptor
);
188 log
.warn("Could not find any execution module matching these requirements.");
193 public void upgrade(NameVersion nameVersion
) {
194 OsgiBundle osgiBundle
= new OsgiBundle(nameVersion
);
195 bundlesManager
.upgradeSynchronous(osgiBundle
);
198 protected synchronized ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
199 String moduleName
, String moduleVersion
) {
200 OsgiBundle osgiBundle
= new OsgiBundle(moduleName
, moduleVersion
);
201 return getExecutionFlowDescriptorConverter(osgiBundle
);
204 protected synchronized ExecutionFlowDescriptorConverter
getExecutionFlowDescriptorConverter(
205 OsgiBundle osgiBundle
) {
206 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
))
207 return executionFlowDescriptorConverters
.get(osgiBundle
);
209 return defaultDescriptorConverter
;
212 public ModuleDescriptor
getModuleDescriptor(String moduleName
,
214 return getExecutionModuleDescriptor(moduleName
, version
);
217 public List
<ModuleDescriptor
> listModules() {
218 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
219 List
<ModuleDescriptor
> lst
= new ArrayList
<ModuleDescriptor
>();
220 for (Bundle bundle
: bundles
) {
221 ModuleDescriptor moduleDescriptor
= new ModuleDescriptor();
222 setMetadataFromBundle(moduleDescriptor
, bundle
);
223 lst
.add(moduleDescriptor
);
228 public void start(NameVersion nameVersion
) {
230 Bundle bundle
= bundlesManager
.findRelatedBundle(new OsgiBundle(
232 bundlesManager
.startSynchronous(bundle
);
233 } catch (BundleException e
) {
234 throw new SlcException("Cannot start " + nameVersion
, e
);
238 public void stop(NameVersion nameVersion
) {
240 Bundle bundle
= bundlesManager
.findRelatedBundle(new OsgiBundle(
242 bundlesManager
.stopSynchronous(bundle
);
243 } catch (BundleException e
) {
244 throw new SlcException("Cannot stop " + nameVersion
, e
);
248 protected void setMetadataFromBundle(ModuleDescriptor md
, Bundle bundle
) {
251 if (md
.getName() == null || md
.getVersion() == null)
252 throw new SlcException("Name and version not available.");
254 Bundle
[] bundles
= bundlesManager
.getBundleContext().getBundles();
255 for (Bundle b
: bundles
) {
256 if (b
.getSymbolicName().equals(md
.getName())
257 && md
.getVersion().equals(
258 getHeaderSafe(b
, Constants
.BUNDLE_VERSION
))) {
267 throw new SlcException("Cannot find bundle.");
269 md
.setName(bdl
.getSymbolicName());
270 md
.setVersion(getHeaderSafe(bdl
, Constants
.BUNDLE_VERSION
));
271 md
.setTitle(getHeaderSafe(bdl
, Constants
.BUNDLE_NAME
));
272 md
.setDescription(getHeaderSafe(bdl
, Constants
.BUNDLE_DESCRIPTION
));
275 private String
getHeaderSafe(Bundle bundle
, Object key
) {
276 Object obj
= bundle
.getHeaders().get(key
);
280 return obj
.toString();
287 /** Registers an execution context. */
288 public synchronized void register(ExecutionContext executionContext
,
289 Map
<String
, String
> properties
) {
290 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
291 Bundle bundle
= bundlesManager
.findRelatedBundle(osgiBundle
);
292 osgiBundle
.setTitle(getHeaderSafe(bundle
, Constants
.BUNDLE_NAME
));
293 osgiBundle
.setDescription(getHeaderSafe(bundle
,
294 Constants
.BUNDLE_DESCRIPTION
));
295 executionContexts
.put(osgiBundle
, executionContext
);
296 if (log
.isTraceEnabled())
297 log
.trace("Registered execution context from " + osgiBundle
);
299 for (ExecutionModulesListener listener
: executionModulesListeners
)
300 listener
.executionModuleAdded(osgiBundle
.getModuleDescriptor());
303 /** Unregisters an execution context. */
304 public synchronized void unregister(ExecutionContext executionContext
,
305 Map
<String
, String
> properties
) {
306 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
307 if (executionContexts
.containsKey(osgiBundle
)) {
308 executionContexts
.remove(osgiBundle
);
309 if (log
.isTraceEnabled())
310 log
.trace("Removed execution context from " + osgiBundle
);
312 for (ExecutionModulesListener listener
: executionModulesListeners
)
313 listener
.executionModuleRemoved(osgiBundle
314 .getModuleDescriptor());
318 /** Registers an execution flow. */
319 public synchronized void register(ExecutionFlow executionFlow
,
320 Map
<String
, String
> properties
) {
321 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
322 if (!executionFlows
.containsKey(osgiBundle
)) {
323 executionFlows
.put(osgiBundle
, new HashSet
<ExecutionFlow
>());
325 executionFlows
.get(osgiBundle
).add(executionFlow
);
326 if (log
.isTraceEnabled())
327 log
.trace("Registered " + executionFlow
+ " from " + osgiBundle
);
330 if (registerFlowsToJmx
)
331 registerMBean(osgiBundle
, executionFlow
);
332 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
333 for (ExecutionModulesListener listener
: executionModulesListeners
)
334 listener
.executionFlowAdded(osgiBundle
.getModuleDescriptor(),
335 efdc
.getExecutionFlowDescriptor(executionFlow
));
338 /** Unregisters an execution flow. */
339 public synchronized void unregister(ExecutionFlow executionFlow
,
340 Map
<String
, String
> properties
) {
341 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
342 if (executionFlows
.containsKey(osgiBundle
)) {
343 Set
<ExecutionFlow
> flows
= executionFlows
.get(osgiBundle
);
344 flows
.remove(executionFlow
);
345 if (log
.isTraceEnabled())
346 log
.trace("Removed " + executionFlow
+ " from " + osgiBundle
);
347 if (flows
.size() == 0) {
348 executionFlows
.remove(osgiBundle
);
349 if (log
.isTraceEnabled())
350 log
.trace("Removed flows set from " + osgiBundle
);
354 if (registerFlowsToJmx
)
355 unregisterMBean(osgiBundle
, executionFlow
);
356 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
357 for (ExecutionModulesListener listener
: executionModulesListeners
)
358 listener
.executionFlowRemoved(osgiBundle
.getModuleDescriptor(),
359 efdc
.getExecutionFlowDescriptor(executionFlow
));
363 /** Registers an execution module listener. */
364 public synchronized void register(
365 ExecutionModulesListener executionModulesListener
,
366 Map
<String
, String
> properties
) {
367 // sync with current state
368 for (OsgiBundle osgiBundle
: executionContexts
.keySet()) {
369 executionModulesListener
.executionModuleAdded(osgiBundle
370 .getModuleDescriptor());
372 for (OsgiBundle osgiBundle
: executionFlows
.keySet()) {
373 ExecutionFlowDescriptorConverter efdc
= getExecutionFlowDescriptorConverter(osgiBundle
);
374 for (ExecutionFlow executionFlow
: executionFlows
.get(osgiBundle
))
375 executionModulesListener
.executionFlowAdded(
376 osgiBundle
.getModuleDescriptor(),
377 efdc
.getExecutionFlowDescriptor(executionFlow
));
379 executionModulesListeners
.add(executionModulesListener
);
382 /** Unregisters an execution module listener. */
383 public synchronized void unregister(
384 ExecutionModulesListener executionModulesListener
,
385 Map
<String
, String
> properties
) {
386 executionModulesListeners
.remove(executionModulesListener
);
389 @SuppressWarnings({ "rawtypes" })
390 public synchronized void bind(Object service
, Map properties
)
392 if (service
instanceof ExecutionFlowDescriptorConverter
) {
393 ExecutionFlowDescriptorConverter executionFlowDescriptorConverter
= (ExecutionFlowDescriptorConverter
) service
;
394 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
395 executionFlowDescriptorConverters
.put(osgiBundle
,
396 executionFlowDescriptorConverter
);
397 if (log
.isTraceEnabled())
398 log
.debug("Registered execution flow descriptor converter from "
405 @SuppressWarnings("rawtypes")
406 public synchronized void unbind(Object service
, Map properties
)
408 if (service
instanceof ExecutionFlowDescriptorConverter
) {
409 OsgiBundle osgiBundle
= asOsgiBundle(properties
);
410 if (executionFlowDescriptorConverters
.containsKey(osgiBundle
)) {
411 executionFlowDescriptorConverters
.remove(osgiBundle
);
412 if (log
.isTraceEnabled())
413 log
.debug("Removed execution flow descriptor converter from "
424 protected MBeanServer
getMBeanServer() {
425 return ManagementFactory
.getPlatformMBeanServer();
428 public void registerMBean(Module module
, ExecutionFlow executionFlow
) {
430 StandardMBean mbean
= new StandardMBean(executionFlow
,
431 ExecutionFlow
.class);
432 getMBeanServer().registerMBean(mbean
,
433 flowMBeanName(module
, executionFlow
));
434 } catch (Exception e
) {
435 String msg
= "Cannot register execution flow " + executionFlow
437 throw new SlcException(msg
, e
);
441 public void unregisterMBean(Module module
, ExecutionFlow executionFlow
) {
443 getMBeanServer().unregisterMBean(
444 flowMBeanName(module
, executionFlow
));
445 } catch (Exception e
) {
446 String msg
= "Cannot unregister execution flow " + executionFlow
448 throw new SlcException(msg
, e
);
452 @SuppressWarnings("deprecation")
453 protected ObjectName
flowMBeanName(Module module
,
454 ExecutionFlow executionFlow
) {
455 String executionModulesPrefix
= "SLCExecutionModules";
456 String path
= executionFlow
.getPath();
457 String name
= executionFlow
.getName();
458 if (path
== null && name
.indexOf('/') >= 0) {
459 path
= name
.substring(0, name
.lastIndexOf('/') - 1);
460 name
= name
.substring(name
.lastIndexOf('/'));
463 StringBuffer buf
= new StringBuffer(executionModulesPrefix
+ ":"
464 + "module=" + module
.getName() + " [" + module
.getVersion()
467 if (path
!= null && !path
.equals("")) {
469 for (String token
: path
.split("/")) {
470 if (!token
.equals("")) {
471 buf
.append("path").append(depth
).append('=');
472 // in order to have directories first
474 buf
.append(token
).append(',');
479 buf
.append("name=").append(name
);
481 return new ObjectName(buf
.toString());
482 } catch (Exception e
) {
483 throw new SlcException("Cannot generate object name based on "
491 @SuppressWarnings("rawtypes")
492 private OsgiBundle
asOsgiBundle(Map properties
) {
493 String bundleSymbolicName
= checkAndGet(Constants
.BUNDLE_SYMBOLICNAME
,
495 String bundleVersion
= checkAndGet(Constants
.BUNDLE_VERSION
, properties
);
496 return new OsgiBundle(bundleSymbolicName
, bundleVersion
);
499 @SuppressWarnings("rawtypes")
500 private String
checkAndGet(Object key
, Map properties
) {
501 if (!properties
.containsKey(key
) || properties
.get(key
) == null)
502 throw new SlcException(key
+ " not set in " + properties
);
504 return properties
.get(key
).toString();
507 public void setBundlesManager(BundlesManager bundlesManager
) {
508 this.bundlesManager
= bundlesManager
;
511 public void setDefaultDescriptorConverter(
512 ExecutionFlowDescriptorConverter defaultDescriptorConverter
) {
513 this.defaultDescriptorConverter
= defaultDescriptorConverter
;
516 public void setRegisterFlowsToJmx(Boolean registerFlowsToJmx
) {
517 this.registerFlowsToJmx
= registerFlowsToJmx
;