Return-Path: X-Original-To: apmail-incubator-ace-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-ace-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 914807623 for ; Mon, 29 Aug 2011 15:07:58 +0000 (UTC) Received: (qmail 63470 invoked by uid 500); 29 Aug 2011 15:07:58 -0000 Delivered-To: apmail-incubator-ace-commits-archive@incubator.apache.org Received: (qmail 63442 invoked by uid 500); 29 Aug 2011 15:07:58 -0000 Mailing-List: contact ace-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ace-dev@incubator.apache.org Delivered-To: mailing list ace-commits@incubator.apache.org Received: (qmail 63430 invoked by uid 99); 29 Aug 2011 15:07:57 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Aug 2011 15:07:57 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Aug 2011 15:07:53 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 9B9C8238888F; Mon, 29 Aug 2011 15:07:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: ace-commits@incubator.apache.org From: marrs@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110829150732.9B9C8238888F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 /**/ 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 GatewayConstants.DISCOVERY_DEFAULT_URL 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();