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