private static List normalizeExportClauses()

in repository/service/src/main/java/org/apache/karaf/cave/repository/service/bundlerepository/ResourceBuilder.java [695:756]


    private static List<ParsedHeaderClause> normalizeExportClauses(
            List<ParsedHeaderClause> clauses,
            String bsn, Version bv) throws BundleException {

        // Verify that "java.*" packages are not exported.
        for (ParsedHeaderClause clause : clauses) {
            // Verify that the named package has not already been declared.
            for (String pkgName : clause.paths) {
                // Verify that java.* packages are not exported.
                if (pkgName.startsWith("java.")) {
                    throw new BundleException("Exporting java.* packages not allowed: " + pkgName);
                    // The character "." has no meaning in the OSGi spec except
                    // when placed on the bundle class path. Some people, however,
                    // mistakenly think it means the default package when imported
                    // or exported. This is not correct. It is invalid.
                } else if (pkgName.equals(".")) {
                    throw new BundleException("Exporing '.' is invalid.");
                    // Make sure a package name was specified.
                } else if (pkgName.length() == 0) {
                    throw new BundleException("Exported package names cannot be zero length.");
                }
            }

            // Check for "version" and "specification-version" attributes
            // and verify they are the same if both are specified.
            Object v = clause.attrs.get(Constants.VERSION_ATTRIBUTE);
            Object sv = clause.attrs.get(Constants.PACKAGE_SPECIFICATION_VERSION);
            if ((v != null) && (sv != null)) {
                // Verify they are equal.
                if (!((String) v).trim().equals(((String) sv).trim())) {
                    throw new IllegalArgumentException("Both version and specification-version are specified, but they are not equal.");
                }
            }

            // Always add the default version if not specified.
            if ((v == null) && (sv == null)) {
                v = Version.emptyVersion;
            }

            // Ensure that only the "version" attribute is used and convert
            // it to the appropriate type.
            if ((v != null) || (sv != null)) {
                // Convert version attribute to type Version.
                clause.attrs.remove(Constants.PACKAGE_SPECIFICATION_VERSION);
                v = (v == null) ? sv : v;
                clause.attrs.put(Constants.VERSION_ATTRIBUTE, VersionTable.getVersion(v.toString()));
            }

            // Find symbolic name and version attribute, if present.
            if (clause.attrs.containsKey(Constants.BUNDLE_VERSION_ATTRIBUTE)
                    || clause.attrs.containsKey(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE)) {
                throw new BundleException("Exports must not specify bundle symbolic name or bundle version.");
            }

            // Now that we know that there are no bundle symbolic name and version
            // attributes, add them since the spec says they are there implicitly.
            clause.attrs.put(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE, bsn);
            clause.attrs.put(Constants.BUNDLE_VERSION_ATTRIBUTE, bv);
        }

        return clauses;
    }