]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiRuntime.java
resolution of Bug 242: ExecutionFlowGenerator implements Ordered instead of PriorityO...
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiRuntime.java
1 package org.argeo.slc.osgi;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.UUID;
6
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;
23
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;
29
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);
39 osgiBundle
40 .setResourceDistribution(new VersionedResourceDistribution(
41 osgiBundle.getName(), osgiBundle.getVersion(),
42 resource));
43 }
44 }
45 return modules;
46 }
47
48 public OsgiBundle installModule(Distribution distribution) {
49 if (!(distribution instanceof StreamReadable))
50 throw new UnsupportedException("distribution", distribution);
51
52 StreamReadable sr = (StreamReadable) distribution;
53 Bundle bundle;
54 try {
55 bundle = bundleContext.installBundle(sr.toString(), sr
56 .getInputStream());
57 } catch (BundleException e) {
58 throw new SlcException(
59 "Cannot install OSGi bundle " + distribution, e);
60 }
61 return new OsgiBundle(bundle);
62 }
63
64 public void uninstallModule(NameVersion nameVersion) {
65 Bundle bundle = findBundle(nameVersion);
66 try {
67 bundle.uninstall();
68 } catch (BundleException e) {
69 throw new SlcException("Cannot uninstall " + bundle, e);
70 }
71 }
72
73 public void startModule(NameVersion nameVersion) {
74 Bundle bundle = findBundle(nameVersion);
75 try {
76 bundle.start();
77 // TODO: use bundle manager
78 } catch (BundleException e) {
79 throw new SlcException("Cannot uninstall " + bundle, e);
80 }
81 }
82
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)) {
88 return bundle;
89 }
90 }
91 throw new SlcException("Could not find bundle " + nameVersion);
92 }
93
94 public void shutdown() {
95 throw new UnsupportedException();
96 }
97
98 public String getDeployedSystemId() {
99 return uuid;
100 }
101
102 public DeploymentData getDeploymentData() {
103 throw new UnsupportedException();
104 }
105
106 public Distribution getDistribution() {
107 throw new UnsupportedException();
108 }
109
110 public TargetData getTargetData() {
111 throw new UnsupportedException();
112 }
113
114 public void setBundleContext(BundleContext bundleContext) {
115 this.bundleContext = bundleContext;
116 }
117
118 public void setResourceLoader(ResourceLoader resourceLoader) {
119 this.resourceLoader = resourceLoader;
120 }
121
122 }