001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build.site;
011:
012: import java.io.File;
013: import java.net.URL;
014: import java.util.*;
015: import org.eclipse.core.runtime.*;
016: import org.eclipse.core.runtime.model.*;
017: import org.eclipse.osgi.service.resolver.*;
018: import org.eclipse.pde.internal.build.*;
019: import org.osgi.framework.Constants;
020: import org.osgi.framework.Version;
021:
022: /**
023: * @deprecated
024: */
025: public class PluginRegistryConverter extends PDEState {
026: private PluginRegistryModel registry;
027:
028: private URL[] removeInvalidURLs(URL[] files) {
029: URL[] validURLs = new URL[files.length];
030: int validURL = 0;
031: for (int i = 0; i < files.length; i++) {
032: if (!files[i].toExternalForm().endsWith("feature.xml") && !files[i].toExternalForm().endsWith("MANIFEST.MF")) //$NON-NLS-1$//$NON-NLS-2$
033: validURLs[validURL++] = files[i];
034: }
035: if (files.length == validURL)
036: return validURLs;
037: URL[] result = new URL[validURL];
038: System.arraycopy(validURLs, 0, result, 0, validURL);
039: return result;
040: }
041:
042: private PluginRegistryModel getPluginRegistry(URL[] files)
043: throws CoreException {
044: if (registry == null) {
045: files = removeInvalidURLs(files);
046: // create the registry according to the site where the code to compile is, and a existing installation of eclipse
047: MultiStatus problems = new MultiStatus(
048: IPDEBuildConstants.PI_PDEBUILD,
049: EXCEPTION_MODEL_PARSE,
050: Messages.exception_pluginParse, null);
051: Factory factory = new Factory(problems);
052: registry = PluginRegistryModel.parsePlugins(files, factory);
053: registry.resolve(true, false);
054: IStatus status = factory.getStatus();
055: if (!status.isOK())
056: throw new CoreException(status);
057: }
058: return registry;
059: }
060:
061: public void addRegistryToState() {
062: PluginModel[] plugins = registry.getPlugins();
063: PluginFragmentModel[] fragments = registry.getFragments();
064:
065: for (int i = 0; i < plugins.length; i++) {
066: BundleDescription bd = state.getFactory()
067: .createBundleDescription(
068: getNextId(),
069: plugins[i].getPluginId(),
070: Version.parseVersion(plugins[i]
071: .getVersion()),
072: plugins[i].getLocation(),
073: createBundleSpecification(plugins[i]
074: .getRequires()),
075: (HostSpecification) null, null, null, null,
076: true);
077: String libs = createClasspath(plugins[i].getRuntime());
078: Properties manifest = new Properties();
079: if (libs != null)
080: manifest.put(Constants.BUNDLE_CLASSPATH, libs);
081: loadPropertyFileIn(manifest, new File(fragments[i]
082: .getLocation()));
083: bd.setUserObject(manifest);
084: addBundleDescription(bd);
085: }
086:
087: for (int i = 0; i < fragments.length; i++) {
088: HostSpecification host = state.getFactory()
089: .createHostSpecification(
090: fragments[i].getPluginId(),
091: new VersionRange(fragments[i]
092: .getPluginVersion()));
093: BundleDescription bd = state.getFactory()
094: .createBundleDescription(
095: getNextId(),
096: fragments[i].getId(),
097: Version.parseVersion(fragments[i]
098: .getVersion()),
099: fragments[i].getLocation(),
100: createBundleSpecification(fragments[i]
101: .getRequires()), host, null, null,
102: null, true);
103: String libs = createClasspath(fragments[i].getRuntime());
104: Properties manifest = new Properties();
105: if (libs != null)
106: manifest.put(Constants.BUNDLE_CLASSPATH, libs);
107: loadPropertyFileIn(manifest, new File(fragments[i]
108: .getLocation()));
109: bd.setUserObject(manifest);
110: addBundleDescription(bd);
111: }
112: }
113:
114: protected BundleSpecification[] createBundleSpecification(
115: PluginPrerequisiteModel[] prereqs) {
116: if (prereqs == null)
117: return new BundleSpecification[0];
118: BundleSpecification[] specs = new BundleSpecification[prereqs.length];
119: for (int i = 0; i < prereqs.length; i++) {
120: specs[i] = state.getFactory().createBundleSpecification(
121: prereqs[i].getPlugin(),
122: new VersionRange(prereqs[i].getVersion()),
123: prereqs[i].getExport(), prereqs[i].getOptional());
124: }
125: return specs;
126: }
127:
128: private String createClasspath(LibraryModel[] libs) {
129: if (libs == null || libs.length == 0)
130: return null;
131:
132: String result = ""; //$NON-NLS-1$
133: for (int i = 0; i < libs.length; i++) {
134: result += libs[i].getName()
135: + (i == libs.length - 1 ? "" : ","); //$NON-NLS-1$//$NON-NLS-2$
136: }
137: return result;
138: }
139:
140: public void addBundles(Collection bundles) {
141: try {
142: getPluginRegistry(Utils.asURL(bundles));
143: } catch (CoreException e) {
144: IStatus status = new Status(IStatus.ERROR,
145: IPDEBuildConstants.PI_PDEBUILD,
146: EXCEPTION_STATE_PROBLEM,
147: Messages.exception_registryResolution, e);
148: BundleHelper.getDefault().getLog().log(status);
149: }
150: for (Iterator iter = bundles.iterator(); iter.hasNext();) {
151: File bundle = (File) iter.next();
152: addBundle(bundle);
153: }
154: }
155: }
|