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
.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 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
+ "."
119 + vmInstall
.getInstallLocation());
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());
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);
147 configuration
.setAttribute(
148 IJavaLaunchConfigurationConstants
.ATTR_VM_ARGUMENTS
, vmArgs
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();
160 IJavaLaunchConfigurationConstants
.ATTR_WORKING_DIRECTORY
,
163 } catch (CoreException e
) {
164 Shell shell
= Display
.getCurrent().getActiveShell();
165 ErrorDialog
.openError(shell
, "Error",
166 "Cannot execute SLC launch shortcut", e
.getStatus());
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());
182 protected String
convertBundleList(List
<String
> bundlesToStart
,
184 StringBuffer bufBundles
= new StringBuffer(1024);
185 StringTokenizer stComa
= new StringTokenizer(original
, ",");
186 boolean first
= true;
187 while (stComa
.hasMoreTokens()) {
191 bufBundles
.append(',');
193 String tkComa
= stComa
.nextToken();
194 int indexAt
= tkComa
.indexOf('@');
195 boolean modified
= false;
197 String bundelId
= tkComa
.substring(0, indexAt
);
199 if (bundlesToStart
.contains(bundelId
)) {
200 bufBundles
.append(bundelId
).append('@').append(
204 System
.out
.println("Will start " + bundelId
);
209 bufBundles
.append(tkComa
);
211 String output
= bufBundles
.toString();
215 protected void addSelectedProjects(List
<String
> bundlesToStart
) {
216 Assert
.isNotNull(selection
);
218 Map
<String
, IPluginModelBase
> bundleProjects
= new HashMap
<String
, IPluginModelBase
>();
219 for (IPluginModelBase modelBase
: PluginRegistry
.getWorkspaceModels()) {
220 IProject bundleProject
= modelBase
.getUnderlyingResource()
222 bundleProjects
.put(bundleProject
.getName(), modelBase
);
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
234 BundleDescription bundleDescription
= null;
235 if (modelBase
.isFragmentModel()) {
236 BundleDescription
[] hosts
= modelBase
237 .getBundleDescription().getHost().getHosts();
238 for (BundleDescription bd
: hosts
) {
240 System
.out
.println("Host for "
241 + modelBase
.getBundleDescription()
242 .getSymbolicName() + ": "
243 + bd
.getSymbolicName());
244 bundleDescription
= bd
;
247 bundleDescription
= modelBase
.getBundleDescription();
250 if (bundleDescription
!= null) {
251 String symbolicName
= bundleDescription
253 String bundleName
= bundleDescription
.getName();
255 bundlesToStart
.add(symbolicName
);
257 if (name
.length() > 0)
259 if (bundleName
!= null)
260 name
.append(bundleName
);
262 name
.append(symbolicName
);
269 protected String
getName(ILaunchConfigurationType type
) {
270 if (name
!= null && !name
.toString().trim().equals(""))
271 return name
.toString();