You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by ja...@apache.org on 2016/01/29 14:20:08 UTC

svn commit: r1727574 - in /ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp: ./ src/ src/org/ src/org/apache/ src/org/apache/ace/ src/org/apache/ace/discovery/ src/org/apache/ace/discovery/upnp/ test/ test/org/ test/org/apache/ test/org/apache/...

Author: jawi
Date: Fri Jan 29 13:20:07 2016
New Revision: 1727574

URL: http://svn.apache.org/viewvc?rev=1727574&view=rev
Log:
Forgot to move this one from ACE-core to ace-extras.

Added:
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.classpath   (with props)
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.project   (with props)
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/bnd.bnd
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/Activator.java   (with props)
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/UPnPBasedDiscovery.java   (with props)
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/upnp/
    ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/upnp/SimpleDiscoveryTest.java   (with props)

Added: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.classpath
URL: http://svn.apache.org/viewvc/ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.classpath?rev=1727574&view=auto
==============================================================================
--- ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.classpath (added)
+++ ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.classpath Fri Jan 29 13:20:07 2016
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="bin" path="src"/>
+	<classpathentry kind="src" output="bin_test" path="test"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+	<classpathentry kind="con" path="aQute.bnd.classpath.container"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

Propchange: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.classpath
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.project
URL: http://svn.apache.org/viewvc/ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.project?rev=1727574&view=auto
==============================================================================
--- ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.project (added)
+++ ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.project Fri Jan 29 13:20:07 2016
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>org.apache.ace.discovery.upnp</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>bndtools.core.bndbuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>bndtools.core.bndnature</nature>
+	</natures>
+</projectDescription>

Propchange: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/.project
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/bnd.bnd
URL: http://svn.apache.org/viewvc/ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/bnd.bnd?rev=1727574&view=auto
==============================================================================
--- ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/bnd.bnd (added)
+++ ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/bnd.bnd Fri Jan 29 13:20:07 2016
@@ -0,0 +1,17 @@
+# Licensed to the Apache Software Foundation (ASF) under the terms of ASLv2 (http://www.apache.org/licenses/LICENSE-2.0).
+
+-buildpath: \
+	${^-buildpath},\
+	testng,\
+	osgi.core;version=6.0.0,\
+	osgi.cmpn,\
+	org.apache.felix.dependencymanager,\
+	org.apache.ace.test;version=latest,\
+	org.apache.ace.discovery.api;version=latest,\
+	org.easymock
+
+Private-Package: org.apache.ace.discovery.upnp
+Bundle-Activator: org.apache.ace.discovery.upnp.Activator
+Bundle-Version: 1.0.2
+Bundle-Name: Apache ACE Discovery UPnP
+Bundle-Description: Registers a service factory for UPnP based Discovery services
\ No newline at end of file

Added: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/Activator.java
URL: http://svn.apache.org/viewvc/ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/Activator.java?rev=1727574&view=auto
==============================================================================
--- ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/Activator.java (added)
+++ ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/Activator.java Fri Jan 29 13:20:07 2016
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.discovery.upnp;
+
+import org.apache.ace.discovery.Discovery;
+import org.apache.felix.dm.DependencyActivatorBase;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+import org.osgi.service.upnp.UPnPDevice;
+
+public class Activator extends DependencyActivatorBase {
+
+    public void init(BundleContext context, DependencyManager manager) throws Exception {
+        String deviceFilter = String.format("(%s=%s)", UPnPDevice.TYPE, UPnPBasedDiscovery.DEVICE_TYPE);
+
+        manager.add(createComponent()
+            .setInterface(new String[] { Discovery.class.getName() }, null)
+            .setImplementation(UPnPBasedDiscovery.class)
+            .add(createServiceDependency()
+                .setService(LogService.class)
+                .setRequired(false))
+            // not required
+            .add(createServiceDependency()
+                .setService(UPnPDevice.class, deviceFilter.toString())
+                .setCallbacks("added", "removed")
+                .setRequired(false)));
+    }
+
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception {
+        // do nothing
+    }
+}

Propchange: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/Activator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/UPnPBasedDiscovery.java
URL: http://svn.apache.org/viewvc/ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/UPnPBasedDiscovery.java?rev=1727574&view=auto
==============================================================================
--- ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/UPnPBasedDiscovery.java (added)
+++ ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/UPnPBasedDiscovery.java Fri Jan 29 13:20:07 2016
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.discovery.upnp;
+
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.ace.discovery.Discovery;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogService;
+import org.osgi.service.upnp.UPnPAction;
+import org.osgi.service.upnp.UPnPDevice;
+import org.osgi.service.upnp.UPnPService;
+
+/**
+ * Simple implementation of the <code>Discovery</code> interface. It 'discovers' the server by means of UPnP.
+ */
+public class UPnPBasedDiscovery implements Discovery {
+    final static String DEVICE_TYPE = "urn:schemas-upnp-org:device:ProvisioningDevice:1";
+    final static String SERVICE_ID = "urn:upnp-org:serviceId:LocationService:1";
+    final static String ACTION_GET_LOCATION = "GetLocation";
+    final static String ACTION_GET_TYPE = "GetServerType";
+    final static String ACTION_GET_LOAD = "GetServerLoad";
+
+    private volatile LogService m_log; /* will be injected by dependencymanager */
+    private volatile List<UPnPDevice> m_devices;
+
+    public void start() {
+        m_devices = new CopyOnWriteArrayList<>();
+    }
+
+    // Called by Felix DM.
+    public void added(ServiceReference<UPnPDevice> ref, UPnPDevice device) {
+        m_devices.add(device);
+    }
+
+    // Called by Felix DM.
+    public void removed(ServiceReference<UPnPDevice> ref, UPnPDevice device) {
+        m_devices.remove(device);
+    }
+
+    public synchronized URL discover() {
+        try {
+            return getLocation();
+        }
+        catch (Exception e) {
+            m_log.log(LogService.LOG_DEBUG, "unable to retrieve location property", e);
+        }
+
+        return null;
+    }
+
+    private URL getLocation() {
+        UPnPAction action = getAction(ACTION_GET_LOCATION);
+        try {
+            Dictionary<String, ?> dict = action.invoke(null);
+            String location = (String) dict.get(action.getOutputArgumentNames()[0]);
+            return new URL(location);
+        }
+        catch (Exception e) {
+            m_log.log(LogService.LOG_DEBUG, "Failed to get location!", e);
+        }
+        return null;
+    }
+
+    @SuppressWarnings("unused")
+    private String getType() {
+        UPnPAction action = getAction(ACTION_GET_TYPE);
+        try {
+            Dictionary<String, ?> dict = action.invoke(null);
+            return (String) dict.get(action.getOutputArgumentNames()[0]);
+        }
+        catch (Exception e) {
+            m_log.log(LogService.LOG_DEBUG, "Failed to get type!", e);
+        }
+        return "Unknown";
+    }
+
+    @SuppressWarnings("unused")
+    private int getLoad() {
+        UPnPAction action = getAction(ACTION_GET_LOAD);
+        try {
+            Dictionary<String, ?> dict = action.invoke(null);
+            Integer val = (Integer) dict.get(action.getOutputArgumentNames()[0]);
+            return val.intValue();
+        }
+        catch (Exception e) {
+            m_log.log(LogService.LOG_DEBUG, "Failed to get load!", e);
+        }
+        return 100;
+    }
+
+    private UPnPAction getAction(String name) {
+        UPnPDevice device = null;
+        // zero-order implementation
+        if (!m_devices.isEmpty()) {
+            device = m_devices.get(0);
+        }
+
+        if (device != null) {
+            UPnPService svc = device.getService(SERVICE_ID);
+            if (svc != null) {
+                return svc.getAction(name);
+            }
+        }
+        return null;
+    }
+}

Propchange: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/src/org/apache/ace/discovery/upnp/UPnPBasedDiscovery.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/upnp/SimpleDiscoveryTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/upnp/SimpleDiscoveryTest.java?rev=1727574&view=auto
==============================================================================
--- ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/upnp/SimpleDiscoveryTest.java (added)
+++ ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/upnp/SimpleDiscoveryTest.java Fri Jan 29 13:20:07 2016
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.discovery.upnp;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import java.net.URL;
+import java.util.Properties;
+
+import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.TestUtils;
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.log.LogService;
+import org.osgi.service.upnp.UPnPAction;
+import org.osgi.service.upnp.UPnPDevice;
+import org.osgi.service.upnp.UPnPService;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class SimpleDiscoveryTest {
+
+    private static final String VALID_URL = "http://test.url.com:" + TestConstants.PORT;
+    private static final String INVALID_URL = "malformed url";
+
+    private UPnPBasedDiscovery m_discovery;
+
+
+    private IMocksControl m_control = EasyMock.createControl();
+
+
+    @BeforeMethod(alwaysRun = true)
+    protected void setUp() throws Exception {
+        m_discovery = new UPnPBasedDiscovery();
+        m_discovery.start();
+        
+        TestUtils.configureObject(m_discovery, LogService.class);
+    }
+
+    /**
+     * Test if discovering while there's no UPnPService
+     * works as expected (returns null)
+     *
+     * @throws Exception
+     */
+    @Test(groups = { UNIT })
+    public void simpleDiscoveryNoUPnPServices() throws ConfigurationException {
+        URL url = m_discovery.discover();
+        assert (url == null) : "Invalid url was returned (should have been null): " + url;
+    }
+
+    private UPnPDevice expectDeviceAdditionForURL(String url, String type) throws Exception {
+        final String returnLocation = "returnLocation";
+        final String returnType = "returnType";
+
+        final Properties p = new Properties();
+        p.put(returnLocation, url);
+        p.put(returnType, type);
+
+        m_control.checkOrder(false);
+
+        final UPnPAction action1 = m_control.createMock(UPnPAction.class);
+        EasyMock.expect(action1.getOutputArgumentNames()).andReturn(new String[]{returnLocation}).anyTimes();
+        EasyMock.expect(action1.invoke(null)).andReturn(p).anyTimes();
+
+        final UPnPAction action2 = m_control.createMock(UPnPAction.class);
+        EasyMock.expect(action2.getOutputArgumentNames()).andReturn(new String[]{returnType}).anyTimes();
+        EasyMock.expect(action2.invoke(null)).andReturn(p).anyTimes();
+
+        final UPnPService service = m_control.createMock(UPnPService.class);
+        EasyMock.expect(service.getAction("GetLocation")).andReturn(action1).anyTimes();
+        EasyMock.expect(service.getAction("GetServerType")).andReturn(action2).anyTimes();
+
+        UPnPDevice device = m_control.createMock(UPnPDevice.class);
+        EasyMock.expect(device.getService(EasyMock.isA(String.class))).andReturn(service).anyTimes();
+
+        return device;
+    }
+
+    /**
+     * Test if the url as provided by a UPnPDevice
+     * is returned as expected.
+     * @throws Exception
+     */
+    @Test(groups = {UNIT})
+    public void simpleDiscoverySingleUPnPDevice() throws Exception {
+
+        m_control.reset();
+
+        UPnPDevice device = expectDeviceAdditionForURL(VALID_URL, "RelayServer");
+        ServiceReference<UPnPDevice> ref = m_control.createMock(ServiceReference.class);
+
+        m_control.replay();
+
+        m_discovery.added(ref, device);
+        m_discovery.discover();
+
+        m_control.verify();
+    }
+
+    /**
+     * Test if the url as provided by a UPnPDevice
+     * is returned as expected.
+     * @throws Exception
+     */
+    @Test(groups = {UNIT})
+    public void simpleDiscoveryMultipleUPnPDevices() throws Exception {
+        m_control.reset();
+
+        UPnPDevice device1 = expectDeviceAdditionForURL(VALID_URL, "RelayServer");
+        UPnPDevice device2 = expectDeviceAdditionForURL(INVALID_URL, "RelayServer");
+
+        ServiceReference<UPnPDevice> ref = m_control.createMock(ServiceReference.class);
+
+        m_control.replay();
+
+        m_discovery.added(ref, device1);
+        m_discovery.added(ref, device2);
+
+        URL url = m_discovery.discover();
+        assert VALID_URL.equals(url.toString()) : "Valid url was not returned";
+
+        m_control.verify();
+    }
+}

Propchange: ace/sandbox/jawi/ace-extra/org.apache.ace.discovery.upnp/test/org/apache/ace/discovery/upnp/SimpleDiscoveryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native