]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/Launcher.java
Create a demo user at initialisation
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / Launcher.java
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.osgi.boot;
17
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.lang.reflect.Method;
21 import java.util.List;
22 import java.util.Vector;
23
24 import org.eclipse.core.runtime.adaptor.EclipseStarter;
25 import org.osgi.framework.BundleContext;
26
27 /** Command line interface. */
28 @SuppressWarnings({ "rawtypes", "unchecked", "restriction" })
29 public class Launcher {
30
31 public static void main(String[] args) {
32 // Try to load system properties
33 String systemPropertiesFilePath = OsgiBootUtils
34 .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE);
35 if (systemPropertiesFilePath != null) {
36 FileInputStream in;
37 try {
38 in = new FileInputStream(systemPropertiesFilePath);
39 System.getProperties().load(in);
40 } catch (IOException e1) {
41 throw new RuntimeException(
42 "Cannot load system properties from "
43 + systemPropertiesFilePath, e1);
44 }
45 if (in != null) {
46 try {
47 in.close();
48 } catch (Exception e) {
49 // silent
50 }
51 }
52 }
53
54 // Start main class
55 startMainClass();
56
57 // Start Equinox
58 BundleContext bundleContext = null;
59 try {
60 bundleContext = EclipseStarter.startup(args, null);
61 } catch (Exception e) {
62 throw new RuntimeException("Cannot start Equinox.", e);
63 }
64
65 // OSGi bootstrap
66 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
67 osgiBoot.bootstrap();
68 }
69
70 protected static void startMainClass() {
71 String className = OsgiBootUtils
72 .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS);
73 if (className == null)
74 return;
75
76 String line = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPARGS,
77 "");
78
79 String[] uiArgs = readArgumentsFromLine(line);
80
81 try {
82 // Launch main method using reflection
83 Class clss = Class.forName(className);
84 Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
85 Object[] mainArgs = { uiArgs };
86 Method mainMethod = clss.getMethod("main", mainArgsClasses);
87 mainMethod.invoke(null, mainArgs);
88 } catch (Exception e) {
89 throw new RuntimeException("Cannot start main class.", e);
90 }
91
92 }
93
94 /**
95 * Transform a line into an array of arguments, taking "" as single
96 * arguments. (nested \" are not supported)
97 */
98 private static String[] readArgumentsFromLine(String lineOrig) {
99 String line = lineOrig.trim();// remove trailing spaces
100 List args = new Vector();
101 StringBuffer curr = new StringBuffer("");
102 boolean inQuote = false;
103 char[] arr = line.toCharArray();
104 for (int i = 0; i < arr.length; i++) {
105 char c = arr[i];
106 switch (c) {
107 case '\"':
108 inQuote = !inQuote;
109 break;
110 case ' ':
111 if (!inQuote) {// otherwise, no break: goes to default
112 if (curr.length() > 0) {
113 args.add(curr.toString());
114 curr = new StringBuffer("");
115 }
116 break;
117 }
118 default:
119 curr.append(c);
120 break;
121 }
122 }
123
124 // Add last arg
125 if (curr.length() > 0) {
126 args.add(curr.toString());
127 curr = null;
128 }
129
130 String[] res = new String[args.size()];
131 for (int i = 0; i < args.size(); i++) {
132 res[i] = args.get(i).toString();
133 }
134 return res;
135 }
136
137 }