Package org.jgroups.util
Class Util
- java.lang.Object
-
- org.jgroups.util.Util
-
public class Util extends java.lang.Object
Collection of various utility routines that can not be assigned to other classes.- Version:
- $Id: Util.java,v 1.137.2.11.2.2 2009/04/07 09:52:40 belaban Exp $
- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_PORT
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
activeThreads()
static boolean
all(java.util.Collection c, java.lang.Object obj)
Returns true if all elements of c match objstatic java.lang.String
array2String(boolean[] array)
static java.lang.String
array2String(int[] array)
static java.lang.String
array2String(long[] array)
static java.lang.String
array2String(short[] array)
static java.lang.String
array2String(java.lang.Object[] array)
static Message
byteBufferToMessage(byte[] buffer, int offset, int length)
static java.util.List<Message>
byteBufferToMessageList(byte[] buffer, int offset, int length)
static void
checkBufferSize(java.lang.String buf_name, long val)
Verifies that val is <= max memorystatic boolean
checkForLinux()
static boolean
checkForSolaris()
static boolean
checkForWindows()
static void
close(java.io.InputStream inp)
static void
close(java.io.OutputStream out)
static void
close(java.net.DatagramSocket my_sock)
static void
close(java.net.Socket s)
static void
close(Channel ch)
static void
close(Channel... channels)
static byte[]
collectionToByteBuffer(java.util.Collection c)
static java.util.List
computeFragOffsets(byte[] buf, int frag_size)
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.static void
crash()
static java.net.DatagramSocket
createDatagramSocket(java.net.InetAddress addr, int port)
Creates a DatagramSocket bound to addr.static java.net.MulticastSocket
createMulticastSocket(int port)
static java.net.MulticastSocket
createMulticastSocket(java.net.InetAddress mcast_addr, int port, org.apache.commons.logging.Log log)
static java.net.ServerSocket
createServerSocket(int start_port)
Finds first available port starting at start_port and returns server socketstatic java.net.ServerSocket
createServerSocket(java.net.InetAddress bind_addr, int start_port)
static View
createView(Address coord, long id, Address... members)
static byte[]
defragmentBuffer(byte[][] fragments)
Concatenates smaller fragments into entire buffers.static java.util.Vector<Address>
determineLeftMembers(java.util.Vector<Address> old_mbrs, java.util.Vector<Address> new_mbrs)
Returns all members that left between 2 views.static void
doubleWrite(byte[] buf, int offset, int length, java.io.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).static void
doubleWrite(byte[] buf, java.io.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).static java.lang.String
dumpQueue(Queue q)
Debugging method used to dump the content of a protocol queue in a condensed form.static void
dumpStack(boolean exit)
static java.lang.String
dumpThreads()
static boolean
fileExists(java.lang.String fname)
static byte[][]
fragmentBuffer(byte[] buf, int frag_size)
static byte[][]
fragmentBuffer(byte[] buf, int frag_size, int length)
Fragments a byte buffer into smaller fragments of (max.) frag_size.static java.lang.String
generateList(java.util.Collection c, java.lang.String separator)
static java.util.List<java.net.NetworkInterface>
getAllAvailableInterfaces()
static java.net.InetAddress
getBindAddress(java.util.Properties props)
Returns the address of the interface to use defined by bind_addr and bind_interfacestatic java.net.InetAddress
getFirstNonLoopbackAddress()
static java.net.InetAddress
getFirstNonLoopbackIPv6Address()
static java.lang.ThreadGroup
getGlobalThreadGroup()
static java.lang.String
getHostname()
static int
getJavaVersion()
static javax.management.MBeanServer
getMBeanServer()
static java.lang.String
getProperty(java.lang.String[] system_props, java.util.Properties props, java.lang.String prop_name, boolean ignore_sysprops, java.lang.String default_value)
Returns a value associated wither with one or more system properties, or found in the props mapstatic java.lang.String
getProperty(Protocol prot, java.lang.String prop_name)
static java.io.InputStream
getResourceAsStream(java.lang.String name, java.lang.Class clazz)
static java.lang.String
getStackTrace(java.lang.Throwable t)
static boolean
interruptAndWaitToDie(java.lang.Thread t)
static boolean
interruptAndWaitToDie(java.lang.Thread t, long timeout)
static boolean
isBindAddressPropertyIgnored()
static boolean
isCoordinator(JChannel ch)
static int
keyPress(java.lang.String msg)
static java.lang.Class
loadClass(java.lang.String classname, java.lang.Class clazz)
Tries to load the class from the current thread's context class loader.static void
main(java.lang.String[] args)
static java.lang.String
mapToString(java.util.Map<? extends java.lang.Object,? extends java.lang.Object> map)
static boolean
match(long[] a1, long[] a2)
static boolean
match(java.lang.Object obj1, java.lang.Object obj2)
static java.lang.String
md5(java.lang.String source)
Converts a java.lang.String in to a MD5 hashed Stringstatic java.lang.String
memStats(boolean gc)
static Buffer
messageToByteBuffer(Message msg)
static Buffer
msgListToByteBuffer(java.util.List<Message> xmit_list)
Marshalls a list of messages.static java.lang.Object
objectFromByteBuffer(byte[] buffer)
Creates an object from a byte bufferstatic java.lang.Object
objectFromByteBuffer(byte[] buffer, int offset, int length)
static byte[]
objectToByteBuffer(java.lang.Object obj)
Serializes/Streams an object into a byte buffer.static java.lang.Object
oldObjectFromByteBuffer(byte[] buffer)
For backward compatibility in JBoss 4.0.2static java.lang.Object
oldObjectFromByteBuffer(byte[] buffer, int offset, int length)
static byte[]
oldObjectToByteBuffer(java.lang.Object obj)
Serializes/Streams an object into a byte buffer.static java.net.InetAddress
parseBindAddress(java.util.Properties props, java.lang.String property)
static boolean
parseBoolean(java.util.Properties props, java.lang.String property, boolean defaultValue)
static long[]
parseCommaDelimitedLongs(java.lang.String s)
Parses comma-delimited longs; e.g., 2000,4000,8000.static java.util.List<java.lang.String>
parseCommaDelimitedStrings(java.lang.String l)
e.g.static int
parseInt(java.util.Properties props, java.lang.String property, int defaultValue)
static java.util.List<java.net.NetworkInterface>
parseInterfaceList(java.lang.String s)
static long
parseLong(java.util.Properties props, java.lang.String property, long defaultValue)
static java.util.List<java.lang.String>
parseStringList(java.lang.String l, java.lang.String separator)
static java.lang.Object
pickRandomElement(java.lang.Object[] array)
static java.lang.Object
pickRandomElement(java.util.List list)
static java.util.Vector
pickSubset(java.util.Vector members, double subset_percentage)
Selects a random subset of members according to subset_percentage and returns them.static java.lang.String
print(java.lang.Throwable t)
static java.lang.String
print(java.util.List<java.net.NetworkInterface> interfaces)
static java.lang.String
printBytes(double bytes)
static java.lang.String
printBytes(long bytes)
static java.lang.String
printEvent(Event evt)
static void
printFragments(byte[][] frags)
static <T> java.lang.String
printListWithDelimiter(java.util.Collection<T> list, java.lang.String delimiter)
static java.lang.String
printMembers(java.util.Vector v)
static java.lang.String
printMessage(Message msg)
Tries to read an object from the message's buffer and prints itstatic java.lang.String
printMethodCall(Message msg)
Tries to read aMethodCall
object from the message's buffer and prints it.static java.lang.String
printPingRsps(java.util.List<PingRsp> rsps)
static java.lang.String
printStackTrace(java.lang.Throwable t)
Use with caution: lots of overheadstatic void
printThreads()
static void
prompt(java.lang.String s)
static long
random(long range)
Returns a random value in the range [1 - range]static Address
readAddress(java.io.DataInputStream in)
static java.util.Collection<Address>
readAddresses(java.io.DataInputStream in, java.lang.Class cl)
static AuthToken
readAuthToken(java.io.DataInputStream in)
static byte[]
readByteBuffer(java.io.DataInputStream in)
static Streamable
readGenericStreamable(java.io.DataInputStream in)
static java.lang.Object
readObject(java.io.DataInputStream in)
static Streamable
readStreamable(java.lang.Class clazz, java.io.DataInputStream in)
static java.lang.String
readString(java.io.DataInputStream in)
static boolean
sameHost(Address one, Address two)
Checks whether 2 Addresses are on the same hoststatic java.lang.String
sha(java.lang.String source)
Converts a java.lang.String in to a SHA hashed Stringstatic java.lang.String
shortName(java.lang.String hostname)
static java.lang.String
shortName(java.net.InetAddress hostname)
static long
size(java.util.Collection addrs)
Returns the marshalled size of a Collection of Addresses.static int
size(Address addr)
static long
sizeOf(java.lang.Object inst)
static long
sizeOf(java.lang.String classname)
static int
sizeOf(Streamable inst)
static void
sleep(long timeout)
Sleep for timeout msecs.static void
sleep(long msecs, boolean busy_sleep)
On most UNIX systems, the minimum sleep time is 10-20ms.static void
sleep(long timeout, int nanos)
static void
sleepRandom(long timeout)
Sleeps between 1 and timeout milliseconds, chosen randomly.static void
sleepRandom(long floor, long ceiling)
Sleeps between floor and ceiling milliseconds, chosen randomlystatic boolean
startFlush(Channel c)
static boolean
startFlush(Channel c, int numberOfAttempts, long randomSleepTimeoutFloor, long randomSleepTimeoutCeiling)
static boolean
startFlush(Channel c, java.util.List<Address> flushParticipants)
static boolean
startFlush(Channel c, java.util.List<Address> flushParticipants, int numberOfAttempts, long randomSleepTimeoutFloor, long randomSleepTimeoutCeiling)
static Streamable
streamableFromByteBuffer(java.lang.Class cl, byte[] buffer)
static Streamable
streamableFromByteBuffer(java.lang.Class cl, byte[] buffer, int offset, int length)
static byte[]
streamableToByteBuffer(Streamable obj)
static java.lang.String
substituteVariable(java.lang.String val)
Replaces variables of ${var:default} with System.getProperty(var, default).static boolean
tossWeightedCoin(double probability)
Tosses a coin weighted with probability and returns true or false.static <T> java.util.Vector<T>
unmodifiableVector(java.util.Vector<? extends T> v)
static void
writeAddress(Address addr, java.io.DataOutputStream out)
static void
writeAddresses(java.util.Collection v, java.io.DataOutputStream out)
Writes a Vector of Addresses.static void
writeAuthToken(AuthToken token, java.io.DataOutputStream out)
static void
writeByteBuffer(byte[] buf, java.io.DataOutputStream out)
static void
writeFully(java.nio.ByteBuffer buf, java.nio.channels.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.static void
writeGenericStreamable(Streamable obj, java.io.DataOutputStream out)
static void
writeObject(java.lang.Object obj, java.io.DataOutputStream out)
static void
writeStreamable(Streamable obj, java.io.DataOutputStream out)
static void
writeString(java.lang.String s, java.io.DataOutputStream out)
-
-
-
Field Detail
-
MAX_PORT
public static final int MAX_PORT
- See Also:
- Constant Field Values
-
-
Method Detail
-
getGlobalThreadGroup
public static java.lang.ThreadGroup getGlobalThreadGroup()
-
checkBufferSize
public static void checkBufferSize(java.lang.String buf_name, long val)
Verifies that val is <= max memory- Parameters:
buf_name
-val
-
-
keyPress
public static int keyPress(java.lang.String msg)
-
close
public static void close(java.io.InputStream inp)
-
close
public static void close(java.io.OutputStream out)
-
close
public static void close(java.net.Socket s)
-
close
public static void close(java.net.DatagramSocket my_sock)
-
close
public static void close(Channel ch)
-
close
public static void close(Channel... channels)
-
objectFromByteBuffer
public static java.lang.Object objectFromByteBuffer(byte[] buffer) throws java.lang.Exception
Creates an object from a byte buffer- Throws:
java.lang.Exception
-
objectFromByteBuffer
public static java.lang.Object objectFromByteBuffer(byte[] buffer, int offset, int length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
objectToByteBuffer
public static byte[] objectToByteBuffer(java.lang.Object obj) throws java.lang.Exception
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- Throws:
java.lang.Exception
-
oldObjectFromByteBuffer
public static java.lang.Object oldObjectFromByteBuffer(byte[] buffer) throws java.lang.Exception
For backward compatibility in JBoss 4.0.2- Throws:
java.lang.Exception
-
oldObjectFromByteBuffer
public static java.lang.Object oldObjectFromByteBuffer(byte[] buffer, int offset, int length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
oldObjectToByteBuffer
public static byte[] oldObjectToByteBuffer(java.lang.Object obj) throws java.lang.Exception
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- Throws:
java.lang.Exception
-
streamableFromByteBuffer
public static Streamable streamableFromByteBuffer(java.lang.Class cl, byte[] buffer) throws java.lang.Exception
- Throws:
java.lang.Exception
-
streamableFromByteBuffer
public static Streamable streamableFromByteBuffer(java.lang.Class cl, byte[] buffer, int offset, int length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
streamableToByteBuffer
public static byte[] streamableToByteBuffer(Streamable obj) throws java.lang.Exception
- Throws:
java.lang.Exception
-
collectionToByteBuffer
public static byte[] collectionToByteBuffer(java.util.Collection c) throws java.lang.Exception
- Throws:
java.lang.Exception
-
size
public static int size(Address addr)
-
writeAuthToken
public static void writeAuthToken(AuthToken token, java.io.DataOutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
readAuthToken
public static AuthToken readAuthToken(java.io.DataInputStream in) throws java.io.IOException, java.lang.IllegalAccessException, java.lang.InstantiationException
- Throws:
java.io.IOException
java.lang.IllegalAccessException
java.lang.InstantiationException
-
writeAddress
public static void writeAddress(Address addr, java.io.DataOutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
readAddress
public static Address readAddress(java.io.DataInputStream in) throws java.io.IOException, java.lang.IllegalAccessException, java.lang.InstantiationException
- Throws:
java.io.IOException
java.lang.IllegalAccessException
java.lang.InstantiationException
-
writeAddresses
public static void writeAddresses(java.util.Collection v, java.io.DataOutputStream out) throws java.io.IOException
Writes a Vector of Addresses. Can contain 65K addresses at most- Parameters:
v
- A Collectionout
-- Throws:
java.io.IOException
-
readAddresses
public static java.util.Collection<Address> readAddresses(java.io.DataInputStream in, java.lang.Class cl) throws java.io.IOException, java.lang.IllegalAccessException, java.lang.InstantiationException
- Parameters:
in
-cl
- The type of Collection, e.g. Vector.class- Returns:
- Collection of Address objects
- Throws:
java.io.IOException
java.lang.IllegalAccessException
java.lang.InstantiationException
-
size
public static long size(java.util.Collection addrs)
Returns the marshalled size of a Collection of Addresses. Assumes elements are of the same type !- Parameters:
addrs
- Collection- Returns:
- long size
-
writeStreamable
public static void writeStreamable(Streamable obj, java.io.DataOutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
readStreamable
public static Streamable readStreamable(java.lang.Class clazz, java.io.DataInputStream in) throws java.io.IOException, java.lang.IllegalAccessException, java.lang.InstantiationException
- Throws:
java.io.IOException
java.lang.IllegalAccessException
java.lang.InstantiationException
-
writeGenericStreamable
public static void writeGenericStreamable(Streamable obj, java.io.DataOutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
readGenericStreamable
public static Streamable readGenericStreamable(java.io.DataInputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
writeObject
public static void writeObject(java.lang.Object obj, java.io.DataOutputStream out) throws java.lang.Exception
- Throws:
java.lang.Exception
-
readObject
public static java.lang.Object readObject(java.io.DataInputStream in) throws java.lang.Exception
- Throws:
java.lang.Exception
-
writeString
public static void writeString(java.lang.String s, java.io.DataOutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
readString
public static java.lang.String readString(java.io.DataInputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
writeByteBuffer
public static void writeByteBuffer(byte[] buf, java.io.DataOutputStream out) throws java.io.IOException
- Throws:
java.io.IOException
-
readByteBuffer
public static byte[] readByteBuffer(java.io.DataInputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
messageToByteBuffer
public static Buffer messageToByteBuffer(Message msg) throws java.io.IOException
- Throws:
java.io.IOException
-
byteBufferToMessage
public static Message byteBufferToMessage(byte[] buffer, int offset, int length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
msgListToByteBuffer
public static Buffer msgListToByteBuffer(java.util.List<Message> xmit_list) throws java.io.IOException
Marshalls a list of messages.- Parameters:
xmit_list
- LinkedList- Returns:
- Buffer
- Throws:
java.io.IOException
-
byteBufferToMessageList
public static java.util.List<Message> byteBufferToMessageList(byte[] buffer, int offset, int length) throws java.lang.Exception
- Throws:
java.lang.Exception
-
match
public static boolean match(java.lang.Object obj1, java.lang.Object obj2)
-
match
public static boolean match(long[] a1, long[] a2)
-
sleep
public static void sleep(long timeout)
Sleep for timeout msecs. Returns when timeout has elapsed or thread was interrupted
-
sleep
public static void sleep(long timeout, int nanos)
-
sleep
public static void sleep(long msecs, boolean busy_sleep)
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.
-
random
public static long random(long range)
Returns a random value in the range [1 - range]
-
sleepRandom
public static void sleepRandom(long timeout)
Sleeps between 1 and timeout milliseconds, chosen randomly. Timeout must be > 1
-
sleepRandom
public static void sleepRandom(long floor, long ceiling)
Sleeps between floor and ceiling milliseconds, chosen randomly
-
tossWeightedCoin
public static boolean tossWeightedCoin(double probability)
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%.
-
getHostname
public static java.lang.String getHostname()
-
dumpStack
public static void dumpStack(boolean exit)
-
dumpThreads
public static java.lang.String dumpThreads()
-
interruptAndWaitToDie
public static boolean interruptAndWaitToDie(java.lang.Thread t)
-
interruptAndWaitToDie
public static boolean interruptAndWaitToDie(java.lang.Thread t, long timeout)
-
dumpQueue
public static java.lang.String dumpQueue(Queue q)
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.
-
printStackTrace
public static java.lang.String printStackTrace(java.lang.Throwable t)
Use with caution: lots of overhead
-
getStackTrace
public static java.lang.String getStackTrace(java.lang.Throwable t)
-
print
public static java.lang.String print(java.lang.Throwable t)
-
crash
public static void crash()
-
printEvent
public static java.lang.String printEvent(Event evt)
-
printMessage
public static java.lang.String printMessage(Message msg)
Tries to read an object from the message's buffer and prints it
-
mapToString
public static java.lang.String mapToString(java.util.Map<? extends java.lang.Object,? extends java.lang.Object> map)
-
printMethodCall
public static java.lang.String printMethodCall(Message msg)
Tries to read aMethodCall
object from the message's buffer and prints it. Returns empty string if object is not a method call
-
printThreads
public static void printThreads()
-
activeThreads
public static java.lang.String activeThreads()
-
printBytes
public static java.lang.String printBytes(long bytes)
-
printBytes
public static java.lang.String printBytes(double bytes)
-
fragmentBuffer
public static byte[][] fragmentBuffer(byte[] buf, int frag_size, int length)
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.- Returns:
- An array of byte buffers (
byte[]
).
-
fragmentBuffer
public static byte[][] fragmentBuffer(byte[] buf, int frag_size)
-
computeFragOffsets
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. 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
-- Returns:
- List. A List
of offset/length pairs
-
computeFragOffsets
public static java.util.List computeFragOffsets(byte[] buf, int frag_size)
-
defragmentBuffer
public static byte[] defragmentBuffer(byte[][] fragments)
Concatenates smaller fragments into entire buffers.- Parameters:
fragments
- An array of byte buffers (byte[]
)- Returns:
- A byte buffer
-
printFragments
public static void printFragments(byte[][] frags)
-
printListWithDelimiter
public static <T> java.lang.String printListWithDelimiter(java.util.Collection<T> list, java.lang.String delimiter)
-
array2String
public static java.lang.String array2String(long[] array)
-
array2String
public static java.lang.String array2String(short[] array)
-
array2String
public static java.lang.String array2String(int[] array)
-
array2String
public static java.lang.String array2String(boolean[] array)
-
array2String
public static java.lang.String array2String(java.lang.Object[] array)
-
all
public static boolean all(java.util.Collection c, java.lang.Object obj)
Returns true if all elements of c match obj
-
isCoordinator
public static boolean isCoordinator(JChannel ch)
-
pickSubset
public static java.util.Vector pickSubset(java.util.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. If the percentage is smaller than 1 -> picks 1 member.
-
pickRandomElement
public static java.lang.Object pickRandomElement(java.util.List list)
-
pickRandomElement
public static java.lang.Object pickRandomElement(java.lang.Object[] array)
-
determineLeftMembers
public static java.util.Vector<Address> determineLeftMembers(java.util.Vector<Address> old_mbrs, java.util.Vector<Address> new_mbrs)
Returns all members that left between 2 views. All members that are element of old_mbrs but not element of new_mbrs are returned.
-
printMembers
public static java.lang.String printMembers(java.util.Vector v)
-
printPingRsps
public static java.lang.String printPingRsps(java.util.List<PingRsp> rsps)
-
doubleWrite
public static void doubleWrite(byte[] buf, java.io.OutputStream out) throws java.lang.Exception
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).- Throws:
java.lang.Exception
-
doubleWrite
public static void doubleWrite(byte[] buf, int offset, int length, java.io.OutputStream out) throws java.lang.Exception
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).- Throws:
java.lang.Exception
-
writeFully
public static void writeFully(java.nio.ByteBuffer buf, java.nio.channels.WritableByteChannel out) throws java.io.IOException
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.- Throws:
java.io.IOException
-
sizeOf
public static long sizeOf(java.lang.String classname)
-
sizeOf
public static long sizeOf(java.lang.Object inst)
-
sizeOf
public static int sizeOf(Streamable inst)
-
loadClass
public static java.lang.Class loadClass(java.lang.String classname, java.lang.Class clazz) throws java.lang.ClassNotFoundException
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.clazz
- Class object used to obtain a class loader if no context class loader is available.- Returns:
- Class, or null on failure.
- Throws:
java.lang.ClassNotFoundException
-
getResourceAsStream
public static java.io.InputStream getResourceAsStream(java.lang.String name, java.lang.Class clazz)
-
sameHost
public static boolean sameHost(Address one, Address two)
Checks whether 2 Addresses are on the same host
-
fileExists
public static boolean fileExists(java.lang.String fname)
-
parseCommaDelimitedLongs
public static long[] parseCommaDelimitedLongs(java.lang.String s)
Parses comma-delimited longs; e.g., 2000,4000,8000. Returns array of long, or null.
-
parseCommaDelimitedStrings
public static java.util.List<java.lang.String> parseCommaDelimitedStrings(java.lang.String l)
e.g. "bela,jeannette,michelle" --> List{"bela", "jeannette", "michelle"}
-
parseStringList
public static java.util.List<java.lang.String> parseStringList(java.lang.String l, java.lang.String separator)
-
parseInt
public static int parseInt(java.util.Properties props, java.lang.String property, int defaultValue)
-
parseLong
public static long parseLong(java.util.Properties props, java.lang.String property, long defaultValue)
-
parseBoolean
public static boolean parseBoolean(java.util.Properties props, java.lang.String property, boolean defaultValue)
-
parseBindAddress
public static java.net.InetAddress parseBindAddress(java.util.Properties props, java.lang.String property) throws java.net.UnknownHostException
- Throws:
java.net.UnknownHostException
-
parseInterfaceList
public static java.util.List<java.net.NetworkInterface> parseInterfaceList(java.lang.String s) throws java.lang.Exception
- Parameters:
s
-- Returns:
- List
- Throws:
java.lang.Exception
-
print
public static java.lang.String print(java.util.List<java.net.NetworkInterface> interfaces)
-
shortName
public static java.lang.String shortName(java.lang.String hostname)
-
shortName
public static java.lang.String shortName(java.net.InetAddress hostname)
-
startFlush
public static boolean startFlush(Channel c, java.util.List<Address> flushParticipants, int numberOfAttempts, long randomSleepTimeoutFloor, long randomSleepTimeoutCeiling)
-
startFlush
public static boolean startFlush(Channel c, int numberOfAttempts, long randomSleepTimeoutFloor, long randomSleepTimeoutCeiling)
-
startFlush
public static boolean startFlush(Channel c)
-
createServerSocket
public static java.net.ServerSocket createServerSocket(int start_port) throws java.io.IOException
Finds first available port starting at start_port and returns server socket- Throws:
java.io.IOException
-
createServerSocket
public static java.net.ServerSocket createServerSocket(java.net.InetAddress bind_addr, int start_port) throws java.io.IOException
- Throws:
java.io.IOException
-
createDatagramSocket
public static java.net.DatagramSocket createDatagramSocket(java.net.InetAddress addr, int port) throws java.lang.Exception
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.port
- The port which the socket should use. If 0, a random port will be used. If > 0, but port is already in use, it will be incremented until an unused port is found, or until MAX_PORT is reached.- Throws:
java.lang.Exception
-
createMulticastSocket
public static java.net.MulticastSocket createMulticastSocket(int port) throws java.io.IOException
- Throws:
java.io.IOException
-
createMulticastSocket
public static java.net.MulticastSocket createMulticastSocket(java.net.InetAddress mcast_addr, int port, org.apache.commons.logging.Log log) throws java.io.IOException
- Throws:
java.io.IOException
-
getBindAddress
public static java.net.InetAddress getBindAddress(java.util.Properties props) throws java.net.UnknownHostException, java.net.SocketException
Returns the address of the interface to use defined by bind_addr and bind_interface- Parameters:
props
-- Returns:
- Throws:
java.net.UnknownHostException
java.net.SocketException
-
checkForLinux
public static boolean checkForLinux()
-
checkForSolaris
public static boolean checkForSolaris()
-
checkForWindows
public static boolean checkForWindows()
-
prompt
public static void prompt(java.lang.String s)
-
getJavaVersion
public static int getJavaVersion()
-
unmodifiableVector
public static <T> java.util.Vector<T> unmodifiableVector(java.util.Vector<? extends T> v)
-
memStats
public static java.lang.String memStats(boolean gc)
-
getFirstNonLoopbackAddress
public static java.net.InetAddress getFirstNonLoopbackAddress() throws java.net.SocketException
- Throws:
java.net.SocketException
-
getFirstNonLoopbackIPv6Address
public static java.net.InetAddress getFirstNonLoopbackIPv6Address() throws java.net.SocketException
- Throws:
java.net.SocketException
-
getAllAvailableInterfaces
public static java.util.List<java.net.NetworkInterface> getAllAvailableInterfaces() throws java.net.SocketException
- Throws:
java.net.SocketException
-
getProperty
public static java.lang.String getProperty(java.lang.String[] system_props, java.util.Properties props, java.lang.String prop_name, boolean ignore_sysprops, java.lang.String default_value)
Returns a value associated wither with one or more system properties, or found in the props map- Parameters:
system_props
-props
- List of properties read from the configuration fileprop_name
- The name of the property, will be removed from props if foundignore_sysprops
- If true, system properties are not used and the values will only be retrieved from props (not system_props)default_value
- Used to return a default value if the properties or system properties didn't have the value- Returns:
- The value, or null if not found
-
isBindAddressPropertyIgnored
public static boolean isBindAddressPropertyIgnored()
-
getMBeanServer
public static javax.management.MBeanServer getMBeanServer()
-
getProperty
public static java.lang.String getProperty(Protocol prot, java.lang.String prop_name)
-
main
public static void main(java.lang.String[] args) throws java.lang.Exception
- Throws:
java.lang.Exception
-
generateList
public static java.lang.String generateList(java.util.Collection c, java.lang.String separator)
-
substituteVariable
public static java.lang.String substituteVariable(java.lang.String val)
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
-- Returns:
- A string with vars replaced, or the same string if no vars found
-
md5
public static java.lang.String md5(java.lang.String source)
Converts a java.lang.String in to a MD5 hashed String- Parameters:
source
- the source String- Returns:
- the MD5 hashed version of the string
-
sha
public static java.lang.String sha(java.lang.String source)
Converts a java.lang.String in to a SHA hashed String- Parameters:
source
- the source String- Returns:
- the MD5 hashed version of the string
-
-