Reflection in Java

/* The Reflection Test Class used to realize the functions of
   java.lang.reflect, referrenced by Corejava Volume I
   Display the fields, constructors, and methods, as well as annotations
   and exceptions respectively
   by kingmmxtj Nov 20, 2008
  
http://hi.baidu.com/kingmmxtj
*/

import java.util.*;
import java.lang.reflect.*;
import java.text.*;
import java.lang.annotation.Annotation; // pay attention to differ the java.text.annotation

public class ClassAnalyzer
{
    public static void main(String[] args)
    {
         // read the class u input
         String name;
         if(args.length > 0)
                name = args[0];
         else
         {
                System.out.println(“Enter one class naem(e.g. java.uitl.Date): “);
                Scanner in = new Scanner(System.in);
                name = in.next();
         }
  
    try
    {
           // print class name and super class name
           Class cl = Class.forName(name);
           Class supercl = cl.getSuperclass();
           System.out.print(“Class ” + name);
           if(supercl != null) // if(supercl != null && supercl != Object.class)
           System.out.print(” extends ” + supercl.getName());
           System.out.print(“n{n”);
          // leave 4 blank at the beginning
           System.out.println(”    ****** Fields of the Class ******”);
            printFields(cl);
           System.out.println();
           System.out.println(”    ****** Constructors of the Class ******”);
            printConstructors(cl);
           System.out.println();
          System.out.println(”    ****** Methods of the Class ******”);
            printMethods(cl);
           System.out.println(“}”);
     }
     catch(ClassNotFoundException e)
      {
              e.printStackTrace();
     }
   }
   
/* print all fields in the class
   @param cl a class
*/
     public static void printFields(Class cl)
     {
               Field[] fields = cl.getDeclaredFields();
     
               for(Field f : fields)
              {
                       Class type = f.getType();
                       String name = f.getName();
                       System.out.print(”    ” + Modifier.toString(f.getModifiers()));
                        System.out.println(” ” + type.getName() + ” ” + name + “;”);
                      // print all the annotations in the field
                       Annotation[] annote = f.getDeclaredAnnotations();
                        for(int i = 0; i < annote.length; i++)
                       {
                                 System.out.println(”    // ” + annote[i].toString());
                       }
                }
      }
    
/* print all constructors in the class
   @param cl a class
*/
    public static void printConstructors(Class cl)
     {
                 Constructor[] constructors = cl.getDeclaredConstructors();
     
               for(Constructor c : constructors)
               {
                        String name = c.getName();
                        System.out.print(”    ” + Modifier.toString(c.getModifiers()));
                        System.out.print(” ” + name + “(“);
       
                        // print parameter types
                        Class[] paramTypes = c.getParameterTypes();
                        for(int j = 0; j < paramTypes.length; j++)
                        {
                                 if(j > 0)
                                         System.out.print(“, “);
                                 System.out.print(paramTypes[j].getName());
                         }
                        System.out.print(“)”);
                        // print the Exceptions in the constructor
                         Class[] ex = c.getExceptionTypes();
                        if(ex.length == 0)
                                      System.out.println(“;”);
                         else
                       {
                                   System.out.print(” throws “);
                                   for(int j = 0; j < ex.length; j++)
                                  {
                                               if(j > 0)
                                                         System.out.print(“, “);
                                                System.out.print(ex[j].getName());
                                 }
                                  System.out.println(“;”);
                       }
                       // print all the annotations in the constructor
                      Annotation[] annote = c.getDeclaredAnnotations();
                        for(int i = 0; i < annote.length; i++)
                       {
                               System.out.println(”    // ” + annote[i].toString());
                       }
                 }
     }
    
/* print all methods in the class
   @param cl a class
*/
      public static void printMethods(Class cl)
      {
                Method[] methods = cl.getDeclaredMethods();
      
                for(Method m : methods)
               {
                         Class retType = m.getReturnType();
                         String name = m.getName();
       
                       // print modifiers, return value and method name
                        System.out.print(”    ” + Modifier.toString(m.getModifiers()));
                        System.out.print(” ” + retType.getName() + ” ” + name + “(“);
       
                       // print parameter types
                       Class[] paramTypes = m.getParameterTypes();
                        for(int j = 0; j < paramTypes.length; j++)
                       {
                               if(j > 0)
                                        System.out.print(“, “);
                               System.out.print(paramTypes[j].getName());
                     }
                       System.out.print(“)”);
                      // print the Exceptions in the method
                       Class[] ex = m.getExceptionTypes();
                      if(ex.length == 0)
                                     System.out.println(“;”);
                        else
                     {
                                   System.out.print(” throws “);
                                   for(int j = 0; j < ex.length; j++)
                                  {
                                            if(j > 0)
                                                      System.out.print(“, “);
                                            System.out.print(ex[j].getName());
                                    }
                                     System.out.println(“;”);
                     }
                    // print all the annotations in the method
                     Annotation[] annote = m.getDeclaredAnnotations();
                      for(int i = 0; i < annote.length; i++)
                   {
                              System.out.println(“    // ” + annote[i].toString());
                     }
             }
      }
}  

Posted in Dave's Tools | Tagged , | 1 Comment

socket (buffer) overflow detection tool – sodt

Name: sodt (套接字缓存溢出探测器)

Language: KSH

Destination: Detect socket buffer overflow for certain process real-timely

Orignal Intention: Confirm messages lost caused by socket buffer overflow

Version: 0.1

Supported Protocol: UDP/TCP/SCTP

Supported OS: Linux/FreeBSD

Note: ‘root’ permission is preferred to avoid OS kernel error

Example: ./sodt -p udp -n H248: detect socket overflow for H248 with sampling rate 1 second

Phase II (if time is ok……): TBD

Src:

#
# Socket (buffer) Overflow Detection Tool – sodt
# Version 0.1
# Aug 9, 2010
#
Dave.Tian@alcatel-lucent.com
#
# Changes:
# Version 0.1 –
#       SCTP support on Linux
# Version 0.0 – Aug 4, 2010
#       TCP/UDP support on Linux/FreeBSD
#

# func:
show_usage() {
        echo “Usage: sodt [-h] [-p <protocol>] [-n <name>] [-s <sampling>]”
        echo ”  -h print this help message”
        echo ”  -p protocol of the socket: udp/tcp”
        echo ”  -n name of process using socket”
        echo ”  -s sampling rate for sodt, default time is 1 second”
        echo “For detailed information about this tool, please refer to:”
        echo “
http://ihgpweb.ih.lucent.com/~daveti/OS/sodt.htm
        echo “Any feedback or suggestion please mail to:”
        echo “
Dave.Tian@alcatel-lucent.com
}

# Check the OS on which sodt is running
# Only Linux/FreeBSD is supported, as Solaris does not have ‘sysctl’

OSNAME=`uname`
if [ “$OSNAME” != “Linux” ] && [ “$OSNAME” != “FreeBSD” ]
then
        echo “Error: only Linux/FreeBSD is supported by ‘sodt'”
        exit 1
fi

# Parameters’ processing for ‘sodt’

typeset -i VAR_H
typeset -i VAR_N
typeset -i VAR_P
typeset -i VAR_S
VAR_H=0
VAR_N=0
VAR_P=0
VAR_S=0
export PROTOCOL=””
export PRONAME=””
export SLEEPTIME=””

if [ “$#” != 0 ]
then
        while getopts hp:n:s: VAR
        do
                case $VAR in
                        h|H)
                                ((VAR_H+=1))
                                ;;
                        n|N)
                                ((VAR_N+=1))
                                PRONAME=${OPTARG}
                                ;;
                        p|P)
                                ((VAR_P+=1))
                                PROTOCOL=${OPTARG}
                                ;;
                        s|S)
                                ((VAR_S+=1))
                                SLEEPTIME=${OPTARG}
                                ;;
                        ?)
                                show_usage
                                exit 1
                                ;;
                esac
        done

        shift $(($OPTIND – 1))
        VAR_R_ARG=”$*”

        if [ -n “$VAR_R_ARG” ] || [ “$VAR_N” = 0 ] || [ “$VAR_P” = 0 ] || [ “$VAR_H” = 1 ]
        then
                show_usage
                exit 1
        else
                if [ “$PROTOCOL” != “udp” ] && [ “$PROTOCOL” != “tcp” ] && [ “$PROTOCOL” != “sctp” ]
                then
                        echo “Error: unsupported protocol. Only UDP/TCP/SCTP is supported by ‘sodt'”
                        exit 1
                fi

                if [ “$VAR_S” = 0 ]
                then
                        echo “sodt: Take default sampling interval 1 second”
                        SLEEPTIME=”1″
                fi
        fi
else
        show_usage
        exit 1
fi

# Check if this machine is Linux, if kernel supports SCTP, if SCTP module is loaded, if SCTP is enabled
# and if ‘netstat’ supports SCTP parameter, when ‘sctp’ is chosen
# Use ‘checksctp’ in ‘lksctp-tool’ to cover all testings above.

if [ “$PROTOCOL” = “sctp” ] && [ `checksctp` != “SCTP supported” ]
then
        echo “Error: SCTP is NOT supported on this machine”
        exit 1
fi

# Check for ‘root’ permission
# ‘sysctl’ and ‘netstat’ needs ‘root’ permission to display more info

echo “Warning: ‘sodt’ needs ‘root’ permission to run smoothly”
# Get the system socket buffer data

sysctl -a | grep net.core > sodt.tmp 2>/dev/null
export DEFAULT_RECV_BUFFER=`cat sodt.tmp | grep net.core.rmem_default | cut -d”=” -f2 | tr -d ” “`
export DEFAULT_SEND_BUFFER=`cat sodt.tmp | grep net.core.wmem_default | cut -d”=” -f2 | tr -d ” “`
export MAX_RECV_BUFFER=`cat sodt.tmp | grep net.core.rmem_max | cut -d”=” -f2 | tr -d ” “`
export MAX_SEND_BUFFER=`cat sodt.tmp | grep net.core.wmem_max | cut -d”=” -f2 | tr -d ” “`
echo “sodt: OS default Socket recv buffer: $DEFAULT_RECV_BUFFER”
echo “sodt: OS default Socket send buffer: $DEFAULT_SEND_BUFFER”
echo “sodt: OS max Socket recv buffer: $MAX_RECV_BUFFER”
echo “sodt: OS max Socet send buffer: $MAX_SEND_BUFFER”

# While loop for real-time detection

echo “sodt: Detection starts”
while true
do
        netstat –$PROTOCOL -p | grep “$PRONAME” > sodt.tmp
        while read line
        do
                RECV_Q=`echo $line | cut -d” ” -f2 | tr -d ” “`
                SEND_Q=`echo $line | cut -d” ” -f3 | tr -d ” “`
                if [ “$RECV_Q” -ge “$MAX_RECV_BUFFER” ] || [ “$SEND_Q” -ge “$MAX_SEND_BUFFER” ]
                then
                        echo “`date`: $line”
                fi
        done < sodt.tmp
        sleep $SLEEPTIME
done

Posted in Dave's Tools | Tagged , , | 123 Comments

Hello world!

Welcome to daveti.blog.com

This is my third blog (previous are are sina.com and baidu.com). Hope this would my last blog. Pages on baidu.com will be imported here.  English and Chinese would be both available on my blog as I am Chinese and works in Alcatel-Lucent R&D. My interests are music, guitar, EE, CS and trivia in life:)

http://hi.baidu.com/kingmmxtj开始搬家到这里,新的开始,新的希望,或者新的生活。。。。。。

Thanks,

Dave

Posted in Uncategorized | 3 Comments

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Posted in Uncategorized | 1 Comment