You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by fu...@apache.org on 2006/07/03 12:03:09 UTC

svn commit: r418731 - in /incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/felix/ src/main/java/org/apache/felix/eventad...

Author: furfari
Date: Mon Jul  3 03:03:08 2006
New Revision: 418731

URL: http://svn.apache.org/viewvc?rev=418731&view=rev
Log:
Test code to play with eventadmin/bridge/upnp 

Added:
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/pom.xml
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/upnp/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/upnp/test/
    incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/upnp/test/Activator.java

Added: incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/pom.xml
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/pom.xml?rev=418731&view=auto
==============================================================================
--- incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/pom.xml (added)
+++ incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/pom.xml Mon Jul  3 03:03:08 2006
@@ -0,0 +1,52 @@
+<project>
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix</artifactId>
+    <version>0.8.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>osgi-bundle</packaging>
+  <name>test bridge</name>
+  <artifactId>org.apache.felix.eventadmin.bridge.upnp.test</artifactId>
+  <dependencies>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+     <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix.plugins</groupId>
+        <artifactId>maven-osgi-plugin</artifactId>
+        <version>${pom.version}</version>
+        <extensions>true</extensions>
+        <configuration>
+          <osgiManifest>
+            <bundleName>Test EA-UPnP Bridge </bundleName>
+            <bundleDescription>
+              A simple bundle to play with the EA-UPnP Bridge
+            </bundleDescription>
+            <bundleActivator>
+              org.apache.felix.eventadmin.bridge.upnp.test.Activator
+            </bundleActivator>
+            <bundleSymbolicName>
+              org.apache.felix.eventadmin.bridge.upnp.test
+            </bundleSymbolicName>
+            <importPackage>
+              javax.swing,javax.swing.text,org.osgi.framework,org.osgi.service.upnp;specification-version=1.1,org.osgi.service.event
+            </importPackage>
+          </osgiManifest>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Added: incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/upnp/test/Activator.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/upnp/test/Activator.java?rev=418731&view=auto
==============================================================================
--- incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/upnp/test/Activator.java (added)
+++ incubator/felix/sandbox/furfari/org.apache.felix.eventadmin.bridge.upnp.test/src/main/java/org/apache/felix/eventadmin/bridge/upnp/test/Activator.java Mon Jul  3 03:03:08 2006
@@ -0,0 +1,210 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.felix.eventadmin.bridge.upnp.test;
+
+
+import java.awt.Container;
+import java.awt.FlowLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.swing.AbstractAction;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+
+
+/**
+ * A simple bundle to play with the EventAdmin UPnP Bridge.
+ * Usage: Each EventHandler is represented by a JFrame.
+ * To register an EH without filter left the filter field empty.
+ * The EH is unregistered closing the window.
+ * 
+ * 
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class Activator implements BundleActivator
+{
+    static BundleContext ctx;
+ 	private ArrayList frames = new ArrayList();
+
+	/**
+     * 
+     * @param context the framework context for the bundle.
+     */
+    public void start( final BundleContext context )
+    {
+       System.out.println( "Starting bundle to test EA-UPnP bridge." );
+       
+        ctx = context;
+        JButton btnHandler = new JButton("new EventHandler");
+        btnHandler.addActionListener(new AbstractAction(){
+			public void actionPerformed(ActionEvent e) {
+				frames.add(new EventHandlerFrame());
+			};
+        });
+        JTextArea area =  new JTextArea(7,20);
+        area.append("(upnp.deviceId=uuid:Felix-BinaryLight)");
+        area.append("\n(upnp.deviceId=uuid:Felix-TV)");
+        area.append("\n(upnp.deviceId=uuid:Felix-Clock)");
+        area.append("\n(upnp.serviceId=urn:upnp-org:serviceId:SwitchPower:1)            Light");
+        area.append("\n(upnp.serviceId=urn:upnp-org:serviceId:SwitchPower.0001)      Intel light");
+        area.append("\n(upnp.serviceId=urn:schemas-upnp-org:serviceId:power:1)       TV");
+        area.append("\n(upnp.serviceId=urn:schemas-upnp-org:serviceId:timer:1)         Clock");
+        
+        JFrame main = new JFrame("Test EventAdmin UPnP Bridge");
+        main.setSize(450,220);
+        main.setResizable(false);
+        main.getContentPane().setLayout(new FlowLayout());
+        main.getContentPane().add(btnHandler);
+        main.getContentPane().add(new JLabel("Copy & Paste to Compose the Filter of the EventHandler:                     "));
+        main.getContentPane().add(area);
+        
+		main.addWindowListener(new WindowAdapter(){
+			public void windowClosing(WindowEvent e) 
+			{
+				try {
+					context.getBundle().stop();
+				} catch (BundleException ex) {
+					ex.printStackTrace();
+				}
+			}
+		});			
+        frames.add(main);
+        main.setVisible(true);
+
+
+    }
+
+
+    /**
+     * @param context the framework context for the bundle.
+     */
+    public void stop( BundleContext context )
+    {
+    	for (int i = 0; i< frames.size();i++)
+    		((JFrame)frames.get(i)).dispose();
+    }
+
+
+ }
+
+class EventHandlerFrame extends JFrame implements EventHandler, ActionListener {
+
+	private JButton btnUpdate;
+	private JButton btnRegister;
+	private JTextField txtFilter;
+	private ServiceRegistration reg;
+	private JTextArea list;
+
+	public EventHandlerFrame(){
+		super("EventHandler");
+	    setSize(500,300);
+	    setResizable(false);
+	    
+		Container contentPane = getContentPane();
+		contentPane.setLayout(new FlowLayout());
+		contentPane.add(new JLabel("Filter:"));
+		txtFilter = new JTextField(40);
+		contentPane.add(txtFilter);
+		
+		btnUpdate = new JButton("update");
+		btnUpdate.addActionListener(this);
+		btnUpdate.setVisible(false);
+		btnRegister = new JButton("register");
+		btnRegister.addActionListener(this);
+		contentPane.add(btnUpdate);	
+		contentPane.add(btnRegister);	
+		
+		list = new JTextArea(12,40);
+		contentPane.add(new JScrollPane(list));	
+		
+		addWindowListener(new WindowAdapter(){
+			public void windowClosing(WindowEvent e) {
+				if (reg != null) reg.unregister();
+				dispose();
+			}
+		});			
+        setVisible(true);
+		
+	}
+	
+	public void handleEvent(Event event) {
+		list.append("--------------- new event received -------------------");
+		list.append("\nDeviceID -> " + event.getProperty("upnp.deviceId"));
+		list.append("\nServiceID -> " + event.getProperty("upnp.serviceId"));
+		list.append("\nevents ->" + event.getProperty("upnp.events") + "\n");
+		
+		
+	}
+
+	Dictionary d = new Hashtable();
+	public void actionPerformed(ActionEvent e) {
+		if(e.getSource()== btnRegister){	     
+	        d.put(EventConstants.EVENT_TOPIC,"org/osgi/service/upnp/UPnPEvent");
+	        String filter = txtFilter.getText().trim();
+	        if (! filter.equals("")){
+	        	try {
+					Activator.ctx.createFilter(filter);
+				} catch (InvalidSyntaxException e1) {
+					JOptionPane.showMessageDialog(this,"Attention, invalid filter syntax !");
+					return;
+				}
+	            d.put(EventConstants.EVENT_FILTER,filter);
+	        }
+	        reg = Activator.ctx.registerService(EventHandler.class.getName(),this,d);
+	        btnRegister.setVisible(false);
+	        btnUpdate.setVisible(true);
+		}
+		else if(e.getSource()== btnUpdate){
+	        String filter = txtFilter.getText().trim();
+	        if (filter.equals("")){
+	            d.remove(EventConstants.EVENT_FILTER);
+	        }
+	        else {
+	        	try {
+					Activator.ctx.createFilter(filter);
+				} catch (InvalidSyntaxException e1) {
+					JOptionPane.showMessageDialog(this,"Attention, invalid filter syntax !");
+					return;
+				}
+	            d.put(EventConstants.EVENT_FILTER,filter);
+	        }
+	        reg.setProperties(d);			
+		}
+	}
+	
+}
+