completed setup for installation, fixed robustness problem.
authorErik Brakkee <erik@brakkee.org>
Tue, 25 Dec 2012 13:31:00 +0000 (14:31 +0100)
committerErik Brakkee <erik@brakkee.org>
Tue, 25 Dec 2012 13:31:00 +0000 (14:31 +0100)
still some minor issues
- upgrade of RPM only stops service but does not start it again
- stopping upnpmonitor when not started gives error.

monitor/src/main/java/org/wamblee/upnpmonitor/Config.java
monitor/src/main/java/org/wamblee/upnpmonitor/Main.java
monitor/src/main/java/org/wamblee/upnpmonitor/Monitor.java
monitor/src/main/java/org/wamblee/upnpmonitor/UpnpStack.java
wamblee-upnpmonitor/files/etc/init.d/upnpmonitor
wamblee-upnpmonitor/files/etc/profile.d/upnpmonitor [deleted file]
wamblee-upnpmonitor/files/etc/sysconfig/upnpmonitor [new file with mode: 0644]
wamblee-upnpmonitor/files/opt/upnpmonitor/bin/upnpmonitor
wamblee-upnpmonitor/files/opt/upnpmonitor/etc/logging.properties [new file with mode: 0644]
wamblee-upnpmonitor/pom.xml

index d4e8c2ca3ba97ebf9217ac396af56a4af6eb4fec..acd51c107d768baf3fd429bbf6c7f25c97cbf600 100644 (file)
@@ -44,4 +44,12 @@ public class Config {
     public String getPattern() {
         return pattern;
     }
+
+    @Override
+    public String toString() {
+        return "Config [intervalSeconds=" + intervalSeconds +
+            ", startupCommand=" + startupCommand + ", shutdownCommand=" +
+            shutdownCommand + ", pattern=" + pattern + "]";
+    }
+
 }
index 4d49b33a7e115b2ca90de619b1ec82f49c253db9..f982fbc3e743ad450e80c348de6a022d54e1bd7e 100644 (file)
@@ -1,5 +1,9 @@
 package org.wamblee.upnpmonitor;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.Properties;
 import java.util.logging.Logger;
 
 public class Main {
@@ -8,15 +12,23 @@ public class Main {
 
     public static void main(String[] aArgs) throws Exception {
 
-        Config config = new Config(30, "echo starting", "echo stopping",
-            "mediatomb");
+        String propertyFile = System
+            .getProperty("org.wamblee.upnpmonitor.propertyfile");
+        Properties props = new Properties();
+        InputStream is = new FileInputStream(new File(propertyFile));
+        props.load(is);
+        is.close();
+
+        Config config = Config.parse(props);
+        LOGGER.info("COnfiguration: " + config);
 
         final Monitor monitor = new Monitor(config);
 
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
-                System.out.println("Signal caught, terminating monitor");
+                System.out
+                    .println("Signal caught, terminating monitor and executing shutdown command");
                 monitor.stop();
             }
         });
index fab9fe643b66292103c5472ce33db7d8bd2939cd..3049144fd970d1497948fc20acf1ddf5f2dc10ce 100644 (file)
@@ -26,6 +26,23 @@ public class Monitor implements Runnable {
 
     public void start() {
         serviceFound = true;
+        stack = new UpnpStack(new UpnpStack.Listener() {
+            @Override
+            public void deviceAdded(String aDeviceString) {
+                LOGGER.fine("Device added: " + aDeviceString);
+                if (aDeviceString.toLowerCase().contains(
+                    config.getPattern().toLowerCase())) {
+                    synchronized (Monitor.this) {
+                        setServiceFound(true);
+                    }
+                }
+            }
+
+            @Override
+            public void deviceRemoved(String aDeviceString) {
+                LOGGER.fine("Device removed:" + aDeviceString);
+            }
+        });
         executor.scheduleWithFixedDelay(this, 0, config.getIntervalSeconds(),
             TimeUnit.SECONDS);
         executeCommand(config.getStartupCommand());
@@ -47,27 +64,7 @@ public class Monitor implements Runnable {
             return;
         }
 
-        shutdownStack();
-
         setServiceFound(false);
-
-        stack = new UpnpStack(new UpnpStack.Listener() {
-            @Override
-            public void deviceAdded(String aDeviceString) {
-                LOGGER.fine("Device added: " + aDeviceString);
-                if (aDeviceString.toLowerCase().contains(
-                    config.getPattern().toLowerCase())) {
-                    synchronized (Monitor.this) {
-                        setServiceFound(true);
-                    }
-                }
-            }
-
-            @Override
-            public void deviceRemoved(String aDeviceString) {
-                LOGGER.fine("Device removed:" + aDeviceString);
-            }
-        });
         stack.search();
     }
 
index 2940e379ea08541c490a0313d320379c182c6dde..385e11339dfc8f0246a6c3296d30ba6353a5134a 100644 (file)
@@ -44,6 +44,7 @@ public class UpnpStack {
     }
 
     public void search() {
+        upnpService.getRegistry().removeAllRemoteDevices();
         controlPoint.search(new DeviceTypeHeader(deviceType));
     }
 
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0dde25e86c4d9385774843e37b3796c3eb6961bc 100755 (executable)
@@ -0,0 +1,99 @@
+#!/bin/bash
+#
+# Author: Erik Brakkee
+#
+# init.d/upnpmonitor
+#
+# System startup script for the upnp monitor.
+#
+### BEGIN INIT INFO
+# Provides: upnpmonitor
+# Required-Start: $remote_fs $syslog 
+# Required-Stop:  $remote_fs $syslog
+# Default-Start:  3 5
+# Default-Stop:   0 1 2 6
+# Description:    Start upnpmonitor 
+### END INIT INFO
+
+test -s /etc/rc.status && . /etc/rc.status && rc_reset
+
+# Determine the base and follow a runlevel link name.
+base=${0##*/}
+link=${base#*[SK][0-9][0-9]}
+
+# Force execution if not called by a runlevel directory.
+#test $link = $base && START_GLASSFISH=yes
+#test "$START_UPNPMONITOR" = yes || exit 0
+
+# setup environment
+[[ -r /etc/profile.local ]] && . /etc/profile.local
+
+exec=/opt/upnpmonitor/bin/upnpmonitor
+prog=${exec##*/}
+lockfile=/var/lock/upnpmonitor
+
+echo "prog = $prog"
+
+start()
+{
+    echo -n "Starting upnp monitor: "
+    if [[ ! -r "$lockfile" ]]
+    then
+      su upnpmonitor -c "$exec" > /var/log/upnpmonitor.log 2>&1 & 
+      pid=$!
+      rv=$?
+      [ $rv -eq 0 ] && echo $pid > $lockfile
+    else
+      rv=0
+    fi
+    return $rv
+}
+
+stop()
+{
+    echo -n "Shutting down upnpmonitor: "
+    kill -2 $( < $lockfile )
+    rv=$?
+    rm -f $lockfile
+    return $rv
+}
+
+restart()
+{
+    stop
+    start
+}
+
+case "$1" in
+    start|stop|restart)
+        if ! $1
+        then
+          return=$rc_failed
+        fi
+        rc_status -v
+        ;;
+    force-reload)
+        restart
+        ;;
+    status)
+        echo -n "Checking for upnp monitor status..."
+        checkproc $prog
+        rc_status -v 
+        ;;
+    try-restart|condrestart)
+        if status $prog >/dev/null ; then
+            restart
+        fi
+        ;;
+    reload)
+        echo "Service ${0##*/} does not support the reload action " 1>&2
+        exit 3
+        ;;
+    *)
+        echo "Usage: $0 {start|stop|status|restart|try-restart|force-reload}" 1>&2
+        exit 2
+        ;;
+esac
+
+
diff --git a/wamblee-upnpmonitor/files/etc/profile.d/upnpmonitor b/wamblee-upnpmonitor/files/etc/profile.d/upnpmonitor
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/wamblee-upnpmonitor/files/etc/sysconfig/upnpmonitor b/wamblee-upnpmonitor/files/etc/sysconfig/upnpmonitor
new file mode 100644 (file)
index 0000000..53d9388
--- /dev/null
@@ -0,0 +1,27 @@
+
+
+########################################################################
+# monitoring interval in seconds
+########################################################################
+org.wamblee.upnpmonitor.intervalSeconds=30
+
+########################################################################
+# Command used to startup the mediaservice. This is executed upon 
+# startup of the service and at recovery. 
+########################################################################
+org.wamblee.upnpmonitor.startupCommand=echo starting
+
+########################################################################
+# Command used to startup the mediaservice. This is executed upon 
+# recovery and at shutdown.
+########################################################################
+org.wamblee.upnpmonitor.shutdownCommand=echo stopping
+
+########################################################################
+# Pattern to match the mediaservice name. The display string of the 
+# mediaservice must contain the string mentioned below. Matching is 
+# case insensitive and is a plain string "contains" match. 
+########################################################################
+org.wamblee.upnpmonitor.pattern=mediatomb
+
+
index 6af0dd484ff92fea7488290043bf0bfbd3bb268d..dbc850b3cebd7bcd4095a1461d1f2774c0c5bd44 100755 (executable)
@@ -8,6 +8,19 @@ do
   classpath="$classpath:$jar"
 done
 
-java -classpath $classpath org.wamblee.upnpmonitor.Main
+etcdir="$( dirname $0 )/../etc"
+
+cd /tmp
+
+java -classpath $classpath \
+     -Djava.util.logging.config.file=$etcdir/logging.properties \
+        -Dorg.wamblee.upnpmonitor.propertyfile=/etc/sysconfig/upnpmonitor \
+        org.wamblee.upnpmonitor.Main & 
+pid=$!
+
+trap "kill -1 $pid; exit 1" EXIT
+
+wait $pid
+
 
 
diff --git a/wamblee-upnpmonitor/files/opt/upnpmonitor/etc/logging.properties b/wamblee-upnpmonitor/files/opt/upnpmonitor/etc/logging.properties
new file mode 100644 (file)
index 0000000..2fb2ee5
--- /dev/null
@@ -0,0 +1,62 @@
+############################################################
+#      Default Logging Configuration File
+#
+# You can use a different file by specifying a filename
+# with the java.util.logging.config.file system property.  
+# For example java -Djava.util.logging.config.file=myfile
+############################################################
+
+############################################################
+#      Global properties
+############################################################
+
+# "handlers" specifies a comma separated list of log Handler 
+# classes.  These handlers will be installed during VM startup.
+# Note that these classes must be on the system classpath.
+# By default we only configure a ConsoleHandler, which will only
+# show messages at the INFO and above levels.
+handlers= java.util.logging.ConsoleHandler
+
+# To also add the FileHandler, use the following line instead.
+#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
+
+# Default global logging level.
+# This specifies which kinds of events are logged across
+# all loggers.  For any given facility this global level
+# can be overriden by a facility specific level
+# Note that the ConsoleHandler also has a separate level
+# setting to limit messages printed to the console.
+.level= WARNING
+org.wamblee.level=INFO
+
+
+
+############################################################
+# Handler specific properties.
+# Describes specific configuration info for Handlers.
+############################################################
+
+# default file output is in user's home directory.
+java.util.logging.FileHandler.pattern = %h/java%u.log
+java.util.logging.FileHandler.limit = 50000
+java.util.logging.FileHandler.count = 1
+java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
+
+# Limit the message that are printed on the console to INFO and above.
+java.util.logging.ConsoleHandler.level = INFO
+java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
+
+# Example to customize the SimpleFormatter output format 
+# to print one-line log message like this:
+#     <level>: <log message> [<date/time>]
+#
+java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
+
+############################################################
+# Facility specific properties.
+# Provides extra control for each logger.
+############################################################
+
+# For example, set the com.xyz.foo logger to only log SEVERE
+# messages:
+com.xyz.foo.level = SEVERE
index 85fcd569be6798c8848d37098e4d676fff90cecb..1f73a3751541c7a35c6eb18ea365a13531ad6c6e 100644 (file)
                         </mapping>
                                                
                                                <mapping>
+                            <directory>/opt/upnpmonitor/etc</directory>
+                            <username>root</username>
+                            <groupname>root</groupname>                                                        
+                            <sources>
+                                <source>
+                                    <location>files/opt/upnpmonitor/etc</location>
+                                </source>
+                            </sources>
+                        </mapping>
+                                               
+                                               <mapping>
                                                    <directory>/opt/upnpmonitor/lib</directory>
                                                        <filemode>755</filemode>
                                                        <username>root</username>
                                                        <groupname>root</groupname>
                                                        <dependency/>
                                                </mapping>
-                       
-                        <mapping>
-                            <directory>/etc/profile.d</directory>
+                                               
+                                               <mapping>
+                            <directory>/etc/sysconfig</directory>
                             <username>root</username>
                             <groupname>root</groupname>
+                                                       <directoryIncluded>false</directoryIncluded>
+                            <configuration>noreplace</configuration>
                             <sources>
                                 <source>
-                                    <location>files/etc/profile.d</location>
+                                    <location>files/etc/sysconfig</location>
                                 </source>
                             </sources>
                         </mapping>
                         <provide>upnpmonitor</provide>
                     </provides>
                     <requires>
-                        <require>java-1_6_0-sun-devel</require>
+                        <require>java</require>
                     </requires>
                                        <preinstallScriptlet>
                                           <script><![CDATA[
                                               
-                                              if grep '^java:' /etc/passwd > /dev/null
+                                              if grep '^upnpmonitor:' /etc/passwd > /dev/null
                                               then
-                                                echo "User java already exists." 1>&2
+                                                echo "User upnpmonitor already exists." 1>&2
                                               else
-                                                /usr/sbin/useradd -m java
+                                                /usr/sbin/useradd upnpmonitor
                                               fi
-                                              (
-                                                echo "Make sure that user java has login enabled!"
-                                                echo "(passwd java)"
-                                              ) 1>&2
-                                              
-                                              if [[ -r /etc/init.d/glassfish ]]
-                                              then
-                                                echo "This RPM does not support upgrade" 1>&2
-                                                exit 1
-                                              fi
-                                          
+
                                           ]]></script>
                                        </preinstallScriptlet>