001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.IOException;
013: import java.io.InputStream;
014: import java.net.URL;
015: import org.eclipse.core.runtime.*;
016: import org.eclipse.osgi.util.NLS;
017: import org.eclipse.pde.internal.build.*;
018: import org.eclipse.update.core.*;
019: import org.eclipse.update.core.model.FeatureModel;
020: import org.eclipse.update.internal.core.FeatureExecutableContentProvider;
021: import org.eclipse.update.internal.core.URLEncoder;
022:
023: /**
024: *
025: *
026: */
027: public class BuildTimeFeatureFactory extends BaseFeatureFactory
028: implements IFeatureFactory, IPDEBuildConstants,
029: IBuildPropertiesConstants {
030: public final static String BUILDTIME_FEATURE_FACTORY_ID = PI_PDEBUILD
031: + ".BuildTimeFeatureFactory"; //$NON-NLS-1$
032:
033: public IFeature createFeature(URL url, ISite site,
034: IProgressMonitor p) throws CoreException {
035: Feature feature = null;
036: InputStream featureStream = null;
037:
038: if (url == null)
039: return createFeature(site);
040:
041: try {
042: // TODO FeatureExecutableContentProvider is a non API class
043: IFeatureContentProvider contentProvider = new FeatureExecutableContentProvider(
044: url);
045:
046: URL nonResolvedURL = contentProvider
047: .getFeatureManifestReference(null).asURL();
048: URL resolvedURL = URLEncoder.encode(nonResolvedURL);
049:
050: featureStream = resolvedURL.openStream();
051: feature = (Feature) this .parseFeature(featureStream);
052:
053: String qualifier = AbstractScriptGenerator.readProperties(
054: new Path(url.getFile()).removeLastSegments(1)
055: .toOSString(), PROPERTIES_FILE, IStatus.OK)
056: .getProperty(PROPERTY_QUALIFIER);
057: String newVersion = QualifierReplacer
058: .replaceQualifierInVersion(feature
059: .getFeatureVersion(), feature
060: .getFeatureIdentifier(), qualifier,
061: ((BuildTimeSite) site).getFeatureVersions());
062: if (newVersion != null) {
063: //a feature version ending in qualifier using context will be further modified based on its included plugins
064: if (feature.getFeatureVersion().endsWith(
065: PROPERTY_QUALIFIER)
066: && (qualifier == null || qualifier
067: .equalsIgnoreCase(PROPERTY_CONTEXT))) {
068: int idx = feature.getFeatureVersion().lastIndexOf(
069: "."); //$NON-NLS-1$
070: ((BuildTimeFeature) feature)
071: .setContextQualifierLength(newVersion
072: .length()
073: - idx - 1);
074: }
075: ((BuildTimeFeature) feature)
076: .setFeatureVersion(newVersion);
077: }
078:
079: feature.setSite(site);
080: feature.setFeatureContentProvider(contentProvider);
081: feature.resolve(url, url);
082: } catch (CoreException e) {
083: String message = NLS.bind(Messages.error_creatingFeature,
084: url);
085: BundleHelper.getDefault().getLog().log(
086: new Status(IStatus.ERROR, PI_PDEBUILD,
087: EXCEPTION_FEATURE_PARSE, message, e));
088: throw e;
089: } catch (Exception e) {
090: String message = NLS.bind(Messages.exception_readingFile,
091: url);
092: Status status = new Status(IStatus.ERROR, PI_PDEBUILD,
093: EXCEPTION_FEATURE_MISSING, message, e);
094: BundleHelper.getDefault().getLog().log(status); //Logging here because the caller consumes CoreExceptions.
095: throw new CoreException(status);
096: } finally {
097: try {
098: if (featureStream != null)
099: featureStream.close();
100: } catch (IOException e) {
101: //ignore
102: }
103: }
104: return feature;
105: }
106:
107: /*
108: * Creates an empty feature on the site
109: */
110: private IFeature createFeature(ISite site) throws CoreException {
111: Feature feature = null;
112: //TODO FeatureExecutableContentProvider is a non API class
113: IFeatureContentProvider contentProvider = new FeatureExecutableContentProvider(
114: null);
115: feature = (Feature) createFeatureModel();
116: feature.setSite(site);
117: feature.setFeatureContentProvider(contentProvider);
118: return feature;
119: }
120:
121: public FeatureModel createFeatureModel() {
122: return new BuildTimeFeature();
123: }
124: }
|