public static InternalResource create()

in src/main/java/org/apache/sling/installer/core/impl/InternalResource.java [55:146]


    public static InternalResource create(
            final String scheme,
            final InstallableResource resource)
    throws IOException {
        // installable resource has an id, a priority and either
        // an input stream or a dictionary
        InputStream is = resource.getInputStream();
        Dictionary<String, Object> dict = resource.getDictionary();

        // Handle deprecated types and map them to new types
        String type = resource.getType();
        if ( InstallableResource.TYPE_BUNDLE.equals(type) ) {
            type = InstallableResource.TYPE_FILE;
        } else if ( InstallableResource.TYPE_CONFIG.equals(type) ) {
            type = InstallableResource.TYPE_PROPERTIES;
        }

        // check for optional uri (only if type is file and digest is available)
        final String resourceUri = (dict != null
                                    && (type == null || InstallableResource.TYPE_FILE.equals(type))
                                    && resource.getDigest() != null
                                    && resource.getDigest().length() > 0) ?
                              (String)dict.get(InstallableResource.RESOURCE_URI_HINT) : null;
        // check if resourceUri is accessible
        boolean useResourceUri = resourceUri != null;
        if ( resourceUri != null ) {
            InputStream resourceUriIS = null;
            try {
                final URI uri = new URI(resourceUri);
                resourceUriIS = uri.toURL().openStream();
                // everything fine
            } catch (final Exception use) {
                useResourceUri = false;
            } finally {
                if ( resourceUriIS != null ) {
                    try {
                        resourceUriIS.close();
                    } catch (final IOException ignore) {
                        // ignore
                    }
                }
            }
        }

        if ( is != null &&
             (InstallableResource.TYPE_PROPERTIES.equals(type) ||
              ((type == null || InstallableResource.TYPE_FILE.equals(type)) && isConfigExtension(resource.getId())))) {
            try {
                dict = readDictionary(is, scheme, resource.getId());
            } catch (final IOException ioe) {
                throw (IOException)new IOException("Unable to read dictionary from input stream: " + resource.getId()).initCause(ioe);
            }
            is = null;
            useResourceUri = false;
        }

        File dataFile = null;
        final String digest;
        if ( is == null ) {
            // if input stream is null, properties is expected!
            type = (type != null ? type : InstallableResource.TYPE_PROPERTIES);
            // we always compute a digest
            digest = FileDataStore.computeDigest(dict);
        } else {
            type = (type != null ? type : InstallableResource.TYPE_FILE);
            if ( resourceUri != null && useResourceUri ) {
                digest = resource.getDigest();
            } else {
                final String url = scheme + ':' + resource.getId();
                // if input stream is not null, file is expected!
                dataFile = FileDataStore.SHARED.createNewDataFile(is,
                        url,
                        resource.getDigest(),
                        resource.getType());
                if (resource.getDigest() != null && resource.getDigest().length() > 0) {
                    digest = resource.getDigest();
                } else {
                    digest = FileDataStore.computeDigest(dataFile);
                    FileDataStore.SHARED.updateDigestCache(url, dataFile, digest);
                }
            }
        }
        return new InternalResource(scheme,
                resource.getId(),
                is,
                dict,
                type,
                digest,
                resource.getPriority(),
                dataFile,
                useResourceUri ? resourceUri : null);
    }