]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/OsgiRuntime.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / 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.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;
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 updateModule(NameVersion nameVersion) {
65 Bundle bundle = findBundle(nameVersion);
66 try {
67 bundle.update();
68 } catch (BundleException e) {
69 throw new SlcException("Cannot update " + bundle, e);
70 }
71 }
72
73 public void uninstallModule(NameVersion nameVersion) {
74 Bundle bundle = findBundle(nameVersion);
75 try {
76 bundle.uninstall();
77 } catch (BundleException e) {
78 throw new SlcException("Cannot uninstall " + bundle, e);
79 }
80 }
81
82 public void startModule(NameVersion nameVersion) {
83 Bundle bundle = findBundle(nameVersion);
84 try {
85 bundle.start();
86 // TODO: use bundle manager
87 } catch (BundleException e) {
88 throw new SlcException("Cannot uninstall " + bundle, e);
89 }
90 }
91
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)) {
97 return bundle;
98 }
99 }
100 throw new SlcException("Could not find bundle " + nameVersion);
101 }
102
103 public void shutdown() {
104 // FIXME use framework
105 throw new UnsupportedException();
106 }
107
108 public String getDeployedSystemId() {
109 return uuid;
110 }
111
112 public DeploymentData getDeploymentData() {
113 throw new UnsupportedException();
114 }
115
116 public Distribution getDistribution() {
117 throw new UnsupportedException();
118 }
119
120 public TargetData getTargetData() {
121 throw new UnsupportedException();
122 }
123
124 public void setBundleContext(BundleContext bundleContext) {
125 this.bundleContext = bundleContext;
126 }
127
128 public void setResourceLoader(ResourceLoader resourceLoader) {
129 this.resourceLoader = resourceLoader;
130 }
131
132 }