// This code was generated by
// AdventNet AgentToolkit Ver4.1 (JMX)
// Any changes made to this file will be lost, if regenerated.
// Package Name (Dont Delete this comment)
package com.lanterncom.rms.snmp ;
import javax.management.*;
import javax.jmx.openmbean.*;
import com.adventnet.utils.jmx.*;
import com.adventnet.common.agent.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
// import the adapters
import com.adventnet.adaptors.snmp.*;
import com.lanterncom.rms.misc.logging.*;
//import javax.management.loading.*;
// importing AdventNet Snmp package
import com.adventnet.snmp.snmp2.*;
import com.adventnet.snmp.snmp2.agent.*;
// This is required if we are going to use SnmpDBAdaptor.
import com.adventnet.snmp.mibs.agent.*;
import org.omg.*;
import org.omg.CosTransactions.*;
import javax.rmi.*;
//==============================================================================
/**
* Half finished class! Repository for any timing measurement.
*
* long l;
* Chrono.start( "SNMP-Set" );
* ...
* loop
* ...
* l = Chrono.elapsed( "SNMP-Set" );
* ...
* loop_done
*
* Chrono.end( "SNMP-Set" );
* l = Chrono.getTime( "SNMP-Set" ); // last iteration
* l = Chrono.getTotalTime( "SNMP-Set" ); // total incl. last iteration
* Chrono.reset( "SNMP-Set" ); // reset total and last iteration
*
*/
class Chrono
{
//static long tTotalTime = 0;
//long tTime = 0;
private static Hashtable timers = new Hashtable(10);
// "key" -> [lTotal, lCurr]
// new : "key" -> [0, 0]
// start: "key" -> [x,-t0] where neg. indicates 'in use'
// start: "key" -> [x,-t0] already in use: rejected
// end : "key" -> [x, dt] Not in use: has total & last time. dt=t1-t0
// end : "key" -> [x, dt] Not started: error?
// start: "key" -> [y,-1] where y==x+dt indicates 'in use'
long getTime( final String sTimerName )
{
Object oVal = timers.get( sTimerName );
if ( null == oVal )
return 0;
long lTime = ((Long[])oVal)[1].longValue();
return lTime;
}
synchronized Object getTimeObj( final String sTimerName )
{
return timers.get( sTimerName ); // Already sync?
}
synchronized void setTime( final String sTimerName, long lTime )
{
timers.put( sTimerName, new Long(lTime) );
}
synchronized void start( final String sTimerName )
{
Object oVal = timers.get( sTimerName );
if ( null == oVal )
{
timers.put( sTimerName, new Long(0) );
return;
}
//timers.put( sTimerName, new Long(lTime) );
}
} // class Chrono
/**
* Misc classes
*/
class Misc
{
static final boolean bMeasureTiming = System.getProperty("timers")!=null;
static final int LONG_SIZE = 8;
public static byte[] toBytes (long l)
{
byte b[] = new byte [LONG_SIZE];
for (int i = 0; i < b.length; i++)
b[i] = (byte) ((l >> (i * 8)) & 0xFF);
return b;
}
public static long toLong (byte [] b)
{
if (b == null )
return 0;
if (b.length > LONG_SIZE)
throw new RuntimeException ("Cannot handle more than " + LONG_SIZE + " bytes");
long l = 0;
for (int i = 0; i < b.length; i++)
l |= (b[i] << (8*i));
return l;
}
/**
* Converts string to int array. Note the first element in the array is the
* length of the string
*/
public static int[] stringToIntArray(String s)
{
if (s== null) return new int[]{};
char[] c = s.toCharArray();
int [] res = new int[c.length +1];
res[0] = c.length;
for (int i = 0 ; i < c.length ; i++)
res[i+1] = toUint(c[i]);
return res;
}
/**
* converts byte to unsigned int
*/
private static int toUint (char b)
{
return b < 0 ? ((int) (b & 0x7F)) | 0x80 : b;
}
/**
* converts int array to string. Note intArray[0] is the length of the string
*/
public static String intArrayToString(int[] intArray)
{
if (intArray == null || intArray.length ==0) return "";
char[] c= new char[intArray.length - 1];
for (int i = 1 ; i < intArray.length ; i++)
c[i-1] = (char) intArray[i];
return new String(c);
}
/**
* converts int array to string. Note intArray[0] is the length of the string
*/
public static String[] intArrayToStringArray(int[] intArray)
{
try
{
if (intArray == null || intArray.length ==0)
return new String[0];
Vector v = new Vector();
for (int i = 0; i < intArray.length; i += intArray[i] +1)
{
int [] temp = new int[intArray[i] + 1];
System.arraycopy (intArray, i, temp, 0, temp.length);
v.addElement (intArrayToString(temp));
}
return (String[])v.toArray (new String[v.size()]);
}
catch (ArrayIndexOutOfBoundsException e)
{
return new String[0];
}
}
// No exceptions, and returned value can be used with CORBA (ie is always non null)
public static String safeSubstring( final String s, int beginIndex, int endIndex )
{
try {
int len = s.length();
return s.substring( beginIndex, endIndex > len ? len : endIndex );
} catch( Throwable t ) {
; // ignore
}
return "";
}
public static String snmpDateAndTimetoString( final byte[] snmpDate )
{
if ( null == snmpDate || (snmpDate.length != 8 && snmpDate.length != 11) )
return null;
// 12345678901234567890123456789012
StringBuffer s = new StringBuffer( 32 ); // "2001.11.12-10:23:34:56 GMT+10:30"
s.append( ( ((int)snmpDate[0]) *0x100) + (int)(snmpDate[1]<0 ? (0x100+snmpDate[1]) : snmpDate[1]) ); // y
s.append( '.' );
s.append( snmpDate[2] ); // moy
s.append( '.' );
s.append( snmpDate[3] ); // dom
s.append( ' ' );
s.append( snmpDate[4] ); // 24h
s.append( ':' );
s.append( snmpDate[5] ); // min
s.append( ':' );
s.append( snmpDate[6] ); // sec
s.append( ':' );
s.append( snmpDate[7] ); // deci-sec
s.append( ' ' );
s.append( "GMT" );
s.append( (char)snmpDate[8] ); // +/-
s.append( snmpDate[9] ); // utc h
s.append( ':' );
s.append( snmpDate[10] ); // utc min
return s.toString();
} // public static String snmpDatetoString( final byte[] snmpDate )
/**
* Converts Java Date to SNMP DateAndTime TC
* Definition of DateAndTime TC in rfc1514
*
* DateAndTime ::= OCTET STRING (SIZE (8 | 11))
* -- A date-time specification for the local time of day.
* -- This data type is intended to provide a consistent
* -- method of reporting date information.
* --
* -- field octets contents range
* -- _____ ______ ________ _____
* -- 1 1-2 year 0..65536
* -- (in network byte order)
* -- 2 3 month 1..12
* -- 3 4 day 1..31
* -- 4 5 hour 0..23
* -- 5 6 minutes 0..59
* -- 6 7 seconds 0..60
* -- (use 60 for leap-second)
* -- 7 8 deci-seconds 0..9
* -- 8 9 direction from UTC "+" / "-"
* -- (in ascii notation)
* -- 9 10 hours from UTC 0..11
* -- 10 11 minutes from UTC 0..59
* --
* -- Note that if only local time is known, then
* -- timezone information (fields 8-10) is not present.
*
**/
public static byte[] datetoSNMPDate( final Date date )
{
if ( null == date )
return null;
byte [] bDate = new byte[11]; // UTC go home!
TimeZone tzLoc = TimeZone.getDefault(); //
Calendar cal = Calendar.getInstance(); // Default time zone & locale
cal.setTime( date );
int year = cal.get( Calendar.YEAR );
int month = cal.get( Calendar.MONTH ) + 1; // neds 1..12; Calendar gives zero based values.
int dom = cal.get( Calendar.DAY_OF_MONTH );
int hour = cal.get( Calendar.HOUR_OF_DAY ); // 24 h
int minute = cal.get( Calendar.MINUTE );
int second = cal.get( Calendar.SECOND );
int deciSec= cal.get( Calendar.MILLISECOND ) % 100;
bDate[0] = (byte)((year & 0xff00) >> 8);
bDate[1] = (byte)(year & 0xff);
bDate[2] = (byte)month;
bDate[3] = (byte)dom;
bDate[4] = (byte)hour;
bDate[5] = (byte)minute;
bDate[6] = (byte)second;
bDate[7] = (byte)deciSec;
int utcOfsetMillis;
utcOfsetMillis = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
int difMinutes = utcOfsetMillis / 1000 / 60;
if ( difMinutes > 0 )
{
bDate[8] = (byte)0x2b;
}
else
{
bDate[8] = (byte)0x2d;
difMinutes = -difMinutes;
}
int difHours = difMinutes / 60;
difMinutes %= 60;
bDate[9 ] = (byte)difHours;
bDate[10] = (byte)difMinutes;
JLogger.Log(JLogger.PKG_SNMP,JLogger.LVL_FINE, "DatetoSNMPDate( "+ date.toString() +"): SNMP date: '"+
Misc.snmpDateAndTimetoString( bDate ) +"', dif='"+difHours+":"+difMinutes/*+"', '"+bDate+"'"*/);
return bDate;
}
//--------------------------------------------------------------------------//
/**
* converts a java.util.Date object to string representation of the day time
* textual convention
* refer to DayTime textual convention defined in LANTERNCOM-MPS-MIB
*/
static String toSnmpDayTime (Date date)
{
if (date == null)
return "";
byte[] snmpDayTime = new byte[5];
Calendar cal = Calendar.getInstance();
cal.setTime (date);
int dow = cal.get( Calendar.DAY_OF_WEEK);
int hour = cal.get( Calendar.HOUR_OF_DAY ); // 24 h
int minute = cal.get( Calendar.MINUTE );
int second = cal.get( Calendar.SECOND );
switch (dow)
{
case Calendar.MONDAY:
snmpDayTime[0] = 'm';
snmpDayTime[1] = 'o';
break;
case Calendar.TUESDAY:
snmpDayTime[0] = 't';
snmpDayTime[1] = 'u';
break;
case Calendar.WEDNESDAY:
snmpDayTime[0] = 'w';
snmpDayTime[1] = 'd';
break;
case Calendar.THURSDAY:
snmpDayTime[0] = 't';
snmpDayTime[1] = 'h';
break;
case Calendar.FRIDAY:
snmpDayTime[0] = 'f';
snmpDayTime[1] = 'r';
break;
case Calendar.SATURDAY:
snmpDayTime[0] = 's';
snmpDayTime[1] = 'a';
break;
case Calendar.SUNDAY:
snmpDayTime[0] = 's';
snmpDayTime[1] = 'u';
break;
default:
JLogger.Log(JLogger.PKG_SNMP,JLogger.LVL_WARNING, "new day in the week? - " + dow);
return "";
}
snmpDayTime[2] = (byte) hour;
snmpDayTime[3] = (byte) minute;
snmpDayTime[4] = (byte) second;
return new String(snmpDayTime);
}
//--------------------------------------------------------------------------//
/**
* converts a string representation of the day time textual convention to
* a java.util.Date object
* refer to DayTime textual convention defined in LANTERNCOM-MPS-MIB
* TODO: 1) doesn't handle we (weekend), al (all week), ws (weekday)
* 2) doesn't accept second field to be equal to 60
* 3) both of the above are because of unavailable equivalencies in
* java.util.date
*/
static Date toJavaDayTime (String s) throws IllegalArgumentException
{
if (s == null)
throw new IllegalArgumentException ("null argument");
byte[] snmpDayTime = s.getBytes();
if (snmpDayTime.length != 5)
// throw new IllegalArgumentException ("illegal length - " + s);
//todo: uncomment the line above and get rid of the line below
return new Date();
if (snmpDayTime[2] < 0 || snmpDayTime [2] > 23 || //hour
snmpDayTime[3] < 0 || snmpDayTime [3] > 59 || //minute
//todo: Date will not accept sec = 60
snmpDayTime[4] < 0 || snmpDayTime [4] > 59) //seconds -
throw new IllegalArgumentException ("illegal value - " + s);
int day = -1;
do
{
if (snmpDayTime[0] == 'm')
{
if (snmpDayTime[1] == 'o')
{
day = Calendar.MONDAY;
break;
}
throw new IllegalArgumentException ("illegal value - " + s);
}
if (snmpDayTime[0] == 't')
{
if (snmpDayTime[1] == 'u')
{
day = Calendar.TUESDAY;
break;
}
if (snmpDayTime[1] == 'h')
{
day = Calendar.THURSDAY;
break;
}
throw new IllegalArgumentException ("illegal value - " + s);
}
if (snmpDayTime[0] == 'w')
{
if (snmpDayTime[1] == 'd')
{
day = Calendar.WEDNESDAY;
break;
}
throw new IllegalArgumentException ("illegal value - " + s);
}
if (snmpDayTime[0] == 'f')
{
if (snmpDayTime[1] == 'r')
{
day = Calendar.FRIDAY;
break;
}
throw new IllegalArgumentException ("illegal value - " + s);
}
if (snmpDayTime[0] == 's')
{
if (snmpDayTime[1] == 'a')
{
day = Calendar.SATURDAY;
break;
}
if (snmpDayTime[1] == 'u')
{
day = Calendar.SUNDAY;
break;
}
throw new IllegalArgumentException ("illegal value - " + s);
}
}while (false);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, day);
cal.set(Calendar.HOUR, snmpDayTime[2]);
cal.set(Calendar.MINUTE, snmpDayTime[3]);
cal.set(Calendar.SECOND, snmpDayTime[4]);
return cal.getTime();
}
//--------------------------------------------------------------------------//
public static char nibToHex( int nib )
{
return hexDigits[ nib & 0xF ];
}
private static final char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String bytesToHexStr( final byte [] bytes )
{
if ( null==bytes || 0==bytes.length )
return null;
StringBuffer s = new StringBuffer( 2*bytes.length );
for ( int i=0; i> 4) & 0xF );
s.append( nibToHex(bytes[i] ) & 0xF );
}
return s.toString();
} // public String bytesToHexStr( final byte [] bytes )
//-----------------------------------------------------
static public void DumpStack( String msg )
{
//Runtime r = Runtime.getRuntime();
//r.traceMethodCalls( true );
try{
throw new Throwable( (null==msg)?"":msg );
} catch ( Throwable t ) {//Exception ex ) {
t.printStackTrace();
//t.fillInStackTrace();
}
}
static public Object[] Enum2Array( final Enumeration enum )
{
Vector v = new Vector(10);
while ( enum.hasMoreElements() )
{
Object obj = enum.nextElement();
v.add( obj );
}
Object objs[] = v.toArray();
return objs;
} // Object[] Enum2Array( Enumeration enum )
//--------------------------------------------------------------------------//
public static void printExceptionStack( final Exception ex, boolean dumpStack )
{
printException( ex );
if ( dumpStack )
ex.printStackTrace();
} // public void printExceptionStack( final Exception ex, boolean dumpStack )
public static void printException( final Exception ex )
{
JLogger.Log(JLogger.PKG_SNMP,JLogger.LVL_WARNING, ex.toString() );
StringBuffer sb = new StringBuffer(128 );
// coordinator: master <-> subordinate(s) comm problem.
if ( ex instanceof Transactions.CommunicationFailure )
{
//CommunicationFailures._commFail_Timeout = 0;
//CommunicationFailures._commFail_other = 1;
Transactions.CommunicationFailureInfo[] commFailureList =
((Transactions.CommunicationFailure)ex).commFailureList;
for ( int i =0; i(" );
// String sRow = CompositeDataToStr( comp );
// System.out.print( sRow +")" );
//
// // System.out.println( ", '" + comp.getOperationType() +"'" );
// }
// System.out.println( "dumpTabularData() done\n---------");
// } // public static void dumpTabularData( TabularData td )
public static String intArrayToStr( final int []objs )
{
if ( null==objs )
return "null";
if ( 0==objs.length )
return "";
StringBuffer sRes = new StringBuffer();
for ( int n = 0; n(" );
String sEntry = ent.toString();// EntryHelper.entryToStr(ent);
System.out.println( sEntry +")" );
}
System.out.println( "dumpHashTable() done\n---------");
} // public static void dumpHashTable( final Hashtable tbl )
public static StringBuffer dumpThreads()
{
return dumpThreads( Thread.currentThread() );
}
public static StringBuffer dumpThreads(Thread thread)
{
StringBuffer s = new StringBuffer(256);
ThreadGroup threadgroup = thread.getThreadGroup();
Thread threads[] = new Thread[ threadgroup.activeCount() * 2];
threadgroup.enumerate( threads, true );
s.append( "Thread group: " + threadgroup.getName() + "\n");
for ( int i = 0; i < threads.length; i++ )
if ( null != threads[i] )
s.append( " Thread #" + i + ": " + threads[i] +
", name: "+ threads[i].getName() +
", priority: "+ threads[i].getPriority() +
", alive: "+ threads[i].isAlive() +
"\n");
return s;
}
} // class Dumper
//==============================================================================
/*
class EntryHelper
{
public static String entryToStr( final AdiskEntry entry)
{
try {
return ""+entry.getAdiskID()+", "+entry.getAdiskName()+", "+entry.getAdiskCapacity()+", "+entry.getAdiskUsed()+", "+entry.getAdiskFree();
} catch ( com.adventnet.common.agent.AgentException e) {
e.printStackTrace();
}
return null;
}
} // class EntryHelper
*/
//==============================================================================
class PP // PrettyPrinter
{
//public static String sPrefix = "";
private static final String sIndent = " ";
private static int indent = -1;
private static void printIndent( final boolean bNewLevel )
{
if ( bNewLevel )
;//indent++; bad usage forced me to this.
for ( int i=0; i 0)
{
isImplied[i] = false;
//now insert the length in the int array
temp = new int[inst.length + 1];
System.arraycopy(inst,0,temp,0,pos);
temp[pos] = length[i];
System.arraycopy(inst,pos,temp,pos +1,inst.length - pos);
inst = temp;
pos += length[i] + 1; //+1 because of the inserted element
}
else
{
//the length is not specified => it is already be in the int array
isImplied[i] = true;
pos += inst[pos];
}
break;
default:
JLogger.Log(JLogger.PKG_SNMP,JLogger.LVL_WARNING, "Error: resolveIndex-> type not supported: " + type[i]);
return null;
}
}
return AgentUtil.resolveIndex (inst,type,isImplied);
}
} // RmsAgentUtil
//--------------------------------------------------------------------------//
public class Helper
{
public static void main( String[] args )
{
System.out.println( Dumper.dumpThreads() );
}
} // public class Helper