#!/bin/bash

#************************ Warning **********************************
#*                                                                 *
#* This configuration is property of OPEN SOURCE CONSULTING, INC.  *
#* Don't distribute this to other project.                         *
#*                                                                 *
#* Contact jchoi@osci.kr(http://www.osci.kr)                       *
#*******************************************************************

#####################################
# This is tomcat env.sh for iosp by Open Source Consulting, Inc
#####################################
export DATE=`date +%Y%m%d_%H%M%S`

#####################################
## Set Tomcat base env
#####################################
export JAVA_HOME=/data2/PlaceWAS/jdk/zulu8.33.0.1-jdk8.0.192-linux_x64
export SERVER_NAME=tomcat-9014-svr-11
export SERVER=tomcat-9014-svr-11
export CATALINA_HOME=/data2/PlaceWAS/was/engine/apache-tomcat-9.0.14
export CATALINA_BASE=/data2/PlaceWAS/was/servers/$SERVER_NAME
export LOG_DIR=${CATALINA_BASE}/logs
export GC_LOG_DIR=${LOG_DIR}/tmp

export PORT_OFFSET=2100
export COMP_USER=osc

#####################################
##### Tomcat Cluster #####
#####################################
#export BIND_ADDR=192.168.0.244
export BIND_ADDR=20.20.20.11
export MULTICAST_ADDR=230.111.222.244
export CLUSTER_PORT=$(expr 7600 + $PORT_OFFSET)

#####################################
## Set Port Configuration
#####################################
#  Default Ports are as below       
#  HTTP_PORT	    : 8080	    
#  SSL_PORT 	    : 8443	    
#  SHUTDOWN_PORT    : 8005	    
#####################################

export CUSTOM_HTTP_PORT=
export CUSTOM_AJP_PORT=
export CUSTOM_SSL_PORT=
export CUSTOM_SHUTDOWN_PORT=


if [ "x$CUSTOM_HTTP_PORT" = "x" ]; then
    export HTTP_PORT=$(expr 8080 + $PORT_OFFSET)
else
    export HTTP_PORT=$CUSTOM_HTTP_PORT
fi

if [ "x$CUSTOM_AJP_PORT" = "x" ]; then
    export AJP_PORT=$(expr 8009 + $PORT_OFFSET)
else
    export AJP_PORT=$CUSTOM_AJP_PORT
fi

if [ "x$CUSTOM_SSL_PORT" = "x" ]; then
    export SSL_PORT=$(expr 8443 + $PORT_OFFSET)
else
    export SSL_PORT=$CUSTOM_SSL_PORT
fi

if [ "x$CUSTOM_SHUTDOWN_PORT" = "x" ]; then
    export SHUTDOWN_PORT=$(expr 8005 + $PORT_OFFSET)
else
    export SHUTDOWN_PORT=$CUSTOM_SHUTDOWN_PORT
fi

#####################################
## JVM Parameter Configuration
#####################################

if [ "x$JAVA_OPTS" = "x" ]; then
   JAVA_OPTS="-server"
   JAVA_OPTS="$JAVA_OPTS -Dserver=$SERVER"
   JAVA_OPTS="$JAVA_OPTS -Djvmid=$SERVER"
   JAVA_OPTS="$JAVA_OPTS -Dhttp.port=$HTTP_PORT"
   JAVA_OPTS="$JAVA_OPTS -Dajp.port=$AJP_PORT"
   JAVA_OPTS="$JAVA_OPTS -Dssl.port=$SSL_PORT"
   JAVA_OPTS="$JAVA_OPTS -Dshutdown.port=$SHUTDOWN_PORT"
   JAVA_OPTS="$JAVA_OPTS -Djava.library.path=${CATALINA_HOME}/lib"

#####################################
## Set Port Configuration
#####################################

    JAVA_OPTS="$JAVA_OPTS -noverify"
    JAVA_OPTS="$JAVA_OPTS -Xms1024m"
    JAVA_OPTS="$JAVA_OPTS -Xmx1024m"
    JAVA_OPTS="$JAVA_OPTS -XX:MetaspaceSize=256m"
    JAVA_OPTS="$JAVA_OPTS -XX:MaxMetaspaceSize=256m"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC"

    JAVA_OPTS="$JAVA_OPTS -verbose:gc"
    JAVA_OPTS="$JAVA_OPTS -Xloggc:${GC_LOG_DIR}/gc.log.$DATE"
    JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails"
    JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDateStamps"
    JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCTimeStamps"
#    JAVA_OPTS="$JAVA_OPTS -XX:+PrintHeapAtGC"

    JAVA_OPTS="$JAVA_OPTS -XX:+UseGCLogFileRotation"
    JAVA_OPTS="$JAVA_OPTS -XX:NumberOfGCLogFiles=5"
    JAVA_OPTS="$JAVA_OPTS -XX:GCLogFileSize=2048K"

    JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError"
    JAVA_OPTS="$JAVA_OPTS -XX:HeapDumpPath=${LOG_DIR}/${SERVER_NAME}.$DATE.hprof"

    JAVA_OPTS="$JAVA_OPTS -XX:+PrintAdaptiveSizePolicy"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseLargePagesInMetaspace"
#    JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC"
    JAVA_OPTS="$JAVA_OPTS -XX:+ExplicitGCInvokesConcurrent"
    JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC"

    JAVA_OPTS="$JAVA_OPTS -XX:ReservedCodeCacheSize=512m"
    JAVA_OPTS="$JAVA_OPTS -XX:-UseCodeCacheFlushing"

    JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
    JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
    JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/urandom"

    JAVA_OPTS="$JAVA_OPTS -Dorg.apache.tomcat.websocket.DEFAULT_BUFFER_SIZE=32768"
#    JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true"

    JAVA_OPTS="$JAVA_OPTS -Dsun.lang.ClassLoader.allowArraySyntax=true "
    JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.client.gcInterval=3600000"
    JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.server.gcInterval=3600000"

#    JAVA_OPTS="$JAVA_OPTS -Dbind.addr=$BIND_ADDR"
#    JAVA_OPTS="$JAVA_OPTS -Dmulticast.addr=$MULTICAST_ADDR"
#    JAVA_OPTS="$JAVA_OPTS -Dcluster.recv.port=$CLUSTER_PORT"

fi

#####################################
## Athena Dolly Clustering
#####################################
#export JAVA_OPTS="$JAVA_OPTS -Ddolly.properties=/opt/dolly-agent-new/dolly1.properties"
#export JAVA_OPTS="$JAVA_OPTS -javaagent:/opt/dolly-agent-new/lib/core-1.0.0-SNAPSHOT.jar"

#####################################
## Set Scouter Tomcat Agent
#####################################
#export JAVA_OPTS="$JAVA_OPTS -javaagent:/opt/apm/scouter/1.7.1/agent.java/scouter-agent-java-1.7.1.jar"
#export JAVA_OPTS="$JAVA_OPTS -Dscouter.config=/opt/apm/scouter/agent.java/conf/${SERVER_NAME}.conf"

export JAVA_OPTS

#echo "================================================"
#echo "JAVA_HOME      =  $JAVA_HOME"
#echo "JAVA_OPTS      =  $JAVA_OPTS"
#echo "CATALINA_HOME  =  $CATALINA_HOME"
#echo "SERVER_HOME    =  $CATALINA_BASE"
#echo "HTTP_PORT      =  $HTTP_PORT"
#echo "SSL_PORT       =  $SSL_PORT"
#echo "AJP_PORT       =  $AJP_PORT"
#echo "SHUTDOWN_PORT  =  $SHUTDOWN_PORT"
#echo "================================================"
