View Javadoc
1   package org.argeo.osgi.boot.internal.springutil;
2   
3   import java.io.InputStream;
4   import java.lang.reflect.Method;
5   import java.net.URL;
6   import java.net.URLClassLoader;
7   import java.nio.file.Files;
8   import java.nio.file.Path;
9   import java.nio.file.Paths;
10  import java.util.ArrayList;
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.Map;
14  
15  public class Bootstrap {
16  
17  	public static void main(String[] args) {
18  		try {
19  			String configurationArea = "file:" + System.getProperty("user.dir") + "/state";
20  			String instanceArea = "file:" + System.getProperty("user.dir") + "/data";
21  			String log4jUrl = "file:" + System.getProperty("user.dir") + "/log4j.properties";
22  
23  			System.setProperty("org.osgi.service.http.port", "7070");
24  			System.setProperty("log4j.configuration", log4jUrl);
25  
26  			System.setProperty("osgi.console", "2323");
27  			Map<String, String> props = new HashMap<String, String>();
28  			props.put("osgi.clean", "true");
29  //			props.put("osgi.console", "2323");
30  			props.put("osgi.configuration.area", configurationArea);
31  			props.put("osgi.instance.area", instanceArea);
32  
33  			System.setProperty("argeo.osgi.start.2.node",
34  					"org.eclipse.equinox.console,org.eclipse.equinox.http.servlet,org.eclipse.equinox.ds,"
35  							+ "org.eclipse.equinox.metatype,org.eclipse.equinox.cm,org.eclipse.rap.rwt.osgi");
36  			System.setProperty("argeo.osgi.start.3.node", "org.argeo.cms");
37  
38  			// URL osgiJar =
39  			// Bootstrap.class.getClassLoader().getResource("/usr/share/osgi/boot/org.eclipse.org.jar");
40  			URL osgiJar = new URL(
41  					"file:///home/mbaudier/dev/git/apache2/argeo-commons/demo/exec/cms-e4-rap/backup/share/osgi/boot/org.eclipse.org.jar");
42  			URL osgiBootJar = new URL(
43  					"file:///home/mbaudier/dev/git/apache2/argeo-commons/demo/exec/cms-e4-rap/backup/share/osgi/boot/org.argeo.osgi.boot.jar");
44  			URL[] jarUrls = { osgiJar };
45  			try (URLClassLoader urlCl = new URLClassLoader(jarUrls)) {
46  
47  				// Class<?> factoryClass =
48  				// urlCl.loadClass("/org/eclipse/osgi/launch/EquinoxFactory");
49  				Class<?> factoryClass = urlCl.loadClass("org.eclipse.osgi.launch.EquinoxFactory");
50  				Class<?> frameworkClass = urlCl.loadClass("org.osgi.framework.launch.Framework");
51  				Class<?> bundleContextClass = urlCl.loadClass("org.osgi.framework.BundleContext");
52  				Class<?> bundleClass = urlCl.loadClass("org.osgi.framework.Bundle");
53  
54  				Object factory = factoryClass.getConstructor().newInstance();
55  				Method newFrameworkMethod = factoryClass.getMethod("newFramework", Map.class);
56  				Object framework = newFrameworkMethod.invoke(factory, props);
57  				Method startFramework = frameworkClass.getMethod("start", new Class[] {});
58  				startFramework.invoke(framework);
59  				Method getBundleContext = frameworkClass.getMethod("getBundleContext", new Class[] {});
60  				Object bundleContext = getBundleContext.invoke(framework);
61  				Class<?>[] installArgs = { String.class, InputStream.class };
62  				Method install = bundleContextClass.getMethod("installBundle", installArgs);
63  				Method startBundle = bundleClass.getMethod("start");
64  				Method getSymbolicName = bundleClass.getMethod("getSymbolicName");
65  
66  				Path basePath = Paths.get(
67  						"/home/mbaudier/dev/git/apache2/argeo-commons/demo/exec/cms-e4-rap/backup/share/osgi/boot/");
68  				List<Object> bundles = new ArrayList<>();
69  				for (Path p : Files.newDirectoryStream(basePath)) {
70  					try (InputStream in = Files.newInputStream(p)) {
71  						Object bundle = install.invoke(bundleContext, "file:" + p, in);
72  						bundles.add(bundle);
73  						System.out.println("Installed " + bundle);
74  					} catch (Exception e) {
75  						if (!p.getFileName().toString().startsWith("org.eclipse.osgi")) {
76  							System.err.println(p);
77  							e.printStackTrace();
78  						}
79  					}
80  				}
81  
82  //				for (Object bundle : bundles) {
83  //					try {
84  //						String symbolicName = getSymbolicName.invoke(bundle).toString();
85  //						startBundle.invoke(bundle);
86  //					} catch (Exception e) {
87  //						// TODO Auto-generated catch block
88  //						e.printStackTrace();
89  //					}
90  //				}
91  
92  				Object osgiBootBundle = install.invoke(bundleContext, osgiBootJar.toString(), osgiBootJar.openStream());
93  				startBundle.invoke(osgiBootBundle);
94  			}
95  		} catch (Exception e) {
96  			e.printStackTrace();
97  		}
98  
99  	}
100 
101 }