ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1162804 - in /incubator/ace/trunk/ace-identification-property/src: main/java/org/apache/ace/identification/property/ main/java/org/apache/ace/identification/property/constants/ test/java/org/apache/ace/identification/property/
Date Mon, 29 Aug 2011 13:54:48 GMT
Author: marrs
Date: Mon Aug 29 13:54:47 2011
New Revision: 1162804

URL: http://svn.apache.org/viewvc?rev=1162804&view=rev
Log:
ACE-172 refactored the identification service to also be a managed service factory

Modified:
    incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/Activator.java
    incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/PropertyBasedIdentification.java
    incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/constants/IdentificationConstants.java
    incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
    incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/SimpleIdentificationTest.java

Modified: incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/Activator.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/Activator.java?rev=1162804&r1=1162803&r2=1162804&view=diff
==============================================================================
--- incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/Activator.java
(original)
+++ incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/Activator.java
Mon Aug 29 13:54:47 2011
@@ -18,16 +18,33 @@
  */
 package org.apache.ace.identification.property;
 
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
 import org.apache.ace.identification.Identification;
 import org.apache.ace.identification.property.constants.IdentificationConstants;
+import org.apache.felix.dm.Component;
 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.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
 import org.osgi.service.log.LogService;
 
-public class Activator extends DependencyActivatorBase {
+public class Activator extends DependencyActivatorBase implements ManagedServiceFactory {
+    private static final String MA_NAME = "ma";
+    private DependencyManager m_manager;
+    private BundleContext m_context;
+    private final Map /*<String, Component>*/ m_instances = new HashMap();
+    private volatile LogService m_log;
+    
     public synchronized void init(BundleContext context, DependencyManager manager) throws
Exception {
-       manager.add(createComponent()
+        m_manager = manager;
+        m_context = context;
+        manager.add(createComponent()
             .setInterface(new String[] {Identification.class.getName()}, null)
             .setImplementation(PropertyBasedIdentification.class)
             .add(createConfigurationDependency()
@@ -36,9 +53,67 @@ public class Activator extends Dependenc
                 .setService(LogService.class)
                 .setRequired(false))
             );
+        manager.add(createComponent()
+           .setInterface(ManagedServiceFactory.class.getName(), new Properties() {{ put(Constants.SERVICE_PID,
IdentificationConstants.IDENTIFICATION_FACTORY_PID); }})
+           .setImplementation(this)
+           .add(createServiceDependency()
+               .setService(LogService.class)
+               .setRequired(false))
+           );
     }
 
     public void destroy(BundleContext context, DependencyManager manager) throws Exception
{
         // do nothing
     }
+
+    public String getName() {
+        return "Identification Service Factory";
+    }
+
+    public void updated(String pid, Dictionary dict) throws ConfigurationException {
+        String ma = (String) dict.get(MA_NAME);
+        String id = (String) dict.get(IdentificationConstants.IDENTIFICATION_TARGETID_KEY);
+
+        boolean needToAddComponent = false;
+        Component component;
+        synchronized (m_instances) {
+            component = (Component) m_instances.get(pid);
+            if (component == null) {
+                Properties props = new Properties();
+                if ((ma != null) && (ma.length() > 0)) {
+                    props.put(MA_NAME, ma);
+                }
+                props.put(IdentificationConstants.IDENTIFICATION_TARGETID_KEY, id);
+                component = m_manager.createComponent()
+                    .setInterface(Identification.class.getName(), props)
+                    .setImplementation(new PropertyBasedIdentification(id))
+                    .add(createServiceDependency()
+                        .setService(LogService.class)
+                        .setRequired(false)
+                    );
+                m_instances.put(pid, component);
+                needToAddComponent = true;
+            }
+        }
+        if (needToAddComponent) {
+            m_manager.add(component);
+        }
+        else {
+            Object service = component.getService();
+            if (service instanceof PropertyBasedIdentification) {
+                PropertyBasedIdentification identification = (PropertyBasedIdentification)
service;
+                identification.setID(id);
+            }
+        }
+    }
+
+    public void deleted(String pid) {
+        Component log;
+        synchronized (m_instances) {
+            log = (Component) m_instances.remove(pid);
+        }
+        if (log != null) {
+            m_manager.remove(log);
+        }
+    }
 }
\ No newline at end of file

Modified: incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/PropertyBasedIdentification.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/PropertyBasedIdentification.java?rev=1162804&r1=1162803&r2=1162804&view=diff
==============================================================================
--- incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/PropertyBasedIdentification.java
(original)
+++ incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/PropertyBasedIdentification.java
Mon Aug 29 13:54:47 2011
@@ -28,31 +28,45 @@ import org.osgi.service.log.LogService;
 
 /**
  * Simple implementation of the <code>Identification</code> interface. Because
- * a gateway identification should not change during it's lifetime the user of this
+ * a target identification should not change during it's lifetime the user of this
  * implementation should set the ID only once.
  */
 public class PropertyBasedIdentification implements ManagedService, Identification {
+    private final Object LOCK = new Object();
     private volatile LogService m_log;
-    private String m_gatewayID;
+    private String m_targetID;
+    
+    public PropertyBasedIdentification() {
+    }
+    
+    public PropertyBasedIdentification(String id) {
+        setID(id);
+    }
 
-    public synchronized String getID() {
-        return m_gatewayID;
+    public String getID() {
+        synchronized (LOCK) {
+            return m_targetID;
+        }
+    }
+    
+    public void setID(String id) {
+        synchronized (LOCK) {
+            if (m_targetID != null) {
+                m_log.log(LogService.LOG_WARNING, "Target ID is being changed from " + m_targetID
+ " to " + id);
+            }
+            m_targetID = id;
+        }
     }
 
     public void updated(Dictionary dictionary) throws ConfigurationException {
         if (dictionary != null) {
-            String id = (String) dictionary.get(IdentificationConstants.IDENTIFICATION_GATEWAYID_KEY);
+            String id = (String) dictionary.get(IdentificationConstants.IDENTIFICATION_TARGETID_KEY);
             if ((id == null) || (id.length() == 0)) {
                 // illegal config
-                throw new ConfigurationException(IdentificationConstants.IDENTIFICATION_GATEWAYID_KEY,
"Illegal gateway ID supplied");
-            }
-            if (m_gatewayID != null) {
-                m_log.log(LogService.LOG_WARNING, "Gateway ID is being changed from " + m_gatewayID
+ " to " + id);
+                throw new ConfigurationException(IdentificationConstants.IDENTIFICATION_TARGETID_KEY,
"Illegal target ID supplied");
             }
             // legal config, set configuration
-            synchronized (this) {
-                m_gatewayID = id;
-            }
+            setID(id);
         }
     }
 }
\ No newline at end of file

Modified: incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/constants/IdentificationConstants.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/constants/IdentificationConstants.java?rev=1162804&r1=1162803&r2=1162804&view=diff
==============================================================================
--- incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/constants/IdentificationConstants.java
(original)
+++ incubator/ace/trunk/ace-identification-property/src/main/java/org/apache/ace/identification/property/constants/IdentificationConstants.java
Mon Aug 29 13:54:47 2011
@@ -18,8 +18,8 @@
  */
 package org.apache.ace.identification.property.constants;
 
-public interface IdentificationConstants
-{
+public interface IdentificationConstants {
     public static final String IDENTIFICATION_PID = "org.apache.ace.identification.property";
-    public static final String IDENTIFICATION_GATEWAYID_KEY = "gatewayID";
+    public static final String IDENTIFICATION_FACTORY_PID = "org.apache.ace.identification.property.factory";
+    public static final String IDENTIFICATION_TARGETID_KEY = "gatewayID";
 }
\ No newline at end of file

Modified: incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java?rev=1162804&r1=1162803&r2=1162804&view=diff
==============================================================================
--- incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
(original)
+++ incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/PropertyBasedIdentificationTest.java
Mon Aug 29 13:54:47 2011
@@ -18,6 +18,7 @@
  */
 package org.apache.ace.identification.property;
 
+import static org.apache.ace.test.utils.TestUtils.UNIT;
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsEqual.equalTo;
 import static org.hamcrest.core.IsNull.nullValue;
@@ -29,35 +30,35 @@ import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.apache.ace.identification.property.constants.IdentificationConstants;
-import org.junit.Test;
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.log.LogService;
+import org.testng.annotations.Test;
 
 public class PropertyBasedIdentificationTest {
-    @Test
+    @Test(groups = { UNIT })
     public void getIdWithoutUpdate() {
         PropertyBasedIdentification basedIdentification = new PropertyBasedIdentification();
         assertThat( basedIdentification.getID(), is( nullValue() ) );
     }
 
-    @Test
+    @Test(groups = { UNIT })
     public void getIdWithUpdate() throws ConfigurationException {
         PropertyBasedIdentification basedIdentification = new PropertyBasedIdentification();
         Dictionary dict = new Hashtable();
-        dict.put( IdentificationConstants.IDENTIFICATION_GATEWAYID_KEY, "myGatewayId" );
+        dict.put( IdentificationConstants.IDENTIFICATION_TARGETID_KEY, "myGatewayId" );
         basedIdentification.updated( dict );
         assertThat( basedIdentification.getID(), is( equalTo( "myGatewayId" ) ) );
     }
 
-    @Test
+    @Test(groups = { UNIT })
     public void getIdOverwrite() throws ConfigurationException {
         PropertyBasedIdentification basedIdentification = new PropertyBasedIdentification();
         injectServices( basedIdentification );
 
         Dictionary dict = new Hashtable();
-        dict.put( IdentificationConstants.IDENTIFICATION_GATEWAYID_KEY, "oldId" );
+        dict.put( IdentificationConstants.IDENTIFICATION_TARGETID_KEY, "oldId" );
         Dictionary dict2 = new Hashtable();
-        dict2.put( IdentificationConstants.IDENTIFICATION_GATEWAYID_KEY, "newId" );
+        dict2.put( IdentificationConstants.IDENTIFICATION_TARGETID_KEY, "newId" );
 
         basedIdentification.updated( dict );
         basedIdentification.updated( dict2 );

Modified: incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/SimpleIdentificationTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/SimpleIdentificationTest.java?rev=1162804&r1=1162803&r2=1162804&view=diff
==============================================================================
--- incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/SimpleIdentificationTest.java
(original)
+++ incubator/ace/trunk/ace-identification-property/src/test/java/org/apache/ace/identification/property/SimpleIdentificationTest.java
Mon Aug 29 13:54:47 2011
@@ -49,7 +49,7 @@ public class SimpleIdentificationTest {
     public void testSimpleIdentification() throws Exception {
         m_identification.updated(
             new Properties() {
-                {put(IdentificationConstants.IDENTIFICATION_GATEWAYID_KEY, TEST_ID);}
+                {put(IdentificationConstants.IDENTIFICATION_TARGETID_KEY, TEST_ID);}
             });
         assert TEST_ID.equals(m_identification.getID()) : "gateway ID does not match configured
gateway ID";
     }



Mime
View raw message