public String getPattern() {
return pattern;
}
+
+ @Override
+ public String toString() {
+ return "Config [intervalSeconds=" + intervalSeconds +
+ ", startupCommand=" + startupCommand + ", shutdownCommand=" +
+ shutdownCommand + ", pattern=" + pattern + "]";
+ }
+
}
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 {
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();
}
});
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());
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();
}
}
public void search() {
+ upnpService.getRegistry().removeAllRemoteDevices();
controlPoint.search(new DeviceTypeHeader(deviceType));
}
+#!/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
+
+
--- /dev/null
+
+
+########################################################################
+# 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
+
+
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
+
--- /dev/null
+############################################################
+# 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
</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>