1 package org
.argeo
.slc
.ide
.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
.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
;
32 public class SlcLaunchShortcut
extends OSGiLaunchShortcut
{
33 public final static String VMS_PROPERTY_PREFIX
= "slc.launch.vm";
35 private Boolean debug
= false;
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";
42 private ISelection selection
= null;
43 private StringBuffer name
= null;
45 private final List
<String
> defaultBundlesToStart
= new ArrayList
<String
>();
46 private List
<String
> bundlesToStart
= new ArrayList
<String
>();
48 public SlcLaunchShortcut() {
50 defaultBundlesToStart
.add(springOsgiExtenderId
);
51 defaultBundlesToStart
.add(slcSupportEquinoxId
);
52 // defaultBundlesToStart.add(slcAgentId);
55 public void launch(ISelection selection
, String mode
) {
56 this.selection
= selection
;
57 this.name
= new StringBuffer();
59 bundlesToStart
= new ArrayList
<String
>();
60 bundlesToStart
.addAll(defaultBundlesToStart
);
62 if (selection
!= null) {
63 addSelectedProjects(bundlesToStart
);
66 super.launch(selection
, mode
);
69 this.selection
= null;
71 bundlesToStart
= null;
74 protected void initializeConfiguration(
75 ILaunchConfigurationWorkingCopy configuration
) {
77 super.initializeConfiguration(configuration
);
79 // Convert bundle lists
80 String targetBundles
= configuration
.getAttribute(
81 IPDELauncherConstants
.TARGET_BUNDLES
, "");
82 configuration
.setAttribute(IPDELauncherConstants
.TARGET_BUNDLES
,
83 convertBundleList(bundlesToStart
, targetBundles
));
85 String wkSpaceBundles
= configuration
.getAttribute(
86 IPDELauncherConstants
.WORKSPACE_BUNDLES
, "");
87 configuration
.setAttribute(IPDELauncherConstants
.WORKSPACE_BUNDLES
,
88 convertBundleList(bundlesToStart
, wkSpaceBundles
));
90 // Update other default information
91 configuration
.setAttribute(
92 IPDELauncherConstants
.DEFAULT_AUTO_START
, false);
93 configuration
.setAttribute(IPDELauncherConstants
.CONFIG_CLEAR_AREA
,
95 String defaultVmArgs
= configuration
.getAttribute(
96 IJavaLaunchConfigurationConstants
.ATTR_VM_ARGUMENTS
, "");
97 StringBuffer vmArgs
= new StringBuffer(defaultVmArgs
);
98 vmArgs
.append(" -Xmx256m");
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
);
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
);
127 configuration
.setAttribute(
128 IJavaLaunchConfigurationConstants
.ATTR_VM_ARGUMENTS
, vmArgs
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();
140 IJavaLaunchConfigurationConstants
.ATTR_WORKING_DIRECTORY
,
143 } catch (CoreException e
) {
144 Shell shell
= Display
.getCurrent().getActiveShell();
145 ErrorDialog
.openError(shell
, "Error",
146 "Cannot execute SLC launch shortcut", e
.getStatus());
151 protected void addVmSysProperty(StringBuffer vmArgs
, String suffix
,
152 IVMInstall vmInstall
) {
153 addSysProperty(vmArgs
, VMS_PROPERTY_PREFIX
+ "." + suffix
, vmInstall
154 .getInstallLocation().getPath());
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
);
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());
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);
189 protected String
convertBundleList(List
<String
> bundlesToStart
,
191 StringBuffer bufBundles
= new StringBuffer(1024);
192 StringTokenizer stComa
= new StringTokenizer(original
, ",");
193 boolean first
= true;
194 while (stComa
.hasMoreTokens()) {
198 bufBundles
.append(',');
200 String tkComa
= stComa
.nextToken();
201 int indexAt
= tkComa
.indexOf('@');
202 boolean modified
= false;
204 String bundelId
= tkComa
.substring(0, indexAt
);
206 if (bundlesToStart
.contains(bundelId
)) {
207 bufBundles
.append(bundelId
).append('@').append(
211 System
.out
.println("Will start " + bundelId
);
216 bufBundles
.append(tkComa
);
218 String output
= bufBundles
.toString();
222 protected void addSelectedProjects(List
<String
> bundlesToStart
) {
223 Assert
.isNotNull(selection
);
225 Map
<String
, IPluginModelBase
> bundleProjects
= new HashMap
<String
, IPluginModelBase
>();
226 for (IPluginModelBase modelBase
: PluginRegistry
.getWorkspaceModels()) {
227 IProject bundleProject
= modelBase
.getUnderlyingResource()
229 bundleProjects
.put(bundleProject
.getName(), modelBase
);
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
241 BundleDescription bundleDescription
= null;
242 if (modelBase
.isFragmentModel()) {
243 BundleDescription
[] hosts
= modelBase
244 .getBundleDescription().getHost().getHosts();
245 for (BundleDescription bd
: hosts
) {
247 System
.out
.println("Host for "
248 + modelBase
.getBundleDescription()
249 .getSymbolicName() + ": "
250 + bd
.getSymbolicName());
251 bundleDescription
= bd
;
254 bundleDescription
= modelBase
.getBundleDescription();
257 if (bundleDescription
!= null) {
258 String symbolicName
= bundleDescription
260 String bundleName
= bundleDescription
.getName();
262 bundlesToStart
.add(symbolicName
);
264 if (name
.length() > 0)
266 if (bundleName
!= null)
267 name
.append(bundleName
);
269 name
.append(symbolicName
);
276 protected String
getName(ILaunchConfigurationType type
) {
277 if (name
!= null && !name
.toString().trim().equals(""))
278 return name
.toString();