You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by fu...@apache.org on 2006/10/10 13:54:58 UTC

svn commit: r454714 - in /incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight: Activator.java LightDevice.java PresentationServlet.java

Author: furfari
Date: Tue Oct 10 04:54:58 2006
New Revision: 454714

URL: http://svn.apache.org/viewvc?view=rev&rev=454714
Log:
Implemented UPnP presentation page for the BinaryLight sample (Felix-71)
Now you can access to its web page from the Network Resources directory in Win platforms.

Added:
    incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java
Modified:
    incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java
    incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java

Modified: incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java?view=diff&rev=454714&r1=454713&r2=454714
==============================================================================
--- incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java (original)
+++ incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/Activator.java Tue Oct 10 04:54:58 2006
@@ -21,9 +21,13 @@
 
 import java.util.Dictionary;
 
+import javax.servlet.Servlet;
+
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.HttpService;
 import org.osgi.service.upnp.UPnPDevice;
 
 
@@ -36,6 +40,7 @@
 	static BundleContext context;
 	private ServiceRegistration serviceRegistration;
 	private LightDevice light;
+	private HttpService httpServ;
 	
 	/**
 	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
@@ -43,8 +48,10 @@
 	public void start(BundleContext context) throws Exception {
 		Activator.context = context;
 		doServiceRegistration();
+		doServletRegistration();
 	}
 
+
 	private void doServiceRegistration() {
 		light = new LightDevice(context);
 		Dictionary dict = light.getDescriptions(null);
@@ -54,6 +61,35 @@
 				light,
 				dict
 			);
+	}
+	
+	private void doServletRegistration() {
+        ServiceReference sr = context.getServiceReference(HttpService.class.getName());
+        if (sr != null){
+        	httpServ = (HttpService) context.getService(sr);
+        	
+            try
+            {
+             Servlet presentationServlet = new PresentationServlet(light.getModel());
+             httpServ.registerServlet("/upnp/binaryLight", presentationServlet,null, null);
+            }
+            catch (Exception e)
+            {
+                System.err.println("Exception registering presentationServlet:" + e);
+            }
+            
+            
+            try
+            {
+                httpServ.registerResources("/upnp/binaryLight/images",
+                        "/org/apache/felix/upnp/sample/binaryLight/images", null);
+            }
+            catch (Exception e)
+            {
+                System.err.println("Exception registering /resource:" + e);
+            }
+
+        }		
 	}
 
 	/**

Modified: incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java?view=diff&rev=454714&r1=454713&r2=454714
==============================================================================
--- incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java (original)
+++ incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/LightDevice.java Tue Oct 10 04:54:58 2006
@@ -19,6 +19,8 @@
 
 package org.apache.felix.upnp.sample.binaryLight;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Dictionary;
 import java.util.Properties;
 
@@ -52,6 +54,9 @@
 		buildEventNotifyer();
 	}
 
+	public LightModel getModel(){
+		return model;
+	}
 	/**
 	 * 
 	 */
@@ -75,7 +80,16 @@
 		dictionary.put(UPnPDevice.MODEL_NAME,"Lucciola");
 		dictionary.put(UPnPDevice.MODEL_NUMBER,"1.0");
 		dictionary.put(UPnPDevice.MODEL_URL,"http://incubator.apache.org/felix/lucciola");
-		dictionary.put(UPnPDevice.PRESENTATION_URL,"http://incubator.apache.org/felix/lucciola/presentation");
+		String port = context.getProperty("org.osgi.service.http.port");
+        InetAddress inet;
+		try {
+			inet = InetAddress.getLocalHost();
+	        String hostname = inet.getHostName();
+            //String hostname = inet.getHostAddress();
+		dictionary.put(UPnPDevice.PRESENTATION_URL,"http://"+hostname + ":"+port+"/upnp/binaryLight/");
+		} catch (UnknownHostException e) {
+			System.out.println("Warning: enable to cacth localhost name");
+		}
 		dictionary.put(UPnPDevice.SERIAL_NUMBER,"123456789");
 		dictionary.put(UPnPDevice.TYPE,"urn:schemas-upnp-org:device:BinaryLight:1");
 		dictionary.put(UPnPDevice.UDN,DEVICE_ID);

Added: incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java?view=auto&rev=454714
==============================================================================
--- incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java (added)
+++ incubator/felix/trunk/upnp.sample.binaryLight/src/main/java/org/apache/felix/upnp/sample/binaryLight/PresentationServlet.java Tue Oct 10 04:54:58 2006
@@ -0,0 +1,87 @@
+/* 
+ * 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.
+ */
+
+package org.apache.felix.upnp.sample.binaryLight;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+class PresentationServlet extends HttpServlet
+{
+	LightModel model;
+
+    public PresentationServlet(LightModel model) {
+		this.model = model;
+	}
+
+	public void init()
+    {
+		//ServletConfig config = getServletConfig();
+        System.out.println("BinaryLight Servlet init called:" );
+    }
+
+    public void destroy() {}
+
+
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+        throws IOException, ServletException
+    {
+        String path = request.getPathInfo();
+        if (path != null){
+	        if (path.equals("/On"))
+	        	model.switchOn();
+	        else if (path.equals("/Off"))
+	        	model.switchOff();
+        }
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        out.println("<HTML>");
+        out.println("<head><title>Apache Felix UPnP BinaryLight</title></head>");
+        out.println("<body>");
+        out.println("  <center>");
+        out.println("  <h1><font face='Arial' color='#808080'>Apache Felix UPnP BinaryLight</font></h1>");
+       
+        if (model.getStatus()== false){
+        	out.println("  <p><a href=/upnp/binaryLight/On><img border='0' src='images/LightOff.gif' width='64' height='64'></a></p>");
+        }
+        else{       	
+        	out.println("  <p><a href=/upnp/binaryLight/Off><img border='0' src='images/LightOn.gif' width='64' height='64'></a></p>");
+        }
+        
+        out.println("  <p><a href=/upnp/binaryLight/>Refresh status</a></p>");
+        out.println("  </center>");
+        out.println("  </BODY>");
+        out.println("</HTML>");
+        out.flush();
+    }
+
+
+    public void doPost(HttpServletRequest request, HttpServletResponse response)
+        throws IOException, ServletException
+    {
+        doGet(request, response);
+    }
+
+}