1 package org
.argeo
.slc
.osgi
;
3 import java
.util
.ArrayList
;
7 import org
.argeo
.slc
.NameVersion
;
8 import org
.argeo
.slc
.SlcException
;
9 import org
.argeo
.slc
.StreamReadable
;
10 import org
.argeo
.slc
.UnsupportedException
;
11 import org
.argeo
.slc
.build
.Distribution
;
12 import org
.argeo
.slc
.core
.build
.VersionedResourceDistribution
;
13 import org
.argeo
.slc
.deploy
.DeploymentData
;
14 import org
.argeo
.slc
.deploy
.DynamicRuntime
;
15 import org
.argeo
.slc
.deploy
.TargetData
;
16 import org
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
17 import org
.osgi
.framework
.Bundle
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.BundleException
;
20 import org
.springframework
.context
.ResourceLoaderAware
;
21 import org
.springframework
.core
.io
.Resource
;
22 import org
.springframework
.core
.io
.ResourceLoader
;
24 public class OsgiRuntime
implements BundleContextAware
, ResourceLoaderAware
,
25 DynamicRuntime
<OsgiBundle
> {
26 private String uuid
= UUID
.randomUUID().toString();
27 private BundleContext bundleContext
;
28 private ResourceLoader resourceLoader
;
30 public List
<OsgiBundle
> listModules() {
31 List
<OsgiBundle
> modules
= new ArrayList
<OsgiBundle
>();
32 Bundle
[] bundles
= bundleContext
.getBundles();
33 for (Bundle bundle
: bundles
) {
34 OsgiBundle osgiBundle
= new OsgiBundle(bundle
);
35 modules
.add(osgiBundle
);
36 String location
= bundle
.getLocation();
37 if (location
!= null) {
38 Resource resource
= resourceLoader
.getResource(location
);
40 .setResourceDistribution(new VersionedResourceDistribution(
41 osgiBundle
.getName(), osgiBundle
.getVersion(),
48 public OsgiBundle
installModule(Distribution distribution
) {
49 if (!(distribution
instanceof StreamReadable
))
50 throw new UnsupportedException("distribution", distribution
);
52 StreamReadable sr
= (StreamReadable
) distribution
;
55 bundle
= bundleContext
.installBundle(sr
.toString(), sr
57 } catch (BundleException e
) {
58 throw new SlcException(
59 "Cannot install OSGi bundle " + distribution
, e
);
61 return new OsgiBundle(bundle
);
64 public void updateModule(NameVersion nameVersion
) {
65 Bundle bundle
= findBundle(nameVersion
);
68 } catch (BundleException e
) {
69 throw new SlcException("Cannot update " + bundle
, e
);
73 public void uninstallModule(NameVersion nameVersion
) {
74 Bundle bundle
= findBundle(nameVersion
);
77 } catch (BundleException e
) {
78 throw new SlcException("Cannot uninstall " + bundle
, e
);
82 public void startModule(NameVersion nameVersion
) {
83 Bundle bundle
= findBundle(nameVersion
);
86 // TODO: use bundle manager
87 } catch (BundleException e
) {
88 throw new SlcException("Cannot uninstall " + bundle
, e
);
92 protected Bundle
findBundle(NameVersion nameVersion
) {
93 Bundle
[] bundles
= bundleContext
.getBundles();
94 for (Bundle bundle
: bundles
) {
95 OsgiBundle osgiBundle
= new OsgiBundle(bundle
);
96 if (osgiBundle
.equals(nameVersion
)) {
100 throw new SlcException("Could not find bundle " + nameVersion
);
103 public void shutdown() {
104 // FIXME use framework
105 throw new UnsupportedException();
108 public String
getDeployedSystemId() {
112 public DeploymentData
getDeploymentData() {
113 throw new UnsupportedException();
116 public Distribution
getDistribution() {
117 throw new UnsupportedException();
120 public TargetData
getTargetData() {
121 throw new UnsupportedException();
124 public void setBundleContext(BundleContext bundleContext
) {
125 this.bundleContext
= bundleContext
;
128 public void setResourceLoader(ResourceLoader resourceLoader
) {
129 this.resourceLoader
= resourceLoader
;