felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From don...@apache.org
Subject svn commit: r517940 - in /incubator/felix/sandbox/donsez/eventadmin.cmd: ./ doc/ 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/eventadmin/ src/main/java/org/apa...
Date Tue, 13 Mar 2007 23:46:30 GMT
Author: donsez
Date: Tue Mar 13 16:46:28 2007
New Revision: 517940

URL: http://svn.apache.org/viewvc?view=rev&rev=517940
Log:
initial commit of the eventadmin command

Added:
    incubator/felix/sandbox/donsez/eventadmin.cmd/
    incubator/felix/sandbox/donsez/eventadmin.cmd/doc/
    incubator/felix/sandbox/donsez/eventadmin.cmd/doc/eventadmin.txt   (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/doc/readme.html   (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/pom.xml   (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventAdminCommand.java
  (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSink.java
  (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSinkWithDelay.java
  (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventPropertiesWrapper.java
  (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventUtil.java
  (with props)
    incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/resources/

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/doc/eventadmin.txt
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/doc/eventadmin.txt?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/doc/eventadmin.txt (added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/doc/eventadmin.txt Tue Mar 13 16:46:28 2007
@@ -0,0 +1,45 @@
+rem This script illustrates the Event Admin service v1.0 (R4 chapter 113)
+rem (c) Didier Donsez, 2005
+rem start the "run" command http://www-adele.imag.fr/users/Didier.Donsez/dev/osgi/scriptcmd/scriptcmd.jar
to run this script
+
+rem failifnot filter (org.osgi.framework.version>=4.0)
+
+cd http://www-adele.imag.fr/users/Didier.Donsez/dev/osgi/
+
+obr install "OSGi Util"
+obr install "OSGi Service"
+obr install "Event Admin"
+
+start eventadmincmd/eventadmincmd.jar
+
+rem handlers for standard events
+eventadmin subscribe org/osgi/framework/*
+eventadmin subscribe org/osgi/service/log/LogEntry/*
+eventadmin subscribe org/osgi/service/cm/ConfigurationEvent/*
+eventadmin subscribe org/osgi/service/wireadmin/WireAdminEvent/*
+
+start eventadmindemo/eventadmindemo.jar
+
+eventadmin subscribe fr/imag/*
+eventadmin subscribe fr/imag/adele/*
+eventadmin subscribe fr/imag/adele/TimerEvent
+eventadmin subscribe org/foo/*
+
+eventadmin send fr/imag/adele/DummyEvent foo=bar&tic=tac%20toe
+eventadmin post fr/imag/adele/DummyEvent bar=foo&tic=tac%20toe+tic
+
+eventadmin list
+
+
+eventadmin remove fr/imag/*
+eventadmin remove fr/imag/adele/*
+
+rem Test the UPnP EA events
+eventadmin subscribe org/osgi/service/upnp/UPnPEvent/*
+obr start "UPnP Base Driver"
+start upnpeventpublisher/upnpeventpublisher.jar
+
+rem Test the heart beat events
+start heartbeateventpublisher/heartbeateventpublisher.jar
+eventadmin subscribe fr/imag/adele/management/HeartBeatEvent/*
+rem in another profile, start http://www-adele.imag.fr/users/Didier.Donsez/dev/osgi/heartbeat/heartbeat.jar
\ No newline at end of file

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/doc/eventadmin.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/doc/readme.html
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/doc/readme.html?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/doc/readme.html (added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/doc/readme.html Tue Mar 13 16:46:28 2007
@@ -0,0 +1,64 @@
+<!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en">
+<html>
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+  <title>Event Admin Command</title>
+</head>
+<body alink="#ff0000" vlink="#551a8b" link="#0000ee" bgcolor="#ffffff"
+ text="#000000">
+<h1><i>Event Admin Command</i></h1>
+
+<p><b>Description</b><br>
+This bundle provides a command to use and test the Event Admin Service version 1.0
+(as defined in the OSGi specification release 4).
+</p>
+</font>
+
+<p><b>Contributor(s)</b><br>
+Apache Felix team
+</p>
+
+<p><b>Distribution</b><br>
+<ul>
+<li><a href="eventadmin.cmd.jar">the bundle jar</a></li>
+<li><a href="eventadmin.cmd-src.jar">the source jar</a></li>
+</ul>
+</p>
+
+
+<p><b>License</b><br>
+ASL2
+</p>
+
+
+<p><b>Services</b><br>
+<ul>
+<li>org.osgi.service.event.EventHandler</li>
+<li>org.ungoverned.osgi.service.shell.Command</li>
+</ul>
+</p>
+
+<p id="doc"><b>Documentation</b><br>
+<ul>
+<li><a href=".">general</a></li>
+<li><a href="api">JavaDoc</a></li>
+</ul>
+</p>
+
+<p id="usage"><b>Usage</b><br>
+<ul>
+<li>See the command lines in the <a href="eventadmin.txt">script</a></li>
+</ul>
+</p>
+
+
+
+<p id="todolist"><b>TODOLIST</b><br>
+<ul>
+<li>tracer persistence option -p (save the topicPattern)</li>
+</ul>
+</p>
+
+
+</body>
+</html>

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/doc/readme.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/pom.xml
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/pom.xml?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/pom.xml (added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/pom.xml Tue Mar 13 16:46:28 2007
@@ -0,0 +1,86 @@
+<!--
+ 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.
+-->
+<project>
+	<properties>
+		<repositoryLocation>http://www-adele.imag.fr/users/Didier.Donsez/dev/felix/sandbox/</repositoryLocation>
+	</properties>  
+
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix</artifactId>
+    <version>0.9.0-incubator-SNAPSHOT</version>
+  </parent>
+
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>bundle</packaging>
+  <name>Apache Felix EventAdmin Command</name>
+  <artifactId>org.apache.felix.eventadmin.cmd</artifactId>
+  <description>provides a command to use and test the Event Admin service.</description>
+    
+  <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>
+
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.shell</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+
+
+  </dependencies>
+
+  <build>
+    <plugins>
+       <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Export-Service>org.apache.felix.shell.Command,org.osgi.service.eventadmin.EventHandler</Export-Service>
+            <Import-Service>org.osgi.service.eventadmin.EventAdmin</Import-Service>
+            <Private-Package>org.apache.felix.eventadmin.cmd.*,org.apache.felix.eventadmin.util.*</Private-Package>
+            <Import-Package>*</Import-Package>
+	    	<Bundle-Activator>${pom.artifactId}.EventAdminCommand</Bundle-Activator>
+            <Bundle-DocUrl>${repositoryLocation}${pom.artifactId}/readme.html</Bundle-DocUrl>
+            <Bundle-Url>${repositoryLocation}${pom.artifactId}/${pom.artifactId}-${pom.version}.jar</Bundle-Url>
+            <Bundle-Source>${repositoryLocation}${pom.artifactId}/${pom.artifactId}-${pom.version}-src.jar</Bundle-Source>
+            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+            <Bundle-Description>provides a command to use and test the Event Admin
service.</Bundle-Description>
+           </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  
+</project>

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventAdminCommand.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventAdminCommand.java?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventAdminCommand.java
(added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventAdminCommand.java
Tue Mar 13 16:46:28 2007
@@ -0,0 +1,234 @@
+/*
+ *   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.cmd;
+
+import java.io.PrintStream;
+import java.net.URLDecoder;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.event.EventConstants;
+import org.osgi.util.tracker.ServiceTracker;
+
+import org.apache.felix.shell.Command;
+
+/**
+ * This class creates a shell command
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class EventAdminCommand implements Command, BundleActivator {
+
+	private BundleContext bundleContext;
+
+	private ServiceTracker serviceTracker;
+	
+	private ServiceRegistration serviceRegistration;
+
+	private Map handlers = new HashMap();
+
+	public void start(BundleContext bundleContext) throws Exception {
+		this.bundleContext = bundleContext;
+		serviceTracker=new ServiceTracker(bundleContext,EventAdmin.class.getName(),null);
+		serviceTracker.open();
+		serviceRegistration = bundleContext.registerService(Command.class.getName(), this, null);
+		
+	}
+
+	public void stop(BundleContext context) throws Exception {
+		Iterator iterator = handlers.values().iterator();
+		while (iterator.hasNext()) {
+			EventSink eventSink = (EventSink) iterator.next();
+			try {
+				eventSink.stop(bundleContext);
+			} catch (Exception e) {
+				System.err.println(e.getMessage());
+			}
+		}
+
+		serviceRegistration.unregister();
+		serviceRegistration = null;
+
+		serviceTracker.close();
+	}
+
+	public String getName() {
+		return "eventadmin";
+	}
+
+	public String getUsage() {
+		return "eventadmin <subscribe|remove|list|send|post|help>";
+	}
+
+	public String getShortDescription() {
+		return "subscribes/send/post events";
+	}
+
+	private void printUsage(PrintStream out) {
+		out.println(getUsage());
+		return;
+	}
+
+	public void execute(String commandline, PrintStream out, PrintStream err) {
+		StringTokenizer st = new StringTokenizer(commandline, " ");
+
+		if (st.countTokens() == 1) {
+			printUsage(out);
+			return;
+		}
+
+		// Ignore the command name.
+		st.nextToken();
+
+		if (!st.hasMoreTokens()) {
+			printUsage(err);
+			return;
+		}
+
+		String subcommand = st.nextToken().trim();
+
+		if (subcommand.equals("subscribe")) {
+			int c=st.countTokens();
+			if(c<1 || c>3){
+				printUsage(err);
+				return;
+			}
+			String topicPattern = st.nextToken().trim();
+			String eventfilter=null;
+			if(st.hasMoreTokens())
+				eventfilter = st.nextToken().trim();
+			long delayInMilliSec=0;
+			if(st.hasMoreTokens()){
+				String delayInMilliSecStr = st.nextToken().trim();
+				try{
+					delayInMilliSec=Long.parseLong(delayInMilliSecStr);
+				} catch(NumberFormatException e){
+					err.println("Invalid number format for delay parameter");
+					return;
+				}
+			}
+			EventSink eventSink = (EventSink) handlers.get(topicPattern);
+			if (eventSink != null)
+				return;
+			if(delayInMilliSec==0){
+			eventSink = new EventSink("subscriber[" + topicPattern + "]",
+					new String[] { topicPattern },
+					eventfilter);
+			}else{
+			eventSink = new EventSinkWithDelay(
+						"subscriber[" + topicPattern + "]",
+						new String[] { topicPattern },
+						eventfilter,
+						delayInMilliSec);
+			}
+			try {
+				eventSink.start(bundleContext);
+			} catch (Exception e) {
+				err.println(e.getMessage());
+				return;
+			}
+			handlers.put(topicPattern, eventSink);
+		} else if(subcommand.equals("list")){
+			if(st.countTokens()!=0){
+				printUsage(err);
+				return;
+			}
+			Iterator iterator=handlers.keySet().iterator();
+			while(iterator.hasNext()){
+				out.print(iterator.next()+" ");
+			}
+			out.println();
+		} else if(subcommand.equals("remove")){
+			if(st.countTokens()!=1){
+				printUsage(err);
+				return;
+			}
+			String topicPattern = st.nextToken().trim();
+			EventSink eventSink = (EventSink) handlers.remove(topicPattern);
+			if(eventSink!=null)
+				try {
+					eventSink.stop(bundleContext);
+				} catch (Exception e) {
+					err.println(e.getMessage());
+				}
+		} else if(subcommand.equals("send") || subcommand.equals("post")){
+			if(st.countTokens()!=2){
+				printUsage(err);
+				return;
+			}
+			EventAdmin eventAdmin=(EventAdmin)serviceTracker.getService();
+			if(eventAdmin==null) {
+				err.println("No EventAdmin available for posting and sending");
+				return;
+			}
+
+			String topic = st.nextToken().trim();
+			String urlencodedpropertiesstr = st.nextToken().trim();
+			Event event=importFrom(topic,urlencodedpropertiesstr,null);
+			
+			if(subcommand.equals("send")){
+				eventAdmin.sendEvent(event);
+			} else {
+				eventAdmin.postEvent(event);				
+			}
+			
+		} else if(subcommand.equals("help")){
+			printUsage(out);
+		} else {
+			printUsage(err);			
+		}
+	}
+	
+	/**
+	 * Import a event from a string of the topic and a url-encoded string for the properties

+	 * <p>Remark: properties types are java.lang.String
+	 */
+	public static Event importFrom(
+			String topicstr,
+			String urlencodedpropertiesstr,
+			Properties additionalProperties) throws IllegalArgumentException {
+		
+		Dictionary properties=new Hashtable();
+		StringTokenizer st=new StringTokenizer(urlencodedpropertiesstr,"&");
+		while(st.hasMoreTokens()){
+			String s=st.nextToken();
+			int e=s.indexOf("=");
+			String key=URLDecoder.decode(s.substring(0,e));
+			if(!key.equals(EventConstants.EVENT_TOPIC))
+				properties.put(key,URLDecoder.decode(s.substring(e+1)));
+		}
+		if(additionalProperties!=null){
+			Iterator iterator=additionalProperties.keySet().iterator();
+			while(iterator.hasNext()){
+				String key=(String)iterator.next();
+				properties.put(key,additionalProperties.get(key));
+			}
+		}
+		return new Event(topicstr,properties);	
+	
+	}
+
+}

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventAdminCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSink.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSink.java?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSink.java
(added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSink.java
Tue Mar 13 16:46:28 2007
@@ -0,0 +1,72 @@
+/*
+ *   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.cmd;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+
+import org.apache.felix.eventadmin.util.EventUtil;
+
+/**
+ * This class provides a sample of a subscriber (ie Event Handler)
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class EventSink implements BundleActivator, EventHandler {
+	
+		private ServiceRegistration serviceRegistration;
+		private String[] topics=null;
+		protected String name=null;
+		private String eventFilter=null;
+
+		public EventSink(String name, String [] topics, String eventFilter){
+			this.name=name;
+			this.topics=topics;
+			this.eventFilter=eventFilter;
+		}
+
+		public EventSink(String name, String [] topics){
+			this.name=name;
+			this.topics=topics;
+		}
+
+		public EventSink(String name){
+			this.name=name;
+			this.topics=new  String [] {"*"};
+		}
+		
+		public void start(BundleContext context) throws Exception {
+			Dictionary d = new Hashtable();
+			d.put(EventConstants.EVENT_TOPIC, topics );
+			if(eventFilter!=null) d.put(EventConstants.EVENT_FILTER, eventFilter );
+			serviceRegistration=context.registerService( EventHandler.class.getName(),this, d );
+		}
+		public void stop( BundleContext context) throws Exception {
+			serviceRegistration.unregister();
+			serviceRegistration=null;
+		}
+		public void handleEvent(Event event ) {
+			// do nothing
+			System.out.print(name+" handles : "+ EventUtil.format(event)+'\n');
+		}
+}

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSink.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSinkWithDelay.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSinkWithDelay.java?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSinkWithDelay.java
(added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSinkWithDelay.java
Tue Mar 13 16:46:28 2007
@@ -0,0 +1,45 @@
+/*
+ *   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.cmd;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+
+/**
+ * This class provides a sample of a subscriber (ie Event Handler)
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class EventSinkWithDelay extends EventSink implements BundleActivator, EventHandler
{
+	
+		private long delayInMilliSeconds=0;
+	
+		public EventSinkWithDelay(String name, String [] topics, String eventFilter, long delayInMilliSeconds){
+			super(name,topics,eventFilter);
+			this.delayInMilliSeconds=delayInMilliSeconds;
+		}
+		
+		public void handleEvent(Event event ) {
+			System.out.println(name + " waits " + delayInMilliSeconds + " ms before handling "+event);
+				try {
+					Thread.sleep(delayInMilliSeconds);
+				} catch (InterruptedException e) {
+					// do nothing
+				}
+			super.handleEvent(event);
+		}
+}

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/cmd/EventSinkWithDelay.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventPropertiesWrapper.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventPropertiesWrapper.java?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventPropertiesWrapper.java
(added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventPropertiesWrapper.java
Tue Mar 13 16:46:28 2007
@@ -0,0 +1,92 @@
+/*
+ *   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.util;
+
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import org.osgi.service.event.Event;
+
+/**
+ * This class provides a dictionary wrapper for on the event properties
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+
+public class EventPropertiesWrapper extends Dictionary {
+	
+	private Event event;
+
+	public EventPropertiesWrapper(Event event){
+		this.event=event;
+	}
+
+	public int size() {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	public boolean isEmpty() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	public Enumeration keys() {
+		return new StringArrayEnumeration(event.getPropertyNames());
+	}
+
+	public Enumeration elements() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public Object get(Object key) {
+		return event.getProperty((String)key);
+	}
+
+	public Object put(Object arg0, Object arg1) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public Object remove(Object key) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	
+
+}
+
+class StringArrayEnumeration implements Enumeration {
+
+	private int last=0;
+	private String[] strings;
+	
+	public StringArrayEnumeration(String[] strings){
+		this.strings=strings;
+	}
+	
+	public boolean hasMoreElements() {
+		return last<strings.length;
+	}
+
+	public Object nextElement() {
+		if(last==strings.length) return null;
+		else return strings[last++];
+	}
+	
+}
+

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventPropertiesWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventUtil.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventUtil.java?view=auto&rev=517940
==============================================================================
--- incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventUtil.java
(added)
+++ incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventUtil.java
Tue Mar 13 16:46:28 2007
@@ -0,0 +1,108 @@
+/*
+ *   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.util;
+
+/**
+ * This utilities class provides methods to compare topics and topic patterns 
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class EventUtil {
+	/**
+	 * matches a topic with a pattern 
+	 * @param topic the topic
+	 * @param topicPattern the pattern
+	 * @return true if the topic matches the pattern
+	 */
+	public static boolean matches(String topic, String topicPattern) {
+		if (topicPattern.equals("*"))
+			return true;
+		int star;
+		if ((star = topicPattern.indexOf("*")) > 0) {
+			return topic.startsWith(topicPattern.substring(0, star - 1));
+		} else {
+			return topic.equals(topicPattern);
+		}
+	}
+
+	/**
+	 * matches a topic with one of the patterns 
+	 * @param topic the topic
+	 * @param topicPatterns the array of patterns
+	 * @return true if the topic matches the pattern
+	 */
+	public static boolean matches(String topic, String[] topicPatterns) {
+		int n = topicPatterns.length;
+
+		for (int i = 0; i < n; i++)
+			if (matches(topic, topicPatterns[i]))
+				return true;
+		return false;
+	}
+
+	
+	/**
+	 * check if a topic implies another one
+	 * <p>"x/y/*" -> "x/y/z" is true "*" -> "x/y" is true "x/y" -> "x/y/z" is
false
+	 * 
+	 * @param topic
+	 * @param topic2
+	 * @return
+	 */
+	public static boolean implies(String topic, String topic2) {
+		if (topic2.equals("*"))
+			return true;
+		int starTopic = topic.indexOf("*");
+		if (starTopic == 0)
+			return true;
+		int starTopic2 = topic2.indexOf("*");
+		if ((starTopic) > 0) {
+			if (starTopic2 == -1) {
+				// "x/y/*" -> "x/y/z" is true
+				return topic2.startsWith(topic.substring(0, starTopic - 1));
+			} else {
+				// "x/y/*" -> "x/y/z/*" is true
+				return topic2.substring(0, starTopic2 - 1).startsWith(
+						topic.substring(0, starTopic - 1));
+			}
+		} else {
+			return topic2.equals(topic);
+		}
+	}
+
+	/**
+	 * check if topic is valid 
+	 * <p>topic-scope ::= ’*’ | ( topic [ ’/*’ ] )
+	 * <p>see 114.4 Event Handler
+	 */ 
+	public static boolean isValidTopic(String topic){
+		return (topic!=null && (topic.equals("*") || topic.startsWith("/*")));
+	}
+	
+	public static String format(org.osgi.service.event.Event event){
+		StringBuffer sb=new StringBuffer();
+		sb.append("[topic=").append(event.getTopic());
+		sb.append(",properties=[");
+		String[] names=event.getPropertyNames();
+		for(int n=0;n<names.length;n++){
+			if(n!=0) sb.append(',');
+			sb.append(names[n]).append('=').append(event.getProperty(names[n]));
+		}
+		sb.append("]]");
+		return sb.toString();
+		
+	}
+}

Propchange: incubator/felix/sandbox/donsez/eventadmin.cmd/src/main/java/org/apache/felix/eventadmin/util/EventUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message