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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations