2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.osgi
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
20 import java
.util
.UUID
;
22 import org
.argeo
.slc
.NameVersion
;
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
.core
.build
.VersionedResourceDistribution
;
28 import org
.argeo
.slc
.deploy
.DeploymentData
;
29 import org
.argeo
.slc
.deploy
.DynamicRuntime
;
30 import org
.argeo
.slc
.deploy
.TargetData
;
31 import org
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
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
;
39 public class OsgiRuntime
implements BundleContextAware
, ResourceLoaderAware
,
40 DynamicRuntime
<OsgiBundle
> {
41 private String uuid
= UUID
.randomUUID().toString();
42 private BundleContext bundleContext
;
43 private ResourceLoader resourceLoader
;
45 public List
<OsgiBundle
> listModules() {
46 List
<OsgiBundle
> modules
= new ArrayList
<OsgiBundle
>();
47 Bundle
[] bundles
= bundleContext
.getBundles();
48 for (Bundle bundle
: bundles
) {
49 OsgiBundle osgiBundle
= new OsgiBundle(bundle
);
50 modules
.add(osgiBundle
);
51 String location
= bundle
.getLocation();
52 if (location
!= null) {
53 Resource resource
= resourceLoader
.getResource(location
);
55 .setResourceDistribution(new VersionedResourceDistribution(
56 osgiBundle
.getName(), osgiBundle
.getVersion(),
63 public OsgiBundle
installModule(Distribution distribution
) {
64 if (!(distribution
instanceof StreamReadable
))
65 throw new UnsupportedException("distribution", distribution
);
67 StreamReadable sr
= (StreamReadable
) distribution
;
70 bundle
= bundleContext
.installBundle(sr
.toString(), sr
72 } catch (BundleException e
) {
73 throw new SlcException(
74 "Cannot install OSGi bundle " + distribution
, e
);
76 return new OsgiBundle(bundle
);
79 public void updateModule(NameVersion nameVersion
) {
80 Bundle bundle
= findBundle(nameVersion
);
83 } catch (BundleException e
) {
84 throw new SlcException("Cannot update " + bundle
, e
);
88 public void uninstallModule(NameVersion nameVersion
) {
89 Bundle bundle
= findBundle(nameVersion
);
92 } catch (BundleException e
) {
93 throw new SlcException("Cannot uninstall " + bundle
, e
);
97 public void startModule(NameVersion nameVersion
) {
98 Bundle bundle
= findBundle(nameVersion
);
101 // TODO: use bundle manager
102 } catch (BundleException e
) {
103 throw new SlcException("Cannot uninstall " + bundle
, e
);
107 protected Bundle
findBundle(NameVersion nameVersion
) {
108 Bundle
[] bundles
= bundleContext
.getBundles();
109 for (Bundle bundle
: bundles
) {
110 OsgiBundle osgiBundle
= new OsgiBundle(bundle
);
111 if (osgiBundle
.equals(nameVersion
)) {
115 throw new SlcException("Could not find bundle " + nameVersion
);
118 public void shutdown() {
119 // FIXME use framework
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
;