1 package org
.argeo
.osgi
.boot
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collections
;
5 import java
.util
.HashMap
;
6 import java
.util
.HashSet
;
9 import java
.util
.Properties
;
11 import java
.util
.TreeMap
;
13 import org
.eclipse
.osgi
.launch
.EquinoxFactory
;
14 import org
.osgi
.framework
.BundleContext
;
15 import org
.osgi
.framework
.BundleException
;
16 import org
.osgi
.framework
.launch
.Framework
;
17 import org
.osgi
.framework
.launch
.FrameworkFactory
;
19 public class OsgiBuilder
{
20 private Map
<Integer
, StartLevel
> startLevels
= new TreeMap
<>();
21 private List
<String
> distributionBundles
= new ArrayList
<>();
23 private Map
<String
, String
> configuration
= new HashMap
<String
, String
>();
24 private Framework framework
;
26 public OsgiBuilder() {
27 // configuration.put("osgi.clean", "true");
28 configuration
.put(OsgiBoot
.CONFIGURATION_AREA_PROP
, System
.getProperty(OsgiBoot
.CONFIGURATION_AREA_PROP
));
29 configuration
.put(OsgiBoot
.INSTANCE_AREA_PROP
, System
.getProperty(OsgiBoot
.INSTANCE_AREA_PROP
));
32 public Framework
launch() {
34 FrameworkFactory frameworkFactory
= new EquinoxFactory();
35 framework
= frameworkFactory
.newFramework(configuration
);
38 } catch (BundleException e
) {
39 throw new OsgiBootException("Cannot start OSGi framework", e
);
42 BundleContext bc
= framework
.getBundleContext();
43 String osgiData
= bc
.getProperty(OsgiBoot
.INSTANCE_AREA_PROP
);
44 // String osgiConf = bc.getProperty(OsgiBoot.CONFIGURATION_AREA_PROP);
45 String osgiConf
= framework
.getDataFile("").getAbsolutePath();
46 if (OsgiBootUtils
.isDebug())
47 OsgiBootUtils
.debug("OSGi starting - data: " + osgiData
+ " conf: " + osgiConf
);
49 OsgiBoot osgiBoot
= new OsgiBoot(framework
.getBundleContext());
51 for (String distributionBundle
: distributionBundles
) {
52 List
<String
> bundleUrls
= osgiBoot
.getDistributionUrls(distributionBundle
, null);
53 osgiBoot
.installUrls(bundleUrls
);
57 osgiBoot
.startBundles(startLevelsToProperties());
59 // if (OsgiBootUtils.isDebug())
60 // for (Bundle bundle : bc.getBundles()) {
61 // OsgiBootUtils.debug(bundle.getLocation());
66 public OsgiBuilder
conf(String key
, String value
) {
68 configuration
.put(key
, value
);
72 public OsgiBuilder
install(String uri
) {
73 // TODO dynamic install
75 if (!distributionBundles
.contains(uri
))
76 distributionBundles
.add(uri
);
80 public OsgiBuilder
start(int startLevel
, String bundle
) {
84 if (!startLevels
.containsKey(startLevel
))
85 startLevels
.put(startLevel
, new StartLevel());
86 sl
= startLevels
.get(startLevel
);
91 public BundleContext
getBc() {
93 return framework
.getBundleContext();
99 private Properties
startLevelsToProperties() {
100 Properties properties
= new Properties();
101 for (Integer startLevel
: startLevels
.keySet()) {
102 String property
= OsgiBoot
.PROP_ARGEO_OSGI_START
+ "." + startLevel
;
103 StringBuilder value
= new StringBuilder();
104 for (String bundle
: startLevels
.get(startLevel
).getBundles()) {
105 value
.append(bundle
);
108 // TODO remove trailing comma
109 properties
.put(property
, value
.toString());
114 private void checkLaunched() {
116 throw new OsgiBootException("OSGi runtime is not launched");
119 private void checkNotLaunched() {
121 throw new OsgiBootException("OSGi runtime already launched");
124 private boolean isLaunched() {
125 return framework
!= null;
128 private static class StartLevel
{
129 private Set
<String
> bundles
= new HashSet
<>();
131 public void add(String bundle
) {
132 String
[] b
= bundle
.split(",");
133 Collections
.addAll(bundles
, b
);
136 public Set
<String
> getBundles() {