Search in sources :

Example 76 with BundleException

use of org.osgi.framework.BundleException in project karaf by apache.

the class ResourceBuilder method parseBundleSymbolicName.

private static ParsedHeaderClause parseBundleSymbolicName(Map<String, String> headerMap) throws BundleException {
    List<ParsedHeaderClause> clauses = parseStandardHeader(headerMap.get(Constants.BUNDLE_SYMBOLICNAME));
    if (clauses.size() > 0) {
        if (clauses.size() > 1 || clauses.get(0).paths.size() > 1) {
            throw new BundleException("Cannot have multiple symbolic names: " + headerMap.get(Constants.BUNDLE_SYMBOLICNAME));
        }
        // Get bundle version.
        Version bundleVersion = Version.emptyVersion;
        if (headerMap.get(Constants.BUNDLE_VERSION) != null) {
            bundleVersion = VersionTable.getVersion(headerMap.get(Constants.BUNDLE_VERSION));
        }
        // Create a require capability and return it.
        ParsedHeaderClause clause = clauses.get(0);
        String symName = clause.paths.get(0);
        clause.attrs.put(BundleRevision.BUNDLE_NAMESPACE, symName);
        clause.attrs.put(Constants.BUNDLE_VERSION_ATTRIBUTE, bundleVersion);
        return clause;
    }
    return null;
}
Also used : Version(org.osgi.framework.Version) BundleException(org.osgi.framework.BundleException)

Example 77 with BundleException

use of org.osgi.framework.BundleException in project karaf by apache.

the class ResourceBuilder method normalizeDynamicImportClauses.

@SuppressWarnings("deprecation")
private static List<ParsedHeaderClause> normalizeDynamicImportClauses(List<ParsedHeaderClause> clauses) throws BundleException {
    // both version and specification-version attributes.
    for (ParsedHeaderClause clause : clauses) {
        // Add the resolution directive to indicate that these are
        // dynamic imports.
        clause.dirs.put(Constants.RESOLUTION_DIRECTIVE, RESOLUTION_DYNAMIC);
        // 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.");
            }
        }
        // it to the VersionRange type.
        if ((v != null) || (sv != null)) {
            clause.attrs.remove(Constants.PACKAGE_SPECIFICATION_VERSION);
            v = (v == null) ? sv : v;
            clause.attrs.put(Constants.VERSION_ATTRIBUTE, VersionRange.parseVersionRange(v.toString()));
        }
        // If bundle version is specified, then convert its type to VersionRange.
        v = clause.attrs.get(Constants.BUNDLE_VERSION_ATTRIBUTE);
        if (v != null) {
            clause.attrs.put(Constants.BUNDLE_VERSION_ATTRIBUTE, VersionRange.parseVersionRange(v.toString()));
        }
        // packages are not imported.
        for (String pkgName : clause.paths) {
            if (pkgName.startsWith("java.")) {
                throw new BundleException("Dynamically importing java.* packages not allowed: " + pkgName);
            } else if (!pkgName.equals("*") && pkgName.endsWith("*") && !pkgName.endsWith(".*")) {
                throw new BundleException("Partial package name wild carding is not allowed: " + pkgName);
            }
        }
    }
    return clauses;
}
Also used : BundleException(org.osgi.framework.BundleException)

Example 78 with BundleException

use of org.osgi.framework.BundleException in project karaf by apache.

the class ResourceBuilder method normalizeImportClauses.

@SuppressWarnings("deprecation")
private static List<ParsedHeaderClause> normalizeImportClauses(List<ParsedHeaderClause> clauses) throws BundleException {
    // Verify that the values are equals if the package specifies
    // both version and specification-version attributes.
    Set<String> dupeSet = new HashSet<>();
    for (ParsedHeaderClause clause : clauses) {
        // 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.");
            }
        }
        // it to the VersionRange type.
        if ((v != null) || (sv != null)) {
            clause.attrs.remove(Constants.PACKAGE_SPECIFICATION_VERSION);
            v = (v == null) ? sv : v;
            clause.attrs.put(Constants.VERSION_ATTRIBUTE, VersionRange.parseVersionRange(v.toString()));
        }
        // If bundle version is specified, then convert its type to VersionRange.
        v = clause.attrs.get(Constants.BUNDLE_VERSION_ATTRIBUTE);
        if (v != null) {
            clause.attrs.put(Constants.BUNDLE_VERSION_ATTRIBUTE, VersionRange.parseVersionRange(v.toString()));
        }
        // Verify java.* is not imported, nor any duplicate imports.
        for (String pkgName : clause.paths) {
            if (!dupeSet.contains(pkgName)) {
                // Verify that java.* packages are not imported.
                if (pkgName.startsWith("java.")) {
                    throw new BundleException("Importing 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("Importing '.' is invalid.");
                // Make sure a package name was specified.
                } else if (pkgName.length() == 0) {
                    throw new BundleException("Imported package names cannot be zero length.");
                }
                dupeSet.add(pkgName);
            } else {
                throw new BundleException("Duplicate import: " + pkgName);
            }
        }
    }
    return clauses;
}
Also used : BundleException(org.osgi.framework.BundleException) HashSet(java.util.HashSet)

Example 79 with BundleException

use of org.osgi.framework.BundleException in project karaf by apache.

the class ResourceBuilder method convertRequireCapabilities.

private static List<Requirement> convertRequireCapabilities(List<ParsedHeaderClause> clauses, Resource resource) throws BundleException {
    // Now convert generic header clauses into requirements.
    List<Requirement> reqList = new ArrayList<>();
    for (ParsedHeaderClause clause : clauses) {
        try {
            String filterStr = clause.dirs.get(Constants.FILTER_DIRECTIVE);
            SimpleFilter sf = (filterStr != null) ? SimpleFilter.parse(filterStr) : SimpleFilter.convert(clause.attrs);
            for (String path : clause.paths) {
                // Create requirement and add to requirement list.
                reqList.add(new RequirementImpl(resource, path, clause.dirs, clause.attrs, sf));
            }
        } catch (Exception ex) {
            throw new BundleException("Error creating requirement: " + ex, ex);
        }
    }
    return reqList;
}
Also used : Requirement(org.osgi.resource.Requirement) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) BundleException(org.osgi.framework.BundleException)

Example 80 with BundleException

use of org.osgi.framework.BundleException in project karaf by apache.

the class BundleInstallSupportImpl method updateBundle.

/* (non-Javadoc)
     * @see org.apache.karaf.features.internal.service.Regions#updateBundle(org.osgi.framework.Bundle, java.lang.String, java.io.InputStream)
     */
@Override
public void updateBundle(Bundle bundle, String uri, InputStream is) throws BundleException {
    // We need to wrap the bundle to insert a Bundle-UpdateLocation header
    try {
        File file = BundleUtils.fixBundleWithUpdateLocation(is, uri);
        bundle.update(new FileInputStream(file));
        file.delete();
    } catch (IOException e) {
        throw new BundleException("Unable to update bundle", e);
    }
}
Also used : IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

BundleException (org.osgi.framework.BundleException)99 Bundle (org.osgi.framework.Bundle)54 IOException (java.io.IOException)31 Test (org.junit.Test)19 File (java.io.File)15 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 FileInputStream (java.io.FileInputStream)9 BundleContext (org.osgi.framework.BundleContext)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Hashtable (java.util.Hashtable)5 Manifest (java.util.jar.Manifest)5 ServiceReference (org.osgi.framework.ServiceReference)5 Version (org.osgi.framework.Version)5 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)5 LowDiskException (android.taobao.atlas.runtime.LowDiskException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4