Author: donsez
Date: Wed Oct 24 09:43:47 2007
New Revision: 587926

URL: http://svn.apache.org/viewvc?rev=587926&view=rev
Log:
add a service org.osgi.service.event.EventHandler to send/post events to the 
bunny 

Modified:
    felix/sandbox/donsez/bunny/pom.xml
    
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceActivator.java
    
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
    
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
    felix/sandbox/donsez/bunny/src/site/script.txt

Modified: felix/sandbox/donsez/bunny/pom.xml
URL: 
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/pom.xml?rev=587926&r1=587925&r2=587926&view=diff
==============================================================================
--- felix/sandbox/donsez/bunny/pom.xml (original)
+++ felix/sandbox/donsez/bunny/pom.xml Wed Oct 24 09:43:47 2007
@@ -65,7 +65,7 @@
                <!-- docs in 
http://cwiki.apache.org/FELIX/bundle-plugin-for-maven-bnd.html and 
http://cwiki.apache.org/FELIX/osgi-plugin-for-maven-2.html -->
                
             
<Private-Package>${pom.artifactId}.impl,${pom.artifactId}.activator,org.apache.felix.sandbox.rest</Private-Package>
-            
<Import-Package>org.osgi.framework,org.osgi.service.cm,org.apache.felix.shell,javax.xml.parsers,
 org.w3c.dom, org.xml.sax, javax.xml.transform, javax.xml.transform.dom, 
javax.xml.transform.stream</Import-Package>
+            
<Import-Package>org.osgi.framework,org.osgi.service.cm,org.osgi.service.event,org.apache.felix.shell,javax.xml.parsers,
 org.w3c.dom, org.xml.sax, javax.xml.transform, javax.xml.transform.dom, 
javax.xml.transform.stream</Import-Package>
             <Export-Package>${pom.artifactId}.model</Export-Package>
 
             
<Bundle-Activator>${pom.artifactId}.activator.Activator</Bundle-Activator>

Modified: 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceActivator.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceActivator.java?rev=587926&r1=587925&r2=587926&view=diff
==============================================================================
--- 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceActivator.java
 (original)
+++ 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceActivator.java
 Wed Oct 24 09:43:47 2007
@@ -27,41 +27,122 @@
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedService;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
 
 
 /**
  * this class provides an activator to instanciate a bunny service
+ * and registers services (ManagedService, EventHandler)
  * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a>
  */
-public class BunnyServiceActivator extends BunnyServiceModelImpl implements 
ManagedService {
+public class BunnyServiceActivator extends BunnyServiceModelImpl implements 
ManagedService, EventHandler {
 
        private ServiceRegistration serviceRegistration;
        
+       private String servicePid=null;
+       private String[] eventTopics=null;
+       private String[] servicesClasses=new String[]{
+                       BunnyServiceModel.class.getName(),
+                       ManagedService.class.getName(),
+                       EventHandler.class.getName()
+               };
+
+       private Dictionary getRegistrationProperties() {
+               Dictionary registrationProperties=new Hashtable();
+               registrationProperties.put(Constants.OBJECTCLASS, 
servicesClasses);
+               registrationProperties.put(Constants.SERVICE_PID, servicePid);
+               registrationProperties.put(EventConstants.EVENT_TOPIC, 
eventTopics);
+               return registrationProperties;
+       }
+       
        public void start(BundleContext bundleContext) throws Exception {
+
+               servicePid=bundleContext.getBundle().getSymbolicName();
+               eventTopics=new String[]{
+                               
bundleContext.getBundle().getSymbolicName().replace('.', '/')/* +"/*" */        
+               };
                
-               Dictionary registrationProperties=new Hashtable();
-               registrationProperties.put(Constants.SERVICE_PID, 
bundleContext.getBundle().getSymbolicName());
                
-               serviceRegistration=bundleContext.registerService(new 
String[]{BunnyServiceModel.class.getName(),ManagedService.class.getName()},
+               serviceRegistration=bundleContext.registerService(
+                               servicesClasses,
                                this,
-                               registrationProperties);
+                               getRegistrationProperties()
+               );
        }
 
        public void stop(BundleContext bundleContext) throws Exception {
                if(serviceRegistration!=null) serviceRegistration.unregister();
        }
 
-       public void updated(Dictionary conigurationProperties) throws 
ConfigurationException {
+       public void updated(Dictionary configurationProperties) throws 
ConfigurationException {
 //             try {
                configure(
-                               (String) 
conigurationProperties.get("serialNumber"),
-                               (String) conigurationProperties.get("token"),
-                               (String) conigurationProperties.get("voice")
+                               (String) 
configurationProperties.get("serialNumber"),
+                               (String) configurationProperties.get("token"),
+                               (String) configurationProperties.get("voice")
                        );
+               
+               Object topic=configurationProperties.get("eventtopic");
+               if(topic!=null) {
+                       if (topic instanceof String[]) {
+                               eventTopics=(String[])topic;
+                               
serviceRegistration.setProperties(getRegistrationProperties());
+                       }
+               } else {
+                       ConfigurationException configurationException= new 
ConfigurationException("eventtopic","must be a String[]");
+                       throw configurationException;   
+               }
+               
 //             } catch (Exception e) {
 //                     ConfigurationException configurationException= new 
ConfigurationException();
 //                     throw configurationException;
 //             }
+       }
+
+       public void handleEvent(Event event) {
+               
+               String name;
+               Object value=null;
+               
+               long newLeftEarPosition=leftEarPosition;
+               long newRightEarPosition=rightEarPosition;
+               String textToSpeech=null;
+               String chor=null;
+               String voice=null;
+               
+               name="posleft";
+               value=event.getProperty(name);
+               if(value!=null && value instanceof Long) {
+                       newLeftEarPosition=((Long)value).longValue();           
        
+               }
+               
+               name="posright";
+               value=event.getProperty(name);
+               if(value!=null && value instanceof Long) {
+                       newRightEarPosition=((Long)value).longValue();          
        
+               }
+
+               name="tts";
+               value=event.getProperty(name);
+               if(value!=null && value instanceof String) {
+                       textToSpeech=(String)value;                     
+               }
+               
+               name="voice";
+               value=event.getProperty(name);
+               if(value!=null && value instanceof String) {
+                       voice=(String)value;                    
+               }
+
+               name="chor";
+               value=event.getProperty(name);
+               if(value!=null && value instanceof String) {
+                       chor=(String)value;                     
+               }
+               
+               this.play(newLeftEarPosition, newRightEarPosition, 
textToSpeech, chor, voice);
        }
 
 }

Modified: 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java?rev=587926&r1=587925&r2=587926&view=diff
==============================================================================
--- 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
 (original)
+++ 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/bunny/impl/BunnyServiceModelImpl.java
 Wed Oct 24 09:43:47 2007
@@ -38,9 +38,11 @@
 
 public class BunnyServiceModelImpl implements BunnyServiceModel {
 
-       private long rightEarPosition = 0;
+       private static String baseUrlString = 
"http://api.nabaztag.com/vl/FR/api.jsp?";;
+       
+       protected long rightEarPosition = 0;
 
-       private long leftEarPosition = 0;
+       protected long leftEarPosition = 0;
 
        private String key = "0";
 
@@ -82,7 +84,7 @@
        public void play(long newLeftEarPosition, long newRightEarPosition,
                        String textToSpeech, String chor, String voice) {
 
-               String urlString = "http://api.nabaztag.com/vl/FR/api.jsp?";;
+               String urlString = baseUrlString;
 
                Map parameters=new HashMap();
                parameters.put("key",key);
@@ -128,7 +130,7 @@
        public void sendRawURL(String encodedExtraURLParameters) {
                Object[] arguments = new Object[3];
 
-               String urlString = "http://api.nabaztag.com/vl/FR/api.jsp?";;
+               String urlString = baseUrlString;
                urlString += encodedExtraURLParameters;
 
                Map parameters=new HashMap();
@@ -141,7 +143,6 @@
 
        public void getEarPositions() {
 
-               String urlString = "http://api.nabaztag.com/vl/FR/api.jsp?";;
 
                Map parameters=new HashMap();
                parameters.put("key",key);
@@ -151,7 +152,7 @@
                
                parameters.put("ears","ok"); // send the ears position
 
-               Document document=RESTFulClient.invoke(urlString,parameters);
+               Document 
document=RESTFulClient.invoke(baseUrlString,parameters);
                if(document!=null) {
                        NodeList nodeList;
                        nodeList=document.getElementsByTagName("leftposition");

Modified: 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
URL: 
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java?rev=587926&r1=587925&r2=587926&view=diff
==============================================================================
--- 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
 (original)
+++ 
felix/sandbox/donsez/bunny/src/main/java/org/apache/felix/sandbox/rest/RESTFulClient.java
 Wed Oct 24 09:43:47 2007
@@ -22,6 +22,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.PrintStream;
 import java.io.StringWriter;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
@@ -34,7 +35,6 @@
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
@@ -51,7 +51,9 @@
 public class RESTFulClient {
 
        private static boolean trace=true;
-
+       
+       private static PrintStream out=System.out;
+       
        /**
         * @return the trace
         */
@@ -75,7 +77,7 @@
                        }
                }
                if (trace)
-                       System.out.println("Bunny Request:" + urlString);
+                       out.println("Bunny Request:" + urlString);
 
                URL url;
                try {
@@ -94,26 +96,26 @@
                        int length = httpURLConnection.getContentLength();
                        if (length == -1) {
                                if (trace)
-                                       System.out.print("Response 
(length=unknown):");
+                                       out.print("Response (length=unknown):");
                                BufferedReader br = new BufferedReader(
                                                new InputStreamReader(in));
                                String line;
                                while ((line = br.readLine()) != null) {
                                        if (trace)
-                                               System.out.println(line);
+                                               out.println(line);
                                }
                                br.close();
 
                        } else {
                                if (trace)
-                                       System.out.print("Response (length=" + 
length + "):");
+                                       out.print("Response (length=" + length 
+ "):");
 
                                for (; length != 0; --length) {
                                        if (trace)
-                                               System.out.print((char) 
in.read());
+                                               out.print((char) in.read());
                                }
                                if (trace)
-                                       System.out.println();
+                                       out.println();
                                in.close();
                        }
 
@@ -131,27 +133,49 @@
                                urlString += '&' + 
URLEncoder.encode(key.toString()) + '=' + URLEncoder.encode(value.toString());
                        }
                }
-               
+
                if (trace)
-                       System.out.println("Request:" + urlString);
+                       out.println("Request:" + urlString);
+               
+               URL url=null;
+               try {
+                       url = new URL(urlString);
+               } catch (MalformedURLException e) {
+                       out.println(e);
+                       return null;
+               }
 
-               DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
-               DocumentBuilder db=null;
                try {
-                       db = dbf.newDocumentBuilder();
-                       // Read the entire document into memory
-                       Document document = db.parse(urlString);
-                       if (trace)
-                               System.out.println("Bunny Response:" + 
toText(document));
-                       return document;
+                       HttpURLConnection httpURLConnection = 
(HttpURLConnection) url.openConnection();
+                       httpURLConnection.connect();
+                       InputStream in = httpURLConnection.getInputStream();
+                       int length = httpURLConnection.getContentLength();
+                       int responseCode=httpURLConnection.getResponseCode();
+                       if((responseCode/100)!=2) {
+                               if(trace) out.println("Response: 
code="+responseCode+ " message: "+httpURLConnection.getResponseMessage());
+                               return null;
+                       }
+                       
+                       if (responseCode==HttpURLConnection.HTTP_NO_CONTENT) {
+                               if(trace) out.println("Response: no content");
+                               return null;
+                       } else {
+                               DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
+                               DocumentBuilder db=null;
+                               db = dbf.newDocumentBuilder();                  
+                               Document document = db.parse(in);
+                               if (trace)
+                                       out.println("Response:" + 
toText(document));
+                               return document;
+                       }
                } catch (ParserConfigurationException e) {
-                       e.printStackTrace(System.err);
+                       out.println(e);
                } catch (SAXException e) {
-                       System.err.println(e);
+                       out.println(e);
                } catch (IOException e) {
-                       System.err.println(e);
+                       out.println(e);
                } catch (TransformerException e) {
-                       System.err.println(e);
+                       out.println(e);
                }
                return null;
        }
@@ -167,9 +191,23 @@
                DOMSource domSource = new DOMSource(document);
         StringWriter writer = new StringWriter();
         StreamResult result = new StreamResult(writer);
-        TransformerFactory tf = TransformerFactory.newInstance();
-        Transformer transformer = tf.newTransformer();
+        TransformerFactory transformerFactory = 
TransformerFactory.newInstance();
+        Transformer transformer = transformerFactory.newTransformer();
         transformer.transform(domSource, result);
         return writer.toString();
+       }
+
+       /**
+        * @return the out
+        */
+       public static synchronized final PrintStream getOut() {
+               return out;
+       }
+
+       /**
+        * @param out the out to set
+        */
+       public static synchronized final void setOut(PrintStream out) {
+               RESTFulClient.out = out;
        }
 }

Modified: felix/sandbox/donsez/bunny/src/site/script.txt
URL: 
http://svn.apache.org/viewvc/felix/sandbox/donsez/bunny/src/site/script.txt?rev=587926&r1=587925&r2=587926&view=diff
==============================================================================
--- felix/sandbox/donsez/bunny/src/site/script.txt (original)
+++ felix/sandbox/donsez/bunny/src/site/script.txt Wed Oct 24 09:43:47 2007
@@ -29,6 +29,14 @@
 
 bunny issleeping 
 
+
+# with EventAdmin (event.topics is org/apache/felix/sandbox/bunny)
+obr install "Event Admin"
+start eventadmincmd/eventadmincmd.jar
+eventadmin subscribe org/apache/felix/sandbox/bunny
+eventadmin send org/apache/felix/sandbox/bunny 
voice=aaron22s&tts=hello+world+the+sun+shines+today+but+the+weather+may+be+cloudy+this+afternoon
+
+
 # send actions with the raw sub-command
 bunny raw action=2
 bunny raw action=3


Reply via email to