]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/osgi/SlcLaunchShortcut.java
Deal with spaces in JVM paths
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / osgi / SlcLaunchShortcut.java
1 package org.argeo.slc.ui.launch.osgi;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.StringTokenizer;
9
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.runtime.Assert;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.debug.core.ILaunchConfigurationType;
14 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
15 import org.eclipse.jdt.internal.debug.ui.jres.VMInstallWizard;
16 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
17 import org.eclipse.jdt.launching.IVMInstall;
18 import org.eclipse.jdt.launching.IVMInstall2;
19 import org.eclipse.jdt.launching.IVMInstallType;
20 import org.eclipse.jdt.launching.JavaRuntime;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.osgi.service.resolver.BundleDescription;
25 import org.eclipse.pde.core.plugin.IPluginModelBase;
26 import org.eclipse.pde.core.plugin.PluginRegistry;
27 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
28 import org.eclipse.pde.ui.launcher.OSGiLaunchShortcut;
29 import org.eclipse.swt.widgets.DirectoryDialog;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Shell;
32
33 public class SlcLaunchShortcut extends OSGiLaunchShortcut {
34 public final static String VMS_PROPERTY_PREFIX = "slc.launch.vm";
35
36 private Boolean debug = false;
37
38 private String springOsgiExtenderId = "org.springframework.osgi.extender";
39 private String slcSupportEquinoxId = "org.argeo.slc.support.equinox";
40 // private String slcAgentId = "org.argeo.slc.agent";
41 // private String osgiBootId = "org.argeo.slc.osgiboot";
42
43 private ISelection selection = null;
44 private StringBuffer name = null;
45
46 private final List<String> defaultBundlesToStart = new ArrayList<String>();
47 private List<String> bundlesToStart = new ArrayList<String>();
48
49 public SlcLaunchShortcut() {
50 super();
51 defaultBundlesToStart.add(springOsgiExtenderId);
52 defaultBundlesToStart.add(slcSupportEquinoxId);
53 // defaultBundlesToStart.add(slcAgentId);
54 }
55
56 public void launch(ISelection selection, String mode) {
57 this.selection = selection;
58 this.name = new StringBuffer();
59
60 bundlesToStart = new ArrayList<String>();
61 bundlesToStart.addAll(defaultBundlesToStart);
62 // Evaluate selection
63 if (selection != null) {
64 addSelectedProjects(bundlesToStart);
65 }
66
67 super.launch(selection, mode);
68
69 // Reset
70 this.selection = null;
71 this.name = null;
72 bundlesToStart = null;
73 }
74
75 protected void initializeConfiguration(
76 ILaunchConfigurationWorkingCopy configuration) {
77 try {
78 super.initializeConfiguration(configuration);
79
80 // Convert bundle lists
81 String targetBundles = configuration.getAttribute(
82 IPDELauncherConstants.TARGET_BUNDLES, "");
83 configuration.setAttribute(IPDELauncherConstants.TARGET_BUNDLES,
84 convertBundleList(bundlesToStart, targetBundles));
85
86 String wkSpaceBundles = configuration.getAttribute(
87 IPDELauncherConstants.WORKSPACE_BUNDLES, "");
88 configuration.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES,
89 convertBundleList(bundlesToStart, wkSpaceBundles));
90
91 // Update other default information
92 configuration.setAttribute(
93 IPDELauncherConstants.DEFAULT_AUTO_START, false);
94 configuration.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA,
95 true);
96 String defaultVmArgs = configuration.getAttribute(
97 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
98 StringBuffer vmArgs = new StringBuffer(defaultVmArgs);
99 vmArgs.append(" -Xmx256m");
100
101 // Add locations of JVMs
102 addVmSysProperty(vmArgs, "default", JavaRuntime
103 .getDefaultVMInstall());
104 IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
105 for (IVMInstallType vmType : vmTypes) {
106 // System.out.println("vmType: id=" + vmType.getId() + ", name="
107 // + vmType.getName() + ", toString=" + vmType);
108 for (IVMInstall vmInstall : vmType.getVMInstalls()) {
109 // printVm("", vmInstall);
110 // properties based on name
111 addVmSysProperty(vmArgs, vmInstall.getName(), vmInstall);
112 if (vmInstall instanceof IVMInstall2) {
113 // properties based on version
114 IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
115 String version = vmInstall2.getJavaVersion();
116 addVmSysProperty(vmArgs, version, vmInstall);
117
118 List<String> tokens = new ArrayList<String>();
119 StringTokenizer st = new StringTokenizer(version, ".");
120 while (st.hasMoreTokens())
121 tokens.add(st.nextToken());
122 if (tokens.size() >= 2)
123 addVmSysProperty(vmArgs, tokens.get(0) + "."
124 + tokens.get(1), vmInstall);
125 }
126 }
127 }
128 configuration.setAttribute(
129 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs
130 .toString());
131
132 // Choose working directory
133 Shell shell = Display.getCurrent().getActiveShell();
134 DirectoryDialog dirDialog = new DirectoryDialog(shell);
135 dirDialog.setText("Working Directory");
136 dirDialog.setMessage("Choose the working directory");
137 String dir = dirDialog.open();
138 if (dir != null)
139 configuration
140 .setAttribute(
141 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
142 dir);
143
144 } catch (CoreException e) {
145 Shell shell = Display.getCurrent().getActiveShell();
146 ErrorDialog.openError(shell, "Error",
147 "Cannot execute SLC launch shortcut", e.getStatus());
148 }
149
150 }
151
152 protected void addVmSysProperty(StringBuffer vmArgs, String suffix,
153 IVMInstall vmInstall) {
154 addSysProperty(vmArgs, VMS_PROPERTY_PREFIX + "." + suffix, vmInstall
155 .getInstallLocation().getPath());
156 }
157
158 protected void addSysProperty(StringBuffer vmArgs, String key, String value) {
159 String str = "-D" + key + "=" + value;
160 if (str.contains(" "))
161 str = "\"" + str + "\"";
162 vmArgs.append(" " + str);
163 }
164
165 protected void printVm(String prefix, IVMInstall vmInstall) {
166 System.out.println(prefix + " vmInstall: id=" + vmInstall.getId()
167 + ", name=" + vmInstall.getName() + ", installLocation="
168 + vmInstall.getInstallLocation() + ", toString=" + vmInstall);
169 if (vmInstall instanceof IVMInstall2) {
170 IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
171 System.out.println(" vmInstall: javaVersion="
172 + vmInstall2.getJavaVersion());
173 }
174 // printVm("Default", JavaRuntime.getDefaultVMInstall());
175 // IExecutionEnvironment[] execEnvs = JavaRuntime
176 // .getExecutionEnvironmentsManager()
177 // .getExecutionEnvironments();
178 // for (IExecutionEnvironment execEnv : execEnvs) {
179 // System.out.println("execEnv: id=" + execEnv.getId() + ", desc="
180 // + execEnv.getDescription());
181 // if (execEnv.getId().startsWith("J2SE")
182 // || execEnv.getId().startsWith("Java")) {
183 // IVMInstall vmInstall = execEnv.getDefaultVM();
184 // printVm("", vmInstall);
185 // }
186 // }
187
188 }
189
190 protected String convertBundleList(List<String> bundlesToStart,
191 String original) {
192 StringBuffer bufBundles = new StringBuffer(1024);
193 StringTokenizer stComa = new StringTokenizer(original, ",");
194 boolean first = true;
195 while (stComa.hasMoreTokens()) {
196 if (first)
197 first = false;
198 else
199 bufBundles.append(',');
200
201 String tkComa = stComa.nextToken();
202 int indexAt = tkComa.indexOf('@');
203 boolean modified = false;
204 if (indexAt >= 0) {
205 String bundelId = tkComa.substring(0, indexAt);
206
207 if (bundlesToStart.contains(bundelId)) {
208 bufBundles.append(bundelId).append('@').append(
209 "default:true");
210 modified = true;
211 if (debug)
212 System.out.println("Will start " + bundelId);
213 }
214 }
215
216 if (!modified)
217 bufBundles.append(tkComa);
218 }
219 String output = bufBundles.toString();
220 return output;
221 }
222
223 protected void addSelectedProjects(List<String> bundlesToStart) {
224 Assert.isNotNull(selection);
225
226 Map<String, IPluginModelBase> bundleProjects = new HashMap<String, IPluginModelBase>();
227 for (IPluginModelBase modelBase : PluginRegistry.getWorkspaceModels()) {
228 IProject bundleProject = modelBase.getUnderlyingResource()
229 .getProject();
230 bundleProjects.put(bundleProject.getName(), modelBase);
231 }
232
233 IStructuredSelection sSelection = (IStructuredSelection) selection;
234 for (Iterator<?> it = sSelection.iterator(); it.hasNext();) {
235 Object obj = it.next();
236 if (obj instanceof IProject) {
237 IProject project = (IProject) obj;
238 if (bundleProjects.containsKey(project.getName())) {
239 IPluginModelBase modelBase = bundleProjects.get(project
240 .getName());
241
242 BundleDescription bundleDescription = null;
243 if (modelBase.isFragmentModel()) {
244 BundleDescription[] hosts = modelBase
245 .getBundleDescription().getHost().getHosts();
246 for (BundleDescription bd : hosts) {
247 if (debug)
248 System.out.println("Host for "
249 + modelBase.getBundleDescription()
250 .getSymbolicName() + ": "
251 + bd.getSymbolicName());
252 bundleDescription = bd;
253 }
254 } else {
255 bundleDescription = modelBase.getBundleDescription();
256 }
257
258 if (bundleDescription != null) {
259 String symbolicName = bundleDescription
260 .getSymbolicName();
261 String bundleName = bundleDescription.getName();
262
263 bundlesToStart.add(symbolicName);
264
265 if (name.length() > 0)
266 name.append(" ");
267 if (bundleName != null)
268 name.append(bundleName);
269 else
270 name.append(symbolicName);
271 }
272 }
273 }
274 }
275 }
276
277 protected String getName(ILaunchConfigurationType type) {
278 if (name != null && !name.toString().trim().equals(""))
279 return name.toString();
280 else
281 return "SLC";
282 }
283
284 }