Author: [email protected]
Date: Thu Nov 24 14:28:01 2011
New Revision: 1760

Log:
AMDATU-468 Refactorred to managed factory and removed deprecated stuff

Added:
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantImpl.java
      - copied, changed from r1759, 
/trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantEntity.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantServiceFactory.java
   trunk/amdatu-core/tenant/src/main/resources/
   trunk/amdatu-core/tenant/src/main/resources/OSGI-INF/
   trunk/amdatu-core/tenant/src/main/resources/OSGI-INF/metatype/
   trunk/amdatu-core/tenant/src/main/resources/OSGI-INF/metatype/metatype.xml
Removed:
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantEntity.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantException.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantManagementService.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantStorageException.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantStorageProvider.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantConfigServiceImpl.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantManagementServiceImpl.java
   
trunk/amdatu-core/tenant/src/test/java/org/amdatu/test/unit/InMemoryTenantStorageProvider.java
   
trunk/amdatu-core/tenant/src/test/java/org/amdatu/test/unit/TenantManagementServiceTest.java
Modified:
   trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/Tenant.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java
   
trunk/amdatu-core/tenant/src/test/java/org/amdatu/test/unit/TenantEntityTest.java

Modified: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/Tenant.java
==============================================================================
--- trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/Tenant.java   
(original)
+++ trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/Tenant.java   
Thu Nov 24 14:28:01 2011
@@ -27,7 +27,9 @@
  *
  * For each tenant, an OSGi service is registered. The <code>id</code>, 
<code>name</code> and other properties
  * will be registered with the service as service properties; the keys will be 
prefixed with {@link #SERVICE_PREFIX}
- * If the properties contain keys 'name' or 'id', the Tenant's name or id will 
prevail.
+ * If the properties contain keys 'name' or 'id', the Tenant's name or id will 
prevail.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
 public interface Tenant {
     /**

Modified: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java
   (original)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java
   Thu Nov 24 14:28:01 2011
@@ -15,17 +15,19 @@
  */
 package org.amdatu.core.tenant.osgi;
 
-import org.amdatu.core.tenant.TenantManagementService;
-import org.amdatu.core.tenant.TenantStorageProvider;
-import org.amdatu.core.tenant.service.TenantConfigServiceImpl;
-import org.amdatu.core.tenant.service.TenantManagementServiceImpl;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.amdatu.core.tenant.service.TenantServiceFactory;
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ManagedServiceFactory;
 import org.osgi.service.log.LogService;
 
 /**
- * This class represents the OSGi activator for the tenant service
+ * This class represents the OSGi activator for the tenant factory
  * 
  * @author ivol
  * 
@@ -33,27 +35,12 @@
 public class Activator extends DependencyActivatorBase {
     @Override
     public void init(BundleContext context, DependencyManager manager) throws 
Exception {
-        // Create and register the Tenant management service
-        manager.add(
-            createComponent()
-                .setInterface(TenantManagementService.class.getName(), null)
-                .setImplementation(TenantManagementServiceImpl.class)
-                .add(createServiceDependency()
-                    .setService(TenantStorageProvider.class)
-                    .setRequired(true))
-                .add(createServiceDependency()
-                    .setService(LogService.class)
-                    .setRequired(false))
-            );
 
-        // Create and register the Tenant config service
+        Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put(Constants.SERVICE_PID, TenantServiceFactory.PID);
         manager.add(createComponent()
-            .setImplementation(TenantConfigServiceImpl.class)
-            .add(createConfigurationDependency()
-                .setPid(TenantManagementService.PID))
-            .add(createServiceDependency()
-                .setService(TenantManagementService.class)
-                .setRequired(true))
+            .setInterface(ManagedServiceFactory.class.getName(), props)
+            .setImplementation(TenantServiceFactory.class)
             .add(createServiceDependency()
                 .setService(LogService.class)
                 .setRequired(false))

Copied: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantImpl.java
 (from r1759, 
/trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantEntity.java)
==============================================================================
--- 
/trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/TenantEntity.java
    (original)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantImpl.java
       Thu Nov 24 14:28:01 2011
@@ -13,89 +13,93 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.core.tenant;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Data-object representing a Tenant.
- */
-public class TenantEntity implements Tenant {
-    private final String m_id;
-    private final Map<String, String> m_properties = new HashMap<String, 
String>();
-
-    private String m_name;
-
-    public TenantEntity(String id, String name, Map<String, String> 
properties) {
-        if (id == null) {
-            throw new NullPointerException("id cannot be null");
-        }
-        m_id = id;
-        m_name = name;
-        if (properties != null) {
-            m_properties.putAll(properties);
-        }
-    }
-
-    public TenantEntity(String id, String name) {
-        this(id, name, new HashMap<String, String>());
-    }
-
-    public String getId() {
-        return m_id;
-    }
-
-    public String getName() {
-        return m_name;
-    }
-
-    public void setName(String name) {
-        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 + ")";
-    }
-
-    @Override
-    public int hashCode() {
-        return m_id.hashCode() * 31;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj)
-            return true;
-        if (obj == null)
-            return false;
-        if (getClass() != obj.getClass())
-            return false;
-        TenantEntity other = (TenantEntity) obj;
-        if (getId() == null && other.getId() != null)
-            return false;
-        if (getId() != null && other.getId() == null)
-            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;
-    }
-}
+package org.amdatu.core.tenant.service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.amdatu.core.tenant.Tenant;
+
+/**
+ * Data-object representing a Tenant.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public class TenantImpl implements Tenant {
+    private final String m_id;
+    private final Map<String, String> m_properties = new HashMap<String, 
String>();
+
+    private String m_name;
+
+    public TenantImpl(String id, String name, Map<String, String> properties) {
+        if (id == null) {
+            throw new NullPointerException("id cannot be null");
+        }
+        m_id = id;
+        m_name = name;
+        if (properties != null) {
+            m_properties.putAll(properties);
+        }
+    }
+
+    public TenantImpl(String id, String name) {
+        this(id, name, new HashMap<String, String>());
+    }
+
+    public String getId() {
+        return m_id;
+    }
+
+    public String getName() {
+        return m_name;
+    }
+
+    public void setName(String name) {
+        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 + ")";
+    }
+
+    @Override
+    public int hashCode() {
+        return m_id.hashCode() * 31;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        TenantImpl other = (TenantImpl) obj;
+        if (getId() == null && other.getId() != null)
+            return false;
+        if (getId() != null && other.getId() == null)
+            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;
+    }
+}

Added: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantServiceFactory.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/service/TenantServiceFactory.java
     Thu Nov 24 14:28:01 2011
@@ -0,0 +1,72 @@
+package org.amdatu.core.tenant.service;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.amdatu.core.tenant.Tenant;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+import org.osgi.service.log.LogService;
+
+/**
+ * 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 class TenantServiceFactory implements ManagedServiceFactory {
+
+    public static final String PID = "org.amdatu.core.tenant.factory";
+
+    public static final String ID_KEY = "id";
+    public static final String NAME_KEY = "name";
+    public static final String HOST_KEY = "host";
+
+    private final Map<String, Component> m_components = new HashMap<String, 
Component>();
+
+    private volatile DependencyManager m_dependencyManager;
+    private volatile LogService m_logService;
+
+    public String getName() {
+        return PID;
+    }
+
+    public synchronized void updated(String pid, Dictionary/* <String, String> 
*/properties)
+        throws ConfigurationException {
+        if (m_components.containsKey(pid)) {
+            m_logService.log(LogService.LOG_DEBUG, "pid already registered for 
tenant '" + properties.get(ID_KEY)
+                + "', skipping registration");
+            return;
+        }
+
+        String id = (String) properties.get(ID_KEY);
+        String name = (String) properties.get(NAME_KEY);
+        String host = (String) properties.get(HOST_KEY);
+
+        Properties serviceProperties = new Properties();
+        serviceProperties.put(Tenant.TENANT_ID_SERVICEPROPERTY, id);
+        serviceProperties.put(Tenant.TENANT_NAME_SERVICEPROPERTY, name);
+        serviceProperties.put(Tenant.TENANT_SERVICEPROPERTY + "hostname", 
host);
+
+        Tenant tenant = new TenantImpl(id, name);
+        tenant.putProperty("hostname", host);
+
+        Component component = m_dependencyManager.createComponent()
+            .setInterface(Tenant.class.getName(), serviceProperties)
+            .setImplementation(tenant);
+
+        m_dependencyManager.add(component);
+        m_components.put(pid, component);
+    }
+
+    public synchronized void deleted(String pid) {
+        m_logService.log(LogService.LOG_INFO, "Unregistering tenant service 
with pid: '" + pid + "'");
+        m_dependencyManager.remove(m_components.remove(pid));
+    }
+}

Added: 
trunk/amdatu-core/tenant/src/main/resources/OSGI-INF/metatype/metatype.xml
==============================================================================
--- (empty file)
+++ trunk/amdatu-core/tenant/src/main/resources/OSGI-INF/metatype/metatype.xml  
Thu Nov 24 14:28:01 2011
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<MetaData xmlns="http://www.osgi.org/xmlns/metatype/v1.0.0"; 
xsi:schemaLocation="http://www.osgi.org/xmlns/metatype/v1.0.0 
http://www.osgi.org/xmlns/metatype/v1.0.0";>
+  <OCD name="Amdatu Tenant Configuration" id="org.amdatu.core.tenant">
+    <AD id="id" type="STRING" cardinality="1" />
+    <AD id="name" type="STRING" cardinality="1" />
+    <AD id="host" type="STRING" cardinality="1" />
+  </OCD>
+  <Designate pid="org.amdatu.core.tenant.factory.0" 
factoryPid="org.amdatu.core.tenant.factory">
+    <Object ocdref="org.amdatu.core.tenant" />
+  </Designate>
+</MetaData>
\ No newline at end of file

Modified: 
trunk/amdatu-core/tenant/src/test/java/org/amdatu/test/unit/TenantEntityTest.java
==============================================================================
--- 
trunk/amdatu-core/tenant/src/test/java/org/amdatu/test/unit/TenantEntityTest.java
   (original)
+++ 
trunk/amdatu-core/tenant/src/test/java/org/amdatu/test/unit/TenantEntityTest.java
   Thu Nov 24 14:28:01 2011
@@ -15,7 +15,7 @@
  */
 package org.amdatu.test.unit;
 
-import org.amdatu.core.tenant.TenantEntity;
+import org.amdatu.core.tenant.service.TenantImpl;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -24,10 +24,10 @@
     @Test
     // AMDATU-260
     public void testEquality() {
-        TenantEntity t1 = new TenantEntity("t13245", "Kwik");
-        TenantEntity t2 = new TenantEntity("t13245", "Kwek");
+        TenantImpl t1 = new TenantImpl("t13245", "Kwik");
+        TenantImpl t2 = new TenantImpl("t13245", "Kwek");
         Assert.assertEquals(t1, t2);
-        TenantEntity t3 = new TenantEntity("t23245", "Kwek");
+        TenantImpl t3 = new TenantImpl("t23245", "Kwek");
         Assert.assertNotSame(t2, t3);
     }
 }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to