| java.lang.Object org.jgroups.util.Util
Util | public class Util (Code) | | Collection of various utility routines that can not be assigned to other classes.
author: Bela Ban version: $Id: Util.java,v 1.105.2.3 2007/04/12 20:24:45 bstansberry Exp $ |
Method Summary | |
public static boolean | acquire(Sync sync) Attempts to acquire the given Sync , suppressing any
InterruptedException . | public static String | activeThreads() | public static boolean | all(Collection c, Object obj) | public static String | array2String(long[] array) | public static String | array2String(int[] array) | public static String | array2String(boolean[] array) | public static String | array2String(Object[] array) | public static LinkedList | byteBufferToMessageList(byte[] buffer, int offset, int length) | public static void | checkBufferSize(String buf_name, long val) | public static boolean | checkForLinux() | public static boolean | checkForSolaris() | public static boolean | checkForWindows() | public static void | close(InputStream inp) | public static void | close(OutputStream out) | public static void | close(Socket s) | public static void | close(DatagramSocket my_sock) | public static byte[] | collectionToByteBuffer(Collection c) | public static java.util.List | computeFragOffsets(int offset, int length, int frag_size) Given a buffer and a fragmentation size, compute a list of fragmentation offset/length pairs, and
return them in a list. | public static java.util.List | computeFragOffsets(byte[] buf, int frag_size) | public static void | crash() | public static DatagramSocket | createDatagramSocket(InetAddress addr, int port) Creates a DatagramSocket bound to addr. | public static ServerSocket | createServerSocket(int start_port) | public static ServerSocket | createServerSocket(InetAddress bind_addr, int start_port) | public static byte[] | defragmentBuffer(byte[] fragments) Concatenates smaller fragments into entire buffers. | public static Vector | determineLeftMembers(Vector old_mbrs, Vector new_mbrs) Returns all members that left between 2 views. | public static void | doubleWrite(byte[] buf, OutputStream out) Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). | public static void | doubleWrite(byte[] buf, int offset, int length, OutputStream out) Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). | public static String | dumpQueue(Queue q) Debugging method used to dump the content of a protocol queue in a condensed form. | public static void | dumpStack(boolean exit) | public static boolean | fileExists(String fname) | public static byte[][] | fragmentBuffer(byte[] buf, int frag_size, int length) Fragments a byte buffer into smaller fragments of (max.) frag_size. | public static byte[][] | fragmentBuffer(byte[] buf, int frag_size) | public static String | generateList(Collection c, String separator) | public static List | getAllAvailableInterfaces() | public static InetAddress | getFirstNonLoopbackAddress() | public static InetAddress | getFirstNonLoopbackIPv6Address() | public static ThreadGroup | getGlobalThreadGroup() | public static String | getHostname() | public static int | getJavaVersion() | public static MBeanServer | getMBeanServer() | public static String | getProperty(String[] system_props, Properties props, String prop_name, boolean ignore_sysprops, String default_value) | public static InputStream | getResourceAsStream(String name, Class clazz) | public static String | getStackTrace(Throwable t) | public static boolean | isBindAddressPropertyIgnored() | public static Class | loadClass(String classname, Class clazz) Tries to load the class from the current thread's context class loader. | public static void | lock(Sync sync) Acquires the given Sync , but will not throw an
InterruptedException. | public static void | main(String args) | public static boolean | match(Object obj1, Object obj2) | public static boolean | match(long[] a1, long[] a2) | public static String | memStats(boolean gc) | public static Buffer | msgListToByteBuffer(LinkedList xmit_list) Marshalls a list of messages. | public static Object | objectFromByteBuffer(byte[] buffer) | public static Object | objectFromByteBuffer(byte[] buffer, int offset, int length) | public static byte[] | objectToByteBuffer(Object obj) Serializes/Streams an object into a byte buffer.
The object has to implement interface Serializable or Externalizable
or Streamable. | public static Object | oldObjectFromByteBuffer(byte[] buffer) | public static Object | oldObjectFromByteBuffer(byte[] buffer, int offset, int length) | public static byte[] | oldObjectToByteBuffer(Object obj) Serializes/Streams an object into a byte buffer.
The object has to implement interface Serializable or Externalizable
or Streamable. | public static InetAddress | parseBindAddress(Properties props, String property) | public static boolean | parseBoolean(Properties props, String property, boolean defaultValue) | public static long[] | parseCommaDelimitedLongs(String s) Parses comma-delimited longs; e.g., 2000,4000,8000. | public static java.util.List | parseCommaDelimitedStrings(String l) e.g. | public static int | parseInt(Properties props, String property, int defaultValue) | public static long | parseLong(Properties props, String property, long defaultValue) | public static List | parseStringList(String l, String separator) | public static Object | pickRandomElement(Vector list) | public static Vector | pickSubset(Vector members, double subset_percentage) Selects a random subset of members according to subset_percentage and returns them.
Picks no member twice from the same membership. | public static String | print(Throwable t) | public static String | printBytes(long bytes) | public static String | printBytes(double bytes) | public static String | printEvent(Event evt) | public static void | printFragments(byte[] frags) | public static String | printMembers(Vector v) | public static String | printMessage(Message msg) | public static String | printMethodCall(Message msg) Tries to read a MethodCall object from the message's buffer and prints it. | public static String | printStackTrace(Throwable t) | public static void | printThreads() | public static void | prompt(String s) | public static long | random(long range) | public static Address | readAddress(DataInputStream in) | public static Collection | readAddresses(DataInputStream in, Class cl) Parameters: in - Parameters: cl - The type of Collection, e.g. | public static AuthToken | readAuthToken(DataInputStream in) | public static byte[] | readByteBuffer(DataInputStream in) | public static Streamable | readGenericStreamable(DataInputStream in) | public static Object | readObject(DataInputStream in) | public static Streamable | readStreamable(Class clazz, DataInputStream in) | public static String | readString(DataInputStream in) | public static void | release(Sync sync) Releases the given Sync , suppressing any Throwable. | public static boolean | sameHost(Address one, Address two) | public static String | shortName(String hostname) | public static String | shortName(InetAddress hostname) | public static int | size(Address addr) | public static long | size(Collection addrs) Returns the marshalled size of a Collection of Addresses. | public static long | sizeOf(String classname) | public static long | sizeOf(Object inst) | public static long | sizeOf(Streamable inst) | public static void | sleep(long timeout) Sleep for timeout msecs. | public static void | sleep(long timeout, int nanos) | public static void | sleep(long msecs, boolean busy_sleep) On most UNIX systems, the minimum sleep time is 10-20ms. | public static void | sleepRandom(long timeout) Sleeps between 1 and timeout milliseconds, chosen randomly. | public static Streamable | streamableFromByteBuffer(Class cl, byte[] buffer) | public static Streamable | streamableFromByteBuffer(Class cl, byte[] buffer, int offset, int length) | public static byte[] | streamableToByteBuffer(Streamable obj) | public static String | substituteVariable(String val) Replaces variables of ${var:default} with System.getProperty(var, default). | public static boolean | tossWeightedCoin(double probability) Tosses a coin weighted with probability and returns true or false. | public static void | unlock(Sync sync) Releases the given Sync , suppressing any Throwable. | public static Vector | unmodifiableVector(Vector v) | public static void | writeAddress(Address addr, DataOutputStream out) | public static void | writeAddresses(Collection v, DataOutputStream out) Writes a Vector of Addresses. | public static void | writeAuthToken(AuthToken token, DataOutputStream out) | public static void | writeByteBuffer(byte[] buf, DataOutputStream out) | public static void | writeFully(ByteBuffer buf, WritableByteChannel out) if we were to register for OP_WRITE and send the remaining data on
readyOps for this channel we have to either block the caller thread or
queue the message buffers that may arrive while waiting for OP_WRITE. | public static void | writeGenericStreamable(Streamable obj, DataOutputStream out) | public static void | writeObject(Object obj, DataOutputStream out) | public static void | writeStreamable(Streamable obj, DataOutputStream out) | public static void | writeString(String s, DataOutputStream out) |
JGROUPS_COMPAT | static boolean JGROUPS_COMPAT(Code) | | |
MAX_PORT | final public static int MAX_PORT(Code) | | |
resolve_dns | static boolean resolve_dns(Code) | | |
acquire | public static boolean acquire(Sync sync)(Code) | | Attempts to acquire the given Sync , suppressing any
InterruptedException .
Parameters: sync - the Sync true if the Sync was acquired, false if an InterruptedException was caught and suppressed. |
array2String | public static String array2String(long[] array)(Code) | | |
array2String | public static String array2String(int[] array)(Code) | | |
array2String | public static String array2String(boolean[] array)(Code) | | |
byteBufferToMessageList | public static LinkedList byteBufferToMessageList(byte[] buffer, int offset, int length) throws Exception(Code) | | |
checkBufferSize | public static void checkBufferSize(String buf_name, long val)(Code) | | Verifies that val is <= max memory
Parameters: buf_name - Parameters: val - |
checkForLinux | public static boolean checkForLinux()(Code) | | |
checkForSolaris | public static boolean checkForSolaris()(Code) | | |
checkForWindows | public static boolean checkForWindows()(Code) | | |
computeFragOffsets | public static java.util.List computeFragOffsets(int offset, int length, int frag_size)(Code) | | Given a buffer and a fragmentation size, compute a list of fragmentation offset/length pairs, and
return them in a list. Example:
Buffer is 10 bytes, frag_size is 4 bytes. Return value will be ({0,4}, {4,4}, {8,2}).
This is a total of 3 fragments: the first fragment starts at 0, and has a length of 4 bytes, the second fragment
starts at offset 4 and has a length of 4 bytes, and the last fragment starts at offset 8 and has a length
of 2 bytes.
Parameters: frag_size - List. A List of offset/length pairs |
crash | public static void crash()(Code) | | |
createDatagramSocket | public static DatagramSocket createDatagramSocket(InetAddress addr, int port) throws Exception(Code) | | Creates a DatagramSocket bound to addr. If addr is null, socket won't be bound. If address is already in use,
start_port will be incremented until a socket can be created.
Parameters: addr - The InetAddress to which the socket should be bound. If null, the socket will not be bound. Parameters: port - The port which the socket should use. If 0, a random port will be used. If > 0, but port is alreadyin use, it will be incremented until an unused port is found, or until MAX_PORT is reached. |
createServerSocket | public static ServerSocket createServerSocket(int start_port)(Code) | | Finds first available port starting at start_port and returns server socket
|
defragmentBuffer | public static byte[] defragmentBuffer(byte[] fragments)(Code) | | Concatenates smaller fragments into entire buffers.
Parameters: fragments - An array of byte buffers (byte[] ) A byte buffer |
determineLeftMembers | public static Vector determineLeftMembers(Vector old_mbrs, Vector new_mbrs)(Code) | | Returns all members that left between 2 views. All members that are element of old_mbrs but not element of
new_mbrs are returned.
|
doubleWrite | public static void doubleWrite(byte[] buf, OutputStream out) throws Exception(Code) | | Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). Two writes ensure that, if the peer closed the connection, the first write
will send the peer from FIN to RST state, and the second will cause a signal (IOException).
|
doubleWrite | public static void doubleWrite(byte[] buf, int offset, int length, OutputStream out) throws Exception(Code) | | Makes sure that we detect when a peer connection is in the closed state (not closed while we send data,
but before we send data). Two writes ensure that, if the peer closed the connection, the first write
will send the peer from FIN to RST state, and the second will cause a signal (IOException).
|
dumpQueue | public static String dumpQueue(Queue q)(Code) | | Debugging method used to dump the content of a protocol queue in a condensed form. Useful
to follow the evolution of the queue's content in time.
|
dumpStack | public static void dumpStack(boolean exit)(Code) | | |
fileExists | public static boolean fileExists(String fname)(Code) | | |
fragmentBuffer | public static byte[][] fragmentBuffer(byte[] buf, int frag_size, int length)(Code) | | Fragments a byte buffer into smaller fragments of (max.) frag_size.
Example: a byte buffer of 1024 bytes and a frag_size of 248 gives 4 fragments
of 248 bytes each and 1 fragment of 32 bytes.
An array of byte buffers (byte[] ). |
fragmentBuffer | public static byte[][] fragmentBuffer(byte[] buf, int frag_size)(Code) | | |
getJavaVersion | public static int getJavaVersion()(Code) | | |
getProperty | public static String getProperty(String[] system_props, Properties props, String prop_name, boolean ignore_sysprops, String default_value)(Code) | | Returns a value associated wither with one or more system properties, or found in the props map
Parameters: system_props - Parameters: props - List of properties read from the configuration file Parameters: prop_name - The name of the property, will be removed from props if found Parameters: ignore_sysprops - If true, system properties are not used and the values will only be retrieved fromprops (not system_props) Parameters: default_value - Used to return a default value if the properties or system properties didn't have the value The value, or null if not found |
isBindAddressPropertyIgnored | public static boolean isBindAddressPropertyIgnored()(Code) | | |
loadClass | public static Class loadClass(String classname, Class clazz) throws ClassNotFoundException(Code) | | Tries to load the class from the current thread's context class loader. If
not successful, tries to load the class from the current instance.
Parameters: classname - Desired class. Parameters: clazz - Class object used to obtain a class loaderif no context class loader is available. Class, or null on failure. |
lock | public static void lock(Sync sync)(Code) | | Acquires the given Sync , but will not throw an
InterruptedException. If an InterruptedException is thrown while
waiting to acquire, it will be ignored, and another attempt will be
made to acquire, continuing until successful. If any InterruptedException
is caught and ignored, before returning
Thread.interrupt will
be invoked, allowing the caller to be aware of the interruption.
Parameters: sync - |
match | public static boolean match(long[] a1, long[] a2)(Code) | | |
objectFromByteBuffer | public static Object objectFromByteBuffer(byte[] buffer) throws Exception(Code) | | Creates an object from a byte buffer
|
objectFromByteBuffer | public static Object objectFromByteBuffer(byte[] buffer, int offset, int length) throws Exception(Code) | | |
objectToByteBuffer | public static byte[] objectToByteBuffer(Object obj) throws Exception(Code) | | Serializes/Streams an object into a byte buffer.
The object has to implement interface Serializable or Externalizable
or Streamable. Only Streamable objects are interoperable w/ jgroups-me
|
oldObjectFromByteBuffer | public static Object oldObjectFromByteBuffer(byte[] buffer) throws Exception(Code) | | For backward compatibility in JBoss 4.0.2
|
oldObjectFromByteBuffer | public static Object oldObjectFromByteBuffer(byte[] buffer, int offset, int length) throws Exception(Code) | | |
oldObjectToByteBuffer | public static byte[] oldObjectToByteBuffer(Object obj) throws Exception(Code) | | Serializes/Streams an object into a byte buffer.
The object has to implement interface Serializable or Externalizable
or Streamable. Only Streamable objects are interoperable w/ jgroups-me
|
parseCommaDelimitedLongs | public static long[] parseCommaDelimitedLongs(String s)(Code) | | Parses comma-delimited longs; e.g., 2000,4000,8000.
Returns array of long, or null.
|
parseCommaDelimitedStrings | public static java.util.List parseCommaDelimitedStrings(String l)(Code) | | e.g. "bela,jeannette,michelle" --> List{"bela", "jeannette", "michelle"}
|
pickSubset | public static Vector pickSubset(Vector members, double subset_percentage)(Code) | | Selects a random subset of members according to subset_percentage and returns them.
Picks no member twice from the same membership. If the percentage is smaller than 1 -> picks 1 member.
|
printBytes | public static String printBytes(double bytes)(Code) | | |
printFragments | public static void printFragments(byte[] frags)(Code) | | |
printMessage | public static String printMessage(Message msg)(Code) | | Tries to read an object from the message's buffer and prints it
|
printMethodCall | public static String printMethodCall(Message msg)(Code) | | Tries to read a MethodCall object from the message's buffer and prints it.
Returns empty string if object is not a method call
|
printStackTrace | public static String printStackTrace(Throwable t)(Code) | | Use with caution: lots of overhead
|
printThreads | public static void printThreads()(Code) | | |
random | public static long random(long range)(Code) | | Returns a random value in the range [1 - range]
|
release | public static void release(Sync sync)(Code) | | Releases the given Sync , suppressing any Throwable.
Parameters: sync - the Sync |
sameHost | public static boolean sameHost(Address one, Address two)(Code) | | Checks whether 2 Addresses are on the same host
|
size | public static long size(Collection addrs)(Code) | | Returns the marshalled size of a Collection of Addresses.
Assumes elements are of the same type !
Parameters: addrs - Collection long size |
sleep | public static void sleep(long timeout)(Code) | | Sleep for timeout msecs. Returns when timeout has elapsed or thread was interrupted
|
sleep | public static void sleep(long timeout, int nanos)(Code) | | |
sleep | public static void sleep(long msecs, boolean busy_sleep)(Code) | | On most UNIX systems, the minimum sleep time is 10-20ms. Even if we specify sleep(1), the thread will
sleep for at least 10-20ms. On Windows, sleep() seems to be implemented as a busy sleep, that is the
thread never relinquishes control and therefore the sleep(x) is exactly x ms long.
|
sleepRandom | public static void sleepRandom(long timeout)(Code) | | Sleeps between 1 and timeout milliseconds, chosen randomly. Timeout must be > 1
|
substituteVariable | public static String substituteVariable(String val)(Code) | | Replaces variables of ${var:default} with System.getProperty(var, default). If no variables are found, returns
the same string, otherwise a copy of the string with variables substituted
Parameters: val - A string with vars replaced, or the same string if no vars found |
tossWeightedCoin | public static boolean tossWeightedCoin(double probability)(Code) | | Tosses a coin weighted with probability and returns true or false. Example: if probability=0.8,
chances are that in 80% of all cases, true will be returned and false in 20%.
|
unlock | public static void unlock(Sync sync)(Code) | | Releases the given Sync , suppressing any Throwable.
Same as @{link
Util.release(Sync) }; just included as a logically
named counterpart to
Util.lock(Sync) .
Parameters: sync - the Sync |
writeFully | public static void writeFully(ByteBuffer buf, WritableByteChannel out) throws IOException(Code) | | if we were to register for OP_WRITE and send the remaining data on
readyOps for this channel we have to either block the caller thread or
queue the message buffers that may arrive while waiting for OP_WRITE.
Instead of the above approach this method will continuously write to the
channel until the buffer sent fully.
|
|
|