Author: reto
Date: Sat Mar 12 12:16:36 2011
New Revision: 1080927

URL: http://svn.apache.org/viewvc?rev=1080927&view=rev
Log:
CLEREZZA-459: added doWith method for executing an action as soon as a service 
is available

Modified:
    incubator/clerezza/trunk/parent/osgi.services/   (props changed)
    
incubator/clerezza/trunk/parent/osgi.services/src/main/scala/org/apache/clerezza/osgi/services/ServicesDsl.scala

Propchange: incubator/clerezza/trunk/parent/osgi.services/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sat Mar 12 12:16:36 2011
@@ -1,5 +1,8 @@
-target
 .classpath
-.project
 .externalToolBuilders
+.project
+target
+shell.iws
+shell.ipr
+shell.iml
 .settings

Modified: 
incubator/clerezza/trunk/parent/osgi.services/src/main/scala/org/apache/clerezza/osgi/services/ServicesDsl.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/osgi.services/src/main/scala/org/apache/clerezza/osgi/services/ServicesDsl.scala?rev=1080927&r1=1080926&r2=1080927&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/osgi.services/src/main/scala/org/apache/clerezza/osgi/services/ServicesDsl.scala
 (original)
+++ 
incubator/clerezza/trunk/parent/osgi.services/src/main/scala/org/apache/clerezza/osgi/services/ServicesDsl.scala
 Sat Mar 12 12:16:36 2011
@@ -18,7 +18,7 @@
  */
 package org.apache.clerezza.osgi.services
 
-import org.osgi.framework.BundleContext
+import org.osgi.framework.{BundleContext, Constants, ServiceEvent, 
ServiceListener}
 import scala.collection.JavaConversions._
 
 class ServicesDsl(bundleContext: BundleContext) {
@@ -36,4 +36,29 @@ class ServicesDsl(bundleContext: BundleC
                        
bundleContext.getService(serviceReference).asInstanceOf[T]
                } else null.asInstanceOf[T]
        }
+
+       /**
+        * executes action as soon as a service exposing T is available, if such
+        * a service is already available the action is executed immedtely and 
the
+        * method blocks until the action finished executing, otherwise the 
method
+        * returns and action will be executed when a respective becomes 
available.
+        */
+       def doWith[T](action: T => Unit)(implicit m: Manifest[T]) {
+               val clazz = m.erasure.asInstanceOf[Class[T]]
+               val service = getService(clazz)
+               if (service != null) {
+                       action(service)
+               } else {
+                       lazy val serviceListener: ServiceListener = new 
ServiceListener {
+                                       def serviceChanged(e: ServiceEvent) = {
+                                               if (e.getType == 
ServiceEvent.REGISTERED) {
+                                                       
bundleContext.removeServiceListener(serviceListener)
+                                                       
action(bundleContext.getService(e.getServiceReference).asInstanceOf[T])
+                                               }
+                                       }
+                               }
+                       bundleContext.addServiceListener(serviceListener,
+                                                                               
         "("+Constants.OBJECTCLASS+"="+clazz.getName+")")
+               }
+       }
 }


Reply via email to