Search in sources :

Example 11 with ManifestElement

use of org.eclipse.osgi.util.ManifestElement in project rt.equinox.framework by eclipse.

the class StateBuilder method createOSGiRequires.

static List<GenericSpecification> createOSGiRequires(ManifestElement[] osgiRequires, List<GenericSpecification> result) throws BundleException {
    if (osgiRequires == null)
        return result;
    if (result == null)
        result = new ArrayList<>();
    for (ManifestElement element : osgiRequires) {
        String[] namespaces = element.getValueComponents();
        for (String namespace : namespaces) {
            GenericSpecificationImpl spec = new GenericSpecificationImpl();
            spec.setType(namespace);
            String filterSpec = element.getDirective(Constants.FILTER_DIRECTIVE);
            if (filterSpec != null) {
                try {
                    FilterImpl filter = FilterImpl.newInstance(filterSpec);
                    spec.setMatchingFilter(filter);
                    String name = filter.getPrimaryKeyValue(namespace);
                    if (name != null)
                        spec.setName(name);
                } catch (InvalidSyntaxException e) {
                    String message = NLS.bind(Msg.MANIFEST_INVALID_HEADER_EXCEPTION, Constants.REQUIRE_CAPABILITY, element.toString());
                    // $NON-NLS-1$
                    throw new BundleException(message + " : filter", BundleException.MANIFEST_ERROR, e);
                }
            }
            String resolutionDirective = element.getDirective(Constants.RESOLUTION_DIRECTIVE);
            int resolution = 0;
            if (Constants.RESOLUTION_OPTIONAL.equals(resolutionDirective))
                resolution |= GenericSpecification.RESOLUTION_OPTIONAL;
            String cardinality = element.getDirective(Namespace.REQUIREMENT_CARDINALITY_DIRECTIVE);
            if (Namespace.CARDINALITY_MULTIPLE.equals(cardinality))
                resolution |= GenericSpecification.RESOLUTION_MULTIPLE;
            spec.setResolution(resolution);
            spec.setAttributes(getAttributes(element, DEFINED_REQUIRE_CAPABILITY_ATTRS));
            spec.setArbitraryDirectives(getDirectives(element, DEFINED_REQUIRE_CAPABILITY_DIRECTIVES));
            result.add(spec);
        }
    }
    return result;
}
Also used : ManifestElement(org.eclipse.osgi.util.ManifestElement) FilterImpl(org.eclipse.osgi.internal.framework.FilterImpl) ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleException(org.osgi.framework.BundleException)

Example 12 with ManifestElement

use of org.eclipse.osgi.util.ManifestElement in project rt.equinox.framework by eclipse.

the class StateBuilder method createOSGiCapabilities.

static List<GenericDescription> createOSGiCapabilities(ManifestElement[] osgiCapabilities, List<GenericDescription> result, Integer profileIndex) throws BundleException {
    if (osgiCapabilities == null)
        return result;
    if (result == null)
        result = new ArrayList<>(osgiCapabilities.length);
    for (ManifestElement element : osgiCapabilities) {
        String[] namespaces = element.getValueComponents();
        for (String namespace : namespaces) {
            if (IdentityNamespace.IDENTITY_NAMESPACE.equals(namespace))
                // $NON-NLS-1$ //$NON-NLS-2$
                throw new BundleException("A bundle is not allowed to define a capability in the " + IdentityNamespace.IDENTITY_NAMESPACE + " name space.");
            GenericDescriptionImpl desc = new GenericDescriptionImpl();
            desc.setType(namespace);
            Map<String, Object> mapAttrs = getAttributes(element, new String[0]);
            if (profileIndex != null)
                mapAttrs.put(ExportPackageDescriptionImpl.EQUINOX_EE, profileIndex);
            Dictionary<String, Object> attrs = mapAttrs == null ? new Hashtable<String, Object>() : new Hashtable<>(mapAttrs);
            desc.setAttributes(attrs);
            Map<String, String> directives = new HashMap<>();
            Enumeration<String> keys = element.getDirectiveKeys();
            if (keys != null)
                for (keys = element.getDirectiveKeys(); keys.hasMoreElements(); ) {
                    String key = keys.nextElement();
                    directives.put(key, element.getDirective(key));
                }
            desc.setDirectives(directives);
            result.add(desc);
        }
    }
    return result;
}
Also used : ManifestElement(org.eclipse.osgi.util.ManifestElement) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException)

Example 13 with ManifestElement

use of org.eclipse.osgi.util.ManifestElement in project gemoc-studio by eclipse.

the class ManifestChangerExportPackage method add.

public void add(String packageName) throws BundleException, IOException, CoreException {
    final String exportPackageHeader = "Export-Package";
    boolean foundHeader = false;
    boolean hasValuesForPackageName = false;
    StringBuilder strBuilder = new StringBuilder();
    Attributes mainAttrs = getManifest().getMainAttributes();
    for (Object entryName : mainAttrs.keySet()) {
        String values;
        String header;
        // Get the values safely
        if (entryName instanceof String) {
            header = (String) entryName;
            values = mainAttrs.getValue(header);
        } else if (entryName instanceof Attributes.Name) {
            header = (String) ((Attributes.Name) entryName).toString();
            values = mainAttrs.getValue((Attributes.Name) entryName);
        } else {
            throw new BundleException("Unknown Main Attribute Key type: " + entryName.getClass() + " (" + entryName + ")");
        }
        // loop to the next header if we don't find ours
        if (!exportPackageHeader.equals(header))
            continue;
        // found it
        foundHeader = true;
        // ManifestElement javadocs for spec
        if (values != null) {
            ManifestElement[] elements = ManifestElement.parseHeader(header, values);
            for (int i = 0; i < elements.length; i++) {
                ManifestElement manifestElement = elements[i];
                boolean lastElement = i >= elements.length - 1;
                if (packageName.equals(manifestElement.getValueComponents()[0])) {
                    hasValuesForPackageName = true;
                    break;
                }
                strBuilder.append(manifestElement.getValue());
                if (!lastElement) {
                    strBuilder.append(",\n");
                }
            }
        }
        break;
    }
    if (!foundHeader) {
        // Add a new one with this package
        getManifest().getMainAttributes().putValue(exportPackageHeader, packageName);
    } else {
        // found it and wish to edit it...
        if (!hasValuesForPackageName) {
            // There are no values for the package we wish to add.
            // ...create a fresh entry
            String existingValues = strBuilder.toString();
            boolean areExistingValues = existingValues.trim().length() != 0;
            String newValue = packageName;
            newValue = (areExistingValues) ? (existingValues + ",\n " + newValue) : newValue;
            getManifest().getMainAttributes().putValue(exportPackageHeader, newValue);
        }
    }
}
Also used : ManifestElement(org.eclipse.osgi.util.ManifestElement) Attributes(java.util.jar.Attributes) BundleException(org.osgi.framework.BundleException)

Example 14 with ManifestElement

use of org.eclipse.osgi.util.ManifestElement in project gemoc-studio by eclipse.

the class ManifestChangerPluginDependency method add.

public void add(String plugin, String version, boolean reexport, boolean overwrite) throws BundleException, IOException, CoreException {
    final String requireBundleHeader = "Require-Bundle";
    final String bundleVersionAttr = "bundle-version";
    final String rexportDirective = "visibility";
    // assert (manifest != null);
    assert (plugin != null);
    if (plugin == null)
        return;
    if (version == null) {
        version = "1.0.0";
    }
    boolean foundHeader = false;
    boolean hasValuesForPlugin = false;
    StringBuilder strBuilder = new StringBuilder();
    Attributes mainAttrs = getManifest().getMainAttributes();
    for (Object entryName : mainAttrs.keySet()) {
        String values;
        String header;
        // Get the values safely
        if (entryName instanceof String) {
            header = (String) entryName;
            values = mainAttrs.getValue(header);
        } else if (entryName instanceof Attributes.Name) {
            header = (String) ((Attributes.Name) entryName).toString();
            values = mainAttrs.getValue((Attributes.Name) entryName);
        } else {
            throw new BundleException("Unknown Main Attribute Key type: " + entryName.getClass() + " (" + entryName + ")");
        }
        // loop to the next header if we don't find ours
        if (!requireBundleHeader.equals(header))
            continue;
        // found it
        foundHeader = true;
        // ManifestElement javadocs for spec
        if (values != null) {
            ManifestElement[] elements = ManifestElement.parseHeader(header, values);
            for (int i = 0; i < elements.length; i++) {
                ManifestElement manifestElement = elements[i];
                Enumeration<?> keys = manifestElement.getKeys();
                Enumeration<?> directiveKeys = manifestElement.getDirectiveKeys();
                StringBuilder valueComponents = new StringBuilder();
                boolean lastElement = i >= elements.length - 1;
                boolean firstElement = i == 0;
                boolean elementIsRequiredPlugin = false;
                for (int j = 0; j < manifestElement.getValueComponents().length; j++) {
                    String pureValue = manifestElement.getValueComponents()[j];
                    if (plugin.equalsIgnoreCase(pureValue)) {
                        hasValuesForPlugin = true;
                        elementIsRequiredPlugin = true;
                        // and we are not overwriting quit now
                        if (!overwrite)
                            return;
                    }
                    // ALWAYS WRITE THE LAST ; -> if we don't have any keys
                    // or directives now - we will have
                    // if this is not the required element we will just
                    // write the line in one go using
                    // manifestElement.getValue()
                    valueComponents.append(pureValue + ";");
                }
                if (!elementIsRequiredPlugin) {
                    // we haven't got a component THIS TIME which is equal
                    // to the component we are looking to change
                    // so just write out the whole of this component without
                    // editing it, and carry on looking
                    strBuilder.append((firstElement ? "" : " ") + manifestElement + (lastElement ? "" : ",\n"));
                    continue;
                } else {
                    // write out the value components found so far - we may
                    // wish to edit bits of it
                    strBuilder.append((firstElement ? "" : " ") + valueComponents);
                }
                boolean foundVersionAttr = false;
                if (keys != null) {
                    while (keys.hasMoreElements()) {
                        String key = (String) keys.nextElement();
                        String value = manifestElement.getAttribute(key);
                        if (bundleVersionAttr.equalsIgnoreCase(key)) {
                            // always write the last ; if we are editing the
                            // values - we will be writing the export
                            // directive
                            strBuilder.append(key + "=\"" + version + "\";");
                            foundVersionAttr = true;
                        } else {
                            // always write the last ; if we are editing the
                            // values - we will be writing the export
                            // directive
                            strBuilder.append(key + "=\"" + value + "\";");
                        }
                    }
                }
                if (!foundVersionAttr) {
                    // always write the last ; if we are editing the values
                    // - we will be writing the export directive
                    strBuilder.append(bundleVersionAttr + "=" + version + ";");
                }
                boolean foundDirective = false;
                if (directiveKeys != null) {
                    while (directiveKeys.hasMoreElements()) {
                        String key = (String) directiveKeys.nextElement();
                        boolean lastDirective = !directiveKeys.hasMoreElements();
                        if (rexportDirective.equalsIgnoreCase(key)) {
                            foundDirective = true;
                            strBuilder.append(key + ":=");
                            String[] dirValues = manifestElement.getDirectives(key);
                            for (int j = 0; j < dirValues.length; j++) {
                                String string = dirValues[j];
                                boolean lastDirectiveValue = j >= dirValues.length - 1;
                                if ("reexport".equalsIgnoreCase(string) && !reexport) {
                                    string = "private";
                                } else if ("private".equalsIgnoreCase(string) && reexport) {
                                    string = "reexport";
                                }
                                strBuilder.append(string + (lastDirectiveValue ? "" : ","));
                            }
                        } else {
                            strBuilder.append(key + ":=" + manifestElement.getDirective(key));
                        }
                        if (!lastDirective) {
                            strBuilder.append(";");
                        }
                    }
                }
                if (!foundDirective) {
                    strBuilder.append(rexportDirective + ":=" + (reexport ? "reexport" : "private"));
                }
                if (!lastElement) {
                    strBuilder.append(",\n");
                }
            }
        }
        break;
    }
    if (!foundHeader) {
        // Add a new one
        getManifest().getMainAttributes().putValue(requireBundleHeader, plugin + ";" + bundleVersionAttr + "=" + version + ";" + rexportDirective + ":=" + (reexport ? "reexport" : "private"));
    } else if (overwrite) {
        // found it and wish to edit it...
        if (hasValuesForPlugin) {
            // we have already edited the values for the plugin we wish to
            // add
            getManifest().getMainAttributes().putValue(requireBundleHeader, strBuilder.toString());
        } else {
            // There are no values for the plugin we wish to add.
            // ...create a fresh entry
            String existingValues = strBuilder.toString();
            boolean areExistingValues = existingValues.trim().length() != 0;
            String newValue = plugin + ";" + bundleVersionAttr + "=" + version + ";" + rexportDirective + ":=" + (reexport ? "reexport" : "private");
            newValue = (areExistingValues) ? (existingValues + ",\n " + newValue) : newValue;
            getManifest().getMainAttributes().putValue(requireBundleHeader, newValue);
        }
    }
}
Also used : ManifestElement(org.eclipse.osgi.util.ManifestElement) Attributes(java.util.jar.Attributes) BundleException(org.osgi.framework.BundleException)

Example 15 with ManifestElement

use of org.eclipse.osgi.util.ManifestElement in project tycho by eclipse.

the class DependencyComputer method isFrameworkExtension.

private boolean isFrameworkExtension(BundleDescription bundle) {
    OsgiManifest mf = manifestReader.loadManifest(new File(bundle.getLocation()));
    ManifestElement[] elements = mf.getManifestElements(Constants.FRAGMENT_HOST);
    if (elements.length == 1) {
        if (Constants.EXTENSION_FRAMEWORK.equals(elements[0].getDirective(Constants.EXTENSION_DIRECTIVE))) {
            return true;
        }
    }
    return false;
}
Also used : ManifestElement(org.eclipse.osgi.util.ManifestElement) File(java.io.File)

Aggregations

ManifestElement (org.eclipse.osgi.util.ManifestElement)34 BundleException (org.osgi.framework.BundleException)14 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)3 Attributes (java.util.jar.Attributes)3 VersionRange (org.osgi.framework.VersionRange)3 File (java.io.File)2 JarFile (java.util.jar.JarFile)2 Bundle (org.osgi.framework.Bundle)2 Version (org.osgi.framework.Version)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 List (java.util.List)1 Manifest (java.util.jar.Manifest)1 DerbyPlugin (org.apache.derby.ui.DerbyPlugin)1 Verifier (org.apache.maven.it.Verifier)1 BuildException (org.apache.tools.ant.BuildException)1