Author: [email protected]
Date: Tue Jan 31 16:12:34 2012
New Revision: 2043

Log:
Not JIRA related issues solved:
- throw a configuration exception if the configuration for an existing tenant 
tries to overwrite the tenant.pid property;
- removed obsolete properties object from TenantService.

Modified:
   
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantService.java
   
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantServiceFactory.java

Modified: 
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantService.java
==============================================================================
--- 
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantService.java
 (original)
+++ 
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantService.java
 Tue Jan 31 16:12:34 2012
@@ -15,9 +15,6 @@
  */
 package org.amdatu.tenant.factory;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.amdatu.tenant.Tenant;
 
 /**
@@ -27,23 +24,14 @@
  */
 public class TenantService implements Tenant {
     private final String m_id;
-    private final Map<String, String> m_properties = new HashMap<String, 
String>();
-
     private String m_name;
 
-    public TenantService(String id, String name, Map<String, String> 
properties) {
+    public TenantService(String id, String name) {
         if (id == null) {
             throw new NullPointerException("id cannot be null");
         }
         m_id = id;
         m_name = name;
-        if (properties != null) {
-            m_properties.putAll(properties);
-        }
-    }
-
-    public TenantService(String id, String name) {
-        this(id, name, new HashMap<String, String>());
     }
 
     public String getId() {
@@ -58,14 +46,6 @@
         m_name = name;
     }
 
-    public Map<String, String> getProperties() {
-        return new HashMap<String, String>(m_properties);
-    }
-
-    public void putProperty(String key, String value) {
-        m_properties.put(key, value);
-    }
-
     public String toString() {
         return m_name + " (" + m_id + ")";
     }
@@ -90,16 +70,4 @@
             return false;
         return getId().equals(other.getId());
     }
-
-    public boolean matches(Map<String, String> properties) {
-        for (String key : properties.keySet()) {
-            if (!m_properties.containsKey(key)) {
-                return false;
-            }
-            else if (!m_properties.get(key).equals(properties.get(key))) {
-                return false;
-            }
-        }
-        return true;
-    }
 }

Modified: 
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantServiceFactory.java
==============================================================================
--- 
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantServiceFactory.java
  (original)
+++ 
trunk/amdatu-core/tenant-factory/src/main/java/org/amdatu/tenant/factory/TenantServiceFactory.java
  Tue Jan 31 16:12:34 2012
@@ -15,13 +15,16 @@
  */
 package org.amdatu.tenant.factory;
 
+import static org.amdatu.tenant.TenantConstants.NAME_KEY;
+import static org.amdatu.tenant.TenantConstants.PID_KEY;
+
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import org.amdatu.tenant.Tenant;
-import org.amdatu.tenant.TenantConstants;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.service.cm.ConfigurationException;
@@ -32,15 +35,15 @@
  * Amdatu core {@link ManagedServiceFactory} implementation responsible for
  * publishing {@link Tenant} services based on configuration provided under
  * factoryPid {@link TenantServiceFactory.PID}.
- *
+ * 
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
- *
+ * 
  */
 public final class TenantServiceFactory implements ManagedServiceFactory {
 
     public static final String PID = "org.amdatu.tenant.factory";
 
-    private final ConcurrentHashMap<String, Component> m_components = new 
ConcurrentHashMap<String, Component>();
+    private final ConcurrentMap<String, Component> m_components = new 
ConcurrentHashMap<String, Component>();
 
     private volatile DependencyManager m_dependencyManager;
     private volatile LogService m_logService;
@@ -58,32 +61,38 @@
     public void updated(String pid, Dictionary/* <String, Object> */properties)
         throws ConfigurationException {
 
-        String id = (String) properties.get(TenantConstants.PID_KEY);
-        String name = (String) properties.get(TenantConstants.NAME_KEY);
-        Tenant tenant = new TenantService(id, name);
-
-        Properties serviceProperties = new Properties();
-        Enumeration<String> keys = properties.keys();
-        while (keys.hasMoreElements()) {
-            String key = keys.nextElement();
-            serviceProperties.put(key, properties.get(key));
-        }
+        Dictionary serviceProperties = copyProperties(properties);
+
+        if (m_components.containsKey(pid)) {
+            // Tenant is already registered; lets update its configuration...
+            Component component = m_components.get(pid);
+
+            // Verify that the tenant PID is not overwritten!
+            Object givenPID = serviceProperties.get(PID_KEY);
+            Object currentPID = component.getServiceProperties().get(PID_KEY);
+            if (givenPID != null && !currentPID.equals(givenPID)) {
+                throw new ConfigurationException(PID_KEY, "Attempt to 
overwrite " + PID_KEY + " property!");
+            }
 
-        Component component = m_dependencyManager.createComponent()
-            .setInterface(Tenant.class.getName(), serviceProperties)
-            .setImplementation(tenant);
-
-        // registeredComponent is newly created Component unless one exists
-        Component registeredComponent = m_components.putIfAbsent(pid, 
component);
-        if (registeredComponent == null) {
-            m_logService.log(LogService.LOG_DEBUG, "Registering tenant service 
with pid: '" + properties.get(TenantConstants.PID_KEY)
-                + "'");
-            m_dependencyManager.add(component);
+            m_logService.log(LogService.LOG_DEBUG, "Updating tenant service 
with pid: '" + currentPID + "'");
+
+            component.setServiceProperties(serviceProperties);
         }
         else {
-            m_logService.log(LogService.LOG_DEBUG, "Updating tenant service 
with pid: '" + properties.get(TenantConstants.PID_KEY)
-                + "'");
-            registeredComponent.setServiceProperties(serviceProperties);
+            // Create a new tenant...
+            String id = (String) serviceProperties.get(PID_KEY);
+            String name = (String) serviceProperties.get(NAME_KEY);
+            Tenant tenantService = new TenantService(id, name);
+
+            Component component = m_dependencyManager.createComponent()
+                .setInterface(Tenant.class.getName(), serviceProperties)
+                .setImplementation(tenantService);
+
+            if (m_components.putIfAbsent(pid, component) == null) {
+                m_logService.log(LogService.LOG_DEBUG, "Registering tenant 
service with pid: '" + id + "'");
+
+                m_dependencyManager.add(component);
+            }
         }
     }
 
@@ -97,4 +106,24 @@
             m_dependencyManager.remove(component);
         }
     }
+
+    /**
+     * Creates a shallow copy of a given dictionary.
+     * 
+     * @param dictionary the dictionary to copy, can be <code>null</code>.
+     * @return a copy of the given dictionary, can only be <code>null</code> 
if the given dictionary was <code>null</code>.
+     */
+    private Dictionary copyProperties(Dictionary dictionary) {
+        Properties copy = null;
+        if (dictionary != null) {
+            copy = new Properties();
+
+            Enumeration<String> keys = dictionary.keys();
+            while (keys.hasMoreElements()) {
+                String key = keys.nextElement();
+                copy.put(key, dictionary.get(key));
+            }
+        }
+        return copy;
+    }
 }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to