1 package org
.argeo
.slc
.osgi
;
3 import java
.util
.ArrayList
;
7 import org
.argeo
.slc
.SlcException
;
8 import org
.argeo
.slc
.StreamReadable
;
9 import org
.argeo
.slc
.UnsupportedException
;
10 import org
.argeo
.slc
.build
.Distribution
;
11 import org
.argeo
.slc
.build
.NameVersion
;
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
.osgi
.framework
.Bundle
;
17 import org
.osgi
.framework
.BundleContext
;
18 import org
.osgi
.framework
.BundleException
;
19 import org
.springframework
.context
.ResourceLoaderAware
;
20 import org
.springframework
.core
.io
.Resource
;
21 import org
.springframework
.core
.io
.ResourceLoader
;
22 import org
.springframework
.osgi
.context
.BundleContextAware
;
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 uninstallModule(NameVersion nameVersion
) {
65 Bundle bundle
= findBundle(nameVersion
);
68 } catch (BundleException e
) {
69 throw new SlcException("Cannot uninstall " + bundle
, e
);
73 public void startModule(NameVersion nameVersion
) {
74 Bundle bundle
= findBundle(nameVersion
);
77 // TODO: use bundle manager
78 } catch (BundleException e
) {
79 throw new SlcException("Cannot uninstall " + bundle
, e
);
83 protected Bundle
findBundle(NameVersion nameVersion
) {
84 Bundle
[] bundles
= bundleContext
.getBundles();
85 for (Bundle bundle
: bundles
) {
86 OsgiBundle osgiBundle
= new OsgiBundle(bundle
);
87 if (osgiBundle
.equals(nameVersion
)) {
91 throw new SlcException("Could not find bundle " + nameVersion
);
94 public void shutdown() {
95 throw new UnsupportedException();
98 public String
getDeployedSystemId() {
102 public DeploymentData
getDeploymentData() {
103 throw new UnsupportedException();
106 public Distribution
getDistribution() {
107 throw new UnsupportedException();
110 public TargetData
getTargetData() {
111 throw new UnsupportedException();
114 public void setBundleContext(BundleContext bundleContext
) {
115 this.bundleContext
= bundleContext
;
118 public void setResourceLoader(ResourceLoader resourceLoader
) {
119 this.resourceLoader
= resourceLoader
;