incubator-ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1162847 - in /incubator/ace/trunk/ace-discovery-property/src: main/java/org/apache/ace/discovery/property/ main/java/org/apache/ace/discovery/property/constants/ test/java/org/apache/ace/discovery/property/
Date Mon, 29 Aug 2011 15:07:32 GMT
Author: marrs
Date: Mon Aug 29 15:07:31 2011
New Revision: 1162847

URL: http://svn.apache.org/viewvc?rev=1162847&view=rev
Log:
ACE-172 refactored property based discovery bundle

Modified:
    incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/Activator.java
    incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/PropertyBasedDiscovery.java
    incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/constants/DiscoveryConstants.java
    incubator/ace/trunk/ace-discovery-property/src/test/java/org/apache/ace/discovery/property/PropertyBasedDiscoveryTest.java

Modified: incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/Activator.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/Activator.java?rev=1162847&r1=1162846&r2=1162847&view=diff
==============================================================================
--- incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/Activator.java
(original)
+++ incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/Activator.java
Mon Aug 29 15:07:31 2011
@@ -18,33 +18,108 @@
  */
 package org.apache.ace.discovery.property;
 
+import java.net.MalformedURLException;
 import java.util.Dictionary;
+import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Map;
+import java.util.Properties;
 
 import org.apache.ace.discovery.Discovery;
 import org.apache.ace.discovery.property.constants.DiscoveryConstants;
+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 void init(BundleContext context, DependencyManager manager) throws Exception {
         Dictionary properties = new Hashtable();
         properties.put(Constants.SERVICE_PID, DiscoveryConstants.DISCOVERY_PID);
         manager.add(createComponent()
-                        .setInterface(new String[] {Discovery.class.getName()}, properties)
-                        .setImplementation(PropertyBasedDiscovery.class)
-                        .add(createConfigurationDependency()
-                            .setPid(DiscoveryConstants.DISCOVERY_PID))
-                        .add(createServiceDependency()
-                            .setService(LogService.class)
-                            .setRequired(false)));
+            .setInterface(new String[] {Discovery.class.getName()}, properties)
+            .setImplementation(PropertyBasedDiscovery.class)
+            .add(createConfigurationDependency()
+                .setPid(DiscoveryConstants.DISCOVERY_PID))
+            .add(createServiceDependency()
+                .setService(LogService.class)
+                .setRequired(false)));
+        manager.add(createComponent()
+            .setInterface(ManagedServiceFactory.class.getName(), new Properties() {{ put(Constants.SERVICE_PID,
DiscoveryConstants.DISCOVERY_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 "Discovery Service Factory";
+    }
+
+    public void updated(String pid, Dictionary dict) throws ConfigurationException {
+        String ma = (String) dict.get(MA_NAME);
+        String id = (String) dict.get(DiscoveryConstants.DISCOVERY_URL_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(DiscoveryConstants.DISCOVERY_URL_KEY, id);
+                component = m_manager.createComponent()
+                    .setInterface(Discovery.class.getName(), props)
+                    .setImplementation(new PropertyBasedDiscovery(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 PropertyBasedDiscovery) {
+                PropertyBasedDiscovery identification = (PropertyBasedDiscovery) service;
+                try {
+                    identification.setURL(id);
+                }
+                catch (MalformedURLException e) {
+                    throw new ConfigurationException(DiscoveryConstants.DISCOVERY_URL_KEY,
"Malformed URL", e);
+                }
+            }
+        }
+    }
+
+    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-discovery-property/src/main/java/org/apache/ace/discovery/property/PropertyBasedDiscovery.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/PropertyBasedDiscovery.java?rev=1162847&r1=1162846&r2=1162847&view=diff
==============================================================================
--- incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/PropertyBasedDiscovery.java
(original)
+++ incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/PropertyBasedDiscovery.java
Mon Aug 29 15:07:31 2011
@@ -36,14 +36,43 @@ import org.osgi.service.log.LogService;
  * in <code>GatewayConstants.DISCOVERY_DEFAULT_URL</code> will be used.
  */
 public class PropertyBasedDiscovery implements Discovery, ManagedService {
-
+    private final Object LOCK = new Object();
     public volatile LogService m_log; /* will be injected by dependencymanager */
     private URL m_serverURL; /* managed by configadmin */
 
+    public PropertyBasedDiscovery() {
+    }
+    
+    public PropertyBasedDiscovery(String url) {
+        try {
+            setURL(url);
+        }
+        catch (MalformedURLException e) {
+            throw new IllegalArgumentException("Cannot create instance, invalid URL argument",
e);
+        }
+    }
+    
+    public URL getURL() {
+        synchronized (LOCK) {
+            return m_serverURL;
+        }
+    }
+    
+    public void setURL(String id) throws MalformedURLException {
+        setURL(new URL(id));
+    }
+    
+    public void setURL(URL id) {
+        synchronized (LOCK) {
+            m_serverURL = id;
+        }
+    }
+    
+
     public synchronized void updated(Dictionary dictionary) throws ConfigurationException
{
         try {
             if(dictionary != null) {
-                m_serverURL = new URL((String) dictionary.get(DiscoveryConstants.DISCOVERY_URL_KEY));
+                setURL(new URL((String) dictionary.get(DiscoveryConstants.DISCOVERY_URL_KEY)));
             }
         }
         catch (MalformedURLException e) {
@@ -52,6 +81,6 @@ public class PropertyBasedDiscovery impl
     }
 
     public synchronized URL discover() {
-        return m_serverURL;
+        return getURL();
     }
 }
\ No newline at end of file

Modified: incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/constants/DiscoveryConstants.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/constants/DiscoveryConstants.java?rev=1162847&r1=1162846&r2=1162847&view=diff
==============================================================================
--- incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/constants/DiscoveryConstants.java
(original)
+++ incubator/ace/trunk/ace-discovery-property/src/main/java/org/apache/ace/discovery/property/constants/DiscoveryConstants.java
Mon Aug 29 15:07:31 2011
@@ -18,8 +18,8 @@
  */
 package org.apache.ace.discovery.property.constants;
 
-public interface DiscoveryConstants
-{
+public interface DiscoveryConstants {
     public static final String DISCOVERY_PID = "org.apache.ace.discovery.property";
+    public static final String DISCOVERY_FACTORY_PID = "org.apache.ace.discovery.property.factory";
     public static final String DISCOVERY_URL_KEY = "serverURL";
 }
\ No newline at end of file

Modified: incubator/ace/trunk/ace-discovery-property/src/test/java/org/apache/ace/discovery/property/PropertyBasedDiscoveryTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-discovery-property/src/test/java/org/apache/ace/discovery/property/PropertyBasedDiscoveryTest.java?rev=1162847&r1=1162846&r2=1162847&view=diff
==============================================================================
--- incubator/ace/trunk/ace-discovery-property/src/test/java/org/apache/ace/discovery/property/PropertyBasedDiscoveryTest.java
(original)
+++ incubator/ace/trunk/ace-discovery-property/src/test/java/org/apache/ace/discovery/property/PropertyBasedDiscoveryTest.java
Mon Aug 29 15:07:31 2011
@@ -15,6 +15,7 @@
  */
 package org.apache.ace.discovery.property;
 
+import static org.apache.ace.test.utils.TestUtils.UNIT;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
@@ -27,18 +28,18 @@ import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.apache.ace.discovery.property.constants.DiscoveryConstants;
-import org.junit.Test;
 import org.osgi.service.cm.ConfigurationException;
+import org.testng.annotations.Test;
 
 public class PropertyBasedDiscoveryTest {
-    @Test
+    @Test(groups = { UNIT })
     public void discoverWithoutPropertyUpdate() {
         PropertyBasedDiscovery discovery = new PropertyBasedDiscovery();
         URL url = discovery.discover();
         assertThat(url, is(nullValue()));
     }
 
-    @Test
+    @Test(groups = { UNIT })
     public void discoverWithPropertyUpdate() throws ConfigurationException, URISyntaxException
{
         PropertyBasedDiscovery discovery = new PropertyBasedDiscovery();
         Dictionary dict = new Hashtable();



Mime
View raw message