From 4506d2d3fb5fa2dc62060e997c46f00d2f7c9c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A4rtel?= Date: Mon, 24 Sep 2012 01:41:50 +0300 Subject: [PATCH] Init script to read config file. --- .gitignore | 1 + initscripts/install.sh | 10 +++++++++- initscripts/template.txt | 9 +++++---- src/main/java/fi/helsinki/cs/tmc/comet/Config.java | 4 +++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 62b40e2..f7e6410 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /daemon.log +/config.properties diff --git a/initscripts/install.sh b/initscripts/install.sh index e6d4cb5..5e6b819 100755 --- a/initscripts/install.sh +++ b/initscripts/install.sh @@ -1,21 +1,29 @@ #!/bin/bash -e THISDIR=`dirname "$0"` BASEDIR=`dirname "$THISDIR"` +THISDIR=`realpath "$THISDIR"` +BASEDIR=`realpath "$BASEDIR"` + TEMPLATE="$THISDIR/template.txt" SCRIPT_NAME=tmc-comet TARGET="/etc/init.d/$SCRIPT_NAME" RUN_AS=`stat -c '%U' "$BASEDIR"` PORT=${1:-8080} +CONFIG_FILE=$BASEDIR/config.properties which nc > /dev/null || (echo "Error: 'nc' command missing! Please install netcat."; exit 1) echo "Installing into $TARGET with port $PORT." echo "Configuring it to run as $RUN_AS (the current owner of the directory)." +echo "The config file will be read from $CONFIG_FILE." +echo + cat "$TEMPLATE" | sed "s@__BASEDIR__@$BASEDIR@" | sed "s@__NAME__@$SCRIPT_NAME@" | sed "s@__USER__@$RUN_AS@" | - sed "s@__PORT__@$PORT@" > $TARGET + sed "s@__PORT__@$PORT@" | + sed "s@__CONFIG_FILE__@$CONFIG_FILE@" > $TARGET chmod a+x "$TARGET" echo "Setting to start/stop by default" diff --git a/initscripts/template.txt b/initscripts/template.txt index e67b97b..b1660af 100644 --- a/initscripts/template.txt +++ b/initscripts/template.txt @@ -9,10 +9,11 @@ # Short-Description: Starts TMC's comet server. ### END INIT INFO -BASEDIR=__BASEDIR__ +BASEDIR="__BASEDIR__" LOG_FILE="$BASEDIR/daemon.log" -HTTP_PORT=__PORT__ -RUN_AS=__USER__ +HTTP_PORT="__PORT__" +RUN_AS="__USER__" +CONFIG_FILE="__CONFIG_FILE__" cd "$BASEDIR" @@ -31,7 +32,7 @@ start() { fi echo -n "Starting jetty" - su -c 'mvn -Djetty.port='"$HTTP_PORT"' jetty:start >> '"$LOG_FILE"' 2>&1 &' "$RUN_AS" + su -c 'mvn -Dfi.helsinki.cs.tmc.comet.configFile='"$CONFIG_FILE"' -Djetty.port='"$HTTP_PORT"' jetty:start >> '"$LOG_FILE"' 2>&1 &' "$RUN_AS" # Wait until started for i in `seq 1 15`; do diff --git a/src/main/java/fi/helsinki/cs/tmc/comet/Config.java b/src/main/java/fi/helsinki/cs/tmc/comet/Config.java index dbf19ad..2b2edc7 100644 --- a/src/main/java/fi/helsinki/cs/tmc/comet/Config.java +++ b/src/main/java/fi/helsinki/cs/tmc/comet/Config.java @@ -78,7 +78,9 @@ private static void loadProperties(Properties props, File file) throws IOExcepti props.load(reader); } finally { try { - reader.close(); + if (reader != null) { + reader.close(); + } } catch (IOException e) { // Ignore }