1 11 package org.eclipse.pde.internal.build.builder; 12 13 import java.io.IOException ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.*; 17 import org.eclipse.core.internal.boot.PlatformURLHandler; 18 import org.eclipse.core.internal.runtime.PlatformURLFragmentConnection; 19 import org.eclipse.core.internal.runtime.PlatformURLPluginConnection; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.osgi.service.resolver.BundleDescription; 22 import org.eclipse.osgi.service.resolver.HostSpecification; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.pde.internal.build.*; 25 import org.eclipse.pde.internal.build.site.PDEState; 26 27 public class ClasspathComputer2_1 implements IClasspathComputer, IPDEBuildConstants, IXMLConstants, IBuildPropertiesConstants { 28 private ModelBuildScriptGenerator generator; 29 30 public ClasspathComputer2_1(ModelBuildScriptGenerator modelGenerator) { 31 this.generator = modelGenerator; 32 } 33 34 43 public List getClasspath(BundleDescription model, ModelBuildScriptGenerator.CompiledEntry jar) throws CoreException { 44 List classpath = new ArrayList(20); 45 List pluginChain = new ArrayList(10); 46 Set addedPlugins=new HashSet(20); 47 String location = generator.getLocation(model); 48 49 addPlugin(getPlugin(PI_BOOT, null), classpath, location); 51 52 addSelf(model, jar, classpath, location, pluginChain,addedPlugins); 54 55 addPrerequisites(model, classpath, location, pluginChain,addedPlugins); 57 58 return classpath; 59 60 } 61 62 69 private void addPlugin(BundleDescription plugin, List classpath, String location) throws CoreException { 70 addRuntimeLibraries(plugin, classpath, location); 71 addFragmentsLibraries(plugin, classpath, location); 72 } 73 74 81 private void addRuntimeLibraries(BundleDescription model, List classpath, String baseLocation) throws CoreException { 82 String [] libraries = getClasspathEntries(model); 83 String root = generator.getLocation(model); 84 IPath base = Utils.makeRelative(new Path(root), new Path(baseLocation)); 85 Properties modelProps = getBuildPropertiesFor(model); 86 for (int i = 0; i < libraries.length; i++) { 87 addDevEntries(model, baseLocation, classpath, Utils.getArrayFromString(generator.getBuildProperties().getProperty(PROPERTY_OUTPUT_PREFIX + libraries[i]))); 88 addPathAndCheck(model.getSymbolicName(), base, libraries[i], modelProps, classpath); 89 } 90 } 91 92 102 private BundleDescription getPlugin(String id, String version) throws CoreException { 103 return generator.getSite(false).getRegistry().getResolvedBundle(id, version); 104 } 105 106 113 private void addFragmentsLibraries(BundleDescription plugin, List classpath, String baseLocation) throws CoreException { 114 BundleDescription[] fragments = plugin.getFragments(); 116 if (fragments == null) 117 return; 118 119 for (int i = 0; i < fragments.length; i++) { 120 if (fragments[i] == generator.getModel()) 121 continue; 122 addPluginLibrariesToFragmentLocations(plugin, fragments[i], classpath, baseLocation); 123 addRuntimeLibraries(fragments[i], classpath, baseLocation); 124 } 125 } 126 127 138 private void addPluginLibrariesToFragmentLocations(BundleDescription plugin, BundleDescription fragment, List classpath, String baseLocation) throws CoreException { 139 144 String [] libraries = getClasspathEntries(plugin); 145 String root = generator.getLocation(fragment); 146 IPath base = Utils.makeRelative(new Path(root), new Path(baseLocation)); 147 Properties modelProps = getBuildPropertiesFor(fragment); 148 for (int i = 0; i < libraries.length; i++) { 149 addPathAndCheck(fragment.getSymbolicName(), base, libraries[i], modelProps, classpath); 150 } 151 } 152 153 private Properties getBuildPropertiesFor(BundleDescription bundle) { 154 try { 155 return AbstractScriptGenerator.readProperties(generator.getLocation(bundle), PROPERTIES_FILE, IStatus.OK); 156 } catch (CoreException e) { 157 } 159 return null; 160 } 161 162 private void addPathAndCheck(String pluginId, IPath basePath, String libraryName, Properties modelProperties, List classpath) { 166 String path = basePath.append(libraryName).toString(); 167 path = generator.replaceVariables(path, pluginId == null ? false : generator.getCompiledElements().contains(pluginId)); 168 if (generator.getCompiledElements().contains(pluginId)) { 169 if (modelProperties == null || modelProperties.getProperty("source." + libraryName) != null) path = Utils.getPropertyFormat(PROPERTY_BUILD_RESULT_FOLDER) + '/' + path; 171 } 172 if (!classpath.contains(path)) 173 classpath.add(path); 174 } 175 176 private void addSelf(BundleDescription model, ModelBuildScriptGenerator.CompiledEntry jar, List classpath, String location, List pluginChain, Set addedPlugins) throws CoreException { 177 HostSpecification host = model.getHost(); 179 if (host != null) { 180 BundleDescription[] hosts = host.getHosts(); 181 for(int i=0; i<hosts.length; i++) 182 addPluginAndPrerequisites(hosts[i], classpath, location, pluginChain, addedPlugins); 183 } 184 185 Properties modelProperties = generator.getBuildProperties(); 187 String jarOrder = (String ) modelProperties.get(PROPERTY_JAR_ORDER); 188 if (jarOrder == null) { 189 String [] libraries = getClasspathEntries(model); 192 if (libraries != null) { 193 for (int i = 0; i < libraries.length; i++) { 194 String libraryName = libraries[i]; 195 if (jar.getName(false).equals(libraryName)) 196 continue; 197 198 boolean isSource = (modelProperties.getProperty(PROPERTY_SOURCE_PREFIX + libraryName) != null); 199 if (isSource) { 200 addDevEntries(model, location, classpath, Utils.getArrayFromString(modelProperties.getProperty(PROPERTY_OUTPUT_PREFIX + libraryName))); 201 } 202 addPathAndCheck(model.getSymbolicName(), Path.EMPTY, libraryName, modelProperties, classpath); 206 } 207 } 208 } else { 209 String [] order = Utils.getArrayFromString(jarOrder); 211 for (int i = 0; i < order.length; i++) { 212 if (order[i].equals(jar.getName(false))) 213 break; 214 addDevEntries(model, location, classpath, Utils.getArrayFromString((String ) modelProperties.get(PROPERTY_OUTPUT_PREFIX + order[i]))); 215 addPathAndCheck(model.getSymbolicName(), Path.EMPTY, order[i], modelProperties, classpath); 216 } 217 String [] libraries = getClasspathEntries(model); 219 for (int i = 0; i < libraries.length; i++) { 220 String libraryName = libraries[i]; 221 if (modelProperties.get(PROPERTY_SOURCE_PREFIX + libraryName) == null) { 222 addPathAndCheck(model.getSymbolicName(), Path.EMPTY, libraryName, modelProperties, classpath); 225 } 226 } 227 } 228 229 String extraClasspath = (String ) modelProperties.get(PROPERTY_JAR_EXTRA_CLASSPATH); 231 if (extraClasspath != null) { 232 String [] extra = Utils.getArrayFromString(extraClasspath, ";,"); 234 for (int i = 0; i < extra.length; i++) { 235 addPathAndCheck(null, new Path(computeExtraPath(extra[i], location)), "", modelProperties, classpath); } 239 } 240 241 String [] jarSpecificExtraClasspath = jar.getExtraClasspath(); 243 for (int i = 0; i < jarSpecificExtraClasspath.length; i++) { 244 addPathAndCheck(null, new Path(computeExtraPath(jarSpecificExtraClasspath[i], location)), "", modelProperties, classpath); } 248 } 249 250 257 private String computeExtraPath(String url, String location) throws CoreException { 258 String relativePath = null; 259 260 String [] urlfragments = Utils.getArrayFromString(url, "/"); 262 if (urlfragments.length > 2 && urlfragments[0].equals(PlatformURLHandler.PROTOCOL + PlatformURLHandler.PROTOCOL_SEPARATOR)) { 264 String modelLocation = null; 265 if (urlfragments[1].equalsIgnoreCase(PlatformURLPluginConnection.PLUGIN)) 266 modelLocation = generator.getLocation(generator.getSite(false).getRegistry().getResolvedBundle(urlfragments[2])); 267 268 if (urlfragments[1].equalsIgnoreCase(PlatformURLFragmentConnection.FRAGMENT)) 269 modelLocation = generator.getLocation(generator.getSite(false).getRegistry().getResolvedBundle(urlfragments[2])); 270 271 if (urlfragments[1].equalsIgnoreCase("resource")) { String message = NLS.bind(Messages.exception_url, generator.getPropertiesFileName() + "::" + url); throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, message, null)); 274 } 275 if (modelLocation != null) { 276 for (int i = 3; i < urlfragments.length; i++) { 277 if (i == 3) 278 modelLocation += urlfragments[i]; 279 else 280 modelLocation += '/' + urlfragments[i]; 281 } 282 return relativePath = Utils.makeRelative(new Path(modelLocation), new Path(location)).toOSString(); 283 } 284 } 285 286 try { 288 URL extraURL = new URL (url); 289 try { 290 relativePath = Utils.makeRelative(new Path(Platform.resolve(extraURL).getFile()), new Path(location)).toOSString(); 291 } catch (IOException e) { 292 String message = NLS.bind(Messages.exception_url, generator.getPropertiesFileName() + "::" + url); throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, message, e)); 294 } 295 } catch (MalformedURLException e) { 296 String message = NLS.bind(Messages.exception_url, PROPERTIES_FILE + "::" + url); throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, IPDEBuildConstants.EXCEPTION_MALFORMED_URL, message, e)); 298 } 299 return relativePath; 300 } 301 302 private void addPrerequisites(BundleDescription target, List classpath, String baseLocation, List pluginChain, Set addedPlugins) throws CoreException { 304 305 if (pluginChain.contains(target)) { 306 if (target == getPlugin(PI_RUNTIME, null)) 307 return; 308 String cycleString = ""; for (Iterator iter = pluginChain.iterator(); iter.hasNext();) 310 cycleString += iter.next().toString() + ", "; cycleString += target.toString(); 312 String message = NLS.bind(Messages.error_pluginCycle, cycleString); 313 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_CLASSPATH_CYCLE, message, null)); 314 } 315 316 if (addedPlugins.contains(target)){ 317 return; 318 } 319 320 if (target != getPlugin(PI_RUNTIME, null)) 322 addPluginAndPrerequisites(getPlugin(PI_RUNTIME, null), classpath, baseLocation, pluginChain,addedPlugins); 323 324 BundleDescription[] requires = PDEState.getDependentBundles(target); 327 if (requires != null) { 328 pluginChain.add(target); 329 for (int i = 0; i < requires.length; i++) { 330 BundleDescription plugin = getPlugin(requires[i].getSymbolicName(), requires[i].getVersion().toString()); 331 if (plugin != null) 332 addPluginAndPrerequisites(plugin, classpath, baseLocation, pluginChain,addedPlugins); 333 } 334 pluginChain.remove(target); 335 addedPlugins.add(target); 336 } 337 338 } 339 340 351 private void addPluginAndPrerequisites(BundleDescription target, List classpath, String baseLocation, List pluginChain, Set addedPlugins) throws CoreException { 352 addPlugin(target, classpath, baseLocation); 353 addPrerequisites(target, classpath, baseLocation, pluginChain,addedPlugins); 354 } 355 356 362 private void addDevEntries(BundleDescription model, String baseLocation, List classpath, String [] jarSpecificEntries) { 363 if (generator.devEntries == null && (jarSpecificEntries == null || jarSpecificEntries.length == 0)) 364 return; 365 366 String [] entries; 367 if (jarSpecificEntries != null && jarSpecificEntries.length > 0) 369 entries = jarSpecificEntries; 370 else 371 entries = generator.devEntries.getDevClassPath(model.getSymbolicName()); 372 373 IPath root = Utils.makeRelative(new Path(generator.getLocation(model)), new Path(baseLocation)); 374 for (int i = 0; i < entries.length; i++) { 375 addPathAndCheck(model.getSymbolicName(), root, entries[i], null, classpath); 376 } 377 } 378 379 private String [] getClasspathEntries(BundleDescription bundle) throws CoreException { 381 return generator.getClasspathEntries(bundle); 382 } 383 } 384 | Popular Tags |