Author: thobbs
Date: Fri Feb  3 14:39:48 2012
New Revision: 1240193

URL: http://svn.apache.org/viewvc?rev=1240193&view=rev
Log:
Added scala helpers.  Started tidying up the easy config stuff, not even nearly 
finished though.

Added:
    river/jtsk/skunk/easystart/scala-src/
    river/jtsk/skunk/easystart/scala-src/org/
    river/jtsk/skunk/easystart/scala-src/org/apache/
    river/jtsk/skunk/easystart/scala-src/org/apache/river/
    river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/
    river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/Djinn.scala
    
river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/RemoteExecution.scala
Modified:
    
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/CheckServices.java
    
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/LookupServiceConfiguration.java
    
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/MahaloServiceConfiguration.java

Added: river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/Djinn.scala
URL: 
http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/Djinn.scala?rev=1240193&view=auto
==============================================================================
--- river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/Djinn.scala 
(added)
+++ river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/Djinn.scala Fri 
Feb  3 14:39:48 2012
@@ -0,0 +1,200 @@
+package org.apache.river.scala
+import net.jini.lookup.ServiceDiscoveryManager
+import java.util.ArrayList
+import java.util.Iterator
+import net.jini.core.lookup.ServiceItem
+import scala.collection.immutable.LinearSeq
+import scala.collection.generic.GenericTraversableTemplate
+import scala.collection.TraversableLike
+import java.rmi.RMISecurityManager
+import scala.collection.mutable.MutableList
+import scala.collection.immutable.List$
+import com.sun.jini.lookup.entry.LookupAttributes
+import net.jini.lookup.entry.Name
+import net.jini.core.entry.Entry
+import java.util.Arrays
+import org.apache.river.extra.easystart.discovery.MulticastDiscovery
+import org.apache.river.extra.easystart.discovery.UnicastDiscovery
+import net.jini.core.lookup.ServiceTemplate
+import net.jini.space.JavaSpace
+import org.apache.river.extra.easystart.discovery.EasyDiscovery
+import net.jini.core.discovery.LookupLocator
+import scala.collection.mutable.ListBuffer
+import net.jini.core.lookup.ServiceID
+import scala.collection.JavaConverters._
+import net.jini.lookup.ServiceDiscoveryEvent
+import net.jini.lookup.ServiceDiscoveryListener
+import scala.actors.Actor
+
+class Djinn(disco: EasyDiscovery) {
+  
+    val doNothing = {e: ServiceDiscoveryEvent => ()}
+  
+       case object Added
+       var serviceAddedFunc = doNothing
+       
+       case object Removed
+       var serviceRemovedFunc = doNothing
+       
+       case object Changed
+       var serviceChangedFunc = doNothing
+       
+       class ServiceEventHandler extends Actor {
+               def act() = {
+                       loop {
+                               react {
+                               case (Added, event :ServiceDiscoveryEvent) => 
serviceAddedFunc(event)
+                               case (Removed, event :ServiceDiscoveryEvent)  
=> serviceRemovedFunc(event)
+                               case (Changed, event :ServiceDiscoveryEvent)  
=> serviceChangedFunc(event)
+                               case _ => doNothing
+                               }
+                       }
+               }
+       }
+  
+  val ml = disco
+  val eventHandler = new ServiceEventHandler()
+  eventHandler.start()
+  
+  ml.addServiceDiscoveryListener(new ServiceDiscoveryListener() {
+         def serviceRemoved(event: ServiceDiscoveryEvent) = {
+           eventHandler ! Removed
+         }
+         def serviceChanged(event: ServiceDiscoveryEvent) = {
+                 eventHandler ! Changed
+         }
+         def serviceAdded(event: ServiceDiscoveryEvent) = {
+                 eventHandler ! (Added, event)
+         }
+  })
+  
+
+  def foreach[A](f: net.jini.core.lookup.ServiceItem => A) = {
+    try {
+      val itr = ml.iterator()
+      while (itr.hasNext()) {
+        val n = itr.next();
+        f(itr.next())
+      }
+    } catch {
+      case e:Throwable => println("Warning! Failed because: "+e.getMessage())
+    }
+  }
+  
+  def map[A] (f: net.jini.core.lookup.ServiceItem => A): 
RemoteExecution[List[A]] = {
+    try {
+           val list = ListBuffer[A]()
+           
+           val itr = ml.iterator()
+           while(itr.hasNext()) {
+             list += (f (itr.next()))
+           }
+           
+        Result(list.toList)
+    } catch {
+      case e:Throwable => Problem(e)
+    }
+  }
+  
+  def filter(template: net.jini.core.lookup.ServiceTemplate): 
RemoteExecution[List[net.jini.core.lookup.ServiceItem]] = {
+    try {
+      val list = ml.lookupAll(template)
+      Result(list.asScala.toList)
+    } catch {
+      case e:Throwable => Problem(e)
+    }
+  }
+  
+  def handleNewService(f: ServiceDiscoveryEvent => Unit) = {
+    serviceAddedFunc = f
+  }
+    
+  def handleRemovedService(f: ServiceDiscoveryEvent => Unit) = {
+    serviceAddedFunc = f
+  }
+  
+  def handleChangedService(f: ServiceDiscoveryEvent => Unit) = {
+    serviceAddedFunc = f
+  }
+  
+  def terminate = ml.terminate()
+  
+}
+
+
+object Djinn {
+  
+  def findName (attribs: Array[Entry]) : String = {
+    def asName (es: Array[Entry]) = es match {
+      case Array() => "No Name"
+      case _ if es.head.isInstanceOf[Name] => es.head.asInstanceOf[Name].name
+    }
+    
+    asName (attribs filter { e: Entry => e.isInstanceOf[Name] })
+    
+  }
+  
+  def main(args: Array[String]) = {
+    println("Starting djinn")
+    
+    val locators = Array[LookupLocator] (new LookupLocator("jini://localhost"))
+    val djinn = new Djinn(new UnicastDiscovery (locators))
+//    val djinn = new Djinn(new MulticastDiscovery)
+    
+    djinn.handleNewService({event: ServiceDiscoveryEvent => println("Found 
one!")})
+    djinn.handleRemovedService({event: ServiceDiscoveryEvent => println("Lost 
one!")})
+    djinn.handleChangedService({event: ServiceDiscoveryEvent => 
println("Service changed!")})
+    
+    val printer = (si:ServiceItem) => println(si.serviceID)
+    val namer = (si:ServiceItem) => println(findName(si.attributeSets))
+    
+//    djinn map namer
+    
+    val spaces = new ServiceTemplate(null, 
+//                                                              
Array[java.lang.Class[_]](classOf[JavaSpace]), 
+                                                                
Array[java.lang.Class[_]](), 
+                                                                null)
+    
+    djinn foreach namer
+    
+//    println(djinn filter spaces)
+    
+//    val allspaces = djinn filter spaces
+//    println(allspaces.flatten)
+    
+//    val printer2 = {(si: ServiceItem) =>
+//      si.serviceID.toString()
+//    }
+//    
+//    val printId = { si: ServiceItem => si.serviceID }
+//    val printIds = { sis: List[ServiceItem] => Some(sis map printId) }
+//    
+//    println(djinn filter spaces flatMap printIds)
+//    djinn filter spaces flatMap printIds
+    
+//     djinn filter spaces map println
+//    val mapped = djinn map {si:ServiceItem => si.serviceID}
+//    
+//    println(mapped.flatten)
+//    
+//    println (
+//      mapped.flatMap({sis:List[ServiceID] => 
+//     if(sis.length > 0) {
+//       Some(sis)
+//     } else {
+//       None
+//     }
+//      })
+//    )
+//    djinn map printer
+    
+    Thread.sleep(5000);
+    
+    djinn terminate
+    
+    println("Exiting");
+    
+    System.exit(0)
+  }
+  
+}
\ No newline at end of file

Added: 
river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/RemoteExecution.scala
URL: 
http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/RemoteExecution.scala?rev=1240193&view=auto
==============================================================================
--- 
river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/RemoteExecution.scala
 (added)
+++ 
river/jtsk/skunk/easystart/scala-src/org/apache/river/scala/RemoteExecution.scala
 Fri Feb  3 14:39:48 2012
@@ -0,0 +1,39 @@
+package org.apache.river.scala
+
+sealed abstract class RemoteExecution[+A] {
+
+  self =>
+
+  def isException: Boolean
+  def get: A
+  def description: Throwable
+  
+  def flatMap[B](f: A => Option[B]): Option[B] =
+    if(isException) {
+      println("Warning! Problem detected: "+description.getMessage())
+         None
+    } else {
+      f(this.get) 
+    }
+    
+//  def flatten : A = 
+//    if(isException) {
+//      println("Warning! Problem detected: "+description.getMessage())
+//      None
+//    } else {
+//      this.get
+//    }
+  
+}
+
+final case class Result[+A](x: A) extends RemoteExecution[A] {
+  def isException = false;
+  def get = x
+  def description = throw new NoSuchElementException("Result.description")
+}
+
+final case class Problem(ex: Throwable) extends RemoteExecution[Nothing] {
+  def isException = true;
+  def get = throw new NoSuchElementException("Problem.get");
+  def description = ex
+}
\ No newline at end of file

Modified: 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/CheckServices.java
URL: 
http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/CheckServices.java?rev=1240193&r1=1240192&r2=1240193&view=diff
==============================================================================
--- 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/CheckServices.java
 (original)
+++ 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/CheckServices.java
 Fri Feb  3 14:39:48 2012
@@ -49,6 +49,8 @@ public class CheckServices {
                checkLookupServices();
                checkSpaces();
                checkTransactionManagers();
+               
+               checkAll();
        }
 
        private static void checkHttpServer(int port) throws IOException {
@@ -113,10 +115,10 @@ public class CheckServices {
                ServiceRegistrar sr = ll.getRegistrar();
                
                if(null != sr) {
-
+                       
                        ServiceTemplate tmpl = new ServiceTemplate(null, new 
Class[] { TransactionManager.class }, null);
                        ServiceMatches sms =sr.lookup(tmpl, Integer.MAX_VALUE);
-
+                       
                        if(0 == sms.items.length) {
                                log.severe("No Transaction Managers found");
                        } else {
@@ -130,5 +132,28 @@ public class CheckServices {
                        log.warning("Lookup service could not be found");
                }
        }
+       
+       private static void checkAll() throws IOException, 
ClassNotFoundException {
+               LookupLocator ll = new LookupLocator("jini://localhost");
+               ServiceRegistrar sr = ll.getRegistrar();
+               
+               if(null != sr) {
+
+                       ServiceTemplate tmpl = new ServiceTemplate(null, null, 
null);
+                       ServiceMatches sms =sr.lookup(tmpl, Integer.MAX_VALUE);
+
+                       if(0 == sms.items.length) {
+                               log.severe("No services found");
+                       } else {
+                               log.info("Found "+sms.items.length+" services");
+                       }
+                       
+                       for(ServiceItem sm : sms.items) {
+                               log.info("Service found: 
"+Arrays.toString(sm.attributeSets));
+                       }
+               } else {
+                       log.warning("Lookup service could not be found");
+               }
+       }
 
 }
\ No newline at end of file

Modified: 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/LookupServiceConfiguration.java
URL: 
http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/LookupServiceConfiguration.java?rev=1240193&r1=1240192&r2=1240193&view=diff
==============================================================================
--- 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/LookupServiceConfiguration.java
 (original)
+++ 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/LookupServiceConfiguration.java
 Fri Feb  3 14:39:48 2012
@@ -81,7 +81,7 @@ public class LookupServiceConfiguration 
                                                " ",
                                            "http://";, 
ConfigUtil.getHostName(), ":"+port+"/jsk-dl.jar" } );
                
-               final String policy = 
"/home/tom/projects/river/easystart/src-extra/reggie.policy";
+               final String policy = 
"/home/tom/projects/river/easystart/src-extra/policy.all";
                final String classpath = 
"/home/tom/projects/river/easystart/lib/reggie.jar";
                final String config = 
"/home/tom/projects/river/easystart/src-extra/empty.config";
 //             final String config = 
"/home/tom/projects/river/easystart/src-extra/jrmp-reggie.config";

Modified: 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/MahaloServiceConfiguration.java
URL: 
http://svn.apache.org/viewvc/river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/MahaloServiceConfiguration.java?rev=1240193&r1=1240192&r2=1240193&view=diff
==============================================================================
--- 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/MahaloServiceConfiguration.java
 (original)
+++ 
river/jtsk/skunk/easystart/src-extra/org/apache/river/extra/easystart/MahaloServiceConfiguration.java
 Fri Feb  3 14:39:48 2012
@@ -73,7 +73,7 @@ public class MahaloServiceConfiguration 
 
        
        private ServiceDescriptor[] serviceDescriptorArray() {
-               final String port = "8081";
+               final String port = "8080";
                try {
                        String codebase = ConfigUtil.concat(
                                    new Object[] { 
@@ -81,7 +81,7 @@ public class MahaloServiceConfiguration 
                                                " ",
                                            "http://";, 
ConfigUtil.getHostName(), ":"+port+"/jsk-dl.jar" } );
                
-               final String policy = 
"/home/tom/projects/river/easystart/src-extra/mahalo.policy";
+               final String policy = 
"/home/tom/projects/river/easystart/src-extra/policy.all";
                final String classpath = 
"/home/tom/projects/river/easystart/lib/mahalo.jar";
                final String config = 
"/home/tom/projects/river/easystart/src-extra/empty.config";
 //             final String config = 
"/home/tom/projects/river/easystart/src-extra/jrmp-outrigger-group.config";


Reply via email to