Author: [email protected]
Date: Sat Feb  4 19:57:42 2012
New Revision: 2057

Log:
AMDATU-522 AMDATU-523 Refactorring, bugfixes and improved itest coverage

Added:
   
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/Constants.java
      - copied, changed from r2056, 
/trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/HostNameTenantResolverConstants.java
Modified:
   
trunk/amdatu-web/itest/base/src/main/java/org/amdatu/web/itest/base/WebConfigs.java
   
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/HostnameTenantResolverTest.java
   
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/ParameterTenantResolverTest.java
   
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/HostNameTenantResolverConstants.java
   
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/osgi/Activator.java
   
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/service/HostnameTenantMappingRegistry.java
   
trunk/amdatu-web/tenantresolver-parameter/src/main/java/org/amdatu/web/tenantresolver/parameter/Constants.java
   
trunk/amdatu-web/tenantresolver-parameter/src/main/java/org/amdatu/web/tenantresolver/parameter/osgi/Activator.java

Modified: 
trunk/amdatu-web/itest/base/src/main/java/org/amdatu/web/itest/base/WebConfigs.java
==============================================================================
--- 
trunk/amdatu-web/itest/base/src/main/java/org/amdatu/web/itest/base/WebConfigs.java
 (original)
+++ 
trunk/amdatu-web/itest/base/src/main/java/org/amdatu/web/itest/base/WebConfigs.java
 Sat Feb  4 19:57:42 2012
@@ -26,7 +26,7 @@
 import org.amdatu.core.itest.base.ProvisionedConfig;
 import org.amdatu.core.itest.base.TestContext;
 import org.amdatu.tenant.TenantConstants;
-import org.amdatu.web.tenantresolver.hostname.HostNameTenantResolverConstants;
+import org.amdatu.web.tenantresolver.hostname.Constants;
 
 /**
  * Provides a mean to provision web-configurations.
@@ -113,7 +113,7 @@
         Properties properties = new Properties();
         properties.put(TenantConstants.PID_KEY, "Default_Web");
         properties.put(TenantConstants.NAME_KEY, "Default Tenant");
-        properties.put(HostNameTenantResolverConstants.HOSTNAMES_KEY, new 
String[] { "localhost" });
+        properties.put(Constants.HOSTNAMES_KEY, new String[] { "localhost" });
         return properties;
     }
 }
\ No newline at end of file

Modified: 
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/HostnameTenantResolverTest.java
==============================================================================
--- 
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/HostnameTenantResolverTest.java
       (original)
+++ 
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/HostnameTenantResolverTest.java
       Sat Feb  4 19:57:42 2012
@@ -34,11 +34,12 @@
 import org.amdatu.core.itest.base.CoreConfigs;
 import org.amdatu.core.itest.base.TestContext;
 import org.amdatu.tenant.TenantConstants;
+import org.amdatu.tenant.factory.TenantServiceFactory;
 import org.amdatu.web.dispatcher.DispatcherService;
 import org.amdatu.web.itest.base.WebBundles;
 import org.amdatu.web.itest.base.WebConfigs;
 import org.amdatu.web.itest.tests.mock.TenantResolverServlet;
-import org.amdatu.web.tenantresolver.hostname.HostNameTenantResolverConstants;
+import org.amdatu.web.tenantresolver.hostname.Constants;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.DependencyManager;
 import org.junit.After;
@@ -63,7 +64,6 @@
 public class HostnameTenantResolverTest {
 
     private final static String SERV_PATH = "/test";
-    private final static String TNT_LOCALHOST = "tenant.localhost";
 
     @Inject
     private static BundleContext m_bundleContext;
@@ -91,14 +91,7 @@
             (String) 
m_testContext.getConfigurationAdmin().getConfiguration("org.osgi.service.http").getProperties()
                 .get("org.osgi.service.http.port");
 
-        // configure a tenant
-        Properties tenantProps = new Properties();
-        tenantProps.put(TenantConstants.PID_KEY, TNT_LOCALHOST);
-        tenantProps.put(TenantConstants.NAME_KEY, "Some tenant at localhost");
-        tenantProps.put(HostNameTenantResolverConstants.HOSTNAMES_KEY, new 
String[] { "localhost" });
-        m_testContext.updateFactoryConfig("org.amdatu.tenant.factory", 
tenantProps);
-
-        // register a servlet
+        // register testservlet
         DependencyManager dm = m_testContext.getDependencyManager();
         Dictionary<String, Object> properties = new Hashtable<String, 
Object>();
         properties.put(DispatcherService.ALIAS_KEY, SERV_PATH);
@@ -123,14 +116,114 @@
     }
 
     @Test
-    public void testTenantResolvesAtLocalhost() throws Exception {
+    public void testTenantResolvingWithConfigurationChanges() throws Exception 
{
+
+        executeRequest("http://localhost:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at localhost",
+            m_tenantResolverServlet.getLastTenant() == null);
+
+        executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at 127.0.0.1",
+            m_tenantResolverServlet.getLastTenant() == null);
+
+        // configure a tenant mapped to localhost
+        Properties properties = new Properties();
+        properties.put(TenantConstants.PID_KEY, "tenant1");
+        properties.put(TenantConstants.NAME_KEY, "Some tenant");
+        properties.put(Constants.HOSTNAMES_KEY, new String[] { "localhost", 
"foobar" });
+        org.osgi.service.cm.Configuration configuration =
+            m_testContext.updateFactoryConfig(TenantServiceFactory.PID, 
properties);
+        m_testContext.waitForSystemToSettle();
+
         executeRequest("http://localhost:"; + m_port + SERV_PATH);
         Assert.assertTrue("Expected tenant to resolve at localhost", 
m_tenantResolverServlet.getLastTenant() != null
-            && 
m_tenantResolverServlet.getLastTenant().getId().equals(TNT_LOCALHOST));
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at 127.0.0.1",
+            m_tenantResolverServlet.getLastTenant() == null);
+
+        // update tenant mapping to 127.0.0.1
+        Dictionary/* <String, Object> */dict = configuration.getProperties();
+        dict.put(Constants.HOSTNAMES_KEY, new String[] { "127.0.0.1" });
+        configuration.update(dict);
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://localhost:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at localhost",
+            m_tenantResolverServlet.getLastTenant() == null);
+
+        executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant to resolve at 127.0.0.1", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        // remove tenant mapping
+        configuration.delete();
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://localhost:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at localhost",
+            m_tenantResolverServlet.getLastTenant() == null);
+
+        executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at 127.0.0.1",
+            m_tenantResolverServlet.getLastTenant() == null);
     }
 
     @Test
-    public void testTenantDoesNotResolveAt127001() throws Exception {
+    public void testTenantResolvingWithMappingOverwrite() throws Exception {
+
+        executeRequest("http://localhost:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at localhost",
+            m_tenantResolverServlet.getLastTenant() == null);
+
+        executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at 127.0.0.1",
+            m_tenantResolverServlet.getLastTenant() == null);
+
+        // configure a tenant1 mapped to localhost
+        Properties properties1 = new Properties();
+        properties1.put(TenantConstants.PID_KEY, "tenant1");
+        properties1.put(TenantConstants.NAME_KEY, "Some tenant");
+        properties1.put(Constants.HOSTNAMES_KEY, new String[] { "localhost", 
"127.0.0.1" });
+        org.osgi.service.cm.Configuration configuration1 =
+            m_testContext.updateFactoryConfig(TenantServiceFactory.PID, 
properties1);
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://localhost:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant1 to resolve at localhost", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant1 to resolve at 127.0.0.1", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        // configure a tenant2 mapped to localhost
+        Properties properties2 = new Properties();
+        properties2.put(TenantConstants.PID_KEY, "tenant2");
+        properties2.put(TenantConstants.NAME_KEY, "Some tenant");
+        properties2.put(Constants.HOSTNAMES_KEY, new String[] { "localhost", 
"foobar" });
+        org.osgi.service.cm.Configuration configuration2 =
+            m_testContext.updateFactoryConfig(TenantServiceFactory.PID, 
properties2);
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://localhost:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant2 to resolve at localhost", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant2"));
+
+        executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant1 to resolve at 127.0.0.1", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        // remove tenant mappings
+        configuration1.delete();
+        configuration2.delete();
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://localhost:"; + m_port + SERV_PATH);
+        Assert.assertTrue("Expected tenant not to resolve at localhost",
+            m_tenantResolverServlet.getLastTenant() == null);
+
         executeRequest("http://127.0.0.1:"; + m_port + SERV_PATH);
         Assert.assertTrue("Expected tenant not to resolve at 127.0.0.1",
             m_tenantResolverServlet.getLastTenant() == null);

Modified: 
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/ParameterTenantResolverTest.java
==============================================================================
--- 
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/ParameterTenantResolverTest.java
      (original)
+++ 
trunk/amdatu-web/itest/tests/src/test/java/org/amdatu/web/itest/tests/ParameterTenantResolverTest.java
      Sat Feb  4 19:57:42 2012
@@ -34,11 +34,11 @@
 import org.amdatu.core.itest.base.CoreConfigs;
 import org.amdatu.core.itest.base.TestContext;
 import org.amdatu.tenant.TenantConstants;
+import org.amdatu.tenant.factory.TenantServiceFactory;
 import org.amdatu.web.dispatcher.DispatcherService;
 import org.amdatu.web.itest.base.WebBundles;
 import org.amdatu.web.itest.base.WebConfigs;
 import org.amdatu.web.itest.tests.mock.TenantResolverServlet;
-import org.amdatu.web.tenantresolver.hostname.HostNameTenantResolverConstants;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.DependencyManager;
 import org.junit.After;
@@ -65,9 +65,6 @@
     private final static String SERV_HOST = "localhost";
     private final static String SERV_PATH = "/test";
 
-    private final static String TNT_KNOWN = "tenant.known";
-    private final static String TNT_UNKNOWN = "tenant.unknown";
-
     @Inject
     private static BundleContext m_bundleContext;
     private TestContext m_testContext;
@@ -94,13 +91,6 @@
             (String) 
m_testContext.getConfigurationAdmin().getConfiguration("org.osgi.service.http").getProperties()
                 .get("org.osgi.service.http.port");
 
-        // configure a tenant
-        Properties tenantProps = new Properties();
-        tenantProps.put(TenantConstants.PID_KEY, TNT_KNOWN);
-        tenantProps.put(TenantConstants.NAME_KEY, "Some tenant");
-        tenantProps.put(HostNameTenantResolverConstants.HOSTNAMES_KEY, new 
String[] { "localhost" });
-        m_testContext.updateFactoryConfig("org.amdatu.tenant.factory", 
tenantProps);
-
         // register a servlet
         DependencyManager dm = m_testContext.getDependencyManager();
         Dictionary<String, Object> properties = new Hashtable<String, 
Object>();
@@ -127,16 +117,78 @@
 
     @Test
     public void testRegisteredTenantResolves() throws Exception {
-        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + TNT_KNOWN);
-        Assert.assertTrue("Expected tenant resolved to " + TNT_KNOWN, 
m_tenantResolverServlet.getLastTenant() != null
-            && 
m_tenantResolverServlet.getLastTenant().getId().equals(TNT_KNOWN));
-    }
 
-    @Test
-    public void testUnregisteredTenantDoesNotResolve() throws Exception {
-        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + TNT_UNKNOWN);
-        Assert.assertTrue("Expected tenant not to resolve " + TNT_UNKNOWN,
-            m_tenantResolverServlet.getLastTenant() == null);
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant1");
+        Assert.assertTrue("Expected tenant1 not to resolve", 
m_tenantResolverServlet.getLastTenant() == null);
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant2");
+        Assert.assertTrue("Expected tenant2 not to resolve", 
m_tenantResolverServlet.getLastTenant() == null);
+
+        // configure tenant1
+        Properties properties1 = new Properties();
+        properties1.put(TenantConstants.PID_KEY, "tenant1");
+        properties1.put(TenantConstants.NAME_KEY, "Some tenant");
+        org.osgi.service.cm.Configuration configuration1 =
+            m_testContext.updateFactoryConfig(TenantServiceFactory.PID, 
properties1);
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant1");
+        Assert.assertTrue("Expected tenant1 to resolve", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant2");
+        Assert.assertTrue("Expected tenant2 not to resolve", 
m_tenantResolverServlet.getLastTenant() == null);
+
+        // configure tenant2
+        Properties properties = new Properties();
+        properties.put(TenantConstants.PID_KEY, "tenant2");
+        properties.put(TenantConstants.NAME_KEY, "Some tenant");
+        org.osgi.service.cm.Configuration configuration2 =
+            m_testContext.updateFactoryConfig(TenantServiceFactory.PID, 
properties);
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant1");
+        Assert.assertTrue("Expected tenant1 to resolve", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant2");
+        Assert.assertTrue("Expected tenant2 to resolve", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant2"));
+
+        // update tenant1
+        Dictionary/* <String, Object> */dict = configuration1.getProperties();
+        dict.put(TenantConstants.NAME_KEY, "New name");
+        configuration1.update(dict);
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant1");
+        Assert.assertTrue("Expected tenant1 to resolve", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant2");
+        Assert.assertTrue("Expected tenant2 to resolve", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant2"));
+
+        // remove tenant2
+        configuration2.delete();
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant1");
+        Assert.assertTrue("Expected tenant1 to resolve", 
m_tenantResolverServlet.getLastTenant() != null
+            && 
m_tenantResolverServlet.getLastTenant().getId().equals("tenant1"));
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant2");
+        Assert.assertTrue("Expected tenant2 not to resolved", 
m_tenantResolverServlet.getLastTenant() == null);
+
+        // remove tenant1
+        configuration1.delete();
+        m_testContext.waitForSystemToSettle();
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant1");
+        Assert.assertTrue("Expected tenant1 not to resolved", 
m_tenantResolverServlet.getLastTenant() == null);
+
+        executeRequest("http://"; + SERV_HOST + ":" + m_port + SERV_PATH + 
"?tenant=" + "tenant2");
+        Assert.assertTrue("Expected tenant2 not to resolved", 
m_tenantResolverServlet.getLastTenant() == null);
     }
 
     private void executeRequest(final String getUrl) throws Exception {

Copied: 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/Constants.java
 (from r2056, 
/trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/HostNameTenantResolverConstants.java)
==============================================================================
--- 
/trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/HostNameTenantResolverConstants.java
 (original)
+++ 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/Constants.java
        Sat Feb  4 19:57:42 2012
@@ -19,6 +19,6 @@
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  * 
  */
-public interface HostNameTenantResolverConstants {
+public interface Constants {
     String HOSTNAMES_KEY = "org.amdatu.web.hostnames";
 }
\ No newline at end of file

Modified: 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/osgi/Activator.java
   (original)
+++ 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/osgi/Activator.java
   Sat Feb  4 19:57:42 2012
@@ -37,12 +37,12 @@
  */
 public final class Activator extends DependencyActivatorBase {
 
+    private final HostnameTenantMappingRegistry m_registry = new 
HostnameTenantMappingRegistry();
+    private final HostnameTenantResolverExtenderFilter m_filter = new 
HostnameTenantResolverExtenderFilter(m_registry);
+
     @Override
     public void init(BundleContext context, DependencyManager manager) throws 
Exception {
 
-        HostnameTenantMappingRegistry registry = new 
HostnameTenantMappingRegistry();
-        HostnameTenantResolverExtenderFilter filter = new 
HostnameTenantResolverExtenderFilter(registry);
-
         Dictionary<String, Object> properties = new Hashtable<String, 
Object>();
         properties.put(Constants.SERVICE_RANKING, 1000);
         properties.put(DispatcherService.PATTERN_KEY, ".*");
@@ -50,11 +50,12 @@
         manager.add(
             createComponent()
                 .setInterface(DispatchExtenderFilter.class.getName(), 
properties)
-                .setImplementation(filter)
+                .setImplementation(m_filter)
+                .setComposition(this, "getComposition")
                 .add(createServiceDependency()
                     .setService(Tenant.class)
                     .setRequired(false)
-                    .setCallbacks(registry, "updateMapping", "updateMapping", 
"updateMapping"))
+                    .setCallbacks("addTenant", "updateTenant", "removeTenant"))
                 .add(createServiceDependency()
                     .setService(LogService.class)
                     .setRequired(false))
@@ -64,4 +65,8 @@
     @Override
     public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
     }
+
+    public Object[] getComposition() {
+        return new Object[] { m_filter, m_registry };
+    }
 }

Modified: 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/service/HostnameTenantMappingRegistry.java
==============================================================================
--- 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/service/HostnameTenantMappingRegistry.java
    (original)
+++ 
trunk/amdatu-web/tenantresolver-hostname/src/main/java/org/amdatu/web/tenantresolver/hostname/service/HostnameTenantMappingRegistry.java
    Sat Feb  4 19:57:42 2012
@@ -23,13 +23,12 @@
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.amdatu.tenant.Tenant;
-import org.amdatu.web.tenantresolver.hostname.HostNameTenantResolverConstants;
+import org.amdatu.web.tenantresolver.hostname.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.log.LogService;
 
 /**
- * {@code HostnameTenantMappingRegistry) keeps a mapping of available {@link 
Tenant} 
- * services using their hostnames as key. This provides fast lookups and 
reduces 
+ * {@code HostnameTenantMappingRegistry) keeps a mapping of available {@link 
Tenant} services using their hostnames as key. This provides fast lookups and 
reduces
  * contention on the service registry.
  * 
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
@@ -63,62 +62,82 @@
         return null;
     }
 
-    public void updateMapping(ServiceReference reference, Tenant tenant) {
-        HostNameTenantMapping newMapping = createMapping(reference, tenant);
-        HostNameTenantMapping oldMapping = null;
+    public void addTenant(ServiceReference reference, Tenant tenant) {
         m_lock.writeLock().lock();
         try {
-            if (newMapping == null) {
-                oldMapping = m_referenceToMapping.remove(reference);
-            }
-            else {
-                oldMapping = m_referenceToMapping.put(reference, newMapping);
-            }
-            if (oldMapping != null) {
-                removeMapping(oldMapping);
-            }
-            if (newMapping != null) {
-                addMapping(newMapping);
-            }
+            addMapping(reference, tenant);
         }
         finally {
             m_lock.writeLock().unlock();
         }
     }
 
-    private void removeMapping(HostNameTenantMapping mapping) {
-        Set<String> removeHostnames = new HashSet<String>();
-        for (Entry<String, HostNameTenantMapping> entry : 
m_hostNameToTenantMapping.entrySet()) {
-            if (entry.getValue() == mapping) {
-                removeHostnames.add(entry.getKey());
-            }
+    public void updateTenant(ServiceReference reference, Tenant tenant) {
+        m_lock.writeLock().lock();
+        try {
+            removeMapping(reference);
+            addMapping(reference, tenant);
         }
-        for (String hostname : removeHostnames) {
-            m_hostNameToTenantMapping.remove(hostname);
+        finally {
+            m_lock.writeLock().unlock();
+        }
+    }
+
+    public void removeTenant(ServiceReference reference, Tenant tenant) {
+        m_lock.writeLock().lock();
+        try {
+            removeMapping(reference);
+        }
+        finally {
+            m_lock.writeLock().unlock();
         }
     }
 
-    private void addMapping(HostNameTenantMapping mapping) {
-        if (mapping.getHostNames() == null) {
+    private void addMapping(ServiceReference reference, Tenant tenant) {
+        String[] hostnames = getHostnames(reference);
+        if (hostnames == null || hostnames.length == 0) {
             return;
         }
-        for (String hostname : mapping.getHostNames()) {
+        HostNameTenantMapping mapping = new HostNameTenantMapping(tenant, 
hostnames);
+        m_referenceToMapping.put(reference, mapping);
+        for (String hostname : hostnames) {
+            hostname = hostname.trim();
+            if ("".equals(hostname)) {
+                m_logService.log(LogService.LOG_WARNING, "Tenant " + 
tenant.getId()
+                    + " specifies illegal hostname mapping to " + hostname);
+                continue;
+            }
+            HostNameTenantMapping oldmapping = 
m_hostNameToTenantMapping.get(hostname);
+            if (oldmapping != null) {
+                m_logService.log(LogService.LOG_WARNING, "Tenant " + 
tenant.getId()
+                    + " overwrites hostname mapping for " + hostname + " 
previously mapped to "
+                    + oldmapping.getTenant().getId());
+            }
             m_hostNameToTenantMapping.put(hostname, mapping);
         }
     }
 
-    private HostNameTenantMapping createMapping(ServiceReference reference, 
Tenant tenant) {
-        if (tenant == null) {
-            return null;
+    private void removeMapping(ServiceReference reference) {
+        HostNameTenantMapping mapping = m_referenceToMapping.remove(reference);
+        if (mapping == null) {
+            return;
+        }
+        Set<String> removeHostnames = new HashSet<String>();
+        for (Entry<String, HostNameTenantMapping> entry : 
m_hostNameToTenantMapping.entrySet()) {
+            if (entry.getValue() == mapping) {
+                removeHostnames.add(entry.getKey());
+            }
+        }
+        for (String hostname : removeHostnames) {
+            m_hostNameToTenantMapping.remove(hostname);
         }
-        return new HostNameTenantMapping(tenant, getHostnames(reference));
     }
 
     private String[] getHostnames(ServiceReference reference) {
-        return (String[]) 
reference.getProperty(HostNameTenantResolverConstants.HOSTNAMES_KEY);
+        return (String[]) reference.getProperty(Constants.HOSTNAMES_KEY);
     }
 
-    class HostNameTenantMapping {
+    private final class HostNameTenantMapping {
 
         private final Tenant m_tenant;
         private final String[] m_hostnames;

Modified: 
trunk/amdatu-web/tenantresolver-parameter/src/main/java/org/amdatu/web/tenantresolver/parameter/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-web/tenantresolver-parameter/src/main/java/org/amdatu/web/tenantresolver/parameter/osgi/Activator.java
 (original)
+++ 
trunk/amdatu-web/tenantresolver-parameter/src/main/java/org/amdatu/web/tenantresolver/parameter/osgi/Activator.java
 Sat Feb  4 19:57:42 2012
@@ -21,8 +21,8 @@
 import org.amdatu.tenant.Tenant;
 import org.amdatu.web.dispatcher.DispatchExtenderFilter;
 import org.amdatu.web.dispatcher.DispatcherService;
-import org.amdatu.web.tenantresolver.parameter.service.TenantRegistry;
 import 
org.amdatu.web.tenantresolver.parameter.service.ParameterTenantResolverExtenderFilter;
+import org.amdatu.web.tenantresolver.parameter.service.TenantRegistry;
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
@@ -37,23 +37,26 @@
  */
 public final class Activator extends DependencyActivatorBase {
 
+    private final TenantRegistry m_registry = new TenantRegistry();
+    private final ParameterTenantResolverExtenderFilter m_filter =
+        new ParameterTenantResolverExtenderFilter(m_registry);
+
     @Override
     public void init(BundleContext context, DependencyManager manager) throws 
Exception {
 
-        TenantRegistry registry = new TenantRegistry();
-        ParameterTenantResolverExtenderFilter filter = new 
ParameterTenantResolverExtenderFilter(registry);
+        Dictionary<String, Object> properties = new Hashtable<String, 
Object>();
+        properties.put(Constants.SERVICE_RANKING, 0);
+        properties.put(DispatcherService.PATTERN_KEY, ".*");
 
-        Dictionary<String, Object> thProperties = new Hashtable<String, 
Object>();
-        thProperties.put(Constants.SERVICE_RANKING, 0);
-        thProperties.put(DispatcherService.PATTERN_KEY, ".*");
         manager.add(
             createComponent()
-                .setInterface(DispatchExtenderFilter.class.getName(), 
thProperties)
-                .setImplementation(filter)
+                .setInterface(DispatchExtenderFilter.class.getName(), 
properties)
+                .setImplementation(m_filter)
+                .setComposition(this, "getComposition")
                 .add(createServiceDependency()
                     .setService(Tenant.class)
                     .setRequired(false)
-                    .setCallbacks(registry, "addTenant", "removeTenant"))
+                    .setCallbacks("addTenant", "removeTenant"))
                 .add(createServiceDependency()
                     .setService(LogService.class)
                     .setRequired(false))
@@ -63,4 +66,8 @@
     @Override
     public void destroy(BundleContext context, DependencyManager manager) 
throws Exception {
     }
+
+    public Object[] getComposition() {
+        return new Object[] { m_filter, m_registry };
+    }
 }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to