blob: 4d04faabbc3b809ff31b99ee23a44d7f21ffb5a7 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2018, 2020 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.jcommons.runtime.bundle;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
import org.eclipse.statet.jcommons.runtime.UriUtils;
@NonNullByDefault
public class BundleEntry implements Comparable<BundleEntry> {
public static class Jar extends BundleEntry {
public Jar(final String bundleId, final Path path) {
super(bundleId, path);
}
@Override
public String getUrlString() {
return super.getUrlString();
}
@Override
public String getJClassPathUrlString() {
String urlString= super.getJClassPathUrlString();
// if it is a jar url, a fully qualified url with path the root directory inside the
// jar (append "!/") is required
if (urlString.startsWith(UriUtils.JAR_SCHEME + ':')) {
try {
urlString= UriUtils.toJarUrlString(urlString);
}
catch (final URISyntaxException e) {
throw new RuntimeException(e);
}
}
return urlString;
}
@Override
public @Nullable Path getResourcePath(final String resource) {
return null;
}
@Override
public @Nullable String getResourceUrlString(final String resource) {
try {
final String urlString= UriUtils.toJarUrlString(super.getUrlString());
if (urlString.endsWith("/")) { //$NON-NLS-1$
return urlString + resource;
}
}
catch (final URISyntaxException e) {
throw new RuntimeException(e);
}
return null;
}
}
protected final String bundleId;
private final Path path;
public BundleEntry(final String bundleId, final Path path) {
this.bundleId= bundleId;
this.path= path;
}
public String getBundleId() {
return this.bundleId;
}
@Override
public int compareTo(final BundleEntry o) {
return this.bundleId.compareTo((o).bundleId);
}
protected Path getPath() {
return this.path;
}
public String getUrlString() {
return this.path.toUri().toString();
}
public String getJClassPathString() {
return this.path.toString();
}
public String getJClassPathUrlString() {
return getUrlString();
}
public String getCodebaseString() {
return this.path.toUri().toString();
}
public @Nullable Path getResourcePath(final String resource) {
final Path path= this.path.resolve(resource);
if (Files.exists(path)) {
return path;
}
return null;
}
public @Nullable String getResourceUrlString(final String resource) {
final Path path= getResourcePath(resource);
if (path != null) {
return path.toUri().toString();
}
return null;
}
@Override
public int hashCode() {
return this.bundleId.hashCode() + getPath().hashCode();
}
@Override
public boolean equals(final @Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof BundleEntry) {
final BundleEntry other= (BundleEntry) obj;
return (this.bundleId.equals(other.bundleId)
&& getPath().equals(other.getPath()) );
}
return super.equals(obj);
}
@Override
public String toString() {
return String.format("'%1$s' -> '%2$s'", this.bundleId, getUrlString()); //$NON-NLS-1$
}
}