Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Watson2011-03-07 16:43:00 -0500
committerGlyn Normington2011-03-07 16:43:00 -0500
commitd205e10a1bff8922ba9ab389aeb89bf27fb7e02c (patch)
tree103afb2b7543ad6cfe320cf86c9029959d441689 /org.eclipse.virgo.kernel.userregionfactory
parent6d28bd05f7fff4b56d2373176db3078ff5d4199f (diff)
downloadorg.eclipse.virgo.kernel-d205e10a1bff8922ba9ab389aeb89bf27fb7e02c.tar.gz
org.eclipse.virgo.kernel-d205e10a1bff8922ba9ab389aeb89bf27fb7e02c.tar.xz
org.eclipse.virgo.kernel-d205e10a1bff8922ba9ab389aeb89bf27fb7e02c.zip
Initial attempt at generalizing RegionFilter
Diffstat (limited to 'org.eclipse.virgo.kernel.userregionfactory')
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java124
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java128
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java5
3 files changed, 91 insertions, 166 deletions
diff --git a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java b/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java
index b902a120..5bbf0af4 100644
--- a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java
+++ b/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java
@@ -15,6 +15,8 @@ package org.eclipse.virgo.kernel.userregionfactory;
import java.net.URI;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
@@ -30,21 +32,23 @@ import org.eclipse.virgo.kernel.osgi.framework.OsgiServiceHolder;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
-import org.eclipse.virgo.kernel.osgi.region.StandardRegionFilter;
import org.eclipse.virgo.medic.eventlog.EventLogger;
import org.eclipse.virgo.osgi.launcher.parser.ArgumentParser;
import org.eclipse.virgo.osgi.launcher.parser.BundleEntry;
import org.eclipse.virgo.util.osgi.ServiceRegistrationTracker;
+import org.eclipse.virgo.util.osgi.VersionRange;
import org.eclipse.virgo.util.osgi.manifest.BundleManifest;
import org.eclipse.virgo.util.osgi.manifest.BundleManifestFactory;
+import org.eclipse.virgo.util.osgi.manifest.ImportedPackage;
import org.eclipse.virgo.util.osgi.manifest.RequireBundle;
import org.eclipse.virgo.util.osgi.manifest.RequiredBundle;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
-import org.osgi.framework.Filter;
+import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.Version;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.event.Event;
@@ -87,6 +91,8 @@ public final class Activator implements BundleActivator {
private static final String EVENT_PROPERTY_REGION_BUNDLECONTEXT = "region.bundleContext";
+ private static final String WILDCARD = "*";
+
private EventAdmin eventAdmin;
private String regionBundles;
@@ -144,7 +150,7 @@ public final class Activator implements BundleActivator {
}
}
- private void createUserRegion(BundleContext userRegionBundleContext, RegionDigraph regionDigraph, EventLogger eventLogger) throws BundleException {
+ private void createUserRegion(BundleContext userRegionBundleContext, RegionDigraph regionDigraph, EventLogger eventLogger) throws BundleException, InvalidSyntaxException {
BundleContext systemBundleContext = getSystemBundleContext();
Bundle userRegionFactoryBundle = userRegionBundleContext.getBundle();
@@ -170,15 +176,14 @@ public final class Activator implements BundleActivator {
}
private RegionFilter createUserRegionFilter() throws BundleException {
- RegionFilter userRegionFilter = new StandardRegionFilter();
- Filter serviceFilter;
+ RegionFilter userRegionFilter = new RegionFilter();
+ Collection<String> serviceFilter = classesToFilter(this.regionServiceExports);
try {
- serviceFilter = this.bundleContext.createFilter(classesToFilter(this.regionServiceExports));
+ userRegionFilter.setFilters(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
} catch (InvalidSyntaxException e) {
throw new BundleException("Invalid " + USER_REGION_SERVICE_EXPORTS_PROPERTY + "in user region configuration: '"
+ this.regionServiceExports + "'", e);
}
- userRegionFilter.setServiceFilter(serviceFilter);
return userRegionFilter;
}
@@ -187,31 +192,23 @@ public final class Activator implements BundleActivator {
return regionDigraph.iterator().next();
}
- private RegionFilter createKernelFilter(BundleContext systemBundleContext, EventLogger eventLogger) throws BundleException {
- RegionFilter kernelFilter = new StandardRegionFilter();
+ private RegionFilter createKernelFilter(BundleContext systemBundleContext, EventLogger eventLogger) throws BundleException, InvalidSyntaxException {
+ RegionFilter kernelFilter = new RegionFilter();
allowImportedBundles(kernelFilter, eventLogger);
- kernelFilter.setPackageImportPolicy(createUserRegionPackageImportPolicy(systemBundleContext));
- Filter serviceFilter;
- try {
- serviceFilter = this.bundleContext.createFilter(classesToFilter(this.regionServiceImports));
- } catch (InvalidSyntaxException e) {
- throw new BundleException("Invalid " + USER_REGION_SERVICE_IMPORTS_PROPERTY + "in user region configuration: '"
- + this.regionServiceImports + "'", e);
- }
- kernelFilter.setServiceFilter(serviceFilter);
+ kernelFilter.setFilters(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, createUserRegionPackageImportPolicy(systemBundleContext));
+ Collection<String> serviceFilter = classesToFilter(this.regionServiceImports);
+ kernelFilter.setFilters(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
return kernelFilter;
}
- private void allowImportedBundles(RegionFilter kernelFilter, EventLogger eventLogger) {
+ private void allowImportedBundles(RegionFilter kernelFilter, EventLogger eventLogger) throws InvalidSyntaxException {
String userRegionBundleImports = this.regionBundleImports != null ? this.regionBundleImports
: this.bundleContext.getProperty(USER_REGION_BUNDLE_IMPORTS_PROPERTY);
RequireBundle bundleImportsAsRequireBundle = representBundleImportsAsRequireBundle(userRegionBundleImports, eventLogger);
- List<RequiredBundle> importedBundles = bundleImportsAsRequireBundle.getRequiredBundles();
- for (RequiredBundle importedBundle : importedBundles) {
- kernelFilter.allowBundle(importedBundle.getBundleSymbolicName(), importedBundle.getBundleVersion());
- }
- }
+ Collection<String> importedBundles = importBundleToFilter(bundleImportsAsRequireBundle.getRequiredBundles());
+ kernelFilter.setFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, importedBundles);
+ }
private RequireBundle representBundleImportsAsRequireBundle(String userRegionBundleImportsProperty, EventLogger eventLogger) {
Dictionary<String, String> headers = new Hashtable<String, String>();
@@ -220,24 +217,82 @@ public final class Activator implements BundleActivator {
return manifest.getRequireBundle();
}
- private String classesToFilter(String classList) {
+ private static Collection<String> importBundleToFilter(List<RequiredBundle> importedBundles) {
+ if (importedBundles == null || importedBundles.isEmpty())
+ return Collections.emptyList();
+ Collection<String> result = new ArrayList<String>(importedBundles.size());
+ for (RequiredBundle importedBundle : importedBundles) {
+ StringBuilder f = new StringBuilder();
+ f.append("(&(").append(RegionFilter.VISIBLE_BUNDLE_NAMESPACE).append('=').append(importedBundle.getBundleSymbolicName()).append(')');
+ addRange(Constants.BUNDLE_VERSION_ATTRIBUTE, importedBundle.getBundleVersion(), f);
+ f.append(')');
+ result.add(f.toString());
+ }
+ return result;
+ }
+
+ private static Collection<String> importPackageToFilter(String importList) {
+ if (importList == null || importList.isEmpty()) {
+ return Collections.emptyList();
+ }
+ if (importList.contains(WILDCARD)) {
+ throw new IllegalArgumentException("Wildcards not supported in region imports: '" + importList + "'");
+ }
+ BundleManifest manifest = BundleManifestFactory.createBundleManifest();
+ manifest.setHeader("Import-Package", importList);
+ List<ImportedPackage> list = manifest.getImportPackage().getImportedPackages();
+ if (list.isEmpty())
+ return Collections.emptyList();
+ Collection<String> filters = new ArrayList<String>(list.size());
+ for (ImportedPackage importedPackage : list) {
+ StringBuilder f = new StringBuilder();
+ f.append("(&(").append(RegionFilter.VISIBLE_PACKAGE_NAMESPACE).append('=').append(importedPackage.getPackageName()).append(')');
+ Map<String, String> attrs = importedPackage.getAttributes();
+ for (Map.Entry<String, String> attr : attrs.entrySet()) {
+ if (Constants.VERSION_ATTRIBUTE.equals(attr.getKey()) || Constants.BUNDLE_VERSION_ATTRIBUTE.equals(attr.getKey())) {
+ addRange(attr.getKey(), new VersionRange(attr.getValue()), f);
+ } else {
+ f.append('(').append(attr.getKey()).append('=').append(attr.getValue()).append(')');
+ }
+ }
+ f.append(')');
+ filters.add(f.toString());
+ }
+ return filters;
+ }
+
+ private static void addRange(String key, VersionRange range, StringBuilder f) {
+ if (range.isFloorInclusive()) {
+ f.append('(' + key + ">=" + range.getFloor() + ')');
+ } else {
+ f.append("(!(" + key + "<=" + range.getFloor() +"))");
+ }
+ Version ceiling = range.getCeiling();
+ if (ceiling != null) {
+ if (range.isCeilingInclusive()) {
+ f.append('(' + key + "<=" + ceiling + ')');
+ } else {
+ f.append("(!(" + key + ">=" + ceiling + "))");
+ }
+ }
+ }
+
+ private static Collection<String> classesToFilter(String classList) {
if (classList == null) {
- return "";
+ return Collections.emptyList();
}
String[] classes = classList.split(CLASS_LIST_SEPARATOR);
if (classes.length == 0) {
- return "";
+ return Collections.emptyList();
}
- StringBuffer filter = new StringBuffer();
- filter.append("(|");
+ Collection<String> result = new ArrayList<String>(classes.length);
for (String className : classes) {
- filter.append("(objectClass=" + className + ")");
+ result.add("(objectClass=" + className + ")");
}
- filter.append(")");
- return filter.toString();
+ return result;
}
- private UserRegionPackageImportPolicy createUserRegionPackageImportPolicy(BundleContext systemBundleContext) {
+ private Collection<String> createUserRegionPackageImportPolicy(BundleContext systemBundleContext) {
String userRegionImportsProperty = this.regionPackageImports != null ? this.regionPackageImports
: this.bundleContext.getProperty(USER_REGION_PACKAGE_IMPORTS_PROPERTY);
String expandedUserRegionImportsProperty = null;
@@ -246,8 +301,7 @@ public final class Activator implements BundleActivator {
systemBundleContext);
}
- UserRegionPackageImportPolicy userRegionPackageImportPolicy = new UserRegionPackageImportPolicy(expandedUserRegionImportsProperty);
- return userRegionPackageImportPolicy;
+ return importPackageToFilter(expandedUserRegionImportsProperty);
}
private BundleContext getSystemBundleContext() {
diff --git a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java b/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java
deleted file mode 100644
index 06a85b13..00000000
--- a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java
+++ /dev/null
@@ -1,128 +0,0 @@
-/*******************************************************************************
- * This file is part of the Virgo Web Server.
- *
- * Copyright (c) 2010 VMware Inc.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * SpringSource, a division of VMware - initial API and implementation and/or initial documentation
- *******************************************************************************/
-
-package org.eclipse.virgo.kernel.userregionfactory;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.eclipse.osgi.service.resolver.VersionRange;
-import org.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
-import org.eclipse.virgo.util.osgi.manifest.BundleManifest;
-import org.eclipse.virgo.util.osgi.manifest.BundleManifestFactory;
-import org.eclipse.virgo.util.osgi.manifest.ImportedPackage;
-import org.osgi.framework.Version;
-
-/**
- * {@link UserRegionPackageImportPolicy} is a {@link RegionPackageImportPolicy} for a user region.
- * <p />
- *
- * <strong>Concurrent Semantics</strong><br />
- *
- * Thread safe.
- *
- */
-class UserRegionPackageImportPolicy implements RegionPackageImportPolicy {
-
- private static final String MANDATORY_ATTRIBUTE_NAME_SEPARATOR = ",";
-
- private static final String MANDATORY_DIRECTIVE_NAME = "mandatory";
-
- private static final String VERSION_ATTRIBUTE_NAME = "version";
-
- private static final String WILDCARD = "*";
-
- private final Map<String, ImportedPackage> importedPackages = new HashMap<String, ImportedPackage>();
-
- /**
- * Construct a {@link UserRegionPackageImportPolicy} for the specified import package list which must not contain
- * wildcards.
- *
- * @param regionImports a string representing a list of imported packages
- */
- UserRegionPackageImportPolicy(String regionImports) {
- if (regionImports != null && !regionImports.isEmpty()) {
- if (regionImports.contains(WILDCARD)) {
- throw new IllegalArgumentException("Wildcards not supported in region imports: '" + regionImports + "'");
- }
- BundleManifest manifest = BundleManifestFactory.createBundleManifest();
- manifest.setHeader("Import-Package", regionImports);
- List<ImportedPackage> list = manifest.getImportPackage().getImportedPackages();
- for (ImportedPackage importedPackage : list) {
- String packageName = importedPackage.getPackageName();
- this.importedPackages.put(packageName, importedPackage);
- }
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isImported(String packageName, Map<String, Object> exportAttributes, Map<String, String> exportDirectives) {
- ImportedPackage importedPackage = this.importedPackages.get(packageName);
- if (importedPackage != null) {
- Map<String, String> importAttributes = importedPackage.getAttributes();
- Set<String> importAttributeNames = importAttributes.keySet();
-
- // Check any attribute values match.
- for (String importAttributeName : importAttributeNames) {
- if (exportAttributes == null) {
- return false;
- }
- Object exportAttributeValue = exportAttributes.get(importAttributeName);
- if (importAttributeName.equals(VERSION_ATTRIBUTE_NAME)) {
- if (exportAttributeValue != null && exportAttributeValue instanceof Version) {
- Version exportVersion = (Version) exportAttributeValue;
- String importAttributeValue = importAttributes.get(importAttributeName);
- VersionRange importVersion = new VersionRange(importAttributeValue);
- if (!importVersion.isIncluded(exportVersion)) {
- return false;
- }
- } else {
- return false;
- }
- } else {
- if (exportAttributeValue != null && exportAttributeValue instanceof String) {
- String exportAttributeValueString = (String) exportAttributeValue;
- String importAttributeValue = importAttributes.get(importAttributeName);
- if (!exportAttributeValueString.equals(importAttributeValue)) {
- return false;
- }
- } else {
- return false;
- }
- }
-
- }
-
- // Check mandatory attributes are present.
- if (exportDirectives != null) {
- String mandatoryDirectiveValue = exportDirectives.get(MANDATORY_DIRECTIVE_NAME);
- if (mandatoryDirectiveValue != null) {
- for (String mandatoryAttribute : mandatoryDirectiveValue.split(MANDATORY_ATTRIBUTE_NAME_SEPARATOR)) {
- if (!importAttributeNames.contains(mandatoryAttribute)) {
- return false;
- }
- }
- }
- }
- return true;
- }
-
- return false;
- }
-
-}
diff --git a/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java b/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java
index 308470db..4adbca53 100644
--- a/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java
+++ b/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java
@@ -16,14 +16,13 @@ package org.eclipse.virgo.kernel.userregionfactory;
import java.util.HashMap;
import java.util.Map;
-import org.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.Version;
import org.osgi.framework.wiring.BundleRevision;
public class UserRegionPackageImportPolicyTests {
-
+/*
@Test
public void testNullPackageString() {
new UserRegionPackageImportPolicy(null);
@@ -111,5 +110,5 @@ public class UserRegionPackageImportPolicyTests {
Assert.assertTrue(userRegionPackageImportPolicy.isImported("q", null, null));
Assert.assertFalse(userRegionPackageImportPolicy.isImported("r", null, null));
}
-
+*/
}

Back to the top