2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.osgi
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.UUID
;
23 import org
.argeo
.slc
.NameVersion
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.argeo
.slc
.StreamReadable
;
26 import org
.argeo
.slc
.UnsupportedException
;
27 import org
.argeo
.slc
.build
.Distribution
;
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
;
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
;
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
);
56 .setResourceDistribution(new VersionedResourceDistribution(
57 osgiBundle
.getName(), osgiBundle
.getVersion(),
64 public OsgiBundle
installModule(Distribution distribution
) {
65 if (!(distribution
instanceof StreamReadable
))
66 throw new UnsupportedException("distribution", distribution
);
68 StreamReadable sr
= (StreamReadable
) distribution
;
71 bundle
= bundleContext
.installBundle(sr
.toString(), sr
73 } catch (BundleException e
) {
74 throw new SlcException(
75 "Cannot install OSGi bundle " + distribution
, e
);
77 return new OsgiBundle(bundle
);
80 public void updateModule(NameVersion nameVersion
) {
81 Bundle bundle
= findBundle(nameVersion
);
84 } catch (BundleException e
) {
85 throw new SlcException("Cannot update " + bundle
, e
);
89 public void uninstallModule(NameVersion nameVersion
) {
90 Bundle bundle
= findBundle(nameVersion
);
93 } catch (BundleException e
) {
94 throw new SlcException("Cannot uninstall " + bundle
, e
);
98 public void startModule(NameVersion nameVersion
) {
99 Bundle bundle
= findBundle(nameVersion
);
102 // TODO: use bundle manager
103 } catch (BundleException e
) {
104 throw new SlcException("Cannot uninstall " + bundle
, e
);
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
)) {
116 throw new SlcException("Could not find bundle " + nameVersion
);
119 public void shutdown() {
120 throw new UnsupportedException();
123 public String
getDeployedSystemId() {
127 public DeploymentData
getDeploymentData() {
128 throw new UnsupportedException();
131 public Distribution
getDistribution() {
132 throw new UnsupportedException();
135 public TargetData
getTargetData() {
136 throw new UnsupportedException();
139 public void setBundleContext(BundleContext bundleContext
) {
140 this.bundleContext
= bundleContext
;
143 public void setResourceLoader(ResourceLoader resourceLoader
) {
144 this.resourceLoader
= resourceLoader
;