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

com.addc.jndi.ecn.AbstractECNEnum Maven / Gradle / Ivy

package com.addc.jndi.ecn;

import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.NoSuchElementException;

import javax.naming.CannotProceedException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import org.omg.CosNaming.Binding;
import org.omg.CosNaming.BindingIterator;
import org.omg.CosNaming.BindingIteratorHolder;
import org.omg.CosNaming.BindingListHolder;
import org.omg.CosNaming.NamingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.addc.jndi.JndiKeys;

/**
 * The AbstractECNEnum supplies the basic finctionality for the
 * {@link NamingEnumeration} implementations that us COS Naming.
 */
@SuppressWarnings({ "PMD.LooseCoupling", "PMD.PreserveStackTrace" })
public abstract class AbstractECNEnum implements NamingEnumeration {
    private static final Logger LOGGER= LoggerFactory.getLogger(AbstractECNEnum.class);
    private final BindingListHolder bindingList;
    private final int batchsize;
    private BindingIterator bindingIter;
    private int counter;
    private boolean closed;

    /**
     * Get the batch size from the environment using the key
     * java.naming.batchsize
     *
     * @param env
     *            The environment to check
     * @return The batch size
     */
    public static int getBatchSize(Hashtable env) {
        int batchsize;
        if (env == null) {
            batchsize= JndiKeys.DEF_BATCHSIZE;
        } else {
            String s= (String) env.get(Context.BATCHSIZE);
            if (s == null) {
                batchsize= JndiKeys.DEF_BATCHSIZE;
            } else {
                try {
                    batchsize= Integer.parseInt(s);
                    if (batchsize < 1) {
                        String msg= MessageFormat.format("Batch size {0} MUST be a positive integer", s);
                        LOGGER.error(msg);
                        throw new IllegalArgumentException(msg);
                    }
                } catch (NumberFormatException e) {
                    String msg= MessageFormat.format("Batch size {0} MUST be a positive integer", s);
                    LOGGER.error(msg);
                    throw new IllegalArgumentException(msg);
                }
            }
        }
        return batchsize;
    }

    /**
     * Create a new AbstractECNEnum
     * 
     * @param batchsize
     *            The batch size for the iterator
     * @param context
     *            The COS NamingContext to use to get the original list
     */
    protected AbstractECNEnum(int batchsize, NamingContext context) {
        this.batchsize= batchsize;
        bindingList= new BindingListHolder();
        BindingIteratorHolder bindingiteratorholder= new BindingIteratorHolder();
        context.list(batchsize, bindingList, bindingiteratorholder);
        LOGGER.debug("Got initial batch of {}", bindingList.value.length);
        bindingIter= bindingiteratorholder.value;
    }

    /**
     * Transform the COS Naming Binding to the type for the enumeration
     *
     * @param binding
     *            The binding to convert
     * @return The transformed object
     * @throws NamingException
     *             If the Binding cannot be mapped
     */
    protected abstract T mapBinding(Binding binding) throws NamingException;

    @Override
    public boolean hasMoreElements() {
        if (closed) {
            return false;
        }
        return (counter < bindingList.value.length);
    }

    @Override
    public T nextElement() {
        try {
            return next();
        } catch (NamingException e) {
            LOGGER.error("Unexpected error", e);
            throw new NoSuchElementException("No bindings remaining.");
        }
    }

    @Override
    public T next() throws NamingException {
        if (counter < bindingList.value.length) {
            T ncp= mapBinding(bindingList.value[counter++]);
            if ((counter == bindingList.value.length) && (bindingIter != null)) {
                if (bindingIter.next_n(batchsize, bindingList)) {
                    LOGGER.debug("Got next batch of {}", bindingList.value.length);
                    counter= 0;
                } else {
                    close();
                }
            }
            return ncp;
        }
        throw new CannotProceedException("No such element.");
    }

    @Override
    public boolean hasMore() throws NamingException {
        return hasMoreElements();
    }

    @Override
    public void close() throws NamingException {
        if (!closed) {
            LOGGER.debug("Close iterators and release remote objects");
            closed= true;
            if (bindingIter != null) {
                bindingIter.destroy();
                bindingIter._release();
                bindingIter= null;
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy