1 package org
.argeo
.slc
.ui
.launch
.osgi
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
5 import java
.util
.Iterator
;
8 import java
.util
.StringTokenizer
;
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
;
33 public class SlcLaunchShortcut
extends OSGiLaunchShortcut
{
34 public final static String VMS_PROPERTY_PREFIX
= "slc.launch.vm";
36 private Boolean debug
= false;
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";
43 private ISelection selection
= null;
44 private StringBuffer name
= null;
46 private final List
<String
> defaultBundlesToStart
= new ArrayList
<String
>();
47 private List
<String
> bundlesToStart
= new ArrayList
<String
>();
49 public SlcLaunchShortcut() {
51 defaultBundlesToStart
.add(springOsgiExtenderId
);
52 defaultBundlesToStart
.add(slcSupportEquinoxId
);
53 // defaultBundlesToStart.add(slcAgentId);
56 public void launch(ISelection selection
, String mode
) {
57 this.selection
= selection
;
58 this.name
= new StringBuffer();
60 bundlesToStart
= new ArrayList
<String
>();
61 bundlesToStart
.addAll(defaultBundlesToStart
);
63 if (selection
!= null) {
64 addSelectedProjects(bundlesToStart
);
67 super.launch(selection
, mode
);
70 this.selection
= null;
72 bundlesToStart
= null;
75 protected void initializeConfiguration(
76 ILaunchConfigurationWorkingCopy configuration
) {
78 super.initializeConfiguration(configuration
);
80 // Convert bundle lists
81 String targetBundles
= configuration
.getAttribute(
82 IPDELauncherConstants
.TARGET_BUNDLES
, "");
83 configuration
.setAttribute(IPDELauncherConstants
.TARGET_BUNDLES
,
84 convertBundleList(bundlesToStart
, targetBundles
));
86 String wkSpaceBundles
= configuration
.getAttribute(
87 IPDELauncherConstants
.WORKSPACE_BUNDLES
, "");
88 configuration
.setAttribute(IPDELauncherConstants
.WORKSPACE_BUNDLES
,
89 convertBundleList(bundlesToStart
, wkSpaceBundles
));
91 // Update other default information
92 configuration
.setAttribute(
93 IPDELauncherConstants
.DEFAULT_AUTO_START
, false);
94 configuration
.setAttribute(IPDELauncherConstants
.CONFIG_CLEAR_AREA
,
96 String defaultVmArgs
= configuration
.getAttribute(
97 IJavaLaunchConfigurationConstants
.ATTR_VM_ARGUMENTS
, "");
98 StringBuffer vmArgs
= new StringBuffer(defaultVmArgs
);
99 vmArgs
.append(" -Xmx256m");
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
);
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
);
128 configuration
.setAttribute(
129 IJavaLaunchConfigurationConstants
.ATTR_VM_ARGUMENTS
, vmArgs
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();
141 IJavaLaunchConfigurationConstants
.ATTR_WORKING_DIRECTORY
,
144 } catch (CoreException e
) {
145 Shell shell
= Display
.getCurrent().getActiveShell();
146 ErrorDialog
.openError(shell
, "Error",
147 "Cannot execute SLC launch shortcut", e
.getStatus());
152 protected void addVmSysProperty(StringBuffer vmArgs
, String suffix
,
153 IVMInstall vmInstall
) {
154 addSysProperty(vmArgs
, VMS_PROPERTY_PREFIX
+ "." + suffix
, vmInstall
155 .getInstallLocation().getPath());
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
);
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());
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);
190 protected String
convertBundleList(List
<String
> bundlesToStart
,
192 StringBuffer bufBundles
= new StringBuffer(1024);
193 StringTokenizer stComa
= new StringTokenizer(original
, ",");
194 boolean first
= true;
195 while (stComa
.hasMoreTokens()) {
199 bufBundles
.append(',');
201 String tkComa
= stComa
.nextToken();
202 int indexAt
= tkComa
.indexOf('@');
203 boolean modified
= false;
205 String bundelId
= tkComa
.substring(0, indexAt
);
207 if (bundlesToStart
.contains(bundelId
)) {
208 bufBundles
.append(bundelId
).append('@').append(
212 System
.out
.println("Will start " + bundelId
);
217 bufBundles
.append(tkComa
);
219 String output
= bufBundles
.toString();
223 protected void addSelectedProjects(List
<String
> bundlesToStart
) {
224 Assert
.isNotNull(selection
);
226 Map
<String
, IPluginModelBase
> bundleProjects
= new HashMap
<String
, IPluginModelBase
>();
227 for (IPluginModelBase modelBase
: PluginRegistry
.getWorkspaceModels()) {
228 IProject bundleProject
= modelBase
.getUnderlyingResource()
230 bundleProjects
.put(bundleProject
.getName(), modelBase
);
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
242 BundleDescription bundleDescription
= null;
243 if (modelBase
.isFragmentModel()) {
244 BundleDescription
[] hosts
= modelBase
245 .getBundleDescription().getHost().getHosts();
246 for (BundleDescription bd
: hosts
) {
248 System
.out
.println("Host for "
249 + modelBase
.getBundleDescription()
250 .getSymbolicName() + ": "
251 + bd
.getSymbolicName());
252 bundleDescription
= bd
;
255 bundleDescription
= modelBase
.getBundleDescription();
258 if (bundleDescription
!= null) {
259 String symbolicName
= bundleDescription
261 String bundleName
= bundleDescription
.getName();
263 bundlesToStart
.add(symbolicName
);
265 if (name
.length() > 0)
267 if (bundleName
!= null)
268 name
.append(bundleName
);
270 name
.append(symbolicName
);
277 protected String
getName(ILaunchConfigurationType type
) {
278 if (name
!= null && !name
.toString().trim().equals(""))
279 return name
.toString();