All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hpccsystems.ws.client.utils.ObjectPool Maven / Gradle / Ivy

Go to download

This project allows a user to interact with ESP services in a controlled manner. The API calls available under org.hpccsystems.ws.client.platform allow for a user to target ESP's across multiple environments running a range of hpccsystems-platform versions. There is no guarantee that if a user utilizes org.hpccsystems.ws.client.gen generated stub code from wsdl, that the calls will be backwards compatible with older hpccsystems-platform versions.

There is a newer version: 9.8.20-1
Show newest version
package org.hpccsystems.ws.client.utils;

import java.util.Enumeration;
import java.util.Hashtable;

public abstract class ObjectPool
{
    private long expirationTime;

    private Hashtable locked, unlocked;

    public ObjectPool(long expirationTime)
    {
        this.expirationTime = expirationTime;
        locked = new Hashtable();
        unlocked = new Hashtable();
    }

    public ObjectPool()
    {
        this(60000); // default 1 minute
    }

    protected abstract T create();

    public abstract boolean validate(T object);

    public abstract void expire(T object);

    public synchronized T checkOut()
    {
        long now = System.currentTimeMillis();
        T t;
        if (unlocked.size() > 0)
        {
            Enumeration e = unlocked.keys();
            while (e.hasMoreElements())
            {
                t = e.nextElement();
                if ((now - unlocked.get(t)) > expirationTime) //create new one if expired
                {
                    unlocked.remove(t);
                    expire(t);
                    t = null;
                }
                else
                {
                    if (validate(t)) // found good one
                    {
                        unlocked.remove(t);
                        locked.put(t, now);
                        return (t);
                    }
                    else // did not pass validate, create new one
                    {
                        unlocked.remove(t);
                        expire(t);
                        t = null;
                    }
                }
            }
        }
        // no objects available, create a new one
        t = create();
        locked.put(t, now);
        return (t);
    }

    public synchronized void checkIn(T t)
    {
        locked.remove(t);
        unlocked.put(t, System.currentTimeMillis());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy