1 package org
.argeo
.slc
.ide
.ui
.launch
.osgi
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
10 import java
.util
.Properties
;
11 import java
.util
.StringTokenizer
;
13 import org
.argeo
.slc
.ide
.ui
.SlcIdeUiPlugin
;
14 import org
.eclipse
.core
.resources
.IFile
;
15 import org
.eclipse
.core
.resources
.IResource
;
16 import org
.eclipse
.core
.runtime
.CoreException
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.variables
.IStringVariableManager
;
20 import org
.eclipse
.core
.variables
.VariablesPlugin
;
21 import org
.eclipse
.debug
.core
.ILaunchConfiguration
;
22 import org
.eclipse
.debug
.core
.ILaunchConfigurationWorkingCopy
;
23 import org
.eclipse
.jdt
.launching
.IJavaLaunchConfigurationConstants
;
24 import org
.eclipse
.jdt
.launching
.IVMInstall
;
25 import org
.eclipse
.jdt
.launching
.IVMInstall2
;
26 import org
.eclipse
.jdt
.launching
.IVMInstallType
;
27 import org
.eclipse
.jdt
.launching
.JavaRuntime
;
28 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
29 import org
.eclipse
.pde
.ui
.launcher
.IPDELauncherConstants
;
30 import org
.eclipse
.swt
.widgets
.Display
;
32 public class OsgiLaunchHelper
implements OsgiLauncherConstants
{
33 private static Boolean debug
= false;
35 /** Expect properties file to be set as mapped resources */
36 public static void updateLaunchConfiguration(
37 ILaunchConfigurationWorkingCopy configuration
) {
39 IFile propertiesFile
= (IFile
) configuration
.getMappedResources()[0];
40 propertiesFile
.refreshLocal(IResource
.DEPTH_ONE
, null);
42 Properties properties
= OsgiLaunchHelper
43 .readProperties(propertiesFile
);
45 List
<String
> bundlesToStart
= new ArrayList
<String
>();
46 Map
<String
, String
> systemPropertiesToAppend
= new HashMap
<String
, String
>();
47 OsgiLaunchHelper
.interpretProperties(properties
, bundlesToStart
,
48 systemPropertiesToAppend
);
50 File workingDir
= getWorkingDirectory(configuration
);
51 File dataDir
= new File(workingDir
, "data");
53 OsgiLaunchHelper
.updateLaunchConfiguration(configuration
,
54 bundlesToStart
, systemPropertiesToAppend
, dataDir
56 } catch (Exception e
) {
57 ErrorDialog
.openError(Display
.getCurrent().getActiveShell(),
58 "Error", "Cannot read properties",
59 new Status(IStatus
.ERROR
, SlcIdeUiPlugin
.ID
,
65 static void updateLaunchConfiguration(
66 ILaunchConfigurationWorkingCopy configuration
,
67 List
<String
> bundlesToStart
,
68 Map
<String
, String
> systemPropertiesToAppend
, String dataDir
)
69 throws CoreException
{
70 // Convert bundle lists
71 String targetBundles
= configuration
.getAttribute(
72 IPDELauncherConstants
.TARGET_BUNDLES
, "");
73 configuration
.setAttribute(IPDELauncherConstants
.TARGET_BUNDLES
,
74 convertBundleList(bundlesToStart
, targetBundles
));
76 String wkSpaceBundles
= configuration
.getAttribute(
77 IPDELauncherConstants
.WORKSPACE_BUNDLES
, "");
78 configuration
.setAttribute(IPDELauncherConstants
.WORKSPACE_BUNDLES
,
79 convertBundleList(bundlesToStart
, wkSpaceBundles
));
81 // Update other default information
82 configuration
.setAttribute(IPDELauncherConstants
.DEFAULT_AUTO_START
,
85 // VM arguments (system properties)
86 String defaultVmArgs
= configuration
.getAttribute(
87 OsgiLauncherConstants
.ATTR_DEFAULT_VM_ARGS
, "");
88 StringBuffer vmArgs
= new StringBuffer(defaultVmArgs
);
90 // Data dir system property
92 addSysProperty(vmArgs
, OsgiLauncherConstants
.ARGEO_OSGI_DATA_DIR
,
94 // Add locations of JVMs
95 if (configuration
.getAttribute(ATTR_ADD_JVM_PATHS
, false))
98 // Add other system properties
99 for (String key
: systemPropertiesToAppend
.keySet())
100 addSysProperty(vmArgs
, key
, systemPropertiesToAppend
.get(key
));
102 vmArgs
.append(" ").append(
103 configuration
.getAttribute(ATTR_ADDITIONAL_VM_ARGS
, ""));
105 configuration
.setAttribute(
106 IJavaLaunchConfigurationConstants
.ATTR_VM_ARGUMENTS
, vmArgs
110 StringBuffer progArgs
= new StringBuffer("");
111 if (dataDir
!= null) {
112 progArgs
.append("-data ");
113 progArgs
.append(surroundSpaces(dataDir
));
115 String additionalProgramArgs
= configuration
.getAttribute(
116 OsgiLauncherConstants
.ATTR_ADDITIONAL_PROGRAM_ARGS
, "");
117 progArgs
.append(' ').append(additionalProgramArgs
);
118 configuration
.setAttribute(
119 IJavaLaunchConfigurationConstants
.ATTR_PROGRAM_ARGUMENTS
,
120 progArgs
.toString());
123 protected static void addVms(StringBuffer vmArgs
) {
124 addVmSysProperty(vmArgs
, "default", JavaRuntime
.getDefaultVMInstall());
125 IVMInstallType
[] vmTypes
= JavaRuntime
.getVMInstallTypes();
126 for (IVMInstallType vmType
: vmTypes
) {
127 for (IVMInstall vmInstall
: vmType
.getVMInstalls()) {
128 // printVm("", vmInstall);
129 // properties based on name
130 addVmSysProperty(vmArgs
, vmInstall
.getName(), vmInstall
);
131 if (vmInstall
instanceof IVMInstall2
) {
132 // properties based on version
133 IVMInstall2 vmInstall2
= (IVMInstall2
) vmInstall
;
134 String version
= vmInstall2
.getJavaVersion();
135 addVmSysProperty(vmArgs
, version
, vmInstall
);
137 List
<String
> tokens
= new ArrayList
<String
>();
138 StringTokenizer st
= new StringTokenizer(version
, ".");
139 while (st
.hasMoreTokens())
140 tokens
.add(st
.nextToken());
141 if (tokens
.size() >= 2)
142 addVmSysProperty(vmArgs
, tokens
.get(0) + "."
143 + tokens
.get(1), vmInstall
);
150 protected static void addVmSysProperty(StringBuffer vmArgs
, String suffix
,
151 IVMInstall vmInstall
) {
152 addSysProperty(vmArgs
, OsgiLauncherConstants
.VMS_PROPERTY_PREFIX
+ "."
153 + suffix
, vmInstall
.getInstallLocation().getPath());
156 protected static void addSysProperty(StringBuffer vmArgs
, String key
,
158 surroundSpaces(value
);
159 String str
= "-D" + key
+ "=" + value
;
160 // surroundSpaces(str);
161 vmArgs
.append(' ').append(str
);
164 protected static String
surroundSpaces(String str
) {
165 if (str
.indexOf(' ') >= 0)
166 return '\"' + str
+ '\"';
171 protected static String
convertBundleList(List
<String
> bundlesToStart
,
173 StringBuffer bufBundles
= new StringBuffer(1024);
174 StringTokenizer stComa
= new StringTokenizer(original
, ",");
175 boolean first
= true;
176 bundles
: while (stComa
.hasMoreTokens()) {
180 bufBundles
.append(',');
182 String bundleId
= stComa
.nextToken();
183 int indexAt
= bundleId
.indexOf('@');
184 boolean modified
= false;
186 bundleId
= bundleId
.substring(0, indexAt
);
189 if (bundleId
.endsWith(".source")) {
191 System
.out
.println("Skip source bundle " + bundleId
);
195 if (bundlesToStart
.contains(bundleId
)) {
196 bufBundles
.append(bundleId
).append('@').append("default:true");
199 System
.out
.println("Will start " + bundleId
);
203 bufBundles
.append(bundleId
);
205 String output
= bufBundles
.toString();
209 protected static Properties
readProperties(IFile file
) throws CoreException
{
210 Properties props
= new Properties();
212 InputStream in
= null;
214 in
= file
.getContents();
216 } catch (IOException e
) {
217 throw new CoreException(new Status(IStatus
.ERROR
,
218 SlcIdeUiPlugin
.ID
, "Cannot read properties file", e
));
223 } catch (IOException e
) {
230 protected static void interpretProperties(Properties properties
,
231 List
<String
> bundlesToStart
,
232 Map
<String
, String
> systemPropertiesToAppend
) {
233 String argeoOsgiStart
= properties
234 .getProperty(OsgiLauncherConstants
.ARGEO_OSGI_START
);
235 if (argeoOsgiStart
!= null) {
236 StringTokenizer st
= new StringTokenizer(argeoOsgiStart
, ",");
237 while (st
.hasMoreTokens())
238 bundlesToStart
.add(st
.nextToken());
241 propKeys
: for (Object keyObj
: properties
.keySet()) {
242 String key
= keyObj
.toString();
243 if (OsgiLauncherConstants
.ARGEO_OSGI_START
.equals(key
))
245 else if (OsgiLauncherConstants
.ARGEO_OSGI_BUNDLES
.equals(key
))
247 else if (OsgiLauncherConstants
.ARGEO_OSGI_LOCATIONS
.equals(key
))
249 else if (OsgiLauncherConstants
.OSGI_BUNDLES
.equals(key
))
252 systemPropertiesToAppend
.put(key
, properties
.getProperty(key
));
258 // org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper.getWorkingDirectory(ILaunchConfiguration)
259 public static File
getWorkingDirectory(ILaunchConfiguration configuration
)
260 throws CoreException
{
263 working
= configuration
.getAttribute(
264 IJavaLaunchConfigurationConstants
.ATTR_WORKING_DIRECTORY
,
265 new File(".").getCanonicalPath()); //$NON-NLS-1$
266 } catch (IOException e
) {
267 working
= "${workspace_loc}/../"; //$NON-NLS-1$
269 File dir
= new File(getSubstitutedString(working
));
276 // org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper.getSubstitutedString(String)
277 private static String
getSubstitutedString(String text
)
278 throws CoreException
{
280 return ""; //$NON-NLS-1$
281 IStringVariableManager mgr
= VariablesPlugin
.getDefault()
282 .getStringVariableManager();
283 return mgr
.performStringSubstitution(text
);
286 // static void initializeConfiguration(
287 // ILaunchConfigurationWorkingCopy configuration) {
288 // new OSGiLaunchConfigurationInitializer().initialize(configuration);