Tuesday, August 25, 2009

Check JNDIs in Oracle Application Server against deployed ESB services

The script retrieves a list of unique JNDIs configured on the Oracle Application Server 10g and compares
them against JNDIs in use or configured in the deployed ESB services on the same server.


Script:

# ################################################################################
#
#  Author:      Ahmed Aboulnaga
#  Filename:    check_jndi_esb.sh
#  Creation:    2008-11-06
#  Description: Checks if JNDIs in deployed ESB services actually exist.
#
# ################################################################################


# ----------------------------------------
# Configurable parameters
# ----------------------------------------
ORACLE_HOME=/u01/app/oracle/product/10.1.3/soa_as_1
CURRENTDIR=$PWD
TMPDIRFILES=$PWD


# ----------------------------------------
# Basic checks
# ----------------------------------------
if [ $# -ne 2 ]; then
   TMPDIREXTRACT=$ORACLE_HOME/temp_extract_`date +%m%d`
else
   TMPDIREXTRACT=$2
   # Converting directory to a fully qualified path in case user inputs relative paths
   D=`dirname "$2"`
   B=`basename "$2"`
   TMPDIREXTRACT="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"
fi

echo ""
echo "Using ORACLE_HOME=$ORACLE_HOME"
echo "Using TMPDIREXTRACT=$TMPDIREXTRACT"


# ----------------------------------------
# Create the temp extract directory
# ----------------------------------------
if [ -d $TMPDIREXTRACT ]; then
  echo "INFO: Temporary directory '$TMPDIREXTRACT' exists."
  echo "Would you like to remove it? [y/n] \c"
  read input
  if [[ $input == [Yy]* ]]; then
     echo "Removing $TMPDIREXTRACT..."
     rm -fr $TMPDIREXTRACT
  else
     echo ""
     echo "ERROR: Temporary directory '$TMPDIREXTRACT' exists. Exiting..."
     echo ""
     exit
  fi
  mkdir $TMPDIREXTRACT
else
  mkdir $TMPDIREXTRACT
fi

if [ ! -d $ORACLE_HOME ]; then
  echo ""
  echo "ERROR: $ORACLE_HOME not found."
  echo ""
  exit
fi


# ----------------------------------------
# Get list of unique JNDIs configured on App Server
# ----------------------------------------
cat /dev/null > $TMPDIRFILES/existing_jndi_esb.tmp2
for i in `ls $ORACLE_HOME/j2ee/oc4j_soa/application-deployments/default`
do
  if [ -d $ORACLE_HOME/j2ee/oc4j_soa/application-deployments/default/$i ]; then
    for j in `ls $ORACLE_HOME/j2ee/oc4j_soa/application-deployments/default/$i` do
    do
      if [ -f $ORACLE_HOME/j2ee/oc4j_soa/application-deployments/default/$i/$j ]; then
        cat $ORACLE_HOME/j2ee/oc4j_soa/application-deployments/default/$i/$j | sed -n '/<!--/,/-->/!p' | grep "<connector-factory location=" | cut -f2 -d'"' >> $TMPDIRFILES/existing_jndi_esb.tmp2
      fi
    done
  fi
done
cat $TMPDIRFILES/existing_jndi_esb.tmp2 | sort -u > $TMPDIRFILES/existing_jndi_esb.tmp
rm -f $TMPDIRFILES/existing_jndi_esb.tmp2


# ----------------------------------------
# Header output
# ----------------------------------------
echo "Getting list of current JNDI's.  This may take a moment..."
printf "%-15s %-30s %-30s \n" "JNDI Status" "JNDI Name" "ESB Service(s) using JNDI"
printf "%-15s %-30s %-30s \n" "--------------" "----------------------" "--------------------------------------"


# ----------------------------------------
# Dump list of JNDIs to output file
# ----------------------------------------
#echo "JNDIs configured on Application Server" >> $CURRENTDIR/check_jndi_esb.csv
#echo "--------------------------------------" >> $CURRENTDIR/check_jndi_esb.csv
#cat $TMPDIRFILES/existing_jndi_esb.tmp >> $CURRENTDIR/check_jndi_esb.csv


# ----------------------------------------
# Copy all ESB projects to temp directory
# ----------------------------------------
cp $ORACLE_HOME/integration/esb/oraesb/deploy/*.zip $TMPDIREXTRACT
cd $TMPDIREXTRACT
for i in `ls *.zip`
do
  cd $TMPDIREXTRACT
  DIRNAME=`echo $i | cut -f1 -d.`
  mkdir $DIRNAME
  mv $i $DIRNAME
  cd $DIRNAME
  $ORACLE_HOME/bin/unzip $DIRNAME/*.zip > /dev/null 2>&1
done


# ----------------------------------------
# Loop through deployed ESB projects
# ----------------------------------------
echo "Message,ESB project,WSDL that was parsed,JNDI found in WSDL,ESB services using this JNDI,ESB GUIDs using this JNDI" > $CURRENTDIR/check_jndi_esb.csv

cd $CURRENTDIR
for i in `ls $TMPDIREXTRACT | grep -v bpelservice_`
do
  # Creating empty WSDL file to avoid errors in script if WSDL not found
  for j in `ls $TMPDIREXTRACT`
  do
    touch $TMPDIREXTRACT/$j/check_jndi_esb.DELETETHIS.wsdl
  done

  for j in `grep "<jca:address location" $TMPDIREXTRACT/$i/*.wsdl | cut -f1 -d:`
  do
    JNDI=`sed -n '/<!--/,/-->/!p' $j | grep "<jca:address location" | cut -f2 -d"=" | cut -f2 -d'"'`

    FOUND=`grep $JNDI $TMPDIRFILES/existing_jndi_esb.tmp`

    # ----------------------------------------
    # Find which ESB files are using this JNDI
    # ----------------------------------------
    ESBSERVICES=""
    ESBNAMES=""
    ESBGUIDS=""
    COUNT=0

    for k in `ls $TMPDIREXTRACT/$i/*.esb*`
    do
      WSDLFILE=`echo $j | sed -e 's^.*/^^'`
      ESBFILE=`grep $WSDLFILE $k | cut -f1 -d:`
      if [ ! "$ESBFILE" == "" ]; then
        COUNT=`expr $COUNT + 1`
        # ----------------------------------------
        # Get ESB service name and GUID
        # ----------------------------------------
        ESBNAME=`cat $k | grep "<service name=" | grep "guid=" | cut -f2 -d'"'`
        ESBGUID=`cat $k | grep "<service name=" | grep "guid=" | cut -f4 -d'"'`

        if [ $COUNT -gt 1 ]; then
          ESBSERVICES="${ESBNAME}(${ESBGUID})|${ESBSERVICES}"
          ESBNAMES="${ESBNAME}|${ESBNAMES}"
          ESBGUIDS="${ESBGUID}|${ESBGUIDS}"
        else
          ESBSERVICES="${ESBNAME}(${ESBGUID})"
          ESBNAMES="${ESBNAME}"
          ESBGUIDS="${ESBGUID}"
        fi
      fi
    done
    if [ $COUNT -eq 0 ]; then
      ESBNAMES="<JNDI not used in any service>"
      ESBGUIDS="<n/a>"
    fi

    # ----------------------------------------
    # If JNDI is not found
    # ----------------------------------------
    IDISPLAY=`echo $i | sed 's^.*/^^'`
    #j=`echo "$j|sed s,/," ",g|awk '{print $5"/"$6}'`
    JDISPLAY=`echo $j | sed 's^.*/^^'`
    if [ "${FOUND}" == "" ]; then
      echo "NOT CONFIGURED,$IDISPLAY,$JDISPLAY,$JNDI,$ESBNAMES,$ESBGUIDS" >> $CURRENTDIR/check_jndi_esb.csv
      printf "%-15s %-30s %-20s \n" "Missing" "$JNDI" "$ESBNAMES"
    else
      echo "CONFIGURED:,$IDISPLAY,$JDISPLAY,$JNDI,$ESBNAMES,$ESBGUIDS" >> $CURRENTDIR/check_jndi_esb.csv
      printf "%-15s %-30s %-20s \n" "Found" "$JNDI" "$ESBNAMES"
    fi

  done

done


# ----------------------------------------
# Cleanup
# ----------------------------------------
rm -f $TMPDIRFILES/existing_jndi_esb.tmp
echo ""
echo "Output file created in: $CURRENTDIR/check_jndi_esb.csv"
echo "Please delete temporary directory '$TMPDIREXTRACT' when done."
echo ""


Applicable Versions:
  • Oracle SOA Suite 10g (Advanced install)

Ahmed Aboulnaga

No comments: