]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiRuntime.java
Improve Javadocs
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiRuntime.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.osgi;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.UUID;
22
23 import org.argeo.slc.SlcException;
24 import org.argeo.slc.StreamReadable;
25 import org.argeo.slc.UnsupportedException;
26 import org.argeo.slc.build.Distribution;
27 import org.argeo.slc.build.NameVersion;
28 import org.argeo.slc.core.build.VersionedResourceDistribution;
29 import org.argeo.slc.deploy.DeploymentData;
30 import org.argeo.slc.deploy.DynamicRuntime;
31 import org.argeo.slc.deploy.TargetData;
32 import org.osgi.framework.Bundle;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.BundleException;
35 import org.springframework.context.ResourceLoaderAware;
36 import org.springframework.core.io.Resource;
37 import org.springframework.core.io.ResourceLoader;
38 import org.springframework.osgi.context.BundleContextAware;
39
40 public class OsgiRuntime implements BundleContextAware, ResourceLoaderAware,
41 DynamicRuntime<OsgiBundle> {
42 private String uuid = UUID.randomUUID().toString();
43 private BundleContext bundleContext;
44 private ResourceLoader resourceLoader;
45
46 public List<OsgiBundle> listModules() {
47 List<OsgiBundle> modules = new ArrayList<OsgiBundle>();
48 Bundle[] bundles = bundleContext.getBundles();
49 for (Bundle bundle : bundles) {
50 OsgiBundle osgiBundle = new OsgiBundle(bundle);
51 modules.add(osgiBundle);
52 String location = bundle.getLocation();
53 if (location != null) {
54 Resource resource = resourceLoader.getResource(location);
55 osgiBundle
56 .setResourceDistribution(new VersionedResourceDistribution(
57 osgiBundle.getName(), osgiBundle.getVersion(),
58 resource));
59 }
60 }
61 return modules;
62 }
63
64 public OsgiBundle installModule(Distribution distribution) {
65 if (!(distribution instanceof StreamReadable))
66 throw new UnsupportedException("distribution", distribution);
67
68 StreamReadable sr = (StreamReadable) distribution;
69 Bundle bundle;
70 try {
71 bundle = bundleContext.installBundle(sr.toString(), sr
72 .getInputStream());
73 } catch (BundleException e) {
74 throw new SlcException(
75 "Cannot install OSGi bundle " + distribution, e);
76 }
77 return new OsgiBundle(bundle);
78 }
79
80 public void updateModule(NameVersion nameVersion) {
81 Bundle bundle = findBundle(nameVersion);
82 try {
83 bundle.update();
84 } catch (BundleException e) {
85 throw new SlcException("Cannot update " + bundle, e);
86 }
87 }
88
89 public void uninstallModule(NameVersion nameVersion) {
90 Bundle bundle = findBundle(nameVersion);
91 try {
92 bundle.uninstall();
93 } catch (BundleException e) {
94 throw new SlcException("Cannot uninstall " + bundle, e);
95 }
96 }
97
98 public void startModule(NameVersion nameVersion) {
99 Bundle bundle = findBundle(nameVersion);
100 try {
101 bundle.start();
102 // TODO: use bundle manager
103 } catch (BundleException e) {
104 throw new SlcException("Cannot uninstall " + bundle, e);
105 }
106 }
107
108 protected Bundle findBundle(NameVersion nameVersion) {
109 Bundle[] bundles = bundleContext.getBundles();
110 for (Bundle bundle : bundles) {
111 OsgiBundle osgiBundle = new OsgiBundle(bundle);
112 if (osgiBundle.equals(nameVersion)) {
113 return bundle;
114 }
115 }
116 throw new SlcException("Could not find bundle " + nameVersion);
117 }
118
119 public void shutdown() {
120 throw new UnsupportedException();
121 }
122
123 public String getDeployedSystemId() {
124 return uuid;
125 }
126
127 public DeploymentData getDeploymentData() {
128 throw new UnsupportedException();
129 }
130
131 public Distribution getDistribution() {
132 throw new UnsupportedException();
133 }
134
135 public TargetData getTargetData() {
136 throw new UnsupportedException();
137 }
138
139 public void setBundleContext(BundleContext bundleContext) {
140 this.bundleContext = bundleContext;
141 }
142
143 public void setResourceLoader(ResourceLoader resourceLoader) {
144 this.resourceLoader = resourceLoader;
145 }
146
147 }