hu.blackbelt.epsilon.runtime.osgi.BundleURIHandler Maven / Gradle / Ivy
package hu.blackbelt.epsilon.runtime.osgi;
import lombok.Builder;
import lombok.NonNull;
import org.apache.commons.io.FilenameUtils;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.URIHandlerImpl;
import org.osgi.framework.Bundle;
import java.io.*;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class BundleURIHandler extends URIHandlerImpl {
@Builder.Default
String urlSchema = "";
@Builder.Default
String rootPath = "";
@NonNull
Bundle bundle;
public BundleURIHandler(String urlSchema, String rootPath, Bundle bundle) {
super();
this.bundle = bundle;
this.urlSchema = urlSchema;
this.rootPath = rootPath;
}
@Override
public boolean canHandle(URI uri) {
if (urlSchema == null || urlSchema.equals("")) {
return true;
} else if (uri == null) {
return false;
} else if (uri.scheme() == null || uri.scheme().equals("")) {
return false;
} else {
return uri.scheme().equals(urlSchema);
}
}
/**
* Creates an output stream for the file path and returns it.
*
* This implementation allocates a {@link OutputStream} and creates subdirectories as necessary.
*
* @return an open output stream.
* @exception IOException if there is a problem obtaining an open output stream.
*/
@Override
public OutputStream createOutputStream(URI uri, Map, ?> options) throws IOException {
throw new IOException("Bundle URIHandler is readonly");
}
/**
* Creates an input stream for the file path and returns it.
*
* This implementation allocates a {@link FileInputStream}.
*
* @return an open input stream.
* @exception IOException if there is a problem obtaining an open input stream.
*/
@Override
public InputStream createInputStream(URI uri, Map, ?> options) throws IOException {
InputStream inputStream = bundle.getEntry(getFullPath(uri)).openStream();
Map