Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 1
#!/bin/sh
2
#
3
#    Licensed to the Apache Software Foundation (ASF) under one or more
4
#    contributor license agreements.  See the NOTICE file distributed with
5
#    this work for additional information regarding copyright ownership.
6
#    The ASF licenses this file to You under the Apache License, Version 2.0
7
#    (the "License"); you may not use this file except in compliance with
8
#    the License.  You may obtain a copy of the License at
9
#
10
#       http://www.apache.org/licenses/LICENSE-2.0
11
#
12
#    Unless required by applicable law or agreed to in writing, software
13
#    distributed under the License is distributed on an "AS IS" BASIS,
14
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
#    See the License for the specific language governing permissions and
16
#    limitations under the License.
17
#
18
 
19
DIRNAME=`dirname $0`
20
PROGNAME=`basename $0`
21
 
22
#
23
# Check/Set up some easily accessible MIN/MAX params for JVM mem usage
24
#
25
 
26
if [ "x$JAVA_MIN_MEM" = "x" ]; then
27
    JAVA_MIN_MEM=128M
28
    export JAVA_MIN_MEM
29
fi
30
 
31
if [ "x$JAVA_MAX_MEM" = "x" ]; then
32
    JAVA_MAX_MEM=512M
33
    export JAVA_MAX_MEM
34
fi
35
 
36
warn() {
37
    echo "${PROGNAME}: $*"
38
}
39
 
40
die() {
41
    warn "$*"
42
    exit 1
43
}
44
 
45
maybeSource() {
46
    file="$1"
47
    if [ -f "$file" ] ; then
48
        . $file
49
    fi
50
}
51
 
52
detectOS() {
53
    # OS specific support (must be 'true' or 'false').
54
    cygwin=false;
55
    darwin=false;
56
    aix=false;
57
    os400=false;
58
    case "`uname`" in
59
        CYGWIN*)
60
            cygwin=true
61
            ;;
62
        Darwin*)
63
            darwin=true
64
            ;;
65
        AIX*)
66
            aix=true
67
            ;;
68
        OS400*)
69
            os400=true
70
            ;;
71
    esac
72
    # For AIX, set an environment variable
73
    if $aix; then
74
         export LDR_CNTRL=MAXDATA=0xB0000000@DSA
75
         export IBM_JAVA_HEAPDUMP_TEXT=true
76
         echo $LDR_CNTRL
77
    fi
78
}
79
 
80
unlimitFD() {
81
    # Use the maximum available, or set MAX_FD != -1 to use that
82
    if [ "x$MAX_FD" = "x" ]; then
83
        MAX_FD="maximum"
84
    fi
85
 
86
    # Increase the maximum file descriptors if we can
87
    if [ "$os400" = "false" ] && [ "$cygwin" = "false" ]; then
88
        MAX_FD_LIMIT=`ulimit -H -n`
89
        if [ "$MAX_FD_LIMIT" != 'unlimited' ]; then
90
            if [ $? -eq 0 ]; then
91
                if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
92
                    # use the system max
93
                    MAX_FD="$MAX_FD_LIMIT"
94
                fi
95
 
96
                ulimit -n $MAX_FD > /dev/null
97
                # echo "ulimit -n" `ulimit -n`
98
                if [ $? -ne 0 ]; then
99
                    warn "Could not set maximum file descriptor limit: $MAX_FD"
100
                fi
101
            else
102
                warn "Could not query system maximum file descriptor limit: $MAX_FD_LIMIT"
103
            fi
104
        fi
105
    fi
106
}
107
 
108
locateHome() {
109
    if [ "x$SSHD_HOME" != "x" ]; then
110
        warn "Ignoring predefined value for SSHD_HOME"
111
    fi
112
 
113
    # In POSIX shells, CDPATH may cause cd to write to stdout
114
    (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
115
 
116
    SSHD_HOME=`cd $DIRNAME/..; pwd`
117
    if [ ! -d "$SSHD_HOME" ]; then
118
        die "SSHD_HOME is not valid: $SSHD_HOME"
119
    fi
120
}
121
 
122
setupNativePath() {
123
    # Support for loading native libraries
124
    LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$SSHD_BASE/lib:$SSHD_HOME/lib"
125
 
126
    # For Cygwin, set PATH from LD_LIBRARY_PATH
127
    if $cygwin; then
128
        LD_LIBRARY_PATH=`cygpath --path --windows "$LD_LIBRARY_PATH"`
129
        PATH="$PATH;$LD_LIBRARY_PATH"
130
        export PATH
131
    fi
132
    export LD_LIBRARY_PATH
133
}
134
 
135
locateJava() {
136
    # Setup the Java Virtual Machine
137
    if $cygwin ; then
138
        [ -n "$JAVA" ] && JAVA=`cygpath --unix "$JAVA"`
139
        [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
140
    fi
141
 
142
    if [ "x$JAVA" = "x" ]; then
143
        if [ "x$JAVA_HOME" = "x" ] && [ "$darwin" = "true" ]; then
144
            JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
145
        fi
146
        if [ "x$JAVA_HOME" != "x" ]; then
147
            if [ ! -d "$JAVA_HOME" ]; then
148
                die "JAVA_HOME is not valid: $JAVA_HOME"
149
            fi
150
            JAVA="$JAVA_HOME/bin/java"
151
        else
152
            warn "JAVA_HOME not set; results may vary"
153
            JAVA="java"
154
        fi
155
    fi
156
}
157
 
158
detectJVM() {
159
   #echo "`$JAVA -version`"
160
   # This service should call `java -version`,
161
   # read stdout, and look for hints
162
   if $JAVA -version 2>&1 | grep "^IBM" ; then
163
       JVM_VENDOR="IBM"
164
   # on OS/400, java -version does not contain IBM explicitly
165
   elif $os400; then
166
       JVM_VENDOR="IBM"
167
   else
168
       JVM_VENDOR="SUN"
169
   fi
170
   # echo "JVM vendor is $JVM_VENDOR"
171
}
172
 
173
setupDebugOptions() {
174
    if [ "x$JAVA_OPTS" = "x" ]; then
175
        JAVA_OPTS="$DEFAULT_JAVA_OPTS"
176
    fi
177
    export JAVA_OPTS
178
 
179
    # Set Debug options if enabled
180
    if [ "x$SSHD_DEBUG" != "x" ]; then
181
        # Use the defaults if JAVA_DEBUG_OPTS was not set
182
        if [ "x$JAVA_DEBUG_OPTS" = "x" ]; then
183
            JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
184
        fi
185
 
186
        JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
187
        warn "Enabling Java debug options: $JAVA_DEBUG_OPTS"
188
    fi
189
}
190
 
191
setupDefaults() {
192
    DEFAULT_JAVA_OPTS="-Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM "
193
 
194
    #Set the JVM_VENDOR specific JVM flags
195
    if [ "$JVM_VENDOR" = "SUN" ]; then
196
        DEFAULT_JAVA_OPTS="-server $DEFAULT_JAVA_OPTS -Dcom.sun.management.jmxremote"
197
    elif [ "$JVM_VENDOR" = "IBM" ]; then
198
        if $os400; then
199
            DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS"
200
        elif $aix; then
201
            DEFAULT_JAVA_OPTS="-Xverify:none -Xlp $DEFAULT_JAVA_OPTS"
202
        else
203
            DEFAULT_JAVA_OPTS="-Xverify:none $DEFAULT_JAVA_OPTS"
204
        fi
205
    fi
206
 
207
    # Add the jars in the lib dir
208
    for file in $SSHD_HOME/lib/*.jar
209
    do
210
        if [ -z "$CLASSPATH" ]; then
211
            CLASSPATH="$file"
212
        else
213
            CLASSPATH="$CLASSPATH:$file"
214
        fi
215
    done
216
    DEFAULT_JAVA_DEBUG_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
217
 
218
    ##
219
    ## TODO: Move to conf/profiler/yourkit.{sh|cmd}
220
    ##
221
    # Uncomment to enable YourKit profiling
222
    #DEFAULT_JAVA_DEBUG_OPTS="-Xrunyjpagent"
223
}
224
 
225
init() {
226
    # Determine if there is special OS handling we must perform
227
    detectOS
228
 
229
    # Unlimit the number of file descriptors if possible
230
    unlimitFD
231
 
232
    # Locate the Sshd home directory
233
    locateHome
234
 
235
    # Setup the native library path
236
    setupNativePath
237
 
238
    # Locate the Java VM to execute
239
    locateJava
240
 
241
    # Determine the JVM vendor
242
    detectJVM
243
 
244
    # Setup default options
245
    setupDefaults
246
 
247
    # Install debug options
248
    setupDebugOptions
249
 
250
}
251
 
252
run() {
253
    if $cygwin; then
254
        SSHD_HOME=`cygpath --path --windows "$SSHD_HOME"`
255
        CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
256
    fi
257
    cd $SSHD_BASE
258
    exec $JAVA $JAVA_OPTS -Dsshd.home="$SSHD_HOME" $OPTS -classpath "$CLASSPATH" org.apache.sshd.SshServer "$@"
259
}
260
 
261
main() {
262
    init
263
    run "$@"
264
}
265
 
266
main "$@"