1 package org
.argeo
.slc
.ide
.ui
.launch
.osgi
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
8 import java
.util
.Properties
;
9 import java
.util
.StringTokenizer
;
11 import org
.argeo
.slc
.ide
.ui
.SlcIdeUiPlugin
;
12 import org
.eclipse
.core
.resources
.IFile
;
13 import org
.eclipse
.core
.runtime
.CoreException
;
14 import org
.eclipse
.core
.runtime
.IStatus
;
15 import org
.eclipse
.core
.runtime
.Status
;
16 import org
.eclipse
.debug
.core
.ILaunchConfigurationWorkingCopy
;
17 import org
.eclipse
.jdt
.launching
.IJavaLaunchConfigurationConstants
;
18 import org
.eclipse
.jdt
.launching
.IVMInstall
;
19 import org
.eclipse
.jdt
.launching
.IVMInstall2
;
20 import org
.eclipse
.jdt
.launching
.IVMInstallType
;
21 import org
.eclipse
.jdt
.launching
.JavaRuntime
;
22 import org
.eclipse
.pde
.ui
.launcher
.IPDELauncherConstants
;
24 public class OsgiLaunchHelper
{
25 private static Boolean debug
= false;
27 public static void updateLaunchConfiguration(
28 ILaunchConfigurationWorkingCopy configuration
,
29 List
<String
> bundlesToStart
,
30 Map
<String
, String
> systemPropertiesToAppend
, String workingDir
,
31 String dataDir
) throws CoreException
{
32 // Convert bundle lists
33 String targetBundles
= configuration
.getAttribute(
34 IPDELauncherConstants
.TARGET_BUNDLES
, "");
35 configuration
.setAttribute(IPDELauncherConstants
.TARGET_BUNDLES
,
36 convertBundleList(bundlesToStart
, targetBundles
));
38 String wkSpaceBundles
= configuration
.getAttribute(
39 IPDELauncherConstants
.WORKSPACE_BUNDLES
, "");
40 configuration
.setAttribute(IPDELauncherConstants
.WORKSPACE_BUNDLES
,
41 convertBundleList(bundlesToStart
, wkSpaceBundles
));
43 // Update other default information
44 configuration
.setAttribute(IPDELauncherConstants
.DEFAULT_AUTO_START
,
46 configuration
.setAttribute(IPDELauncherConstants
.CONFIG_CLEAR_AREA
,
48 String defaultVmArgs
= configuration
.getAttribute(
49 OsgiLauncherConstants
.ATTR_DEFAULT_VM_ARGS
, "");
50 StringBuffer vmArgs
= new StringBuffer(defaultVmArgs
);
51 vmArgs
.append(" -Xmx256m");
53 // Add locations of JVMs
54 addVmSysProperty(vmArgs
, "default", JavaRuntime
.getDefaultVMInstall());
55 IVMInstallType
[] vmTypes
= JavaRuntime
.getVMInstallTypes();
56 for (IVMInstallType vmType
: vmTypes
) {
57 for (IVMInstall vmInstall
: vmType
.getVMInstalls()) {
58 // printVm("", vmInstall);
59 // properties based on name
60 addVmSysProperty(vmArgs
, vmInstall
.getName(), vmInstall
);
61 if (vmInstall
instanceof IVMInstall2
) {
62 // properties based on version
63 IVMInstall2 vmInstall2
= (IVMInstall2
) vmInstall
;
64 String version
= vmInstall2
.getJavaVersion();
65 addVmSysProperty(vmArgs
, version
, vmInstall
);
67 List
<String
> tokens
= new ArrayList
<String
>();
68 StringTokenizer st
= new StringTokenizer(version
, ".");
69 while (st
.hasMoreTokens())
70 tokens
.add(st
.nextToken());
71 if (tokens
.size() >= 2)
72 addVmSysProperty(vmArgs
, tokens
.get(0) + "."
73 + tokens
.get(1), vmInstall
);
78 // Add other system properties
79 for (String key
: systemPropertiesToAppend
.keySet())
80 addSysProperty(vmArgs
, key
, systemPropertiesToAppend
.get(key
));
83 addSysProperty(vmArgs
, OsgiLauncherConstants
.ARGEO_OSGI_DATA_DIR
,
86 configuration
.setAttribute(
87 IJavaLaunchConfigurationConstants
.ATTR_VM_ARGUMENTS
, vmArgs
91 String defaultProgArgs
= configuration
.getAttribute(
92 OsgiLauncherConstants
.ATTR_DEFAULT_PROGRAM_ARGS
, "");
93 StringBuffer progArgs
= new StringBuffer(defaultProgArgs
);
94 if (dataDir
!= null) {
95 progArgs
.append(" -data ");
96 progArgs
.append(surroundSpaces(dataDir
));
98 configuration
.setAttribute(
99 IJavaLaunchConfigurationConstants
.ATTR_PROGRAM_ARGUMENTS
,
100 progArgs
.toString());
102 // String dir = findWorkingDirectory();
103 if (workingDir
!= null)
104 configuration
.setAttribute(
105 IJavaLaunchConfigurationConstants
.ATTR_WORKING_DIRECTORY
,
110 protected static void addVmSysProperty(StringBuffer vmArgs
, String suffix
,
111 IVMInstall vmInstall
) {
112 addSysProperty(vmArgs
, OsgiLauncherConstants
.VMS_PROPERTY_PREFIX
+ "."
113 + suffix
, vmInstall
.getInstallLocation().getPath());
116 protected static void addSysProperty(StringBuffer vmArgs
, String key
,
118 String str
= "-D" + key
+ "=" + value
;
120 vmArgs
.append(" " + str
);
123 protected static String
surroundSpaces(String str
) {
124 if (str
.contains(" "))
125 return "\"" + str
+ "\"";
130 protected static String
convertBundleList(List
<String
> bundlesToStart
,
132 StringBuffer bufBundles
= new StringBuffer(1024);
133 StringTokenizer stComa
= new StringTokenizer(original
, ",");
134 boolean first
= true;
135 bundles
: while (stComa
.hasMoreTokens()) {
139 bufBundles
.append(',');
141 String bundleId
= stComa
.nextToken();
142 int indexAt
= bundleId
.indexOf('@');
143 boolean modified
= false;
145 bundleId
= bundleId
.substring(0, indexAt
);
148 if (bundleId
.endsWith(".source")) {
150 System
.out
.println("Skip source bundle " + bundleId
);
154 if (bundlesToStart
.contains(bundleId
)) {
155 bufBundles
.append(bundleId
).append('@').append("default:true");
158 System
.out
.println("Will start " + bundleId
);
162 bufBundles
.append(bundleId
);
164 String output
= bufBundles
.toString();
168 protected static Properties
readProperties(IFile file
) throws CoreException
{
169 Properties props
= new Properties();
171 InputStream in
= null;
173 in
= file
.getContents();
175 } catch (IOException e
) {
176 throw new CoreException(new Status(IStatus
.ERROR
,
177 SlcIdeUiPlugin
.ID
, "Cannot read properties file", e
));
182 } catch (IOException e
) {
189 protected static void interpretProperties(Properties properties
,
190 List
<String
> bundlesToStart
,
191 Map
<String
, String
> systemPropertiesToAppend
) {
192 String argeoOsgiStart
= properties
193 .getProperty(OsgiLauncherConstants
.ARGEO_OSGI_START
);
194 StringTokenizer st
= new StringTokenizer(argeoOsgiStart
, ",");
195 while (st
.hasMoreTokens())
196 bundlesToStart
.add(st
.nextToken());
198 propKeys
: for (Object keyObj
: properties
.keySet()) {
199 String key
= keyObj
.toString();
200 if (OsgiLauncherConstants
.ARGEO_OSGI_START
.equals(key
))
202 else if (OsgiLauncherConstants
.ARGEO_OSGI_BUNDLES
.equals(key
))
204 else if (OsgiLauncherConstants
.ARGEO_OSGI_LOCATIONS
.equals(key
))
206 else if (OsgiLauncherConstants
.OSGI_BUNDLES
.equals(key
))
209 systemPropertiesToAppend
.put(key
, properties
.getProperty(key
));