org.ow2.orchestra.util
Class Misc

java.lang.Object
  extended by org.ow2.orchestra.util.Misc

public abstract class Misc
extends Object

Author:
Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes, Pierre Vigneras

Nested Class Summary
static class Misc.NullCheckResult
          Represents null value returned by findNull(Object...).
 
Field Summary
static long DAY
          Number of milleseconds in a given day
static String LINE_SEPARATOR
          The line separator as defined by the property line.separator.
static Random RANDOM
          A RANDOM instance.
static File TMP_DIR
          The file that represents the temporary directory.
 
Method Summary
static void badStateIfEquals(Object a, Object b, String msg)
          This method throw an IllegalStateException if the given parameters are equals (using Object.equals(Object)
static void badStateIfFalse(boolean valueToCheck, String msg)
          This method throw an IllegalStateException if the given parameter is false
static void badStateIfNotNull(Object valueToCheck, String msg)
          This method throw an IllegalStateException if the given parameter is not null
static void badStateIfNull(Object valueToCheck, String msg)
          This method throw an IllegalStateException if the given parameter is null
static void badStateIfTrue(boolean valueToCheck, String msg)
          This method throw an IllegalStateException if the given parameter is true
static void checkArgsNotNull(int offset, Object... params)
          Check that the given parameters are not null.
static void checkArgsNotNull(Object... params)
          Check that the given parameters are not null.
static
<T extends Serializable>
Exception
checkReallySerializable(T object)
          Check that the given object is actually serializable.
static Exception close(Closeable closeable)
          Equivalent to #reflectClose(closeable)
static Exception close(XMLDecoder decoder)
          Equivalent to {@link #reflectClose(decoder)
static Exception close(XMLEncoder encoder)
          Equivalent to {@link #reflectClose(encoder)
static String componentsToString(Object[] args, boolean deepToString)
           Smart toString() implementation of arrays.
static LogRecord createLogRecord(Logger logger, Level level, String className, String methodName, String msg, Object... args)
          This method creates a LogRecord and fills it with the given parameters
static File createTempFile(String prefix, String suffix, File directory)
          Perform java.io.File.createTempFile with retries when it fail (limit of 10 retries) (Use to by-pass bug #6325169 on SUN JDK 1.5 on windows) Sames parameter as File.createTempFile method
static String deepToString(Object o)
           
static boolean deleteDir(File dir)
          Delete a given directory and all its contents (whether they are file or directory) WARNING: this method unconditionally modifies the underlying filesystem by erasing files and directories.
static Serializable deserialize(byte[] buf)
           
static void dynamicLog(int offset, Level level, String msg, Object... args)
          Log a message through the logger of the caller at the given offset in the stack trace.
static void fastDynamicLog(int offset, Logger logger, Level level, String msg, Object... args)
          Log a message through the given logger for the caller at the given offset in the stack trace.
static void fastDynamicLog(Logger logger, Level level, String msg, Object... args)
          Equivalent to {@link Misc#fastDynamicLog(int, Logger, Level, String, Object...)
static Set<Class<?>> findAllInterfaces(Class<?> type)
           Return the list of Class objects representing all interfaces a given class implements.
static Set<Class<?>> findAllSuperTypes(Class<?> type)
           Return the list of Class objects representing all super type a given class implements.
static Set<Class<?>> findAllTypes(Class<?> type)
           Return the list of Class objects representing every types a given class implements.
static Class<?>[] findConstructorClassArgs(Class<?>[] subClasses, Class<?> classToTest)
           Return the Class[] array representing the types a constructor take as parameters.
static Class<?>[] findMethodClassArgs(Class<?>[] subClasses, Class<?> classToTest, String methodName)
           Return the Class[] array representing the types a given method take as parameters.
static Misc.NullCheckResult findNull(Object... params)
          Find null parameters in the given list.
static String formatDelay(double delay)
           Equivalent to formatDelay(long).
static String formatDelay(long delay)
           Format a delay.
static byte[] generateJar(Class<?>... classes)
           
static byte[] generateJar(Map<String,byte[]> resources)
          Generates Jar from resources
static byte[] getAllContentFrom(File file)
          Equivalent to getAllContentFrom(new FileInputStream(file));
static byte[] getAllContentFrom(InputSource source)
          Equivalent to getAllContentFrom(source.getByteStream(source));
static byte[] getAllContentFrom(InputStream in)
          Return the whole underlying stream content into a single String.
static byte[] getAllContentFrom(Reader in)
          Return the whole underlying stream content into a single String.
static byte[] getAllContentFrom(URL url)
          Return the whole underlying stream content into a single String.
static StackTraceElement getCaller(int offset)
          Return the StackTraceElement at the given offset from this method invocation.
static
<T> T
getChainOf(List<T> elements)
          Return a proxy that forward void-method invocations to each object specified in the list elements.
static String getCurrentThreadStackTrace()
           
static Map<String,byte[]> getDirContentFrom(File dir)
          Get all files in this directory.
static String getGenericFullName(Class<?> clazz)
          Return the generic name of a given class.
static long getHumanReadableId()
          Generates a human readable id as a long.
static String getHumanReadableId(String prefix)
          Generates a human readable id prefixed by the given String.
static
<T> T
getLoggerProxyFor(T target, Logger logger)
          Return a proxy that log method invocations through the provided logger.
static
<T> T
getMBeanProxy(Class<T> mbeanInterface, String jmxServiceUrl, String jmxObjectName)
          Return a proxy implementing all the interfaces specified that forward method invocations to the specified MBean.
static int getPermissionsSize(Permissions permissions)
           
static String getRandomString(int size)
          Generate a RANDOM String of the given size.
static Map<String,byte[]> getResources(Class<?>... classes)
           
static Map<String,byte[]> getResourcesFromZip(byte[] zipContent)
           
static StringBuilder getStackTraceFrom(StackTraceElement[] stes)
           
static String getStackTraceFrom(Throwable t)
           
static List<String> getStringFrom(Misc.NullCheckResult nullCheckResult, String... names)
          Return strings mapped to null values in a given @{link Misc.NullCheckResult. .
static String getUniqueId(String prefix)
          Generate a unique identifier prefixed by the given String.
static String identityToString(Object o)
           Return an "identity string" for a given object.
static
<T> T
lookup(String name, Properties environment)
           
static String prefixAllLines(String message, String prefix)
          Return a new string based on the given message string where all lines are prefixed by the given prefix.
static String primitiveComponentsToString(Object array)
           Smart toString() implementation of an array of primitive types.
static int random(int min, int max)
          Returns a random number between min and max value
static Exception reflectClose(Object o)
          Invoke the close() method on the given object.
static byte[] serialize(Serializable object)
           
static void slowLog(Level level, String msg, Object... args)
          Equivalent to {@link Misc#dynamicLog(int, Level, String, Object...)
static
<E extends Enum<E>>
E
stringToEnum(Class<E> c, String s)
           
static void unreachableStatement()
           
static void unreachableStatement(String reason)
           
static void warnIfEquals(Level level, Object a, Object b)
          This method logs at the given level a "warning" if the given parameter are equals
static void warnIfFalse(Level level, boolean valueToCheck, String variableName)
          This method logs at the given level a "warning" if the given parameter is false
static void warnIfNotEquals(Level level, Object a, Object b)
          This method logs at the given level a "warning" if the given parameter are not equals
static void warnIfNotNull(Level level, Object valueToCheck, String variableName)
          This method logs at the given level a "warning" if the given parameter is not null
static void warnIfNull(Level level, Object valueToCheck, String variableName)
          This method logs at the given level a "warning message" if the given parameter is null
static void warnIfTrue(Level level, boolean valueToCheck, String variableName)
          This method logs at the given level a "warning" if the given parameter is true
static void write(byte[] fileContent, File file)
           
static void write(String s, File f)
          Write the given String to the given file using the default encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RANDOM

public static final Random RANDOM
A RANDOM instance. Prevent creation of many instances.


LINE_SEPARATOR

public static final String LINE_SEPARATOR
The line separator as defined by the property line.separator.


TMP_DIR

public static final File TMP_DIR
The file that represents the temporary directory. Usually, it is /tmp on Unix platform and C:/TMP on MS Windows.


DAY

public static final long DAY
Number of milleseconds in a given day

See Also:
Constant Field Values
Method Detail

getUniqueId

public static String getUniqueId(String prefix)
Generate a unique identifier prefixed by the given String.

Parameters:
prefix - the prefix String
Returns:
an UUID String prefixed by prefix
See Also:
UUID

getHumanReadableId

public static String getHumanReadableId(String prefix)
Generates a human readable id prefixed by the given String. The generated identifier is unique only for the duration of the existence of this class.

Parameters:
prefix - the string prefiy
Returns:
a human readable id prefixed by prefix.

getHumanReadableId

public static long getHumanReadableId()
Generates a human readable id as a long. The generated identifier is unique only for the duration of the existence of this class.

Returns:
a unique id as a long.

random

public static int random(int min,
                         int max)
Returns a random number between min and max value

Parameters:
min - a positive integer
max - a positive integer
Returns:
a random number between min and max value
Throws:
IllegalArgumentException - if min >= max

getRandomString

public static String getRandomString(int size)
Generate a RANDOM String of the given size.

Parameters:
size - the size of the generated string
Returns:
the RANDOM String

formatDelay

public static final String formatDelay(long delay)

Format a delay.

This method returns a human readable string for delay such as the one used in benchmarks. This method is thread safe.

Parameters:
delay - a long value
Returns:
a String value

formatDelay

public static final String formatDelay(double delay)

Equivalent to formatDelay(long).

Parameters:
delay - a double value
Returns:
a String value

findAllTypes

public static Set<Class<?>> findAllTypes(Class<?> type)

Return the list of Class objects representing every types a given class implements.

Parameters:
type - a Class value
Returns:
a Set value

getGenericFullName

public static String getGenericFullName(Class<?> clazz)
Return the generic name of a given class. For example, given java.util.Map.class, it returns string java.util.Map

Parameters:
clazz - the class
Returns:
the generic name of a given class.

findAllSuperTypes

public static Set<Class<?>> findAllSuperTypes(Class<?> type)

Return the list of Class objects representing all super type a given class implements.

Parameters:
type - a Class value
Returns:
a Set value

findAllInterfaces

public static Set<Class<?>> findAllInterfaces(Class<?> type)

Return the list of Class objects representing all interfaces a given class implements.

Parameters:
type - a Class value
Returns:
a List value

findMethodClassArgs

public static Class<?>[] findMethodClassArgs(Class<?>[] subClasses,
                                             Class<?> classToTest,
                                             String methodName)
                                      throws NoSuchMethodException

Return the Class[] array representing the types a given method take as parameters.

Parameters:
subClasses - the classes which are to be subclasses of parameters
classToTest - the class which declares the given method
methodName - the method name
Returns:
the formal parameters class array
Throws:
NoSuchMethodException - if a method cannot be found.

findConstructorClassArgs

public static Class<?>[] findConstructorClassArgs(Class<?>[] subClasses,
                                                  Class<?> classToTest)
                                           throws NoSuchMethodException

Return the Class[] array representing the types a constructor take as parameters.

Parameters:
subClasses - the classes which are to be subclasses of parameters
classToTest - the class which declares the constructor
Returns:
the formal parameters class array
Throws:
NoSuchMethodException - if a constructor cannot be found

identityToString

public static String identityToString(Object o)

Return an "identity string" for a given object.

The string returned is:

Parameters:
o - the object to return the identity string of
Returns:
the identity string as defined above

componentsToString

public static String componentsToString(Object[] args,
                                        boolean deepToString)

Smart toString() implementation of arrays.

Parameters:
args - the array to return a smart string of.
Returns:
the smart string of the given array.

primitiveComponentsToString

public static String primitiveComponentsToString(Object array)

Smart toString() implementation of an array of primitive types.

Parameters:
array - the array to return a smart string of.
Returns:
the smart string of the given array.

getAllContentFrom

public static byte[] getAllContentFrom(File file)
                                throws IOException
Equivalent to getAllContentFrom(new FileInputStream(file));

Parameters:
file - the file to read
Returns:
the whole content of the file in a single String.
Throws:
IOException - If an I/O exception occured

getDirContentFrom

public static Map<String,byte[]> getDirContentFrom(File dir)
                                            throws IOException
Get all files in this directory. Files in sub-directories are added too.

Parameters:
dir -
Returns:
a map containing: relative path of the file (relative to dir) as key, content of file as value.
Throws:
IOException

getAllContentFrom

public static byte[] getAllContentFrom(InputSource source)
                                throws IOException
Equivalent to getAllContentFrom(source.getByteStream(source));

Parameters:
file - the file to read
Returns:
the whole content of the file in a single String.
Throws:
IOException - If an I/O exception occured

getAllContentFrom

public static byte[] getAllContentFrom(URL url)
                                throws IOException
Return the whole underlying stream content into a single String. Warning: the whole content of stream will be kept in memory!! Use with care!

Parameters:
in - the stream to read
Returns:
the whole content of the stream in a single String.
Throws:
IOException - if an I/O exception occured

getAllContentFrom

public static byte[] getAllContentFrom(InputStream in)
                                throws IOException
Return the whole underlying stream content into a single String. Warning: the whole content of stream will be kept in memory!! Use with care!

Parameters:
in - the stream to read
Returns:
the whole content of the stream in a single String.
Throws:
IOException - if an I/O exception occured

getAllContentFrom

public static byte[] getAllContentFrom(Reader in)
                                throws IOException
Return the whole underlying stream content into a single String. Warning: the whole content of stream will be kept in memory!! Use with care!

Parameters:
in - the stream to read
Returns:
the whole content of the stream in a single String.
Throws:
IOException - if an I/O exception occured

close

public static Exception close(Closeable closeable)
Equivalent to #reflectClose(closeable)


close

public static Exception close(XMLEncoder encoder)
Equivalent to {@link #reflectClose(encoder)


close

public static Exception close(XMLDecoder decoder)
Equivalent to {@link #reflectClose(decoder)


reflectClose

public static Exception reflectClose(Object o)
Invoke the close() method on the given object. This method uses the reflection API to find a close() method with no arguments. Any exception thrown (including NoSuchMethodException) will be both logged using LOG and returned. If the parameter is null, nothing is done and null is returned.

Parameters:
o - the object to call the close() method on.
Returns:
the exception thrown if any, null otherwise.

getPermissionsSize

public static int getPermissionsSize(Permissions permissions)
Parameters:
permissions -
Returns:

getMBeanProxy

public static <T> T getMBeanProxy(Class<T> mbeanInterface,
                                  String jmxServiceUrl,
                                  String jmxObjectName)
                       throws IOException,
                              MalformedObjectNameException,
                              InstanceNotFoundException,
                              MBeanException,
                              ReflectionException
Return a proxy implementing all the interfaces specified that forward method invocations to the specified MBean.

Type Parameters:
T -
Parameters:
mbeanInterface - the interface the proxy should implement (the MBean should obviously also implement that interface).
jmxServiceUrl - the JMX service URL
jmxObjectName - the name the MBean has been registered to
Returns:
a proxy implementing the specified interface and that forward method invocations to the specified MBean.
Throws:
IOException - for any IO problem
MalformedObjectNameException - for any JMX Naming problem
MBeanException - for any MBean problem
ReflectionException - for any problem related to reflection
InstanceNotFoundException

getChainOf

public static <T> T getChainOf(List<T> elements)
Return a proxy that forward void-method invocations to each object specified in the list elements. The invocation order follows the given list order.

Type Parameters:
T - the interface type of the returned proxy and of each elements.
Parameters:
elements - the elements to forward method invocations to
Returns:
the forwarding chaining proxy
See Also:
Chainer, InvocationHandler, Proxy

getLoggerProxyFor

public static <T> T getLoggerProxyFor(T target,
                                      Logger logger)
Return a proxy that log method invocations through the provided logger.

Type Parameters:
T - the target object type
Parameters:
target - the target object method invocations should be forwarded to
logger - the logger to use for logging
Returns:
a proxy that log method invocations through the provided logger.
See Also:
LoggingInvocationHandler, InvocationHandler, Proxy

findNull

public static Misc.NullCheckResult findNull(Object... params)
Find null parameters in the given list. This method returns a Misc.NullCheckResult.

Parameters:
params - the parameters to check
Returns:
a Misc.NullCheckResult representing null parameters.
See Also:
Misc.NullCheckResult

checkArgsNotNull

public static void checkArgsNotNull(Object... params)
Check that the given parameters are not null. This method should only be used to check that some parameters given to a given method are not null. The exception message tries its best to produce a helpful message by scanning the stack trace.

Parameters:
params - the parameters to check
Throws:
an - IllegalArgumentException if at least one of the parameters is null

checkArgsNotNull

public static void checkArgsNotNull(int offset,
                                    Object... params)
Check that the given parameters are not null. This method should only be used to check that some parameters given to a given method are not null. The exception message tries its best to produce a helpful message by scanning the stack trace.

Parameters:
offset - the offset to use in the stack trace to produce error message
params - the parameters to check
Throws:
an - IllegalArgumentException if at least one of the parameters is null

getCaller

public static StackTraceElement getCaller(int offset)
Return the StackTraceElement at the given offset from this method invocation.

Parameters:
offset -
Returns:
a StackTraceElement

getStringFrom

public static List<String> getStringFrom(Misc.NullCheckResult nullCheckResult,
                                         String... names)
Return strings mapped to null values in a given @{link Misc.NullCheckResult. . If the returned @{link List} of String is called l then, it verifies: l.contains(names[i]) if and only if nullCheckResult.isNull(i) returns true. Note that the number of String names given should be of the same size that the one used to get the given Misc.NullCheckResult using findNull(Object...). An IllegalArgumentException is thrown otherwise.

Parameters:
nullCheckResult - the result as returned by findNull(Object...)
names - the strings that should be mapped to null values
Returns:
a List of string mapped to the given Misc.NullCheckResult.
Throws:
IllegalArgumentException - if the number of given names is different that NullCheckResult#size()
See Also:
findNull(Object...), Misc.NullCheckResult

badStateIfNull

public static void badStateIfNull(Object valueToCheck,
                                  String msg)
This method throw an IllegalStateException if the given parameter is null

Parameters:
valueToCheck - the value to check
msg - the message for the thrown exception
See Also:
IllegalStateException

badStateIfNotNull

public static void badStateIfNotNull(Object valueToCheck,
                                     String msg)
This method throw an IllegalStateException if the given parameter is not null

Parameters:
valueToCheck - the value to check
msg - the message for the thrown exception
See Also:
IllegalStateException

badStateIfTrue

public static void badStateIfTrue(boolean valueToCheck,
                                  String msg)
This method throw an IllegalStateException if the given parameter is true

Parameters:
valueToCheck - the value to check
msg - the message for the thrown exception
See Also:
IllegalStateException

badStateIfFalse

public static void badStateIfFalse(boolean valueToCheck,
                                   String msg)
This method throw an IllegalStateException if the given parameter is false

Parameters:
valueToCheck - the value to check
msg - the message for the thrown exception
See Also:
IllegalStateException

badStateIfEquals

public static void badStateIfEquals(Object a,
                                    Object b,
                                    String msg)
This method throw an IllegalStateException if the given parameters are equals (using Object.equals(Object)

Parameters:
a - the first object
b - the second object
msg - the message for the thrown exception
See Also:
IllegalStateException

dynamicLog

public static void dynamicLog(int offset,
                              Level level,
                              String msg,
                              Object... args)
Log a message through the logger of the caller at the given offset in the stack trace. If A.f() calls B.g() that finally calls dynamicLog(1, msg) then, the msg will be logged with a code similar to:
Logger.getLogger(B.getClass().getName()).log(level, msg);
If the call was dynamicLog(2, msg) then, the code would be similar to:
Logger.getLogger(A.getClass().getName()).log(level, msg);
Note that even if the message construction (using String.format(String, Object...) is done only when Logger.isLoggable(Level) returns true for efficiency reasons, this method tries to find the caller (in order to get the Logger) using string comparisons in the caller stack trace.
Therefore, it might not be as efficient as one would like it to be! ;-) (Note that a profiler should always be used to determine potential bottlenecks! Remember the Pareto principle: 80% of the time in spent in only 20% of the code).
If you found (by profiling, as advised, or by just guessing as often ;-)) that this method is far too slow for your application, use the fastDynamicLog(int, Logger, Level, String, Object...) where the Logger is given directly.

Parameters:
offset - the offset in the stack trace where the caller should be found (to get the Logger)
level - the level to log the message to
msg - the string to use when calling String.format(String, Object...) in order to create the final message
args - the parameters to use when calling String.format(String, Object...) in order to create the final message

slowLog

public static void slowLog(Level level,
                           String msg,
                           Object... args)
Equivalent to Misc.dynamicLog(2, level, msg, args)

See Also:
dynamicLog(int, Level, String, Object...)

createLogRecord

public static LogRecord createLogRecord(Logger logger,
                                        Level level,
                                        String className,
                                        String methodName,
                                        String msg,
                                        Object... args)
This method creates a LogRecord and fills it with the given parameters

Parameters:
logger - the logger to set the LogRecord.setLoggerName(String) to.
level - the level to set the LogRecord.setLevel(Level) to.
className - the class name to set the LogRecord.setSourceClassName(String) to.
methodName - the method name to set the LogRecord.setSourceMethodName(String) to.
msg - the string to use when calling String.format(String, Object...) in order to create the message passed to LogRecord.setMessage(String).
args - the parameters to use when calling String.format(String, Object...) in order to create the message passed to LogRecord.setMessage(String).
Returns:
a LogRecord filled, ready to be logged.

fastDynamicLog

public static void fastDynamicLog(int offset,
                                  Logger logger,
                                  Level level,
                                  String msg,
                                  Object... args)
Log a message through the given logger for the caller at the given offset in the stack trace. This method is similar to dynamicLog(int, Level, String, Object...) except that it does not have to search for the Logger. Therefore, it can check directly the result of Logger.isLoggable(Level) preventing the call to getCaller(int) (used to get the class name and the method name of the caller) and to String.format(String, Object...) (to create the actual message) which may be time consuming.

Parameters:
offset - the offset in the stack trace where the caller should be found
level - the level to log the message to
msg - the string to use when calling String.format(String, Object...) in order to create the final message
args - the parameters to use when calling String.format(String, Object...) in order to create the final message

fastDynamicLog

public static void fastDynamicLog(Logger logger,
                                  Level level,
                                  String msg,
                                  Object... args)
Equivalent to Misc.fastDynamicLog(2, logger, level, msg, args)

See Also:
fastDynamicLog(int, Logger, Level, String, Object...)

warnIfNull

public static void warnIfNull(Level level,
                              Object valueToCheck,
                              String variableName)
This method logs at the given level a "warning message" if the given parameter is null

Parameters:
level - a log level
valueToCheck - the value to check
variableName - the variable name holding valueToCheck. Can be null.

warnIfNotNull

public static void warnIfNotNull(Level level,
                                 Object valueToCheck,
                                 String variableName)
This method logs at the given level a "warning" if the given parameter is not null

Parameters:
level - a log level
valueToCheck - the value to check
variableName - the variable name holding valueToCheck. Can be null.

warnIfTrue

public static void warnIfTrue(Level level,
                              boolean valueToCheck,
                              String variableName)
This method logs at the given level a "warning" if the given parameter is true

Parameters:
level - a log level
valueToCheck - the value to check
variableName - the variable name holding valueToCheck. Can be null.

warnIfFalse

public static void warnIfFalse(Level level,
                               boolean valueToCheck,
                               String variableName)
This method logs at the given level a "warning" if the given parameter is false

Parameters:
level - a log level
valueToCheck - the value to check
variableName - the variable name holding valueToCheck. Can be null.

warnIfEquals

public static void warnIfEquals(Level level,
                                Object a,
                                Object b)
This method logs at the given level a "warning" if the given parameter are equals

Parameters:
level - a log level
a - an object
b - another object

warnIfNotEquals

public static void warnIfNotEquals(Level level,
                                   Object a,
                                   Object b)
This method logs at the given level a "warning" if the given parameter are not equals

Parameters:
level - a log level
a - an object
b - another object

getCurrentThreadStackTrace

public static String getCurrentThreadStackTrace()

getStackTraceFrom

public static String getStackTraceFrom(Throwable t)

getStackTraceFrom

public static StringBuilder getStackTraceFrom(StackTraceElement[] stes)

deepToString

public static String deepToString(Object o)

deleteDir

public static boolean deleteDir(File dir)
Delete a given directory and all its contents (whether they are file or directory) WARNING: this method unconditionally modifies the underlying filesystem by erasing files and directories. Use with care!

Parameters:
dir - the directory to delete

unreachableStatement

public static void unreachableStatement()

unreachableStatement

public static void unreachableStatement(String reason)

stringToEnum

public static <E extends Enum<E>> E stringToEnum(Class<E> c,
                                                 String s)

serialize

public static byte[] serialize(Serializable object)
                        throws IOException,
                               ClassNotFoundException
Throws:
IOException
ClassNotFoundException

deserialize

public static Serializable deserialize(byte[] buf)
                                throws IOException,
                                       ClassNotFoundException
Throws:
IOException
ClassNotFoundException

checkReallySerializable

public static <T extends Serializable> Exception checkReallySerializable(T object)
Check that the given object is actually serializable. Implementation tries to serialize and deserialize the given object (in memory). Note that null is returned when the given object is serializable. It returns the exception thrown by either the serialization or the deserialization process. This means, that the object is not really serializable. Therefore the pattern for checking should be written like:
final Exception error = checkReallySerializable(myObject); if (error == null) { // success } else { // failure, do something with error... error.printStackTrace(); }

Type Parameters:
T - A type (usually extending Serializable
Parameters:
object - the object to check
Returns:
null on success, the exception thrown on error

write

public static void write(String s,
                         File f)
                  throws IOException
Write the given String to the given file using the default encoding.

Parameters:
s - the string to be written
f - the file to write the given string to
Throws:
IOException - if an IO error is encoutered (file not found, read-only file, and so on).

write

public static void write(byte[] fileContent,
                         File file)
                  throws IOException
Throws:
IOException

createTempFile

public static File createTempFile(String prefix,
                                  String suffix,
                                  File directory)
                           throws IOException
Perform java.io.File.createTempFile with retries when it fail (limit of 10 retries) (Use to by-pass bug #6325169 on SUN JDK 1.5 on windows) Sames parameter as File.createTempFile method

Parameters:
prefix - Prefix of the file
suffix - Suffix of the file
directory - Target directory
Returns:
An abstract pathname denoting a newly-created empty file
Throws:
IOException - If a file could not be created

generateJar

public static byte[] generateJar(Class<?>... classes)
                          throws IOException
Throws:
IOException

getResources

public static Map<String,byte[]> getResources(Class<?>... classes)
                                       throws IOException
Throws:
IOException

generateJar

public static byte[] generateJar(Map<String,byte[]> resources)
                          throws IOException
Generates Jar from resources

Parameters:
resources: - map of resources: key is the file name, value the content.
Returns:
content of generated jar.
Throws:
IOException

getResourcesFromZip

public static Map<String,byte[]> getResourcesFromZip(byte[] zipContent)
                                              throws IOException
Throws:
IOException

lookup

public static <T> T lookup(String name,
                           Properties environment)
                throws NamingException
Throws:
NamingException

prefixAllLines

public static String prefixAllLines(String message,
                                    String prefix)
Return a new string based on the given message string where all lines are prefixed by the given prefix.

Parameters:
message - the message to transform
prefix - the prefix to use
Returns:
the prefixed string


Copyright © 2012 OW2 Consortium. All Rights Reserved.