FileDocCategorySizeDatePackage
Utility.javaAPI DocGlassfish v2 API15242Fri May 04 22:36:00 BST 2007com.sun.enterprise.util

Utility

public final class Utility extends Object
Handy class full of static functions.

Fields Summary
static Logger
_logger
private static LocalStringManagerImpl
localStrings
private static byte[]
mech
Constructors Summary
Methods Summary
public static intbytesToInt(byte[] array, int offset)
Unmarshal a byte array to an integer. Assume the bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte.

param
array The array of bytes.
param
offset The offset from which to start unmarshalling.

	int b1, b2, b3, b4;

        b1 = (array[offset++] << 24) & 0xFF000000;
        b2 = (array[offset++] << 16) & 0x00FF0000;
        b3 = (array[offset++] << 8)  & 0x0000FF00;
        b4 = (array[offset++] << 0)  & 0x000000FF;

	return (b1 | b2 | b3 | b4);
    
public static longbytesToLong(byte[] array, int offset)
Unmarshal a byte array to an long. Assume the bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+7] is the least-significant-byte.

param
array The array of bytes.
param
offset The offset from which to start unmarshalling.

	long l1, l2;

	l1 = (long)bytesToInt(array, offset) << 32;
	l2 = (long)bytesToInt(array, offset+4) & 0xFFFFFFFFL;

	return (l1 | l2);
    
public static voidcheckJVMVersion()

        // do not perform any JVM version checking
    
public static java.lang.ClassLoadergetClassLoader()
Get the current thread's context class loader which is set to the CommonClassLoader by ApplicationServer

return
the thread's context classloader if it exists; else the system class loader.

	if (Thread.currentThread().getContextClassLoader() != null) {
	    return Thread.currentThread().getContextClassLoader();
	} else {
	    return ClassLoader.getSystemClassLoader();
	}
    
public static java.lang.StringgetLocalAddress()
Return the hostname of the local machine.

	String address = null;
	try {
	    InetAddress ia = InetAddress.getLocalHost();
	    address = ia.getHostAddress();
	} catch(UnknownHostException e) {
	    return "127.0.0.1";
	}
	return address;
    
public static java.lang.StringgetLocalHost()
Return the hostname of the local machine.

	String hostname = null;
	try {
	    InetAddress ia = InetAddress.getLocalHost();
	    hostname = ia.getHostName();
	} catch(UnknownHostException e) {
	    return "localhost";
	}
	return hostname;
    
public static byte[]getMechanism()
Return the ASN.1 encoded representation of a GSS mechanism identifier. Currently only the GSSUP Mechanism is supported.


     
        try {
	    mech = GSSUtils.getDER(GSSUtils.GSSUP_MECH_OID);
	} catch(IOException io) {
	    mech = null;
	}
    
        return mech;
    
public static java.util.PropertiesgetPropertiesFromFile(java.lang.String file)

	InputStream is = ClassLoader.getSystemResourceAsStream(file);
	if (is != null) {
	    Properties config = new Properties();
	    config.load(is);
	    return config;
	} else {
	    String remoteclient = "/" + file;
	    InputStream is2 = Utility.class.getResourceAsStream(remoteclient);
	    Properties config = new Properties();
	    config.load(is2);
	    return config;
	}
    
public static voidintToBytes(int value, byte[] array, int offset)
Marshal an integer to a byte array. The bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte.

param
array The array of bytes.
param
offset The offset from which to start marshalling.

        array[offset++] = (byte)((value >>> 24) & 0xFF);
        array[offset++] = (byte)((value >>> 16) & 0xFF);
        array[offset++] = (byte)((value >>> 8) & 0xFF);
        array[offset++] = (byte)((value >>> 0) & 0xFF);
    
public static shortintToShort(int value)

	if (value > 32767)
            return (short)(value - 65536) ;
	return (short)value ;
    
public static voidinvokeApplicationMain(java.lang.Class mainClass, java.lang.String[] args)
Verify and invoke main if present in the specified class.

	    String err = localStrings.getLocalString ("utility.no.main", "",
                     new Object[] {mainClass});

	    // determine the main method using reflection
	    // verify that it is public static void and takes
	    // String[] as the only argument
	    Method mainMethod = null;
	    try {
	        mainMethod = mainClass.getMethod("main", 
		    new Class[] { String[].class } );
	    } catch(NoSuchMethodException msme) {
		_logger.log(Level.SEVERE,"enterprise_util.excep_in_utility",msme);
		throw new ClassNotFoundException(err);
	    }

	    // check modifiers: public static
	    int modifiers = mainMethod.getModifiers ();
	    if (!Modifier.isPublic (modifiers) || 
		!Modifier.isStatic (modifiers))  {
		    err = localStrings.getLocalString(
			"utility.main.notpublicorstatic", 
			"The main method is either not public or not static");
		    _logger.log(Level.SEVERE,"enterprise_util.excep_in_utility_main.notpublicorstatic");
	    	    throw new ClassNotFoundException(err);
	    }

	    // check return type and exceptions
	    if (!mainMethod.getReturnType().equals (Void.TYPE)) {
		err = localStrings.getLocalString(
			"utility.main.notvoid",
			"The main method's return type is not void ");
		_logger.log(Level.SEVERE,"enterprise_util.excep_in_utility_main.notvoid");
		throw new ClassNotFoundException(err);
	    }

	    // build args to the main and call it
	    Object params [] = new Object [1];
	    params[0] = args;
	    mainMethod.invoke(null, params);

    
public static voidinvokeSetMethod(java.lang.Object obj, java.lang.String prop, java.lang.String value)

        Class cl = obj.getClass();
        // change first letter to uppercase
        String setMeth = "set" + prop.substring(0,1).toUpperCase() +
            prop.substring(1);

        // try string method
        try {
            Class[] cldef = {String.class};
            Method meth = cl.getMethod(setMeth, cldef);
            Object[] params = {value};
            meth.invoke(obj, params);
            return;
        } catch (NoSuchMethodException ex) {
            try { 
                // try int method
                Class[] cldef = {Integer.TYPE};
                Method meth = cl.getMethod(setMeth, cldef);
                Object[] params = {Integer.valueOf(value)};
                meth.invoke(obj, params);
                return;
            } catch(NoSuchMethodException nsmex) {
                // try boolean method
                Class[] cldef = {Boolean.TYPE};
                Method meth = cl.getMethod(setMeth, cldef);
                Object[] params = {Boolean.valueOf(value)};
                meth.invoke(obj, params);
                return;
            }
        }
    
public static voidinvokeSetMethodCaseInsensitive(java.lang.Object obj, java.lang.String prop, java.lang.String value)

            String alternateMethodName = null;
            Class cl = obj.getClass();

            String setMeth = "set" + prop;


            Method[] methodsList = cl.getMethods();
            boolean methodFound = false;
            int i=0;
            for (i =0; i<methodsList.length; ++i)
            {
                if(methodsList[i].getName().equalsIgnoreCase(setMeth) == true)
                {
                  Class[] parameterTypes = methodsList[i].getParameterTypes();
                  if(parameterTypes.length == 1 )
                  {
                       if(parameterTypes[0].getName().equals("java.lang.String"))
                       {
                           methodFound = true;
                           break;
                       }
                       else 
                           alternateMethodName = methodsList[i].getName();
                   }
                  
                }
            }
            if(methodFound == true)
            {
                Object[] params = {value};
                methodsList[i].invoke(obj, params);
                return;
            }
            if(alternateMethodName != null) 
            {
                 try 
                 {
                // try int method
                    Class[] cldef = {Integer.TYPE};
                    Method meth = cl.getMethod(alternateMethodName, cldef);
                    Object[] params = {Integer.valueOf(value)};
                    meth.invoke(obj, params);
                    return;
                 } 
                 catch(NoSuchMethodException nsmex) 
                 {
                    // try boolean method
                    Class[] cldef = {Boolean.TYPE};
                    Method meth = cl.getMethod(alternateMethodName, cldef);
                    Object[] params = {Boolean.valueOf(value)};
                    meth.invoke(obj, params);
                    return;
                 }

            }
            else
                 throw new NoSuchMethodException(setMeth);
    
public static java.lang.ClassloadClass(java.lang.String className)
Loads the class with the common class loader.

param
the class name
return
the loaded class
exception
if the class is not found.
see
getClassLoader()

	return getClassLoader().loadClass(className);
    
public static voidlongToBytes(long value, byte[] array, int offset)
Marshal an long to a byte array. The bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+7] is the least-significant-byte.

param
array The array of bytes.
param
offset The offset from which to start marshalling.

        array[offset++] = (byte)((value >>> 56) & 0xFF);
        array[offset++] = (byte)((value >>> 48) & 0xFF);
        array[offset++] = (byte)((value >>> 40) & 0xFF);
        array[offset++] = (byte)((value >>> 32) & 0xFF);
        array[offset++] = (byte)((value >>> 24) & 0xFF);
        array[offset++] = (byte)((value >>> 16) & 0xFF);
        array[offset++] = (byte)((value >>> 8) & 0xFF);
        array[offset++] = (byte)((value >>> 0) & 0xFF);
    
public static java.rmi.RemotelookupObject(java.lang.String publishedName, java.lang.Class anInterface)
This is a convenience method to lookup a remote object by name within the naming context.

exception
javax.naming.NamingException if the object with that name could not be found.


	Context ic = new InitialContext();
	java.lang.Object objRef = ic.lookup(publishedName);
	return (java.rmi.Remote) 
	    PortableRemoteObject.narrow(objRef, anInterface);
    
public static java.lang.ClassLoadersetContextClassLoader(java.lang.ClassLoader newClassLoader)
Utility routine for setting the context class loader. Returns previous class loader.


        // Can only reference final local variables from dopriveleged block
        final ClassLoader classLoaderToSet = newClassLoader;

        final Thread currentThread = Thread.currentThread();
        ClassLoader originalClassLoader = currentThread.getContextClassLoader();

        if (classLoaderToSet != originalClassLoader) {
            if (System.getSecurityManager() == null) {
                currentThread.setContextClassLoader(classLoaderToSet);
            } else {
                java.security.AccessController.doPrivileged(
                        new java.security.PrivilegedAction() {
                            public java.lang.Object run() {
                                currentThread.setContextClassLoader(classLoaderToSet);
                                return null;
                            }
                        }
                );
            }
        }
        return originalClassLoader;
    
public static voidsetEnvironment()

        Environment.obtain().activateEnvironment();
    
public static intshortToInt(short value)

    	if (value < 0)
            return value + 65536 ;
	return value ;