Search in sources :

Example 1 with Entry

use of org.apache.sling.commons.osgi.ManifestHeader.Entry in project sling by apache.

the class SlingInitialContentMounter method mount.

/**
     * Add configurations to a running OSGi instance for initial content.
     * @param targetUrl The web console base url
     * @param bundleFile The artifact (bundle)
     * @throws MojoExecutionException
     */
public void mount(final String targetUrl, final File bundleFile) throws MojoExecutionException {
    // first, let's get the manifest and see if initial content is configured
    ManifestHeader header = null;
    try {
        final Manifest mf = getManifest(bundleFile);
        final String value = mf.getMainAttributes().getValue(HEADER_INITIAL_CONTENT);
        if (value == null) {
            log.debug("Bundle has no initial content - no file system provider config created.");
            return;
        }
        header = ManifestHeader.parse(value);
        if (header == null || header.getEntries().length == 0) {
            log.warn("Unable to parse header or header is empty: " + value);
            return;
        }
    } catch (IOException ioe) {
        throw new MojoExecutionException("Unable to read manifest from file " + bundleFile, ioe);
    }
    log.info("Trying to configure file system provider...");
    // quick check if resources are configured
    final List resources = project.getResources();
    if (resources == null || resources.size() == 0) {
        throw new MojoExecutionException("No resources configured for this project.");
    }
    final List<FsResourceConfiguration> cfgs = new ArrayList<>();
    final Entry[] entries = header.getEntries();
    for (final Entry entry : entries) {
        String path = entry.getValue();
        if (path != null && !path.endsWith("/")) {
            path += "/";
        }
        // check if we should ignore this
        final String ignoreValue = entry.getDirectiveValue("maven:mount");
        if (ignoreValue != null && ignoreValue.equalsIgnoreCase("false")) {
            log.debug("Ignoring " + path);
            continue;
        }
        String installPath = entry.getDirectiveValue("path");
        if (installPath == null) {
            installPath = "/";
        }
        // search the path in the resources (usually this should be the first resource
        // entry but this might be reconfigured
        File dir = null;
        final Iterator i = resources.iterator();
        while (dir == null && i.hasNext()) {
            final Resource rsrc = (Resource) i.next();
            String child = path;
            // if resource mapping defines a target path: remove target path from checked resource path
            String targetPath = rsrc.getTargetPath();
            if (targetPath != null && !targetPath.endsWith("/")) {
                targetPath = targetPath + "/";
            }
            if (targetPath != null && path.startsWith(targetPath)) {
                child = child.substring(targetPath.length());
            }
            dir = new File(rsrc.getDirectory(), child);
            if (!dir.exists()) {
                dir = null;
            }
        }
        if (dir == null) {
            throw new MojoExecutionException("No resource entry found containing " + path);
        }
        // check for root mapping - which we don't support atm
        if ("/".equals(installPath)) {
            throw new MojoExecutionException("Mapping to root path not supported by fs provider at the moment. Please adapt your initial content configuration.");
        }
        // check further initial content directives
        StringBuilder importOptions = new StringBuilder();
        String overwriteValue = entry.getDirectiveValue("overwrite");
        if (StringUtils.isNotBlank(overwriteValue)) {
            importOptions.append("overwrite:=" + overwriteValue);
        }
        String ignoreImportProvidersValue = entry.getDirectiveValue("ignoreImportProviders");
        if (StringUtils.isNotBlank(overwriteValue)) {
            if (importOptions.length() > 0) {
                importOptions.append(";");
            }
            importOptions.append("ignoreImportProviders:=\"" + ignoreImportProvidersValue + "\"");
        }
        cfgs.add(new FsResourceConfiguration().fsMode(FsMode.INITIAL_CONTENT).contentRootDir(dir.getAbsolutePath()).providerRootPath(installPath).initialContentImportOptions(importOptions.toString()));
    }
    if (!cfgs.isEmpty()) {
        helper.addConfigurations(targetUrl, cfgs);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ManifestHeader(org.apache.sling.commons.osgi.ManifestHeader) ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) Entry(org.apache.sling.commons.osgi.ManifestHeader.Entry) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 JarFile (java.util.jar.JarFile)1 Manifest (java.util.jar.Manifest)1 Resource (org.apache.maven.model.Resource)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 ManifestHeader (org.apache.sling.commons.osgi.ManifestHeader)1 Entry (org.apache.sling.commons.osgi.ManifestHeader.Entry)1