1 11 package org.eclipse.pde.internal.build.site; 12 13 import java.io.File ; 14 import java.net.URL ; 15 import java.util.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.core.runtime.model.*; 18 import org.eclipse.osgi.service.resolver.*; 19 import org.eclipse.pde.internal.build.*; 20 import org.osgi.framework.Constants; 21 import org.osgi.framework.Version; 22 23 26 public class PluginRegistryConverter extends PDEState { 27 private PluginRegistryModel registry; 28 29 private URL [] removeInvalidURLs(URL [] files) { 30 URL [] validURLs = new URL [files.length]; 31 int validURL = 0; 32 for (int i = 0; i < files.length; i++) { 33 if (!files[i].toExternalForm().endsWith("feature.xml") && !files[i].toExternalForm().endsWith("MANIFEST.MF")) validURLs[validURL++] = files[i]; 35 } 36 if (files.length == validURL) 37 return validURLs; 38 URL [] result = new URL [validURL]; 39 System.arraycopy(validURLs, 0, result, 0, validURL); 40 return result; 41 } 42 43 private PluginRegistryModel getPluginRegistry(URL [] files) throws CoreException { 44 if (registry == null) { 45 files = removeInvalidURLs(files); 46 MultiStatus problems = new MultiStatus(IPDEBuildConstants.PI_PDEBUILD, EXCEPTION_MODEL_PARSE, Messages.exception_pluginParse, null); 48 Factory factory = new Factory(problems); 49 registry = PluginRegistryModel.parsePlugins(files, factory); 50 registry.resolve(true, false); 51 IStatus status = factory.getStatus(); 52 if (!status.isOK()) 53 throw new CoreException(status); 54 } 55 return registry; 56 } 57 58 public void addRegistryToState() { 59 PluginModel[] plugins = registry.getPlugins(); 60 PluginFragmentModel[] fragments = registry.getFragments(); 61 62 for (int i = 0; i < plugins.length; i++) { 63 BundleDescription bd = state.getFactory().createBundleDescription(getNextId(), plugins[i].getPluginId(), Version.parseVersion(plugins[i].getVersion()), plugins[i].getLocation(), createBundleSpecification(plugins[i].getRequires()), (HostSpecification) null, null, null, null, true); 64 String libs = createClasspath(plugins[i].getRuntime()); 65 Properties manifest = new Properties(); 66 if (libs != null) 67 manifest.put(Constants.BUNDLE_CLASSPATH, libs); 68 loadPropertyFileIn(manifest, new File (fragments[i].getLocation())); 69 bd.setUserObject(manifest); 70 addBundleDescription(bd); 71 } 72 73 for (int i = 0; i < fragments.length; i++) { 74 HostSpecification host = state.getFactory().createHostSpecification(fragments[i].getPluginId(), new VersionRange(fragments[i].getPluginVersion())); 75 BundleDescription bd = state.getFactory().createBundleDescription(getNextId(), fragments[i].getId(), Version.parseVersion(fragments[i].getVersion()), fragments[i].getLocation(), createBundleSpecification(fragments[i].getRequires()), host, null, null, null, true); 76 String libs = createClasspath(fragments[i].getRuntime()); 77 Properties manifest = new Properties(); 78 if (libs != null) 79 manifest.put(Constants.BUNDLE_CLASSPATH, libs); 80 loadPropertyFileIn(manifest, new File (fragments[i].getLocation())); 81 bd.setUserObject(manifest); 82 addBundleDescription(bd); 83 } 84 } 85 86 protected BundleSpecification[] createBundleSpecification(PluginPrerequisiteModel[] prereqs) { 87 if (prereqs == null) 88 return new BundleSpecification[0]; 89 BundleSpecification[] specs = new BundleSpecification[prereqs.length]; 90 for (int i = 0; i < prereqs.length; i++) { 91 specs[i] = state.getFactory().createBundleSpecification(prereqs[i].getPlugin(), new VersionRange(prereqs[i].getVersion()), prereqs[i].getExport(), prereqs[i].getOptional()); 92 } 93 return specs; 94 } 95 96 private String createClasspath(LibraryModel[] libs) { 97 if (libs == null || libs.length == 0) 98 return null; 99 100 String result = ""; for (int i = 0; i < libs.length; i++) { 102 result += libs[i].getName() + (i == libs.length - 1 ? "" : ","); } 104 return result; 105 } 106 107 public void addBundles(Collection bundles) { 108 try { 109 getPluginRegistry(Utils.asURL(bundles)); 110 } catch (CoreException e) { 111 IStatus status = new Status(IStatus.ERROR, IPDEBuildConstants.PI_PDEBUILD, EXCEPTION_STATE_PROBLEM, Messages.exception_registryResolution, e); 112 BundleHelper.getDefault().getLog().log(status); 113 } 114 for (Iterator iter = bundles.iterator(); iter.hasNext();) { 115 File bundle = (File ) iter.next(); 116 addBundle(bundle); 117 } 118 } 119 } 120 | Popular Tags |