All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.blackbelt.epsilon.runtime.osgi.BundleURIHandler Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
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 response = getResponse(options); if (response != null) { response.put(URIConverter.RESPONSE_TIME_STAMP_PROPERTY, bundle.getLastModified()); } return inputStream; } @Override public void delete(URI uri, Map options) throws IOException { throw new IOException("Bundle URIHandler is readonly"); } @Override public boolean exists(URI uri, Map options) { try { String parent = FilenameUtils.getFullPathNoEndSeparator(getFullPath(uri)); String fileName = FilenameUtils.getName(getFullPath(uri)); return bundle.findEntries(parent, fileName, false).hasMoreElements(); } catch (NullPointerException | InvalidPathException e) { return false; } } @Override public Map getAttributes(URI uri, Map options) { Map result = new HashMap(); if (exists(uri, options)) { Set requestedAttributes = getRequestedAttributes(options); if (requestedAttributes == null || requestedAttributes.contains(URIConverter.ATTRIBUTE_TIME_STAMP)) { result.put(URIConverter.ATTRIBUTE_TIME_STAMP, bundle.getLastModified()); } if (requestedAttributes == null || requestedAttributes.contains(URIConverter.ATTRIBUTE_LENGTH)) { result.put(URIConverter.ATTRIBUTE_LENGTH, -1); } if (requestedAttributes == null || requestedAttributes.contains(URIConverter.ATTRIBUTE_READ_ONLY)) { result.put(URIConverter.ATTRIBUTE_READ_ONLY, true); } if (requestedAttributes == null || requestedAttributes.contains(URIConverter.ATTRIBUTE_HIDDEN)) { result.put(URIConverter.ATTRIBUTE_HIDDEN, false); } if (requestedAttributes == null || requestedAttributes.contains(URIConverter.ATTRIBUTE_DIRECTORY)) { result.put(URIConverter.ATTRIBUTE_DIRECTORY, false); } } return result; } @Override public void setAttributes(URI uri, Map attributes, Map options) throws IOException { throw new IOException("Bundle URIHandler is readonly"); } private String getFullPath(URI uri) { String uriPath = ""; if (uri.isFile()) { uriPath = uri.path(); } else if (uri.hasOpaquePart()) { uriPath = uri.opaquePart(); } if (rootPath == null || rootPath.equals("")) { return uriPath; } else { return rootPath + File.separator + uriPath; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy