Skip to content

Commit

Permalink
[PDE-Build] Use File 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 26, 2024
1 parent caccef6 commit 1387bb1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -257,72 +255,6 @@ public static String[] getArrayFromString(String list) {
return getArrayFromString(list, ","); //$NON-NLS-1$
}

/**
* Converts an array of strings into an array of URLs.
*
* @return URL[]
*/
public static URL[] asURL(String[] target) throws CoreException {
if (target == null)
return null;
try {
URL[] result = new URL[target.length];
for (int i = 0; i < target.length; i++)
result[i] = new URL(target[i]);
return result;
} catch (MalformedURLException e) {
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, e.getMessage(), e));
}
}

public static URL[] asURL(Collection<File> target) throws CoreException {
if (target == null)
return null;
try {
URL[] result = new URL[target.size()];
int i = 0;
for (File file : target) {
result[i++] = file.toURL();
}
return result;
} catch (MalformedURLException e) {
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, e.getMessage(), e));
}
}

public static File[] asFile(String[] target) {
if (target == null)
return new File[0];
File[] result = new File[target.length];
for (int i = 0; i < result.length; i++) {
result[i] = new File(target[i]);
}
return result;
}

public static File[] asFile(URL[] target) {
if (target == null)
return new File[0];
File[] result = new File[target.length];
for (int i = 0; i < result.length; i++) {
result[i] = new File(target[i].getFile());
}
return result;
}

public static File[] asFile(Collection<?> collection) {
if (collection.size() == 0)
return new File[0];
Object first = collection.iterator().next();
if (first instanceof String)
return asFile(collection.toArray(new String[collection.size()]));
else if (first instanceof URL)
return asFile(collection.toArray(new URL[collection.size()]));
else if (first instanceof File)
return collection.toArray(new File[collection.size()]);
throw new IllegalArgumentException();
}

/**
* Return a string which is a concatination of each member of the given
* collection, separated by the given separator.
Expand Down Expand Up @@ -396,19 +328,13 @@ public static IPath makeRelative(IPath location, IPath base) {
}

static public void copyFile(String src, String dest) throws IOException {
File source = new File(src);
if (!source.exists())
Path source = Path.of(src);
if (!Files.isRegularFile(source)) {
return;
File destination = new File(dest);
File destDir = destination.getParentFile();
if ((!destDir.exists() && !destDir.mkdirs()) || destDir.isFile())
return; //we will fail trying to create the file, TODO log warning/error

copy(source, destination);
}

public static void copy(File source, File destination) throws IOException {
org.eclipse.pde.internal.publishing.Utils.copy(source, destination);
}
Path destination = Path.of(dest);
Files.createDirectories(destination.getParent());
Files.copy(source, destination);
}

public static void writeProperties(Properties properites, File outputFile, String comment) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String getInstalledBaseURL() {
public Collection<File> getPluginPaths() {
Collection<File> pluginsToCompile = findPluginXML(files);
if (installedBaseURL != null) {
pluginsToCompile.addAll(Arrays.asList(PluginPathFinder.getPluginPaths(installedBaseURL, filterP2Base)));
pluginsToCompile.addAll(PluginPathFinder.getPluginPaths(installedBaseURL, filterP2Base));
}
return pluginsToCompile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public BuildTimeSite createSite() throws CoreException {
featureXMLs.addAll(installedFeatures);

// extract features from platform.xml
File[] featureDirectories = PluginPathFinder.getFeaturePaths(installedBaseURL);
List<File> featureDirectories = PluginPathFinder.getFeaturePaths(installedBaseURL);
for (File element : featureDirectories) {
File featureXML = new File(element, Constants.FEATURE_FILENAME_DESCRIPTOR);
if (featureXML.exists())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -45,7 +43,7 @@ public class P2Utils {

/**
* Returns bundles defined by the 'bundles.info' file in the
* specified location, or <code>null</code> if none. The "bundles.info" file
* specified location, or an empty list if none. The "bundles.info" file
* is assumed to be at a fixed relative location to the specified file. This
* method will also look for a "source.info". If available, any source
* bundles found will also be added to the returned list.
Expand All @@ -54,7 +52,7 @@ public class P2Utils {
* @return URLs of all bundles in the installation or <code>null</code> if not able
* to locate a bundles.info
*/
public static URL[] readBundlesTxt(String platformHome) {
public static List<File> readBundlesTxt(String platformHome) {
SimpleConfiguratorManipulator manipulator = BundleHelper.getDefault().acquireService(SimpleConfiguratorManipulator.class);

File root = new File(platformHome);
Expand All @@ -72,22 +70,7 @@ public static URL[] readBundlesTxt(String platformHome) {
// TODO Auto-generated catch block
e.printStackTrace();
}

URL[] bundles = null;
if (infos.size() > 0) {
bundles = new URL[infos.size()];
for (int i = 0; i < bundles.length; i++) {
try {
bundles[i] = new File(infos.get(i).getLocation()).toURL();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
bundles = new URL[0];
}
return bundles;
return infos.stream().map(BundleInfo::getLocation).map(File::new).toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -25,7 +24,6 @@

import org.eclipse.core.runtime.IPath;
import org.eclipse.pde.internal.build.IPDEBuildConstants;
import org.eclipse.pde.internal.build.Utils;

public class PluginPathFinder {
private static final String DROPINS = "dropins"; //$NON-NLS-1$
Expand Down Expand Up @@ -66,8 +64,8 @@ private static String getSitePath(String platformHome, File linkFile, boolean fe
* @param features false for plugin sites, true for feature sites
* @return array of ".../plugins" or ".../features" Files
*/
private static File[] getSites(String platformHome, boolean features) {
ArrayList<File> sites = new ArrayList<>();
private static List<File> getSites(String platformHome, boolean features) {
List<File> sites = new ArrayList<>();

File file = new File(platformHome, features ? IPDEBuildConstants.DEFAULT_FEATURE_LOCATION : IPDEBuildConstants.DEFAULT_PLUGIN_LOCATION);
if (!features && !file.exists())
Expand All @@ -84,7 +82,7 @@ private static File[] getSites(String platformHome, boolean features) {
}
}
}
return sites.toArray(new File[sites.size()]);
return sites;
}

private static List<File> getDropins(String platformHome, boolean features) {
Expand Down Expand Up @@ -132,41 +130,37 @@ private static List<File> getDropins(String platformHome, boolean features) {
}
}

results.addAll(scanLocations(sites.toArray(new File[sites.size()])));
results.addAll(scanLocations(sites));
return results;
}

public static File[] getFeaturePaths(String platformHome) {
public static List<File> getFeaturePaths(String platformHome) {
return getPaths(platformHome, true, false);
}

public static File[] getPluginPaths(String platformHome, boolean filterP2Base) {
public static List<File> getPluginPaths(String platformHome, boolean filterP2Base) {
return getPaths(platformHome, false, filterP2Base);
}

public static File[] getPluginPaths(String platformHome) {
return getPaths(platformHome, false, false);
}

public static File[] getPaths(String platformHome, boolean features, boolean filterP2Base) {
public static List<File> getPaths(String platformHome, boolean features, boolean filterP2Base) {

if (filterP2Base) {
URL[] urls = P2Utils.readBundlesTxt(platformHome);
if (urls != null && urls.length > 0) {
return Utils.asFile(urls);
List<File> files = P2Utils.readBundlesTxt(platformHome);
if (files != null && !files.isEmpty()) {
return files;
}
}

List<File> list = scanLocations(getSites(platformHome, features));
list.addAll(getDropins(platformHome, features));
return Utils.asFile(list);
return list;
}

/**
* Scan given plugin/feature directories or jars for existence
* @return URLs to plugins/features
*/
private static List<File> scanLocations(File[] sites) {
private static List<File> scanLocations(List<File> sites) {
ArrayList<File> result = new ArrayList<>();
for (File site : sites) {
if (site == null || !site.exists())
Expand Down

0 comments on commit 1387bb1

Please sign in to comment.