View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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  package org.argeo.slc.osgi;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.UUID;
21  
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;
38  
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;
44  
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);
54  				osgiBundle
55  						.setResourceDistribution(new VersionedResourceDistribution(
56  								osgiBundle.getName(), osgiBundle.getVersion(),
57  								resource));
58  			}
59  		}
60  		return modules;
61  	}
62  
63  	public OsgiBundle installModule(Distribution distribution) {
64  		if (!(distribution instanceof StreamReadable))
65  			throw new UnsupportedException("distribution", distribution);
66  
67  		StreamReadable sr = (StreamReadable) distribution;
68  		Bundle bundle;
69  		try {
70  			bundle = bundleContext.installBundle(sr.toString(), sr
71  					.getInputStream());
72  		} catch (BundleException e) {
73  			throw new SlcException(
74  					"Cannot install OSGi bundle " + distribution, e);
75  		}
76  		return new OsgiBundle(bundle);
77  	}
78  
79  	public void updateModule(NameVersion nameVersion) {
80  		Bundle bundle = findBundle(nameVersion);
81  		try {
82  			bundle.update();
83  		} catch (BundleException e) {
84  			throw new SlcException("Cannot update " + bundle, e);
85  		}
86  	}
87  
88  	public void uninstallModule(NameVersion nameVersion) {
89  		Bundle bundle = findBundle(nameVersion);
90  		try {
91  			bundle.uninstall();
92  		} catch (BundleException e) {
93  			throw new SlcException("Cannot uninstall " + bundle, e);
94  		}
95  	}
96  
97  	public void startModule(NameVersion nameVersion) {
98  		Bundle bundle = findBundle(nameVersion);
99  		try {
100 			bundle.start();
101 			// TODO: use bundle manager
102 		} catch (BundleException e) {
103 			throw new SlcException("Cannot uninstall " + bundle, e);
104 		}
105 	}
106 
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)) {
112 				return bundle;
113 			}
114 		}
115 		throw new SlcException("Could not find bundle " + nameVersion);
116 	}
117 
118 	public void shutdown() {
119 		// FIXME use framework
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 }