]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLaunchHelper.java
Clean up code
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ide.ui / src / main / java / org / argeo / slc / ide / ui / launch / osgi / OsgiLaunchHelper.java
1 package org.argeo.slc.ide.ui.launch.osgi;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Properties;
11 import java.util.StringTokenizer;
12
13 import org.argeo.slc.ide.ui.SlcIdeUiPlugin;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.variables.IStringVariableManager;
20 import org.eclipse.core.variables.VariablesPlugin;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
24 import org.eclipse.jdt.launching.IVMInstall;
25 import org.eclipse.jdt.launching.IVMInstall2;
26 import org.eclipse.jdt.launching.IVMInstallType;
27 import org.eclipse.jdt.launching.JavaRuntime;
28 import org.eclipse.jface.dialogs.ErrorDialog;
29 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
30 import org.eclipse.swt.widgets.Display;
31
32 public class OsgiLaunchHelper implements OsgiLauncherConstants {
33 private static Boolean debug = false;
34
35 /** Expect properties file to be set as mapped resources */
36 public static void updateLaunchConfiguration(
37 ILaunchConfigurationWorkingCopy configuration) {
38 try {
39 IFile propertiesFile = (IFile) configuration.getMappedResources()[0];
40 propertiesFile.refreshLocal(IResource.DEPTH_ONE, null);
41
42 Properties properties = OsgiLaunchHelper
43 .readProperties(propertiesFile);
44
45 List<String> bundlesToStart = new ArrayList<String>();
46 Map<String, String> systemPropertiesToAppend = new HashMap<String, String>();
47 OsgiLaunchHelper.interpretProperties(properties, bundlesToStart,
48 systemPropertiesToAppend);
49
50 File workingDir = getWorkingDirectory(configuration);
51 File dataDir = new File(workingDir, "data");
52
53 OsgiLaunchHelper.updateLaunchConfiguration(configuration,
54 bundlesToStart, systemPropertiesToAppend, dataDir
55 .getAbsolutePath());
56 } catch (Exception e) {
57 ErrorDialog.openError(Display.getCurrent().getActiveShell(),
58 "Error", "Cannot read properties",
59 new Status(IStatus.ERROR, SlcIdeUiPlugin.ID,
60 e.getMessage(), e));
61 return;
62 }
63 }
64
65 static void updateLaunchConfiguration(
66 ILaunchConfigurationWorkingCopy configuration,
67 List<String> bundlesToStart,
68 Map<String, String> systemPropertiesToAppend, String dataDir)
69 throws CoreException {
70 // Convert bundle lists
71 String targetBundles = configuration.getAttribute(
72 IPDELauncherConstants.TARGET_BUNDLES, "");
73 configuration.setAttribute(IPDELauncherConstants.TARGET_BUNDLES,
74 convertBundleList(bundlesToStart, targetBundles));
75
76 String wkSpaceBundles = configuration.getAttribute(
77 IPDELauncherConstants.WORKSPACE_BUNDLES, "");
78 configuration.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES,
79 convertBundleList(bundlesToStart, wkSpaceBundles));
80
81 // Update other default information
82 configuration.setAttribute(IPDELauncherConstants.DEFAULT_AUTO_START,
83 false);
84
85 // VM arguments (system properties)
86 String defaultVmArgs = configuration.getAttribute(
87 OsgiLauncherConstants.ATTR_DEFAULT_VM_ARGS, "");
88 StringBuffer vmArgs = new StringBuffer(defaultVmArgs);
89
90 // Data dir system property
91 if (dataDir != null)
92 addSysProperty(vmArgs, OsgiLauncherConstants.ARGEO_OSGI_DATA_DIR,
93 dataDir);
94 // Add locations of JVMs
95 if (configuration.getAttribute(ATTR_ADD_JVM_PATHS, false))
96 addVms(vmArgs);
97
98 // Add other system properties
99 for (String key : systemPropertiesToAppend.keySet())
100 addSysProperty(vmArgs, key, systemPropertiesToAppend.get(key));
101
102 vmArgs.append(" ").append(
103 configuration.getAttribute(ATTR_ADDITIONAL_VM_ARGS, ""));
104
105 configuration.setAttribute(
106 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs
107 .toString());
108
109 // Program arguments
110 StringBuffer progArgs = new StringBuffer("");
111 if (dataDir != null) {
112 progArgs.append("-data ");
113 progArgs.append(surroundSpaces(dataDir));
114 }
115 String additionalProgramArgs = configuration.getAttribute(
116 OsgiLauncherConstants.ATTR_ADDITIONAL_PROGRAM_ARGS, "");
117 progArgs.append(' ').append(additionalProgramArgs);
118 configuration.setAttribute(
119 IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
120 progArgs.toString());
121 }
122
123 protected static void addVms(StringBuffer vmArgs) {
124 addVmSysProperty(vmArgs, "default", JavaRuntime.getDefaultVMInstall());
125 IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
126 for (IVMInstallType vmType : vmTypes) {
127 for (IVMInstall vmInstall : vmType.getVMInstalls()) {
128 // printVm("", vmInstall);
129 // properties based on name
130 addVmSysProperty(vmArgs, vmInstall.getName(), vmInstall);
131 if (vmInstall instanceof IVMInstall2) {
132 // properties based on version
133 IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
134 String version = vmInstall2.getJavaVersion();
135 addVmSysProperty(vmArgs, version, vmInstall);
136
137 List<String> tokens = new ArrayList<String>();
138 StringTokenizer st = new StringTokenizer(version, ".");
139 while (st.hasMoreTokens())
140 tokens.add(st.nextToken());
141 if (tokens.size() >= 2)
142 addVmSysProperty(vmArgs, tokens.get(0) + "."
143 + tokens.get(1), vmInstall);
144 }
145 }
146 }
147
148 }
149
150 protected static void addVmSysProperty(StringBuffer vmArgs, String suffix,
151 IVMInstall vmInstall) {
152 addSysProperty(vmArgs, OsgiLauncherConstants.VMS_PROPERTY_PREFIX + "."
153 + suffix, vmInstall.getInstallLocation().getPath());
154 }
155
156 protected static void addSysProperty(StringBuffer vmArgs, String key,
157 String value) {
158 surroundSpaces(value);
159 String str = "-D" + key + "=" + value;
160 // surroundSpaces(str);
161 vmArgs.append(' ').append(str);
162 }
163
164 protected static String surroundSpaces(String str) {
165 if (str.indexOf(' ') >= 0)
166 return '\"' + str + '\"';
167 else
168 return str;
169 }
170
171 protected static String convertBundleList(List<String> bundlesToStart,
172 String original) {
173 StringBuffer bufBundles = new StringBuffer(1024);
174 StringTokenizer stComa = new StringTokenizer(original, ",");
175 boolean first = true;
176 bundles: while (stComa.hasMoreTokens()) {
177 if (first)
178 first = false;
179 else
180 bufBundles.append(',');
181
182 String bundleId = stComa.nextToken();
183 int indexAt = bundleId.indexOf('@');
184 boolean modified = false;
185 if (indexAt >= 0) {
186 bundleId = bundleId.substring(0, indexAt);
187 }
188
189 if (bundleId.endsWith(".source")) {
190 if (debug)
191 System.out.println("Skip source bundle " + bundleId);
192 continue bundles;
193 }
194
195 if (bundlesToStart.contains(bundleId)) {
196 bufBundles.append(bundleId).append('@').append("default:true");
197 modified = true;
198 if (debug)
199 System.out.println("Will start " + bundleId);
200 }
201
202 if (!modified)
203 bufBundles.append(bundleId);
204 }
205 String output = bufBundles.toString();
206 return output;
207 }
208
209 protected static Properties readProperties(IFile file) throws CoreException {
210 Properties props = new Properties();
211
212 InputStream in = null;
213 try {
214 in = file.getContents();
215 props.load(in);
216 } catch (IOException e) {
217 throw new CoreException(new Status(IStatus.ERROR,
218 SlcIdeUiPlugin.ID, "Cannot read properties file", e));
219 } finally {
220 if (in != null)
221 try {
222 in.close();
223 } catch (IOException e) {
224 // silent
225 }
226 }
227 return props;
228 }
229
230 protected static void interpretProperties(Properties properties,
231 List<String> bundlesToStart,
232 Map<String, String> systemPropertiesToAppend) {
233 String argeoOsgiStart = properties
234 .getProperty(OsgiLauncherConstants.ARGEO_OSGI_START);
235 if (argeoOsgiStart != null) {
236 StringTokenizer st = new StringTokenizer(argeoOsgiStart, ",");
237 while (st.hasMoreTokens())
238 bundlesToStart.add(st.nextToken());
239 }
240
241 propKeys: for (Object keyObj : properties.keySet()) {
242 String key = keyObj.toString();
243 if (OsgiLauncherConstants.ARGEO_OSGI_START.equals(key))
244 continue propKeys;
245 else if (OsgiLauncherConstants.ARGEO_OSGI_BUNDLES.equals(key))
246 continue propKeys;
247 else if (OsgiLauncherConstants.ARGEO_OSGI_LOCATIONS.equals(key))
248 continue propKeys;
249 else if (OsgiLauncherConstants.OSGI_BUNDLES.equals(key))
250 continue propKeys;
251 else
252 systemPropertiesToAppend.put(key, properties.getProperty(key));
253 }
254
255 }
256
257 // Hacked from
258 // org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper.getWorkingDirectory(ILaunchConfiguration)
259 public static File getWorkingDirectory(ILaunchConfiguration configuration)
260 throws CoreException {
261 String working;
262 try {
263 working = configuration.getAttribute(
264 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
265 new File(".").getCanonicalPath()); //$NON-NLS-1$
266 } catch (IOException e) {
267 working = "${workspace_loc}/../"; //$NON-NLS-1$
268 }
269 File dir = new File(getSubstitutedString(working));
270 if (!dir.exists())
271 dir.mkdirs();
272 return dir;
273 }
274
275 // Hacked from
276 // org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper.getSubstitutedString(String)
277 private static String getSubstitutedString(String text)
278 throws CoreException {
279 if (text == null)
280 return ""; //$NON-NLS-1$
281 IStringVariableManager mgr = VariablesPlugin.getDefault()
282 .getStringVariableManager();
283 return mgr.performStringSubstitution(text);
284 }
285
286 // static void initializeConfiguration(
287 // ILaunchConfigurationWorkingCopy configuration) {
288 // new OSGiLaunchConfigurationInitializer().initialize(configuration);
289 // }
290
291 }