1#!/bin/sh
2
3# Use --debug to activate debug mode with an optional argument to specify the port.
4# Usage : standalone.sh --debug
5#         standalone.sh --debug 9797
6
7# By default debug mode is disabled.
8DEBUG_MODE="${DEBUG:-false}"
9DEBUG_PORT="${DEBUG_PORT:-8787}"
10SERVER_OPTS=""
11while [ "$#" -gt 0 ]
12do
13    case "$1" in
14      --debug)
15          DEBUG_MODE=true
16          if [ -n "$2" ] && [ "$2" = `echo "$2" | sed 's/-//'` ]; then
17              DEBUG_PORT=$2
18              shift
19          fi
20          ;;
21      -Djava.security.manager*)
22          echo "ERROR: The use of -Djava.security.manager has been removed. Please use the -secmgr command line argument or SECMGR=true environment variable."
23          exit 1
24          ;;
25      -secmgr)
26          SECMGR="true"
27          ;;
28      --)
29          shift
30          break;;
31      *)
32          SERVER_OPTS="$SERVER_OPTS '$1'"
33          ;;
34    esac
35    shift
36done
37
38DIRNAME=`dirname "$0"`
39PROGNAME=`basename "$0"`
40GREP="grep"
41
42# Use the maximum available, or set MAX_FD != -1 to use that
43MAX_FD="maximum"
44
45# OS specific support (must be 'true' or 'false').
46cygwin=false;
47darwin=false;
48linux=false;
49solaris=false;
50freebsd=false;
51other=false
52case "`uname`" in
53    CYGWIN*)
54        cygwin=true
55        ;;
56
57    Darwin*)
58        darwin=true
59        ;;
60    FreeBSD)
61        freebsd=true
62        ;;
63    Linux)
64        linux=true
65        ;;
66    SunOS*)
67        solaris=true
68        ;;
69    *)
70        other=true
71        ;;
72esac
73
74# For Cygwin, ensure paths are in UNIX format before anything is touched
75if $cygwin ; then
76    [ -n "$JBOSS_HOME" ] &&
77        JBOSS_HOME=`cygpath --unix "$JBOSS_HOME"`
78    [ -n "$JAVA_HOME" ] &&
79        JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
80    [ -n "$JAVAC_JAR" ] &&
81        JAVAC_JAR=`cygpath --unix "$JAVAC_JAR"`
82fi
83
84# Setup JBOSS_HOME
85RESOLVED_JBOSS_HOME=`cd "$DIRNAME/.." >/dev/null; pwd`
86if [ "x$JBOSS_HOME" = "x" ]; then
87    # get the full path (without any relative bits)
88    JBOSS_HOME=$RESOLVED_JBOSS_HOME
89else
90 SANITIZED_JBOSS_HOME=`cd "$JBOSS_HOME"; pwd`
91 if [ "$RESOLVED_JBOSS_HOME" != "$SANITIZED_JBOSS_HOME" ]; then
92   echo ""
93   echo "   WARNING:  JBOSS_HOME may be pointing to a different installation - unpredictable results may occur."
94   echo ""
95   echo "             JBOSS_HOME: $JBOSS_HOME"
96   echo ""
97   sleep 2s
98 fi
99fi
100export JBOSS_HOME
101
102# Read an optional running configuration file
103if [ "x$RUN_CONF" = "x" ]; then
104    RUN_CONF="$DIRNAME/standalone.conf"
105fi
106if [ -r "$RUN_CONF" ]; then
107    . "$RUN_CONF"
108fi
109
110# Set debug settings if not already set
111if [ "$DEBUG_MODE" = "true" ]; then
112    DEBUG_OPT=`echo $JAVA_OPTS | $GREP "\-agentlib:jdwp"`
113    if [ "x$DEBUG_OPT" = "x" ]; then
114        JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=n"
115    else
116        echo "Debug already enabled in JAVA_OPTS, ignoring --debug argument"
117    fi
118fi
119
120# Setup the JVM
121if [ "x$JAVA" = "x" ]; then
122    if [ "x$JAVA_HOME" != "x" ]; then
123        JAVA="$JAVA_HOME/bin/java"
124    else
125        JAVA="java"
126    fi
127fi
128
129if [ "$PRESERVE_JAVA_OPTS" != "true" ]; then
130    # Check for -d32/-d64 in JAVA_OPTS
131    JVM_D64_OPTION=`echo $JAVA_OPTS | $GREP "\-d64"`
132    JVM_D32_OPTION=`echo $JAVA_OPTS | $GREP "\-d32"`
133
134    # Check If server or client is specified
135    SERVER_SET=`echo $JAVA_OPTS | $GREP "\-server"`
136    CLIENT_SET=`echo $JAVA_OPTS | $GREP "\-client"`
137
138    if [ "x$JVM_D32_OPTION" != "x" ]; then
139        JVM_OPTVERSION="-d32"
140    elif [ "x$JVM_D64_OPTION" != "x" ]; then
141        JVM_OPTVERSION="-d64"
142    elif $darwin && [ "x$SERVER_SET" = "x" ]; then
143        # Use 32-bit on Mac, unless server has been specified or the user opts are incompatible
144        "$JAVA" -d32 $JAVA_OPTS -version > /dev/null 2>&1 && PREPEND_JAVA_OPTS="-d32" && JVM_OPTVERSION="-d32"
145    fi
146
147    if [ "x$CLIENT_SET" = "x" -a "x$SERVER_SET" = "x" ]; then
148        # neither -client nor -server is specified
149        if $darwin && [ "$JVM_OPTVERSION" = "-d32" ]; then
150            # Prefer client for Macs, since they are primarily used for development
151            PREPEND_JAVA_OPTS="$PREPEND_JAVA_OPTS -client"
152        else
153            PREPEND_JAVA_OPTS="$PREPEND_JAVA_OPTS -server"
154        fi
155    fi
156
157    # EAP6-121 feature disabled
158    # Enable rotating GC logs if the JVM supports it and GC logs are not already enabled
159    #NO_GC_LOG_ROTATE=`echo $JAVA_OPTS | $GREP "\-verbose:gc"`
160    #if [ "x$NO_GC_LOG_ROTATE" = "x" ]; then
161        # backup prior gc logs
162        #mv "$JBOSS_LOG_DIR/gc.log.0" "$JBOSS_LOG_DIR/backupgc.log.0" >/dev/null 2>&1
163        #mv "$JBOSS_LOG_DIR/gc.log.1" "$JBOSS_LOG_DIR/backupgc.log.1" >/dev/null 2>&1
164        #mv "$JBOSS_LOG_DIR/gc.log.2" "$JBOSS_LOG_DIR/backupgc.log.2" >/dev/null 2>&1
165        #mv "$JBOSS_LOG_DIR/gc.log.3" "$JBOSS_LOG_DIR/backupgc.log.3" >/dev/null 2>&1
166        #mv "$JBOSS_LOG_DIR/gc.log.4" "$JBOSS_LOG_DIR/backupgc.log.4" >/dev/null 2>&1
167        #mv "$JBOSS_LOG_DIR/gc.log.*.current" "$JBOSS_LOG_DIR/backupgc.log.current" >/dev/null 2>&1
168        #"$JAVA" $JVM_OPTVERSION -verbose:gc -Xloggc:"$JBOSS_LOG_DIR/gc.log" -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=3M -XX:-TraceClassUnloading -version >/dev/null 2>&1 && mkdir -p $JBOSS_LOG_DIR && PREPEND_JAVA_OPTS="$PREPEND_JAVA_OPTS -verbose:gc -Xloggc:\"$JBOSS_LOG_DIR/gc.log\" -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=3M -XX:-TraceClassUnloading"
169    #fi
170
171    JAVA_OPTS="$PREPEND_JAVA_OPTS $JAVA_OPTS"
172fi
173
174if [ "x$JBOSS_MODULEPATH" = "x" ]; then
175    JBOSS_MODULEPATH="$JBOSS_HOME/modules"
176fi
177
178if $linux; then
179    # consolidate the server and command line opts
180    CONSOLIDATED_OPTS="$JAVA_OPTS $SERVER_OPTS"
181    # process the standalone options
182    for var in $CONSOLIDATED_OPTS
183    do
184       # Remove quotes
185       p=`echo $var | tr -d "'"`
186       case $p in
187         -Djboss.server.base.dir=*)
188              JBOSS_BASE_DIR=`readlink -m ${p#*=}`
189              ;;
190         -Djboss.server.log.dir=*)
191              JBOSS_LOG_DIR=`readlink -m ${p#*=}`
192              ;;
193         -Djboss.server.config.dir=*)
194              JBOSS_CONFIG_DIR=`readlink -m ${p#*=}`
195              ;;
196       esac
197    done
198fi
199
200if $solaris; then
201    # consolidate the server and command line opts
202    CONSOLIDATED_OPTS="$JAVA_OPTS $SERVER_OPTS"
203    # process the standalone options
204    for var in $CONSOLIDATED_OPTS
205    do
206       # Remove quotes
207       p=`echo $var | tr -d "'"`
208      case $p in
209        -Djboss.server.base.dir=*)
210             JBOSS_BASE_DIR=`echo $p | awk -F= '{print $2}'`
211             ;;
212        -Djboss.server.log.dir=*)
213             JBOSS_LOG_DIR=`echo $p | awk -F= '{print $2}'`
214             ;;
215        -Djboss.server.config.dir=*)
216             JBOSS_CONFIG_DIR=`echo $p | awk -F= '{print $2}'`
217             ;;
218      esac
219    done
220fi
221
222# No readlink -m on BSD
223if $darwin || $freebsd || $other ; then
224    # consolidate the server and command line opts
225    CONSOLIDATED_OPTS="$JAVA_OPTS $SERVER_OPTS"
226    # process the standalone options
227    for var in $CONSOLIDATED_OPTS
228    do
229       # Remove quotes
230       p=`echo $var | tr -d "'"`
231       case $p in
232         -Djboss.server.base.dir=*)
233              JBOSS_BASE_DIR=`cd ${p#*=} ; pwd -P`
234              ;;
235         -Djboss.server.log.dir=*)
236              if [ -d "${p#*=}" ]; then
237                JBOSS_LOG_DIR=`cd ${p#*=} ; pwd -P`
238             else
239                #since the specified directory doesn't exist we don't validate it
240                JBOSS_LOG_DIR=${p#*=}
241             fi
242             ;;
243         -Djboss.server.config.dir=*)
244              JBOSS_CONFIG_DIR=`cd ${p#*=} ; pwd -P`
245              ;;
246       esac
247    done
248fi
249
250# determine the default base dir, if not set
251if [ "x$JBOSS_BASE_DIR" = "x" ]; then
252   JBOSS_BASE_DIR="$JBOSS_HOME/standalone"
253fi
254# determine the default log dir, if not set
255if [ "x$JBOSS_LOG_DIR" = "x" ]; then
256   JBOSS_LOG_DIR="$JBOSS_BASE_DIR/log"
257fi
258# determine the default configuration dir, if not set
259if [ "x$JBOSS_CONFIG_DIR" = "x" ]; then
260   JBOSS_CONFIG_DIR="$JBOSS_BASE_DIR/configuration"
261fi
262
263# For Cygwin, switch paths to Windows format before running java
264if $cygwin; then
265    JBOSS_HOME=`cygpath --path --windows "$JBOSS_HOME"`
266    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
267    JBOSS_MODULEPATH=`cygpath --path --windows "$JBOSS_MODULEPATH"`
268    JBOSS_BASE_DIR=`cygpath --path --windows "$JBOSS_BASE_DIR"`
269    JBOSS_LOG_DIR=`cygpath --path --windows "$JBOSS_LOG_DIR"`
270    JBOSS_CONFIG_DIR=`cygpath --path --windows "$JBOSS_CONFIG_DIR"`
271fi
272
273if [ "x$JBOSS_MODULEPATH" = "x" ]; then
274    JBOSS_MODULEPATH="$JBOSS_HOME/modules"
275fi
276
277# Process the JAVA_OPTS and fail the script of a java.security.manager was found
278SECURITY_MANAGER_SET=`echo $JAVA_OPTS | $GREP "java\.security\.manager"`
279if [ "x$SECURITY_MANAGER_SET" != "x" ]; then
280    echo "ERROR: The use of -Djava.security.manager has been removed. Please use the -secmgr command line argument or SECMGR=true environment variable."
281    exit 1
282fi
283
284# Set up the module arguments
285MODULE_OPTS=""
286if [ "$SECMGR" = "true" ]; then
287    MODULE_OPTS="$MODULE_OPTS -secmgr";
288fi
289
290# Display our environment
291echo "========================================================================="
292echo ""
293echo "  JBoss Bootstrap Environment"
294echo ""
295echo "  JBOSS_HOME: $JBOSS_HOME"
296echo ""
297echo "  JAVA: $JAVA"
298echo ""
299echo "  JAVA_OPTS: $JAVA_OPTS"
300echo ""
301echo "========================================================================="
302echo ""
303
304while true; do
305   if [ "x$LAUNCH_JBOSS_IN_BACKGROUND" = "x" ]; then
306      # Execute the JVM in the foreground
307      eval \"$JAVA\" -D\"[Standalone]\" $JAVA_OPTS \
308         \"-Dorg.jboss.boot.log.file="$JBOSS_LOG_DIR"/server.log\" \
309         \"-Dlogging.configuration=file:"$JBOSS_CONFIG_DIR"/logging.properties\" \
310         -jar \""$JBOSS_HOME"/jboss-modules.jar\" \
311         $MODULE_OPTS \
312         -mp \""${JBOSS_MODULEPATH}"\" \
313         org.jboss.as.standalone \
314         -Djboss.home.dir=\""$JBOSS_HOME"\" \
315         -Djboss.server.base.dir=\""$JBOSS_BASE_DIR"\" \
316         "$SERVER_OPTS"
317      JBOSS_STATUS=$?
318   else
319      # Execute the JVM in the background
320      eval \"$JAVA\" -D\"[Standalone]\" $JAVA_OPTS \
321         \"-Dorg.jboss.boot.log.file="$JBOSS_LOG_DIR"/server.log\" \
322         \"-Dlogging.configuration=file:"$JBOSS_CONFIG_DIR"/logging.properties\" \
323         -jar \""$JBOSS_HOME"/jboss-modules.jar\" \
324         $MODULE_OPTS \
325         -mp \""${JBOSS_MODULEPATH}"\" \
326         org.jboss.as.standalone \
327         -Djboss.home.dir=\""$JBOSS_HOME"\" \
328         -Djboss.server.base.dir=\""$JBOSS_BASE_DIR"\" \
329         "$SERVER_OPTS" "&"
330      JBOSS_PID=$!
331      # Trap common signals and relay them to the jboss process
332      trap "kill -HUP  $JBOSS_PID" HUP
333      trap "kill -TERM $JBOSS_PID" INT
334      trap "kill -QUIT $JBOSS_PID" QUIT
335      trap "kill -PIPE $JBOSS_PID" PIPE
336      trap "kill -TERM $JBOSS_PID" TERM
337      if [ "x$JBOSS_PIDFILE" != "x" ]; then
338        echo $JBOSS_PID > $JBOSS_PIDFILE
339      fi
340      # Wait until the background process exits
341      WAIT_STATUS=128
342      while [ "$WAIT_STATUS" -ge 128 ]; do
343         wait $JBOSS_PID 2>/dev/null
344         WAIT_STATUS=$?
345         if [ "$WAIT_STATUS" -gt 128 ]; then
346            SIGNAL=`expr $WAIT_STATUS - 128`
347            SIGNAL_NAME=`kill -l $SIGNAL`
348            echo "*** JBossAS process ($JBOSS_PID) received $SIGNAL_NAME signal ***" >&2
349         fi
350      done
351      if [ "$WAIT_STATUS" -lt 127 ]; then
352         JBOSS_STATUS=$WAIT_STATUS
353      else
354         JBOSS_STATUS=0
355      fi
356      if [ "$JBOSS_STATUS" -ne 10 ]; then
357            # Wait for a complete shudown
358            wait $JBOSS_PID 2>/dev/null
359      fi
360      if [ "x$JBOSS_PIDFILE" != "x" ]; then
361            grep "$JBOSS_PID" $JBOSS_PIDFILE && rm $JBOSS_PIDFILE
362      fi
363   fi
364   if [ "$JBOSS_STATUS" -eq 10 ]; then
365      echo "Restarting application server..."
366   else
367      exit $JBOSS_STATUS
368   fi
369done
370