it.sauronsoftware.junique
Class JUnique

java.lang.Object
  extended by it.sauronsoftware.junique.JUnique

public class JUnique
extends java.lang.Object

Point-of-entry of the JUnique library.

Author:
Carlo Pelliccia

Constructor Summary
JUnique()
           
 
Method Summary
static void acquireLock(java.lang.String id)
          This method tries to acquire a lock in the user-space for a given ID.
static void acquireLock(java.lang.String id, MessageHandler messageHandler)
          This method tries to acquire a lock in the user-space for a given ID.
static void releaseLock(java.lang.String id)
          It releases a previously acquired lock on an ID.
static java.lang.String sendMessage(java.lang.String id, java.lang.String message)
          It sends a message to the JVM process that has previously locked the given ID.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JUnique

public JUnique()
Method Detail

acquireLock

public static void acquireLock(java.lang.String id)
                        throws AlreadyLockedException
This method tries to acquire a lock in the user-space for a given ID.

Parameters:
id - The lock ID.
Throws:
AlreadyLockedException - If the lock cannot be acquired, since it has been already taken in the user-space.

acquireLock

public static void acquireLock(java.lang.String id,
                               MessageHandler messageHandler)
                        throws AlreadyLockedException
This method tries to acquire a lock in the user-space for a given ID.

Parameters:
id - The lock ID.
messageHandler - An optional message handler that will be used after the lock has be acquired to handle incoming messages on the lock channel.
Throws:
AlreadyLockedException - If the lock cannot be acquired, since it has been already taken in the user-space.

releaseLock

public static void releaseLock(java.lang.String id)
It releases a previously acquired lock on an ID. Please note that a lock can be realeased only by the same JVM that has previously acquired it. If the given ID doens't correspond to a lock that belongs to the current JVM, no action will be taken.

Parameters:
id - The lock ID.

sendMessage

public static java.lang.String sendMessage(java.lang.String id,
                                           java.lang.String message)
It sends a message to the JVM process that has previously locked the given ID. The message will be delivered only if the lock for the given ID has been actually acquired, and only if who has acquired it is interested in message handling.

Parameters:
id - The lock ID.
message - The message.
Returns:
A response for the message. It returns null if the message cannot be delivered. It returns an empty string if the message has been delivered but the recipient hasn't supplied a response for it.