Skip to content

Commit

Permalink
[PDE-Build] Use Path instead of URL to represent local files
Browse files Browse the repository at this point in the history
This avoids unnecessary conversations that also use deprecated methods
like File.toURL()
  • Loading branch information
HannesWell committed Jul 27, 2024
1 parent 1387bb1 commit 134bf6e
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void testBug187809() throws Exception {
assertResourceFile(buildFolder, "features/sdk/feature.xml");
IFile feature = buildFolder.getFile("features/sdk/feature.xml");
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocationURI().toURL());
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocation().toPath());

FeatureEntry[] included = model.getIncludedFeatureReferences();
assertEquals(included.length, 3);
Expand Down Expand Up @@ -485,7 +485,7 @@ public void testBug198536() throws Exception {
buildFolder.getLocation().toOSString(), null);

BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocationURI().toURL());
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocation().toPath());
FeatureEntry[] pluginEntryModels = feature.getPluginEntries();
assertEquals(pluginEntryModels[0].getId(), "foo");
assertEquals(pluginEntryModels[0].getVersion(), "1.0.0.vA");
Expand Down Expand Up @@ -532,7 +532,7 @@ public void testBug206679() throws Exception {

BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
try {
factory.parseBuildFeature(featureXML.getLocationURI().toURL());
factory.parseBuildFeature(featureXML.getLocation().toPath());
} catch (CoreException e) {
assertTrue(e.getStatus().toString().indexOf(Messages.feature_parse_emptyRequires) > 0);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void testBug206679_247198() throws Exception {
IFolder jdtSource = buildFolder.getFolder("features").getFolder("jdt.source");
IFile featureXML = jdtSource.getFile("feature.xml");
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocationURI().toURL());
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocation().toPath());
assertTrue(feature.getDescription() != null);
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public void testbug184920() throws Exception {
IFile feature = buildFolder.getFile("features/a.feature.source/feature.xml");

BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
factory.parseBuildFeature(feature.getLocationURI().toURL());
factory.parseBuildFeature(feature.getLocation().toPath());
}

// test that source can come before the feature it is based on
Expand Down Expand Up @@ -302,7 +302,7 @@ public void testBug228537() throws Exception {
IFile featureFile = buildFolder.getFile("features/source/feature.xml");

BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
BuildTimeFeature feature = factory.parseBuildFeature(featureFile.getLocationURI().toURL());
BuildTimeFeature feature = factory.parseBuildFeature(featureFile.getLocation().toPath());
FeatureEntry[] entries = feature.getRawIncludedFeatureReferences();
assertTrue(entries.length == 1);
assertEquals(entries[0].getId(), "org.eclipse.rcp");
Expand Down Expand Up @@ -374,7 +374,7 @@ public void testIndividualSourceBundles() throws Exception {

IFile feature = buildFolder.getFile("features/rcp.source/feature.xml");
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocationURI().toURL());
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocation().toPath());

FeatureEntry[] included = model.getPluginEntries();
assertEquals(included.length, 2);
Expand Down Expand Up @@ -661,7 +661,7 @@ public void testBug290828() throws Exception {
IFile feature = buildFolder.getFile("tmp/eclipse/features/f.source_1.0.0/feature.xml");
assertResourceFile(feature);
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocationURI().toURL());
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocation().toPath());
FeatureEntry[] included = model.getPluginEntries();
assertEquals(1, included.length);
for (FeatureEntry element : included) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public void testLicenseFeatureOldP2() throws Exception {
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();

IFile licenseFeatureFile = buildFolder.getFile("features/L1/feature.xml");
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocationURI().toURL());
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocation().toPath());

IFile originalFeatureFile = buildFolder.getFile("features/F1/feature.xml");
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocationURI().toURL());
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocation().toPath());

assertEquals(licenseFeature.getLicenseURL(), URIUtil.toUnencodedString(actualLicense.getLocation()));
assertEquals(licenseFeature.getLicense(), actualLicense.getBody());
Expand All @@ -130,7 +130,7 @@ public void testLicenseFeatureOldP2() throws Exception {
IFile actualFeatureFile = buildFolder.getFile("checkFeature.xml");
Utils.extractFromZip(buildFolder, "I.TestBuild/F1-TestBuild.zip", "eclipse/features/F1_1.0.0/feature.xml",
actualFeatureFile);
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocationURI().toURL());
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocation().toPath());

assertNotNull(actualFeature.getLicense());
assertEquals(licenseFeature.getLicense(), actualFeature.getLicense());
Expand Down Expand Up @@ -435,13 +435,13 @@ private void checkBuiltFeature(IFolder buildFolder, String errorMessage) throws
IFile actualFeatureFile = buildFolder.getFile("checkFeature.xml");
assertTrue(Utils.extractFromZip(buildFolder, "I.TestBuild/F1-TestBuild.zip",
"eclipse/features/F1_1.0.0/feature.xml", actualFeatureFile));
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocationURI().toURL());
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocation().toPath());

IFile licenseFeatureFile = buildFolder.getFile("features/L1/feature.xml");
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocationURI().toURL());
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocation().toPath());

IFile originalFeatureFile = buildFolder.getFile("features/F1/feature.xml");
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocationURI().toURL());
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocation().toPath());

assertNotNull(errorMessage + "license was null", actualFeature.getLicense());
assertEquals(errorMessage + "license text not equal", licenseFeature.getLicense(), actualFeature.getLicense());
Expand Down Expand Up @@ -529,7 +529,7 @@ public void testBug338835_MissingLicenseSection() throws Exception {
runAntScript(buildXml.getLocation().toOSString(), new String[] { "test" },
buildFolder.getLocation().toOSString(), null);
BuildTimeFeature feature = new BuildTimeFeatureFactory()
.parseBuildFeature(featureFolder.getFile("feature.xml").getLocationURI().toURL());
.parseBuildFeature(featureFolder.getFile("feature.xml").getLocation().toPath());

assertEquals(feature.getLicense().trim(), "This is legal stuff");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void testPublishFeature_versionReplacement() throws Exception {
assertFalse(hasZipped);

BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
BuildTimeFeature model = factory.parseBuildFeature(featureXML.getLocationURI().toURL());
BuildTimeFeature model = factory.parseBuildFeature(featureXML.getLocation().toPath());
assertEquals("1.0.0.12345", model.getVersion());
assertEquals("1.0.0.12345", model.getPluginEntries()[0].getVersion());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ protected void retrieveFeature(String elementName, String elementType, Map<Strin
File featuresFolder = new File(root, DEFAULT_FEATURE_LOCATION);
File featureLocation = new File(featuresFolder, elementName);
try {
feature = factory.createFeature(featureLocation.toURL(), null);
feature = factory.createFeature(featureLocation.toPath(), null);
featureProperties = new Properties();
try (InputStream featureStream = new BufferedInputStream(new FileInputStream(new File(featureLocation, PROPERTIES_FILE)))) {
featureProperties.load(featureStream);
Expand Down Expand Up @@ -555,8 +555,8 @@ protected void retrieveFeature(String elementName, String elementType, Map<Strin
}
try {
BuildTimeFeatureFactory factory = BuildTimeFeatureFactory.getInstance();
File featureFolder = new File(destination.toString());
feature = factory.createFeature(featureFolder.toURL(), null);
File featureFolder = destination.toFile();
feature = factory.createFeature(featureFolder.toPath(), null);

//We only delete here, so if an exception is thrown the user can still see the retrieve.xml
target.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public interface IPDEBuildConstants {
public final static int EXCEPTION_STATE_PROBLEM = 15;
public final static int EXCEPTION_GENERIC = 16;
public final static int EXCEPTION_FEATURE_PARSE = Constants.EXCEPTION_FEATURE_PARSE;
public final static int WARNING_MISSING_SOURCE = 20;
public final static int WARNING_ELEMENT_NOT_FETCHED = 21;
public final static int EXCEPTION_CONFIG_FORMAT = 22;
public final static int EXCEPTION_PRODUCT_FORMAT = 23;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ private String getFinalName(BundleDescription bundle, String shape) {

private String getFinalName(BuildTimeFeature feature) {
if (!AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_AS_NORMALIZER)) {
IPath featurePath = IPath.fromOSString(feature.getURL().getPath());
return featurePath.segment(featurePath.segmentCount() - 2);
return feature.getPath().getParent().getFileName().toString();
}
return feature.getId() + "_" + feature.getVersion(); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
package org.eclipse.pde.internal.build.site;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;

import org.eclipse.core.runtime.URIUtil;
import org.eclipse.equinox.p2.publisher.eclipse.Feature;
import org.eclipse.equinox.p2.publisher.eclipse.FeatureEntry;
import org.eclipse.pde.build.Constants;
Expand Down Expand Up @@ -52,7 +49,7 @@ public BuildTimeFeature() {
private int contextQualifierLength = -1;
private BuildTimeSiteContentProvider contentProvider = null;
private BuildTimeSite site = null;
private URL url = null;
private Path path = null;
private String rootLocation = null;

public FeatureEntry[] getRawIncludedFeatureReferences() {
Expand Down Expand Up @@ -156,31 +153,28 @@ public BuildTimeSiteContentProvider getFeatureContentProvider() {
return contentProvider;
}

public URL getURL() {
return url;
public Path getPath() {
return path;
}

public void setURL(URL url) {
this.url = url;
public void setPath(Path path) {
this.path = path;
}

/**
* @return the local filesystem location of the directory containing the feature.xml file.
*/
public String getRootLocation() {
if (rootLocation == null) {
URL location = getURL();
if (location == null)
Path location = getPath();
if (location == null) {
return null;
try {
URI locationURI = URIUtil.toURI(location);
rootLocation = URIUtil.toFile(locationURI).getAbsolutePath();
} catch (URISyntaxException e) {
rootLocation = location.getPath();
}
int i = rootLocation.lastIndexOf(Constants.FEATURE_FILENAME_DESCRIPTOR);
if (i != -1)
rootLocation = rootLocation.substring(0, i);
location = location.toAbsolutePath();
if (location.endsWith(Constants.FEATURE_FILENAME_DESCRIPTOR)) {
location = location.getParent();
}
rootLocation = location.toString() + File.separator;
}
return rootLocation;
}
Expand All @@ -194,4 +188,12 @@ public FeatureEntry findPluginEntry(String id, String version) {
}
return null;
}

private static final Path FEATURE_XML_PATH = Path.of(FEATURE_XML);

static Path ensureEndsWithFeatureXml(Path path) {
return path != null && !path.endsWith(BuildTimeFeature.FEATURE_XML_PATH) //
? path.resolve(BuildTimeFeature.FEATURE_XML_PATH)
: path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
package org.eclipse.pde.internal.build.site;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
Expand All @@ -28,7 +27,7 @@
import org.eclipse.pde.internal.build.Messages;
import org.xml.sax.SAXException;

public class BuildTimeFeatureFactory /*extends BaseFeatureFactory */implements /*IFeatureFactory,*/IPDEBuildConstants, IBuildPropertiesConstants {
public class BuildTimeFeatureFactory /*extends BaseFeatureFactory */ implements /*IFeatureFactory,*/IPDEBuildConstants, IBuildPropertiesConstants {
public final static String BUILDTIME_FEATURE_FACTORY_ID = PI_PDEBUILD + ".BuildTimeFeatureFactory"; //$NON-NLS-1$

private static BuildTimeFeatureFactory factoryInstance = null;
Expand All @@ -43,20 +42,18 @@ public static BuildTimeFeatureFactory getInstance() {
return factoryInstance;
}

public BuildTimeFeature createFeature(URL url, BuildTimeSite site) throws CoreException {
BuildTimeFeature feature = null;

if (url == null) {
if (site != null)
public BuildTimeFeature createFeature(Path featurePath, BuildTimeSite site) throws CoreException {
if (featurePath == null) {
if (site != null) {
return createFeature(site);
}
return null;
}

try {
URL featureURL = new URL(url, BuildTimeFeature.FEATURE_XML);
feature = parseBuildFeature(featureURL);
featurePath = BuildTimeFeature.ensureEndsWithFeatureXml(featurePath);
BuildTimeFeature feature = parseBuildFeature(featurePath);

String qualifier = AbstractScriptGenerator.readProperties(IPath.fromOSString(url.getFile()).removeLastSegments(1).toOSString(), PROPERTIES_FILE, IStatus.OK).getProperty(PROPERTY_QUALIFIER);
String qualifier = AbstractScriptGenerator.readProperties(featurePath.getParent().toString(), PROPERTIES_FILE, IStatus.OK).getProperty(PROPERTY_QUALIFIER);
String newVersion = QualifierReplacer.replaceQualifierInVersion(feature.getVersion(), feature.getId(), qualifier, site != null ? site.getFeatureVersions() : null);
if (newVersion != null) {
//a feature version ending in qualifier using context will be further modified based on its included plugins
Expand All @@ -68,36 +65,36 @@ public BuildTimeFeature createFeature(URL url, BuildTimeSite site) throws CoreEx
}

feature.setSite(site);
feature.setURL(featureURL);
feature.setPath(featurePath);
return feature;
} catch (CoreException e) {
String message = NLS.bind(Messages.error_creatingFeature, url);
String message = NLS.bind(Messages.error_creatingFeature, featurePath);
BundleHelper.getDefault().getLog().log(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_PARSE, message, e));
throw e;
} catch (Exception e) {
String message = NLS.bind(Messages.exception_readingFile, url);
String message = NLS.bind(Messages.exception_readingFile, featurePath);
Status status = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_MISSING, message, e);
BundleHelper.getDefault().getLog().log(status); //Logging here because the caller consumes CoreExceptions.
throw new CoreException(status);
}
return feature;
}

public BuildTimeFeature parseBuildFeature(URL featureURL) throws CoreException {
public BuildTimeFeature parseBuildFeature(Path featurePath) throws CoreException {
BuildTimeFeatureParser parser = new BuildTimeFeatureParser();
BuildTimeFeature feature = null;
try {
feature = (BuildTimeFeature) parser.parse(featureURL);
feature = (BuildTimeFeature) parser.parse(featurePath);
if (parser.getStatus() != null) {
// some internalError were detected
throw new CoreException(parser.getStatus());
}
} catch (SAXException e) {
String message = NLS.bind(Messages.error_creatingFeature, featureURL.toString());
String message = NLS.bind(Messages.error_creatingFeature, featurePath);
Status status = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_PARSE, message, e);
BundleHelper.getDefault().getLog().log(status);
throw new CoreException(status);
} catch (IOException e) {
String message = NLS.bind(Messages.exception_readingFile, featureURL.toString());
String message = NLS.bind(Messages.exception_readingFile, featurePath);
Status status = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e);
BundleHelper.getDefault().getLog().log(status); //Logging here because the caller consumes CoreExceptions.
throw new CoreException(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureManifestParser;
import org.eclipse.equinox.p2.publisher.eclipse.Feature;
Expand All @@ -30,10 +31,9 @@ protected Feature createFeature(String id, String version) {
return new BuildTimeFeature(id, version);
}

public Feature parse(URL featureURL) throws SAXException, IOException {
InputStream in = featureURL.openStream();
try (in) {
return super.parse(in, featureURL);
public Feature parse(Path featurePath) throws SAXException, IOException {
try (InputStream in = Files.newInputStream(featurePath)) {
return super.parse(in, featurePath.toUri().toURL());
}
}
}
Loading

0 comments on commit 134bf6e

Please sign in to comment.