⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataquery.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ca.directory.jxplorer;

import com.ca.directory.jxplorer.broker.Broker;
import com.ca.commons.naming.*;

import java.util.Hashtable;
import java.util.Vector;
import java.util.ArrayList;
import javax.naming.NamingException;

/**
 *    Helper class for Broker, this encapsulates an ldap-like data
 *    request that is placed on the Broker queue for eventual resolution.
 *    Broker Request objects are intended to be used once, and then
 *    discarded.
 */

public class DataQuery
{
    /**
     *    The following gives the 'type' of the query.
     */
    // could use OO for this, but seems unnecessary; would only create class sprawl for
    // so many similar classes. - CB

    public static final int UNKNOWN = 0;     /** an unknown type **/
    public static final int EXISTS = 1;      /** an existance check query **/
    public static final int READENTRY = 2;   /** a read of a single entry **/
    public static final int LIST = 4;        /** list the immediate children of an entry **/
    public static final int SEARCH = 8;      /** a search query **/
    public static final int MODIFY = 16;     /** a modify request **/
    public static final int COPY = 32;       /** a copy request **/
    //public static final int GETALLOC = 64;   /** get all object class request **/
    public static final int GETRECOC = 128;  /** get recommended object classes request **/
    public static final int EXTENDED = 256;  /** an extended data query: i.e. a user inherited extension class **/

    protected Vector listeners = new Vector();

    protected int type = 0;

    static int noRequests = 0;

    public int id;  // debugging

    protected boolean myStatus = false;

    protected ArrayList myArrayList = null;

    protected DXEntry myEntry = null;

    protected DXNamingEnumeration myEnum = null;

    protected Exception myEx = null;


    // The following data variables are set initially in the
    // constructor, and never again modified.

    protected DN requestDN = null;
    protected DN oldDN = null;

    protected DXEntry oldEntry = null;
    protected DXEntry newEntry = null;

    protected String filter = null;
	
	protected String[] returnAttrs = null;

    protected int searchLevel = 2;	//TE: 0 = search base object, 1 = search next level, 2 = seach full subtree.

    protected Hashtable extendedData = null;  // convenience object for extended queries


    // These booleans relate to which stage of the query life cycle the
    // query is at - at the start, they are all false.  When the query
    // is being executed 'working' is true.  Finally 'ready' is true.
    // If the user cancels the query, the broker should pick this up
    // and ignore the query.

    protected boolean ready = false;        // whether the query if completed

    protected boolean cancelled = false;  // whether the user has cancelled this query.

    protected boolean working = false;    // whether this request is currently being run by a broker thread

    protected boolean squelched = false;    // whether this request has been consumed by a selfish listener

    // XXX Break into sub-classes?

    public DataQuery()
    {
        id = noRequests++;
    }

    /**
     *    Constructor for Get all Object Class requests
     */

    public DataQuery(int type)
    {
        this.type = type;

        if (type == EXTENDED)
            extendedData = new Hashtable();

        id = noRequests++;
    }

    /**
     *    Constructor for List, Read Entry, Exists, and get Recommended Object Class requests.
     */

    public DataQuery(int type, DN dn)
    {
        this(type);
        requestDN = dn;
        if (type != DataQuery.LIST &&  type != DataQuery.READENTRY && type != DataQuery.EXISTS )// && type != DataQuery.GETRECOC)
            setException(new Exception("Bad Constructor call (ii) for DataQuery of Type " + type));
    }

    /**
     *    Constructor for Copy requests
     */

    public DataQuery(int type, DN oldDN, DN newDN)
    {
        this(type);
        requestDN = newDN;
        this.oldDN = oldDN;
        if (type != DataQuery.COPY)
            setException(new Exception("Bad Constructor call (iii) for DataQuery of Type " + type));
    }

    /**
     *    Constructor for Modify requests
     */

    public DataQuery(int type, DXEntry oldEntry, DXEntry newEntry)
    {
        this(type);
        this.oldEntry = oldEntry;
        this.newEntry = newEntry;
        if (type != DataQuery.MODIFY)
            setException(new Exception("Bad Constructor call (iv) for DataQuery of Type " + type));
    }
	
    /**
     *    Constructor for Search requests
     */	

    public DataQuery(int type, DN dn, String filter, int level, String[] returnAttrs)
    {
        this(type);
        requestDN = dn;
        this.filter = filter;
		this.returnAttrs = returnAttrs;
        searchLevel = level;
        if (type != DataQuery.SEARCH)
            setException(new Exception("Bad Constructor call (v) for DataQuery of Type " + type));
    }

    /**
     *    Prevents any other data listeners from using the data query.  Indicates that
     *    the current data listener has handled the data, and that no other data listener
     *    should bother.  This method nullifies any data (including exception information).
     */

    public void squelch()
    {
        squelched = true;

        myArrayList = null;
        myEntry = null;
        myEnum = null;
        myEx = null;
        requestDN = null;
        oldDN = null;
        oldEntry = null;
        newEntry = null;
        filter = null;
    }


    /**
    *    Flags whether a data listener has already consumed this data query ('event'), and
    *    the data has been 'used up'.
    */

    public boolean isSquelched() { return squelched; }

    /**
     *    Attempt to cancel this request by setting a cancelled
     *    flag and an exception.  There is no way to 'uncancel' a
     *    request.  The cancel flag should be picked up by the broker.
     */

    public void cancel()
    {
        cancelled = true;
        setException(new Exception("Request Cancelled"));
    }

    /**
     *    Returns whether this request has been cancelled.
     */

    public boolean isCancelled()
    {
        return cancelled;
    }

    /**
     *    Registers that this query is currently being operated on by a broker.
     *    This is cancelled when the query is 'finished()'.
     */

     public void setRunning()
     {
         working = true;
     }

     /**
      *    Returns whether the query is currently being operated on by a broker.
      */

     public boolean isRunning()
     {
         return working;
     }



    /**
     *    Called when the operation is complete.  While it
     *    is not an error to call this method multiple times,
     *    subsequent calls will have no effect.
     */

    public synchronized void finish()
    {
        if (ready == true) return; // only run this method once!

        ready = true;
        if (!cancelled)
        {
            notifyAll();
            fireDataEvent();
        }
        working = false;
    }

    /**
     *    Notifies all interested listeners.  Note that the most
     *    recently added listener is notified first, giving it
     *    first option on handling any errors generated.
     */

    protected void fireDataEvent()
    {
        while(listeners.size() > 0 && !squelched)    // while there are still listeners, and this hasn't been squelched.
        {
            DataListener current = (DataListener)listeners.lastElement();
            listeners.removeElementAt(listeners.size()-1);
            current.dataReady(this);
        }
    }

    /**
     *    Utility translation method
     */

    public String getTypeString()
    {
        switch (type)
        {
            case UNKNOWN:   return "Unknown";
            case EXISTS:    return "Existance check";
            case READENTRY: return "Read entry";
            case LIST:      return "List";
            case SEARCH:    return "Search";
            case MODIFY:    return "Modify";
            case COPY:      return "Copy";
//            case GETALLOC:  return "Get all objectclasses from schema";
//            case GETRECOC:  return "Get recommended objectclasses from schema";
            case EXTENDED:  return "Extended request";
        }
        return "UNDEFINED";
    }

    /**
     *    Return a string representation of the Broker Request (usually for debugging).
     */

    public String toString()
    {
        String ret = "query ("+id+"): "  + getTypeString();

        if (type == SEARCH)
            ret += " filter: " + filter;
        else if (requestDN != null)
            ret += " on " + requestDN;

        if (ready() == false)
            return (ret + " : (request pending)");
        else
            return (ret + " : (completed)");

/*        {

            Object o = getResult();
            if (o == null)
                return (ret + " : (null)");
            else
                return (ret + " : " + getResult().toString());
        }
*/
    }

    /**
     *    Get the result of the request as a generic object (which must be cast
     *    correctly to be used).  (Usually for debugging).
     */

    public Object getResult()
    {
        try
        {
            switch (type)
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -