package dk.statsbiblioteket.util.rpc;

/**
 * A thread safe reference counted object wrapping a connection of type
 * {@code E}.
 *
 * @see ConnectionManager
 */
public class ConnectionContext<E> {

    private E con;
    private int refCount;
    private long lastUse;
    private String connectionId;

    /**
     * Package private constructor. Only {@link ConnectionManager}s should
     * create {@code ConnectionContext}s
     * @param connection connection to wrap
     * @param connectionId opaque handle which is meaningful in the context
     *                     of the {@link ConnectionManager}s
     *                     {@link ConnectionFactory}.
     */
    ConnectionContext(E connection, String connectionId) {
        this.con = connection;
        this.refCount = 0;
        this.lastUse = System.currentTimeMillis();
        this.connectionId = connectionId;
    }

    /**
     * Package private method to increase the reference count by one.
     */
    synchronized void ref () {
        refCount++;
        lastUse = System.currentTimeMillis();
    }

    /**
     * Package private method to decrease the reference count by one.
     */
    synchronized void unref () {
        refCount--;
        lastUse = System.currentTimeMillis();
    }

    /**
     * Get the {@code connectionId} as passed to the constructor.
     * @return
     */
    public String getConnectionId() {
        return connectionId;
    }

    /**
     * Get the refernce count for this context - only for debugging!
     * @return
     */
    public synchronized int getRefCount () {
        return refCount;
    }

    /**
     * Package private method used for book keeping by the connection monitor
     * thread in the {@link ConnectionManager} owning this context.
     * @return last usage time as read from {@link System#currentTimeMillis}
     */
    synchronized long getLastUse () {
        return lastUse;
    }

    /**
     * Get the connection wrapped by this context
     * @return the connection wrapped by this context
     */
    public synchronized E getConnection () {
        lastUse = System.currentTimeMillis();
        return con;
    }

}


@@ -900,42 +900,257 @@
   * 
-Package private method to increase
+Grab a reference to a ConnectionContext. Contexts obtained via%0A     * %7B@link ConnectionManager#get%7D are already referenced. You should only%0A     * use this method if you obtain a %7B@code ConnectionContext%7D by other%0A     * means.%0A     *%0A     * @return
  the
@@ -1155,38 +1155,33 @@
 he reference
+d
  co
-u
 nt
- by one.
+ext
 %0A     */%0A   
@@ -1173,32 +1173,39 @@
 ext%0A     */%0A    
+public 
 synchronized voi
@@ -1201,20 +1201,36 @@
 ronized 
-void
+ConnectionContext%3CE%3E
  ref () 
@@ -1305,87 +1305,169 @@
     
-%7D%0A%0A    /**%0A     * Package private method to decrease the reference count by one
+    return this;%0A    %7D%0A%0A    /**%0A     * Release a reference to a %7B@code ConnectionContext%7D. This is equivalent%0A     * to calling %7B@link ConnectionManager#release%7D
 .%0A  
@@ -1468,32 +1468,39 @@
 e%7D.%0A     */%0A    
+public 
 synchronized voi

Make ConnectionContext.ref() and ConnectionContext.unref() public.