From 9fcdbb28f7e1f5e2e2d0e0729271cb1305df5928 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Tue, 25 Dec 2012 14:31:00 +0100 Subject: [PATCH] completed setup for installation, fixed robustness problem. still some minor issues - upgrade of RPM only stops service but does not start it again - stopping upnpmonitor when not started gives error. --- .../java/org/wamblee/upnpmonitor/Config.java | 8 ++ .../java/org/wamblee/upnpmonitor/Main.java | 18 +++- .../java/org/wamblee/upnpmonitor/Monitor.java | 37 ++++--- .../org/wamblee/upnpmonitor/UpnpStack.java | 1 + .../files/etc/init.d/upnpmonitor | 99 +++++++++++++++++++ .../files/etc/profile.d/upnpmonitor | 0 .../files/etc/sysconfig/upnpmonitor | 27 +++++ .../files/opt/upnpmonitor/bin/upnpmonitor | 15 ++- .../opt/upnpmonitor/etc/logging.properties | 62 ++++++++++++ wamblee-upnpmonitor/pom.xml | 41 ++++---- 10 files changed, 265 insertions(+), 43 deletions(-) delete mode 100644 wamblee-upnpmonitor/files/etc/profile.d/upnpmonitor create mode 100644 wamblee-upnpmonitor/files/etc/sysconfig/upnpmonitor create mode 100644 wamblee-upnpmonitor/files/opt/upnpmonitor/etc/logging.properties diff --git a/monitor/src/main/java/org/wamblee/upnpmonitor/Config.java b/monitor/src/main/java/org/wamblee/upnpmonitor/Config.java index d4e8c2c..acd51c1 100644 --- a/monitor/src/main/java/org/wamblee/upnpmonitor/Config.java +++ b/monitor/src/main/java/org/wamblee/upnpmonitor/Config.java @@ -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 + "]"; + } + } diff --git a/monitor/src/main/java/org/wamblee/upnpmonitor/Main.java b/monitor/src/main/java/org/wamblee/upnpmonitor/Main.java index 4d49b33..f982fbc 100644 --- a/monitor/src/main/java/org/wamblee/upnpmonitor/Main.java +++ b/monitor/src/main/java/org/wamblee/upnpmonitor/Main.java @@ -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(); } }); diff --git a/monitor/src/main/java/org/wamblee/upnpmonitor/Monitor.java b/monitor/src/main/java/org/wamblee/upnpmonitor/Monitor.java index fab9fe6..3049144 100644 --- a/monitor/src/main/java/org/wamblee/upnpmonitor/Monitor.java +++ b/monitor/src/main/java/org/wamblee/upnpmonitor/Monitor.java @@ -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(); } diff --git a/monitor/src/main/java/org/wamblee/upnpmonitor/UpnpStack.java b/monitor/src/main/java/org/wamblee/upnpmonitor/UpnpStack.java index 2940e37..385e113 100644 --- a/monitor/src/main/java/org/wamblee/upnpmonitor/UpnpStack.java +++ b/monitor/src/main/java/org/wamblee/upnpmonitor/UpnpStack.java @@ -44,6 +44,7 @@ public class UpnpStack { } public void search() { + upnpService.getRegistry().removeAllRemoteDevices(); controlPoint.search(new DeviceTypeHeader(deviceType)); } diff --git a/wamblee-upnpmonitor/files/etc/init.d/upnpmonitor b/wamblee-upnpmonitor/files/etc/init.d/upnpmonitor index e69de29..0dde25e 100755 --- a/wamblee-upnpmonitor/files/etc/init.d/upnpmonitor +++ b/wamblee-upnpmonitor/files/etc/init.d/upnpmonitor @@ -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 index e69de29..0000000 diff --git a/wamblee-upnpmonitor/files/etc/sysconfig/upnpmonitor b/wamblee-upnpmonitor/files/etc/sysconfig/upnpmonitor new file mode 100644 index 0000000..53d9388 --- /dev/null +++ b/wamblee-upnpmonitor/files/etc/sysconfig/upnpmonitor @@ -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 + + diff --git a/wamblee-upnpmonitor/files/opt/upnpmonitor/bin/upnpmonitor b/wamblee-upnpmonitor/files/opt/upnpmonitor/bin/upnpmonitor index 6af0dd4..dbc850b 100755 --- a/wamblee-upnpmonitor/files/opt/upnpmonitor/bin/upnpmonitor +++ b/wamblee-upnpmonitor/files/opt/upnpmonitor/bin/upnpmonitor @@ -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 index 0000000..2fb2ee5 --- /dev/null +++ b/wamblee-upnpmonitor/files/opt/upnpmonitor/etc/logging.properties @@ -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: +# : [] +# +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 diff --git a/wamblee-upnpmonitor/pom.xml b/wamblee-upnpmonitor/pom.xml index 85fcd56..1f73a37 100644 --- a/wamblee-upnpmonitor/pom.xml +++ b/wamblee-upnpmonitor/pom.xml @@ -60,20 +60,33 @@ + /opt/upnpmonitor/etc + root + root + + + files/opt/upnpmonitor/etc + + + + + /opt/upnpmonitor/lib 755 root root - - - /etc/profile.d + + + /etc/sysconfig root root + false + noreplace - files/etc/profile.d + files/etc/sysconfig @@ -94,28 +107,18 @@ upnpmonitor - java-1_6_0-sun-devel + java -- 2.31.1