summaryrefslogtreecommitdiff
path: root/common/bin/qpid-run
blob: 6d7837c1208796d1743de98b70999fe3c6b1f93d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

# Test if we're running on cygwin.
cygwin=false
if [[ "$(uname -a | fgrep Cygwin)" != "" ]]; then
  cygwin=true
fi

die() {
  if [[ $1 = -usage ]]; then
    shift
    usage=true
  else
    usage=false
  fi
  echo "$@"
  $usage && echo
  $usage && usage
  exit 1
}

if [ -z $AMQJ_LOGGING_LEVEL ]; then
    export AMQJ_LOGGING_LEVEL=info
fi

if [ -z "$QPID_HOME" ]; then
  die "QPID_HOME must be set"
fi

if [ -z "$QPID_WORK" ]; then
    echo Setting QPID_WORK to $HOME as default
    QPID_WORK=$HOME
fi

if $cygwin; then
  QPID_HOME=$(cygpath -w $QPID_HOME)
  QPID_WORK=$(cygpath -w $QPID_WORK)
fi

#Set the default system properties that we'll use now that they have
#all been initialised
SYSTEM_PROPS="-Damqj.logging.level=$AMQJ_LOGGING_LEVEL -DQPID_HOME=$QPID_HOME -DQPID_WORK=$QPID_WORK"

#If logprefix or logsuffix set to use PID make that happen
#Otherwise just pass the value through for these props
#Using X character to avoid probs with empty strings
if [ -n "$QPID_LOG_PREFIX" ]; then
    if [ "X$QPID_LOG_PREFIX" = "XPID" ]; then
        echo Using pid in qpid log name prefix
        LOG_PREFIX=" -Dlogprefix=$$"
    else
        echo Using qpid logprefix property
        LOG_PREFIX=" -Dlogprefix=$QPID_LOG_PREFIX"
    fi
    SYSTEM_PROPS+=$LOG_PREFIX
fi

if [ -n "$QPID_LOG_SUFFIX" ]; then
    if [ "X$QPID_LOG_SUFFIX" = "XPID" ]; then
        echo Using pid in qpid log name suffix
        LOG_SUFFIX=" -Dlogsuffix=$$"
    else
        echo Using qpig logsuffix property
        LOG_SUFFIX=" -Dlogsuffix=$QPID_LOG_SUFFIX"
    fi
    SYSTEM_PROPS+=$LOG_SUFFIX
fi

echo System Properties set to $SYSTEM_PROPS

program=$(basename $0)
sourced=${BASH_SOURCE[0]}
if [[ -z ${sourced:-''} ]]; then
  sourced=$(which qpid-run) || ${QPID_HOME}/bin/qpid-run
fi

usage() {
  echo Usage: $program ... "[-run:<option>]" ...
  echo
  echo Options:
  egrep -B 1 "^\s*#USAGE: " ${sourced} |\
      sed "s/#USAGE:/       /" |\
      sed "s/-run:\(.*\))/-run:\1/" |\
      sed "s/-run:\(.*\)=\*/-run:\1=<value>/" |\
      sed "s/^--$//"
}

export EXTERNAL_CLASSPATH=$CLASSPATH
unset CLASSPATH

conf=$QPID_HOME/etc/$program.conf
if [ ! -e $conf ]; then
  conf=$QPID_HOME/etc/$(basename ${sourced}).conf
fi

if [ -r $conf ]; then
  . $conf
else
  die "unable to source $conf"
fi

declare -a RUN_ARGS JAVA_ARGS
for arg in "$@"; do
  if [[ $arg == -run:* ]]; then
    RUN_ARGS[${#RUN_ARGS[@]}]="$arg"
  else
    JAVA_ARGS[${#JAVA_ARGS[@]}]="$arg"
  fi
done

# this defines the default behavior, it may be modified during option
# processing below
DISPATCH() {
  if $debug; then
    echo "CLASSPATH=${CLASSPATH}"
    echo "${COMMAND[@]}"
  fi

  exec "${COMMAND[@]}"
}

exclusive() {
  if [ -z "$PREVIOUS_ARGS" ]; then
    PREVIOUS_ARGS=$1
  else
    PREVIOUS_ARGS+=", $1"
    DISPATCH() {
      die -usage "you must choose one of: $PREVIOUS_ARGS"
    }
  fi
}

debug=false

for arg in "${RUN_ARGS[@]}"; do
  case $arg in
    -run:debug)
#USAGE: print the classpath and command before running it
      debug=true
      ;;
    -run:jpda)
#USAGE: adds debugging options to the java command, use
#USAGE: JDPA_TRANSPORT and JPDA_ADDRESS to customize the debugging
#USAGE: behavior and use JPDA_OPTS to override it entirely
      if [ -z "$JPDA_OPTS" ]; then
        JPDA_OPTS="-Xdebug -Xrunjdwp:transport=${JPDA_TRANSPORT:-dt_socket},address=${JPDA_ADDRESS:-8000},server=y,suspend=n"
      fi
      QPID_OPTS+=" ${JPDA_OPTS}"
      ;;
    -run:external-classpath=*)
#USAGE: controls how the CLASSPATH environment variable is used by
#USAGE: this script, value can be one of ignore (the default), first,
#USAGE: last, and only
      case $arg in
        *=ignore)
          # do nothing
          ;;
        *=first)
          CLASSPATH=$EXTERNAL_CLASSPATH:$CLASSPATH
          ;;
        *=last)
          CLASSPATH=$CLASSPATH:$EXTERNAL_CLASSPATH
          ;;
        *=only)
          CLASSPATH=$EXTERNAL_CLASSPATH
          ;;
        *)
          die -usage $(echo $arg | sed "s/=/: invalid value '/")\'
         ;;
      esac
      ;;
    -run:print-classpath)
#USAGE: print the classpath
      DISPATCH() {
        echo $CLASSPATH
      }
      exclusive $arg
      ;;
    -run:print-command)
#USAGE: print the command
      DISPATCH() {
        echo "${COMMAND[@]}"
      }
      exclusive $arg
      ;;
    -run:help)
#USAGE: print this message
      DISPATCH() {
        usage
      }
      exclusive $arg
      ;;
    *)
      die -usage "unrecognized -run option '$arg'"
      ;;
  esac
done

if $cygwin; then
  CLASSPATH=$(cygpath -w -p $CLASSPATH)
  JAVA=$(cygpath -u $JAVA)
fi

COMMAND=($JAVA $JAVA_VM $JAVA_MEM $SYSTEM_PROPS $JAVA_OPTS $QPID_OPTS "${JAVA_ARGS[@]}")

DISPATCH