]> 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
Restructure SLC development environment
[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.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Properties;
9 import java.util.StringTokenizer;
10
11 import org.argeo.slc.ide.ui.SlcIdeUiPlugin;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
17 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
18 import org.eclipse.jdt.launching.IVMInstall;
19 import org.eclipse.jdt.launching.IVMInstall2;
20 import org.eclipse.jdt.launching.IVMInstallType;
21 import org.eclipse.jdt.launching.JavaRuntime;
22 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
23
24 public class OsgiLaunchHelper {
25 private static Boolean debug = false;
26
27 public static void updateLaunchConfiguration(
28 ILaunchConfigurationWorkingCopy configuration,
29 List<String> bundlesToStart,
30 Map<String, String> systemPropertiesToAppend, String workingDir,
31 String dataDir) throws CoreException {
32 // Convert bundle lists
33 String targetBundles = configuration.getAttribute(
34 IPDELauncherConstants.TARGET_BUNDLES, "");
35 configuration.setAttribute(IPDELauncherConstants.TARGET_BUNDLES,
36 convertBundleList(bundlesToStart, targetBundles));
37
38 String wkSpaceBundles = configuration.getAttribute(
39 IPDELauncherConstants.WORKSPACE_BUNDLES, "");
40 configuration.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES,
41 convertBundleList(bundlesToStart, wkSpaceBundles));
42
43 // Update other default information
44 configuration.setAttribute(IPDELauncherConstants.DEFAULT_AUTO_START,
45 false);
46 configuration.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA,
47 true);
48 String defaultVmArgs = configuration.getAttribute(
49 OsgiLauncherConstants.ATTR_DEFAULT_VM_ARGS, "");
50 StringBuffer vmArgs = new StringBuffer(defaultVmArgs);
51 vmArgs.append(" -Xmx256m");
52
53 // Add locations of JVMs
54 addVmSysProperty(vmArgs, "default", JavaRuntime.getDefaultVMInstall());
55 IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
56 for (IVMInstallType vmType : vmTypes) {
57 for (IVMInstall vmInstall : vmType.getVMInstalls()) {
58 // printVm("", vmInstall);
59 // properties based on name
60 addVmSysProperty(vmArgs, vmInstall.getName(), vmInstall);
61 if (vmInstall instanceof IVMInstall2) {
62 // properties based on version
63 IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
64 String version = vmInstall2.getJavaVersion();
65 addVmSysProperty(vmArgs, version, vmInstall);
66
67 List<String> tokens = new ArrayList<String>();
68 StringTokenizer st = new StringTokenizer(version, ".");
69 while (st.hasMoreTokens())
70 tokens.add(st.nextToken());
71 if (tokens.size() >= 2)
72 addVmSysProperty(vmArgs, tokens.get(0) + "."
73 + tokens.get(1), vmInstall);
74 }
75 }
76 }
77
78 // Add other system properties
79 for (String key : systemPropertiesToAppend.keySet())
80 addSysProperty(vmArgs, key, systemPropertiesToAppend.get(key));
81
82 if (dataDir != null)
83 addSysProperty(vmArgs, OsgiLauncherConstants.ARGEO_OSGI_DATA_DIR,
84 dataDir);
85
86 configuration.setAttribute(
87 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs
88 .toString());
89
90 // Program arguments
91 String defaultProgArgs = configuration.getAttribute(
92 OsgiLauncherConstants.ATTR_DEFAULT_PROGRAM_ARGS, "");
93 StringBuffer progArgs = new StringBuffer(defaultProgArgs);
94 if (dataDir != null) {
95 progArgs.append(" -data ");
96 progArgs.append(surroundSpaces(dataDir));
97 }
98 configuration.setAttribute(
99 IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
100 progArgs.toString());
101
102 // String dir = findWorkingDirectory();
103 if (workingDir != null)
104 configuration.setAttribute(
105 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
106 workingDir);
107
108 }
109
110 protected static void addVmSysProperty(StringBuffer vmArgs, String suffix,
111 IVMInstall vmInstall) {
112 addSysProperty(vmArgs, OsgiLauncherConstants.VMS_PROPERTY_PREFIX + "."
113 + suffix, vmInstall.getInstallLocation().getPath());
114 }
115
116 protected static void addSysProperty(StringBuffer vmArgs, String key,
117 String value) {
118 String str = "-D" + key + "=" + value;
119 surroundSpaces(str);
120 vmArgs.append(" " + str);
121 }
122
123 protected static String surroundSpaces(String str) {
124 if (str.contains(" "))
125 return "\"" + str + "\"";
126 else
127 return str;
128 }
129
130 protected static String convertBundleList(List<String> bundlesToStart,
131 String original) {
132 StringBuffer bufBundles = new StringBuffer(1024);
133 StringTokenizer stComa = new StringTokenizer(original, ",");
134 boolean first = true;
135 bundles: while (stComa.hasMoreTokens()) {
136 if (first)
137 first = false;
138 else
139 bufBundles.append(',');
140
141 String bundleId = stComa.nextToken();
142 int indexAt = bundleId.indexOf('@');
143 boolean modified = false;
144 if (indexAt >= 0) {
145 bundleId = bundleId.substring(0, indexAt);
146 }
147
148 if (bundleId.endsWith(".source")) {
149 if (debug)
150 System.out.println("Skip source bundle " + bundleId);
151 continue bundles;
152 }
153
154 if (bundlesToStart.contains(bundleId)) {
155 bufBundles.append(bundleId).append('@').append("default:true");
156 modified = true;
157 if (debug)
158 System.out.println("Will start " + bundleId);
159 }
160
161 if (!modified)
162 bufBundles.append(bundleId);
163 }
164 String output = bufBundles.toString();
165 return output;
166 }
167
168 protected static Properties readProperties(IFile file) throws CoreException {
169 Properties props = new Properties();
170
171 InputStream in = null;
172 try {
173 in = file.getContents();
174 props.load(in);
175 } catch (IOException e) {
176 throw new CoreException(new Status(IStatus.ERROR,
177 SlcIdeUiPlugin.ID, "Cannot read properties file", e));
178 } finally {
179 if (in != null)
180 try {
181 in.close();
182 } catch (IOException e) {
183 // silent
184 }
185 }
186 return props;
187 }
188
189 protected static void interpretProperties(Properties properties,
190 List<String> bundlesToStart,
191 Map<String, String> systemPropertiesToAppend) {
192 String argeoOsgiStart = properties
193 .getProperty(OsgiLauncherConstants.ARGEO_OSGI_START);
194 StringTokenizer st = new StringTokenizer(argeoOsgiStart, ",");
195 while (st.hasMoreTokens())
196 bundlesToStart.add(st.nextToken());
197
198 propKeys: for (Object keyObj : properties.keySet()) {
199 String key = keyObj.toString();
200 if (OsgiLauncherConstants.ARGEO_OSGI_START.equals(key))
201 continue propKeys;
202 else if (OsgiLauncherConstants.ARGEO_OSGI_BUNDLES.equals(key))
203 continue propKeys;
204 else if (OsgiLauncherConstants.ARGEO_OSGI_LOCATIONS.equals(key))
205 continue propKeys;
206 else if (OsgiLauncherConstants.OSGI_BUNDLES.equals(key))
207 continue propKeys;
208 else
209 systemPropertiesToAppend.put(key, properties.getProperty(key));
210 }
211
212 }
213 }