]> 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/SlcLaunchShortcut.java
Refactor / rename
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ide.ui / src / main / java / org / argeo / slc / ide / ui / launch / osgi / SlcLaunchShortcut.java
1 package org.argeo.slc.ide.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 addVmSysProperty(vmArgs, "default", JavaRuntime
102 .getDefaultVMInstall());
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 addVmSysProperty(vmArgs, vmInstall.getName(), vmInstall);
111 if (vmInstall instanceof IVMInstall2) {
112 // properties based on version
113 IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
114 String version = vmInstall2.getJavaVersion();
115 addVmSysProperty(vmArgs, version, vmInstall);
116
117 List<String> tokens = new ArrayList<String>();
118 StringTokenizer st = new StringTokenizer(version, ".");
119 while (st.hasMoreTokens())
120 tokens.add(st.nextToken());
121 if (tokens.size() >= 2)
122 addVmSysProperty(vmArgs, tokens.get(0) + "."
123 + tokens.get(1), vmInstall);
124 }
125 }
126 }
127 configuration.setAttribute(
128 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs
129 .toString());
130
131 // Choose working directory
132 Shell shell = Display.getCurrent().getActiveShell();
133 DirectoryDialog dirDialog = new DirectoryDialog(shell);
134 dirDialog.setText("Working Directory");
135 dirDialog.setMessage("Choose the working directory");
136 String dir = dirDialog.open();
137 if (dir != null)
138 configuration
139 .setAttribute(
140 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
141 dir);
142
143 } catch (CoreException e) {
144 Shell shell = Display.getCurrent().getActiveShell();
145 ErrorDialog.openError(shell, "Error",
146 "Cannot execute SLC launch shortcut", e.getStatus());
147 }
148
149 }
150
151 protected void addVmSysProperty(StringBuffer vmArgs, String suffix,
152 IVMInstall vmInstall) {
153 addSysProperty(vmArgs, VMS_PROPERTY_PREFIX + "." + suffix, vmInstall
154 .getInstallLocation().getPath());
155 }
156
157 protected void addSysProperty(StringBuffer vmArgs, String key, String value) {
158 String str = "-D" + key + "=" + value;
159 if (str.contains(" "))
160 str = "\"" + str + "\"";
161 vmArgs.append(" " + str);
162 }
163
164 protected void printVm(String prefix, IVMInstall vmInstall) {
165 System.out.println(prefix + " vmInstall: id=" + vmInstall.getId()
166 + ", name=" + vmInstall.getName() + ", installLocation="
167 + vmInstall.getInstallLocation() + ", toString=" + vmInstall);
168 if (vmInstall instanceof IVMInstall2) {
169 IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
170 System.out.println(" vmInstall: javaVersion="
171 + vmInstall2.getJavaVersion());
172 }
173 // printVm("Default", JavaRuntime.getDefaultVMInstall());
174 // IExecutionEnvironment[] execEnvs = JavaRuntime
175 // .getExecutionEnvironmentsManager()
176 // .getExecutionEnvironments();
177 // for (IExecutionEnvironment execEnv : execEnvs) {
178 // System.out.println("execEnv: id=" + execEnv.getId() + ", desc="
179 // + execEnv.getDescription());
180 // if (execEnv.getId().startsWith("J2SE")
181 // || execEnv.getId().startsWith("Java")) {
182 // IVMInstall vmInstall = execEnv.getDefaultVM();
183 // printVm("", vmInstall);
184 // }
185 // }
186
187 }
188
189 protected String convertBundleList(List<String> bundlesToStart,
190 String original) {
191 StringBuffer bufBundles = new StringBuffer(1024);
192 StringTokenizer stComa = new StringTokenizer(original, ",");
193 boolean first = true;
194 while (stComa.hasMoreTokens()) {
195 if (first)
196 first = false;
197 else
198 bufBundles.append(',');
199
200 String tkComa = stComa.nextToken();
201 int indexAt = tkComa.indexOf('@');
202 boolean modified = false;
203 if (indexAt >= 0) {
204 String bundelId = tkComa.substring(0, indexAt);
205
206 if (bundlesToStart.contains(bundelId)) {
207 bufBundles.append(bundelId).append('@').append(
208 "default:true");
209 modified = true;
210 if (debug)
211 System.out.println("Will start " + bundelId);
212 }
213 }
214
215 if (!modified)
216 bufBundles.append(tkComa);
217 }
218 String output = bufBundles.toString();
219 return output;
220 }
221
222 protected void addSelectedProjects(List<String> bundlesToStart) {
223 Assert.isNotNull(selection);
224
225 Map<String, IPluginModelBase> bundleProjects = new HashMap<String, IPluginModelBase>();
226 for (IPluginModelBase modelBase : PluginRegistry.getWorkspaceModels()) {
227 IProject bundleProject = modelBase.getUnderlyingResource()
228 .getProject();
229 bundleProjects.put(bundleProject.getName(), modelBase);
230 }
231
232 IStructuredSelection sSelection = (IStructuredSelection) selection;
233 for (Iterator<?> it = sSelection.iterator(); it.hasNext();) {
234 Object obj = it.next();
235 if (obj instanceof IProject) {
236 IProject project = (IProject) obj;
237 if (bundleProjects.containsKey(project.getName())) {
238 IPluginModelBase modelBase = bundleProjects.get(project
239 .getName());
240
241 BundleDescription bundleDescription = null;
242 if (modelBase.isFragmentModel()) {
243 BundleDescription[] hosts = modelBase
244 .getBundleDescription().getHost().getHosts();
245 for (BundleDescription bd : hosts) {
246 if (debug)
247 System.out.println("Host for "
248 + modelBase.getBundleDescription()
249 .getSymbolicName() + ": "
250 + bd.getSymbolicName());
251 bundleDescription = bd;
252 }
253 } else {
254 bundleDescription = modelBase.getBundleDescription();
255 }
256
257 if (bundleDescription != null) {
258 String symbolicName = bundleDescription
259 .getSymbolicName();
260 String bundleName = bundleDescription.getName();
261
262 bundlesToStart.add(symbolicName);
263
264 if (name.length() > 0)
265 name.append(" ");
266 if (bundleName != null)
267 name.append(bundleName);
268 else
269 name.append(symbolicName);
270 }
271 }
272 }
273 }
274 }
275
276 protected String getName(ILaunchConfigurationType type) {
277 if (name != null && !name.toString().trim().equals(""))
278 return name.toString();
279 else
280 return "SLC";
281 }
282
283 }