[jboss-osgi-commits] JBoss-OSGI SVN: r97544 - in projects/jboss-osgi/trunk: testsuite/example/src/test/java/org/jboss/test/osgi/example/event and 1 other directory.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Tue Dec 8 09:28:22 EST 2009


Author: thomas.diesler at jboss.com
Date: 2009-12-08 09:28:22 -0500 (Tue, 08 Dec 2009)
New Revision: 97544

Modified:
   projects/jboss-osgi/trunk/distribution/docbook/en/modules/ch080-provided-examples.xml
   projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/event/EventAdminTestCase.java
Log:
Document EventAdmin example

Modified: projects/jboss-osgi/trunk/distribution/docbook/en/modules/ch080-provided-examples.xml
===================================================================
--- projects/jboss-osgi/trunk/distribution/docbook/en/modules/ch080-provided-examples.xml	2009-12-08 14:23:37 UTC (rev 97543)
+++ projects/jboss-osgi/trunk/distribution/docbook/en/modules/ch080-provided-examples.xml	2009-12-08 14:28:22 UTC (rev 97544)
@@ -26,7 +26,7 @@
     Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 14.417 sec
     ...
     
-    Tests run: 24, Failures: 0, Errors: 0, Skipped: 0
+    Tests run: 25, Failures: 0, Errors: 0, Skipped: 0
     
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESSFUL
@@ -71,9 +71,33 @@
     </itemizedlist>
   </sect1>
   
-  <sect1 xml:id="SecSimpleExample">  
-    <title>Simple Example</title>
-    <para>The simple example is covered in: <link linkend="SecWritingTests">Writing Test Cases</link></para>
+  <sect1 xml:id="SecEventAdminExample">  
+    <title>Event Admin Example</title>
+    
+    <para>The <emphasis role="bold">example-event.jar</emphasis> bundle uses the 
+    <ulink url="http://www.osgi.org/javadoc/r4v42/org/osgi/service/event/EventAdmin.html">EventAdmin</ulink> service
+    to send/receive events.</para>
+    
+    <programlisting role="JAVA">
+     public void testEventHandler() throws Exception
+     {
+        TestEventHandler eventHandler = new TestEventHandler();
+        
+        // Register the EventHandler
+        Dictionary param = new Hashtable();
+        param.put(EventConstants.EVENT_TOPIC, new String[] { TOPIC });
+        context.registerService(EventHandler.class.getName(), eventHandler, param);
+  
+        // Send event through the the EventAdmin
+        ServiceReference sref = context.getServiceReference(EventAdmin.class.getName());
+        EventAdmin eventAdmin = (EventAdmin)context.getService(sref);
+        eventAdmin.sendEvent(new Event(TOPIC, null));
+        
+        // Verify received event
+        assertEquals("Event received", 1, eventHandler.received.size());
+        assertEquals(TOPIC, eventHandler.received.get(0).getTopic());
+     }
+    </programlisting>
   </sect1>
   
   <sect1 xml:id="SecBlueprintContainerExample">  
@@ -354,6 +378,107 @@
     
   </sect1>
   
+  <sect1 xml:id="SecInterceptorExample">  
+    <title>Lifecycle Interceptor</title>
+
+    <para>The interceptor example deployes a bundle that contains some metadata and an interceptor bundle that processes 
+    the metadata and registeres an http endpoint from it. The idea is that the bundle does not process its own metadata. 
+    Instead this work is delegated to some specialized metadata processor (i.e. the interceptor).</para>
+    
+    <para>Each interceptor is itself registered as a service. This is the well known <ulink url="www.osgi.org/wiki/uploads/Links/whiteboard.pdf">
+    Whiteboard Pattern</ulink>.</para>
+    
+    <programlisting role="JAVA">
+    public class InterceptorActivator implements BundleActivator
+    {
+       public void start(BundleContext context)
+       {
+          LifecycleInterceptor publisher = new PublisherInterceptor();
+          LifecycleInterceptor parser = new ParserInterceptor();
+          
+          // Add the interceptors, the order of which is handles by the service
+          context.registerService(LifecycleInterceptor.class.getName(), publisher, null);
+          context.registerService(LifecycleInterceptor.class.getName(), parser, null);
+       }
+    }
+    </programlisting>
+    
+    <programlisting role="JAVA">
+    public class ParserInterceptor extends AbstractLifecycleInterceptor
+    {
+       ParserInterceptor()
+       {
+          // Add the provided output
+          addOutput(HttpMetadata.class);
+       }
+    
+       public void invoke(int state, InvocationContext context)
+       {
+          // Do nothing if the metadata is already available  
+          HttpMetadata metadata = context.getAttachment(HttpMetadata.class);
+          if (metadata != null)
+             return;
+    
+          // Parse and create metadta on STARTING
+          if (state == Bundle.STARTING)
+          {
+              VirtualFile root = context.getRoot();
+              VirtualFile propsFile = root.getChild("/http-metadata.properties");
+              if (propsFile != null)
+              {
+                 log.info("Create and attach HttpMetadata");
+                 metadata = createHttpMetadata(propsFile);
+                 context.addAttachment(HttpMetadata.class, metadata);
+              }
+          }
+       }
+       ...
+    }
+    </programlisting>
+    
+    <programlisting role="JAVA">
+    public class PublisherInterceptor extends AbstractLifecycleInterceptor
+    {
+       PublisherInterceptor()
+       {
+          // Add the required input
+          addInput(HttpMetadata.class);
+       }
+    
+       public void invoke(int state, InvocationContext context)
+       {
+          // HttpMetadata is guaratied to be available because we registered
+          // this type as required input
+          HttpMetadata metadata = context.getAttachment(HttpMetadata.class);
+    
+          // Register HttpMetadata on STARTING 
+          if (state == Bundle.STARTING)
+          {
+             String servletName = metadata.getServletName();
+    
+             // Load the endpoint servlet from the bundle
+             Bundle bundle = context.getBundle();
+             Class servletClass = bundle.loadClass(servletName);
+             HttpServlet servlet = (HttpServlet)servletClass.newInstance();
+
+             // Register the servlet with the HttpService
+             HttpService httpService = getHttpService(context, true);
+             httpService.registerServlet("/servlet", servlet, null, null);
+          }
+    
+          // Unregister the endpoint on STOPPING 
+          else if (state == Bundle.STOPPING)
+          {
+             log.info("Unpublish HttpMetadata: " + metadata);
+             HttpService httpService = getHttpService(context, false);
+             if (httpService != null)
+                httpService.unregister("/servlet");
+          }
+       }
+    }
+    </programlisting>
+  </sect1>
+  
   <sect1 xml:id="SecMicrocontainerServiceExample">  
     <title>Microcontainer Service</title>
     
@@ -492,105 +617,4 @@
     </programlisting>
   </sect1>
   
-  <sect1 xml:id="SecInterceptorExample">  
-    <title>Lifecycle Interceptor</title>
-
-    <para>The interceptor example deployes a bundle that contains some metadata and an interceptor bundle that processes 
-    the metadata and registeres an http endpoint from it. The idea is that the bundle does not process its own metadata. 
-    Instead this work is delegated to some specialized metadata processor (i.e. the interceptor).</para>
-    
-    <para>Each interceptor is itself registered as a service. This is the well known <ulink url="www.osgi.org/wiki/uploads/Links/whiteboard.pdf">
-    Whiteboard Pattern</ulink>.</para>
-    
-    <programlisting role="JAVA">
-    public class InterceptorActivator implements BundleActivator
-    {
-       public void start(BundleContext context)
-       {
-          LifecycleInterceptor publisher = new PublisherInterceptor();
-          LifecycleInterceptor parser = new ParserInterceptor();
-          
-          // Add the interceptors, the order of which is handles by the service
-          context.registerService(LifecycleInterceptor.class.getName(), publisher, null);
-          context.registerService(LifecycleInterceptor.class.getName(), parser, null);
-       }
-    }
-    </programlisting>
-    
-    <programlisting role="JAVA">
-    public class ParserInterceptor extends AbstractLifecycleInterceptor
-    {
-       ParserInterceptor()
-       {
-          // Add the provided output
-          addOutput(HttpMetadata.class);
-       }
-    
-       public void invoke(int state, InvocationContext context)
-       {
-          // Do nothing if the metadata is already available  
-          HttpMetadata metadata = context.getAttachment(HttpMetadata.class);
-          if (metadata != null)
-             return;
-    
-          // Parse and create metadta on STARTING
-          if (state == Bundle.STARTING)
-          {
-              VirtualFile root = context.getRoot();
-              VirtualFile propsFile = root.getChild("/http-metadata.properties");
-              if (propsFile != null)
-              {
-                 log.info("Create and attach HttpMetadata");
-                 metadata = createHttpMetadata(propsFile);
-                 context.addAttachment(HttpMetadata.class, metadata);
-              }
-          }
-       }
-       ...
-    }
-    </programlisting>
-    
-    <programlisting role="JAVA">
-    public class PublisherInterceptor extends AbstractLifecycleInterceptor
-    {
-       PublisherInterceptor()
-       {
-          // Add the required input
-          addInput(HttpMetadata.class);
-       }
-    
-       public void invoke(int state, InvocationContext context)
-       {
-          // HttpMetadata is guaratied to be available because we registered
-          // this type as required input
-          HttpMetadata metadata = context.getAttachment(HttpMetadata.class);
-    
-          // Register HttpMetadata on STARTING 
-          if (state == Bundle.STARTING)
-          {
-             String servletName = metadata.getServletName();
-    
-             // Load the endpoint servlet from the bundle
-             Bundle bundle = context.getBundle();
-             Class servletClass = bundle.loadClass(servletName);
-             HttpServlet servlet = (HttpServlet)servletClass.newInstance();
-
-             // Register the servlet with the HttpService
-             HttpService httpService = getHttpService(context, true);
-             httpService.registerServlet("/servlet", servlet, null, null);
-          }
-    
-          // Unregister the endpoint on STOPPING 
-          else if (state == Bundle.STOPPING)
-          {
-             log.info("Unpublish HttpMetadata: " + metadata);
-             HttpService httpService = getHttpService(context, false);
-             if (httpService != null)
-                httpService.unregister("/servlet");
-          }
-       }
-    }
-    </programlisting>
-  </sect1>
-  
 </chapter>

Modified: projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/event/EventAdminTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/event/EventAdminTestCase.java	2009-12-08 14:23:37 UTC (rev 97543)
+++ projects/jboss-osgi/trunk/testsuite/example/src/test/java/org/jboss/test/osgi/example/event/EventAdminTestCase.java	2009-12-08 14:28:22 UTC (rev 97544)
@@ -23,7 +23,7 @@
 
 //$Id$
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assume.assumeNotNull;
 
 import java.util.ArrayList;
@@ -94,16 +94,19 @@
 
       assumeNotNull(context);
 
+      TestEventHandler eventHandler = new TestEventHandler();
+      
       // Register the EventHandler
       Dictionary param = new Hashtable();
       param.put(EventConstants.EVENT_TOPIC, new String[] { TOPIC });
-      TestEventHandler eventHandler = new TestEventHandler();
       context.registerService(EventHandler.class.getName(), eventHandler, param);
 
+      // Send event through the the EventAdmin
       ServiceReference sref = context.getServiceReference(EventAdmin.class.getName());
       EventAdmin eventAdmin = (EventAdmin)context.getService(sref);
       eventAdmin.sendEvent(new Event(TOPIC, null));
       
+      // Verify received event
       assertEquals("Event received", 1, eventHandler.received.size());
       assertEquals(TOPIC, eventHandler.received.get(0).getTopic());
    }



More information about the jboss-osgi-commits mailing list