]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OsgiBuilder.java
7a8fd96bf18ee50de3474e9e5e92aacb2aface9b
[lgpl/argeo-commons.git] / OsgiBuilder.java
1 package org.argeo.osgi.boot;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Properties;
10 import java.util.Set;
11 import java.util.TreeMap;
12
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;
18
19 public class OsgiBuilder {
20 private Map<Integer, StartLevel> startLevels = new TreeMap<>();
21 private List<String> distributionBundles = new ArrayList<>();
22
23 private Map<String, String> configuration = new HashMap<String, String>();
24 private Framework framework;
25
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));
30 }
31
32 public Framework launch() {
33 // start OSGi
34 FrameworkFactory frameworkFactory = new EquinoxFactory();
35 framework = frameworkFactory.newFramework(configuration);
36 try {
37 framework.start();
38 } catch (BundleException e) {
39 throw new OsgiBootException("Cannot start OSGi framework", e);
40 }
41
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);
48
49 OsgiBoot osgiBoot = new OsgiBoot(framework.getBundleContext());
50 // install bundles
51 for (String distributionBundle : distributionBundles) {
52 List<String> bundleUrls = osgiBoot.getDistributionUrls(distributionBundle, null);
53 osgiBoot.installUrls(bundleUrls);
54 }
55
56 // start bundles
57 osgiBoot.startBundles(startLevelsToProperties());
58
59 // if (OsgiBootUtils.isDebug())
60 // for (Bundle bundle : bc.getBundles()) {
61 // OsgiBootUtils.debug(bundle.getLocation());
62 // }
63 return framework;
64 }
65
66 public OsgiBuilder conf(String key, String value) {
67 checkNotLaunched();
68 configuration.put(key, value);
69 return this;
70 }
71
72 public OsgiBuilder install(String uri) {
73 // TODO dynamic install
74 checkNotLaunched();
75 if (!distributionBundles.contains(uri))
76 distributionBundles.add(uri);
77 return this;
78 }
79
80 public OsgiBuilder start(int startLevel, String bundle) {
81 // TODO dynamic start
82 checkNotLaunched();
83 StartLevel sl;
84 if (!startLevels.containsKey(startLevel))
85 startLevels.put(startLevel, new StartLevel());
86 sl = startLevels.get(startLevel);
87 sl.add(bundle);
88 return this;
89 }
90
91 public BundleContext getBc() {
92 checkLaunched();
93 return framework.getBundleContext();
94 }
95
96 //
97 // UTILITIES
98 //
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);
106 value.append(',');
107 }
108 // TODO remove trailing comma
109 properties.put(property, value.toString());
110 }
111 return properties;
112 }
113
114 private void checkLaunched() {
115 if (!isLaunched())
116 throw new OsgiBootException("OSGi runtime is not launched");
117 }
118
119 private void checkNotLaunched() {
120 if (isLaunched())
121 throw new OsgiBootException("OSGi runtime already launched");
122 }
123
124 private boolean isLaunched() {
125 return framework != null;
126 }
127
128 private static class StartLevel {
129 private Set<String> bundles = new HashSet<>();
130
131 public void add(String bundle) {
132 String[] b = bundle.split(",");
133 Collections.addAll(bundles, b);
134 }
135
136 public Set<String> getBundles() {
137 return bundles;
138 }
139 }
140 }