1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 import java.util.Set ; 23 import java.util.StringTokenizer ; 24 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.core.variables.IStringVariableManager; 30 import org.eclipse.core.variables.VariablesPlugin; 31 import org.eclipse.debug.core.ILaunchConfiguration; 32 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 33 import org.eclipse.osgi.service.resolver.BundleDescription; 34 import org.eclipse.pde.core.plugin.IPluginElement; 35 import org.eclipse.pde.core.plugin.IPluginExtension; 36 import org.eclipse.pde.core.plugin.IPluginModelBase; 37 import org.eclipse.pde.core.plugin.IPluginObject; 38 import org.eclipse.pde.core.plugin.PluginRegistry; 39 import org.eclipse.pde.core.plugin.TargetPlatform; 40 import org.eclipse.pde.internal.core.PDECore; 41 import org.eclipse.pde.internal.core.TargetPlatformHelper; 42 import org.eclipse.pde.internal.ui.PDEPlugin; 43 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 44 45 public class LaunchConfigurationHelper { 46 47 public static void synchronizeManifests(ILaunchConfiguration config, File configDir) { 48 try { 49 String programArgs = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, 50 ""); if (programArgs.indexOf("-clean") != -1) return; 53 } catch (CoreException e) { 54 } 55 File dir = new File (configDir, "org.eclipse.osgi/manifests"); if (dir.exists() && dir.isDirectory()) { 57 PDECore.getDefault().getJavaElementChangeListener().synchronizeManifests(dir); 58 } 59 } 60 61 public static File getConfigurationArea(ILaunchConfiguration config) { 62 File dir = getConfigurationLocation(config); 63 if (!dir.exists()) 64 dir.mkdirs(); 65 return dir; 66 } 67 68 public static File getConfigurationLocation(ILaunchConfiguration config) { 69 File dir = new File (PDECore.getDefault().getStateLocation().toOSString(), config.getName()); 70 try { 71 if (!config.getAttribute(IPDELauncherConstants.CONFIG_USE_DEFAULT_AREA, true)) { 72 String userPath = config.getAttribute(IPDELauncherConstants.CONFIG_LOCATION, (String )null); 73 if (userPath != null) { 74 userPath = getSubstitutedString(userPath); 75 dir = new File (userPath).getAbsoluteFile(); 76 } 77 } 78 } catch (CoreException e) { 79 } 80 return dir; 81 } 82 83 private static String getSubstitutedString(String text) throws CoreException { 84 if (text == null) 85 return ""; IStringVariableManager mgr = VariablesPlugin.getDefault().getStringVariableManager(); 87 return mgr.performStringSubstitution(text); 88 } 89 90 public static Properties createConfigIniFile(ILaunchConfiguration configuration, String productID, Map map, File directory) throws CoreException { 91 Properties properties = null; 92 if (configuration.getAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true)) { 94 properties = TargetPlatformHelper.getConfigIniProperties(); 95 if (properties == null) 97 properties = new Properties (); 98 else if (productID == null || !productID.equals(properties.get("eclipse.product"))) properties.clear(); 101 String bundleList = properties.getProperty("osgi.bundles"); if (bundleList != null) 104 properties.setProperty("osgi.bundles", computeOSGiBundles(TargetPlatformHelper.stripPathInformation(bundleList), map)); } else { 106 String templateLoc = configuration.getAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, (String )null); 107 if (templateLoc != null) { 108 properties = loadFromTemplate(getSubstitutedString(templateLoc)); 109 String osgiBundles = properties.getProperty("osgi.bundles"); if (osgiBundles != null) 112 properties.setProperty("osgi.bundles", TargetPlatformHelper.stripPathInformation(osgiBundles)); } 114 } 115 if (properties != null) 117 addRequiredProperties(properties, productID, map); 118 else 119 properties = new Properties (); 120 setBundleLocations(map, properties); 121 if (!directory.exists()) 122 directory.mkdirs(); 123 save(new File (directory, "config.ini"), properties); return properties; 125 } 126 127 private static void addRequiredProperties(Properties properties, String productID, Map map) { 128 if (!properties.containsKey("osgi.install.area")) properties.setProperty("osgi.install.area", "file:" + TargetPlatform.getLocation()); if (!properties.containsKey("osgi.configuration.cascaded")) properties.setProperty("osgi.configuration.cascaded", "false"); if (!properties.containsKey("osgi.framework")) properties.setProperty("osgi.framework", "org.eclipse.osgi"); if (!properties.containsKey("osgi.splashPath") && productID != null) addSplashLocation(properties, productID, map); 136 if (properties.containsKey("osgi.splashPath")) resolveLocationPath(properties.getProperty("osgi.splashPath"), properties, map); if (!properties.containsKey("osgi.bundles")) properties.setProperty("osgi.bundles", computeOSGiBundles(TargetPlatform.getBundleList(), map)); if (!properties.containsKey("osgi.bundles.defaultStartLevel")) properties.setProperty("osgi.bundles.defaultStartLevel", "4"); } 144 145 private static String computeOSGiBundles(String bundleList, Map map) { 146 StringBuffer buffer = new StringBuffer (); 147 Set initialBundleSet = new HashSet (); 148 StringTokenizer tokenizer = new StringTokenizer (bundleList, ","); while (tokenizer.hasMoreTokens()) { 150 String token = tokenizer.nextToken(); 151 int index = token.indexOf('@'); 152 String id = index != -1 ? token.substring(0, index) : token; 153 if (map.containsKey(id)) { 154 if (buffer.length() > 0) 155 buffer.append(','); 156 buffer.append(id); 157 if (index != -1 && index < token.length() -1) 158 buffer.append(token.substring(index)); 159 initialBundleSet.add(id); 160 } 161 } 162 if (!initialBundleSet.contains("org.eclipse.update.configurator")) { initialBundleSet.add("org.eclipse.osgi"); Iterator iter = map.keySet().iterator(); 166 while (iter.hasNext()) { 167 String id = iter.next().toString(); 168 if (!initialBundleSet.contains(id)) { 169 if (buffer.length() > 0) 170 buffer.append(','); 171 buffer.append(id); 172 } 173 } 174 } 175 return buffer.toString(); 176 } 177 178 private static Properties loadFromTemplate(String templateLoc) throws CoreException { 179 Properties properties = new Properties (); 180 File templateFile = new File (templateLoc); 181 if (templateFile.exists() && templateFile.isFile()) { 182 FileInputStream stream = null; 183 try { 184 stream = new FileInputStream (templateFile); 185 properties.load(stream); 186 } catch (Exception e) { 187 String message = e.getMessage(); 188 if (message != null) 189 throw new CoreException( 190 new Status( 191 IStatus.ERROR, 192 PDEPlugin.getPluginId(), 193 IStatus.ERROR, 194 message, 195 e)); 196 } finally { 197 if (stream != null) { 198 try { 199 stream.close(); 200 } catch (IOException e) { 201 } 202 } 203 } 204 } 205 return properties; 206 } 207 208 private static void addSplashLocation(Properties properties, String productID, Map map) { 209 Properties targetConfig = TargetPlatformHelper.getConfigIniProperties(); 210 String targetProduct = targetConfig == null ? null : targetConfig.getProperty("eclipse.product"); String targetSplash = targetConfig == null ? null : targetConfig.getProperty("osgi.splashPath"); if (!productID.equals(targetProduct) || targetSplash == null) { 213 ArrayList locations = new ArrayList (); 214 String plugin = getContributingPlugin(productID); 215 locations.add(plugin); 216 IPluginModelBase model = (IPluginModelBase)map.get(plugin); 217 if (model != null) { 218 BundleDescription desc = model.getBundleDescription(); 219 if (desc != null) { 220 BundleDescription[] fragments = desc.getFragments(); 221 for (int i = 0; i < fragments.length; i++) 222 locations.add(fragments[i].getSymbolicName()); 223 } 224 } 225 resolveLocationPath(locations, properties, map); 226 } else 227 resolveLocationPath(targetSplash, properties, map); 228 } 229 230 private static void resolveLocationPath(String splashPath, Properties properties, Map map) { 231 ArrayList locations = new ArrayList (); 232 StringTokenizer tok = new StringTokenizer (splashPath, ","); while (tok.hasMoreTokens()) 234 locations.add(tok.nextToken()); 235 resolveLocationPath(locations, properties, map); 236 } 237 238 private static void resolveLocationPath(ArrayList locations, Properties properties, Map map) { 239 StringBuffer buffer = new StringBuffer (); 240 for (int i = 0; i < locations.size(); i++) { 241 String location = (String )locations.get(i); 242 if (location.startsWith("platform:/base/plugins/")) { location = location.replaceFirst("platform:/base/plugins/", ""); } 245 String url = getBundleURL(location, map); 246 if (url == null) 247 continue; 248 if (buffer.length() > 0) 249 buffer.append(","); buffer.append(url); 251 } 252 if (buffer.length() > 0) 253 properties.setProperty("osgi.splashPath", buffer.toString()); } 255 256 public static String getBundleURL(String id, Map pluginMap) { 257 IPluginModelBase model = (IPluginModelBase)pluginMap.get(id.trim()); 258 return getBundleURL(model); 259 } 260 261 public static String getBundleURL(IPluginModelBase model) { 262 if (model == null) 263 return null; 264 return "file:" + new Path(model.getInstallLocation()).removeTrailingSeparator().toString(); } 266 267 private static void setBundleLocations(Map map, Properties properties) { 268 String framework = properties.getProperty("osgi.framework"); if (framework != null) { 270 if (framework.startsWith("platform:/base/plugins/")) { framework.replaceFirst("platform:/base/plugins/", ""); } 273 String url = getBundleURL(framework, map); 274 if (url != null) 275 properties.setProperty("osgi.framework", url); } 277 278 String bundles = properties.getProperty("osgi.bundles"); if (bundles != null) { 280 StringBuffer buffer = new StringBuffer (); 281 StringTokenizer tokenizer = new StringTokenizer (bundles, ","); while (tokenizer.hasMoreTokens()) { 283 String token = tokenizer.nextToken().trim(); 284 String url = getBundleURL(token, map); 285 int index = -1; 286 if (url == null) { 287 index = token.indexOf('@'); 288 if (index != -1) 289 url = getBundleURL(token.substring(0,index), map); 290 if (url == null) { 291 index = token.indexOf(':'); 292 if (index != -1) 293 url = getBundleURL(token.substring(0,index), map); 294 } 295 } 296 if (url != null) { 297 if (buffer.length() > 0) { 298 buffer.append(","); } 300 buffer.append("reference:" + url); if (index != -1) 302 buffer.append(token.substring(index)); 303 } 304 } 305 properties.setProperty("osgi.bundles", buffer.toString()); } 307 } 308 309 public static void save(File file, Properties properties) { 310 try { 311 FileOutputStream stream = new FileOutputStream (file); 312 properties.store(stream, "Configuration File"); stream.flush(); 314 stream.close(); 315 } catch (IOException e) { 316 PDECore.logException(e); 317 } 318 } 319 320 public static String getContributingPlugin(String productID) { 321 if (productID == null) 322 return null; 323 int index = productID.lastIndexOf('.'); 324 return index == -1 ? productID : productID.substring(0, index); 325 } 326 327 public static String getProductID(ILaunchConfiguration configuration) 328 throws CoreException { 329 if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) { 330 return configuration.getAttribute(IPDELauncherConstants.PRODUCT, 331 (String ) null); 332 } 333 334 String appID = configuration.getAttribute(IPDELauncherConstants.APPLICATION, 337 TargetPlatform.getDefaultApplication()); 338 IPluginModelBase[] plugins = PluginRegistry.getActiveModels(); 339 for (int i = 0; i < plugins.length; i++) { 340 String id = plugins[i].getPluginBase().getId(); 341 IPluginExtension[] extensions = plugins[i].getPluginBase().getExtensions(); 342 for (int j = 0; j < extensions.length; j++) { 343 String point = extensions[j].getPoint(); 344 String extId = extensions[j].getId(); 345 if ("org.eclipse.core.runtime.products".equals(point) && extId != null) { IPluginObject[] children = extensions[j].getChildren(); 347 if (children.length != 1) 348 continue; 349 if (!"product".equals(children[0].getName())) continue; 351 if (appID.equals(((IPluginElement) children[0]).getAttribute( 352 "application").getValue())) { return id + "." + extId; } 355 } 356 } 357 } 358 return null; 359 360 } 361 362 } 363 | Popular Tags |