blob: e57a8c1a10786943699447b7aa5699deacb22c95 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;
import com.google.inject.Inject;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* <p>This is a utility class to help with installing, starting, stopping and uninstalling OSGi Bundles. You can choose
* to inject an instance of this class, or it can be created explicitly by reference to a {@link OsgiFramework}.</p>
*
* <p>Please see commentary on {@link OsgiFramework#installBundle(String)} for a description of exception-safety issues
* to consider when installing bundles that use the {@link OsgiHeader#PREINSTALL_BUNDLE} manifest instruction.</p>
*
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public final class BundleInstaller {
private final OsgiFramework osgiFramework;
@Inject
public BundleInstaller(OsgiFramework osgiFramework) {
this.osgiFramework = osgiFramework;
}
public List<Bundle> installAndStart(String... locations) throws BundleException {
return installAndStart(Arrays.asList(locations));
}
public List<Bundle> installAndStart(Iterable<String> locations) throws BundleException {
List<Bundle> bundles = new LinkedList<>();
try {
for (String location : locations) {
bundles.addAll(osgiFramework.installBundle(location));
}
} catch (BundleInstallationException e) {
bundles.addAll(e.installedBundles());
throw new BundleInstallationException(bundles, e);
} catch (Exception e) {
throw new BundleInstallationException(bundles, e);
}
try {
for (Bundle bundle : bundles) {
start(bundle);
}
} catch (Exception e) {
throw new BundleInstallationException(bundles, e);
}
return bundles;
}
public void stopAndUninstall(Bundle... bundles) throws BundleException {
stopAndUninstall(Arrays.asList(bundles));
}
public void stopAndUninstall(Iterable<Bundle> bundles) throws BundleException {
for (Bundle bundle : bundles) {
stop(bundle);
}
for (Bundle bundle : bundles) {
bundle.uninstall();
}
}
private void start(Bundle bundle) throws BundleException {
if (bundle.getState() == Bundle.ACTIVE) {
throw new BundleException("OSGi bundle " + bundle.getSymbolicName() + " already started.");
}
if (!OsgiHeader.asList(bundle, OsgiHeader.APPLICATION).isEmpty()) {
throw new BundleException("OSGi header '" + OsgiHeader.APPLICATION + "' not allowed for " +
"non-application bundle " + bundle.getSymbolicName() + ".");
}
osgiFramework.startBundles(Arrays.asList(bundle), false);
}
private void stop(Bundle bundle) throws BundleException {
if (bundle.getState() != Bundle.ACTIVE) {
throw new BundleException("OSGi bundle " + bundle.getSymbolicName() + " not started.");
}
bundle.stop();
}
}
|