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