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

📄 offlinebroker.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

package com.ca.directory.jxplorer.broker;

import javax.naming.*;
import javax.naming.directory.*;

import java.util.*;
import java.util.logging.Logger;
import java.util.logging.Level;

import com.ca.directory.jxplorer.*;
import com.ca.commons.naming.*;
import com.ca.commons.jndi.SchemaOps;

import java.awt.Component;

/** 
 * 	    This sets up a 'virtual broker' that reads info in, and allows
 *      the user to operate on it, without any server being involved.  The
 *      data is usually (always?) read from an ldif file.
 */

public class OfflineBroker extends Broker
{
    Hashtable nodes;  // stored node dn keys (as strings), and nodes
    Component display;

    private static Logger log = Logger.getLogger(OfflineBroker.class.getName());

    /**
     *    Data node class.  Tuned for 10ish children.
     *
     */
    
    class Node
    {
        DXEntry entry;
        
        //DXAttributes myAtts;
        Vector children;
        DN dn;
        NameClassPair namePair;

        /** 
         *    constructor for a node with a dn and a set of attribute
         */        
        public Node(DXEntry entry) 
        {
            this.entry = entry;
            dn = entry.getDN();
            children = new Vector(10);
            namePair = new NameClassPair(dn.getLowestRDN().toString(), dn.getLowestRDN().getAtt());    
        }
        
        public void addChild(Node n) { children.add(n); }
        
        public void removeChild(Node n) { children.remove(n); }
        
        public void updateAttributes(DXAttributes a) 
        { 
            try
            {
                NamingEnumeration attset = a.getAll();
                while (attset.hasMore())
                    entry.put((Attribute)attset.next()); 
            }   
            catch (NamingException e)
            {
                log.log(Level.WARNING, "unusual error in OfflineBroker::updateAttributes", e);
            }
        }
        
        /** 
         *    Returns enumeration of Children as NameClassPairs
         */
         
        public DXNamingEnumeration getChildren()
        {
            DXNamingEnumeration result = new DXNamingEnumeration();
            for (int i=0; i<children.size(); i++)
                result.add(((Node)children.elementAt(i)).getNameClassPair());
            return result;                            
        }
     
        /** 
         *    Returns enumeration of children as Nodes
         */
         
        public NamingEnumeration getChildNodes()
        {
            DXNamingEnumeration result = new DXNamingEnumeration();
            for (int i=0; i<children.size(); i++)
                result.add(children.elementAt(i));
            return result;                            
        }

        /** 
         *    Gets a NameClassPair for the present node, i.e.
         *    something like NameClassPair(cn=Fred Bloggs, cn)
         */     
        public NameClassPair getNameClassPair() { return namePair; }         
        
        /**
         *    Get Attributes for current Node.
         */
        public DXEntry getEntry() { return entry; }
        
        /** 
         *    Gets the full dn for the current node.
         */
        public DN getDN() { return dn; }
        
        /**
         *    Looks up the parent in the big nodes Hashtable.
         *    @return Returns null if the parent isn't found,
         *            returns parent node otherwise.
         */
         
        public Node getParent()
        {
            DN parentDN = dn.parentDN();
            if (parentDN == null) return null;
            Node Parent = (Node)nodes.get(parentDN.toString());
            return Parent;
        }
        
        public String toString() { return dn.toString(); }
    }

    /**
     *    Constructor for Offline Broker does nothing except 
     *    initialise the big hashtable that is at the core
     *    of the class.
     */
    public OfflineBroker(Component graphicsDisplay)
    {
        display = graphicsDisplay;
        nodes = new Hashtable(1000);
    }

    /**
     *    Empties the core Hashtable in preparation for a new
     *    ldif file to be read in.
     */
     
     public void clear()
     {
         nodes.clear();   
     }

    /**
     *    gets the children of a particular DN as an enumeration
     *    @param nodeDN the DN to retrieve children for
     *
     *    @return an enumeration of NameClassPair-s to be the
     *     sub-nodes of the given node.  returns null if nodeDN
     *     not found at all.
     */
     
     public DXNamingEnumeration children(DN nodeDN)
     {
         Node N = (Node)nodes.get(nodeDN.toString());
         return (N==null)? null : N.getChildren() ;
     }
    

    /**
     *    whether the data source is currently on-line.
     *    @return on-line status
     */  
     
     public boolean isActive() { return true; }

    /**
     * Whether there are any nodes in our offline broker (effectively, if the ldif file read was successful)
     * @return
     */
     public boolean hasData() { return !nodes.isEmpty(); }  

    /**
     *    Gets a list of all known schemaOps object classes.
     *    @return all known object classes, as .  This will be
     *            null if this data source does not support
     *            this feature.
     */
    public Vector objectClasses() { return null; }
    
    /**
     *    Gets a list of the object classes most likely
     *    to be used for the next Level of the DN...
     *    @param dn the dn of the parent to determine likely
     *              child object classes for
     *    @return list of recommended object classes...             
     */
     
     public Vector recommendedObjectClasses(DN dn) { return null; }
     
     /**
      *    Returns the context for the schemaOps.  This
      *    may be null if the directory does not support
      *    schemaOps.
      *    @return null - not implemented.
      */
      
     public SchemaOps getSchemaOps() { return null; }
     
   /**
    *    Make a new entry with the provided DN.
    *    @param entry the DN and attributes of the new object.
    *    @return the operation's success status
    */
    
    protected boolean addNode(DXEntry entry)
    {
        DN nodeDN = entry.getDN();
        
        log.fine("adding node " + nodeDN);
        
        Node N = new Node(entry);
        nodes.put(nodeDN.toString(), N);
        Node P = N.getParent();
        
        if (P==null)
        {
            if (nodeDN.size()>1)  // so there *should* be a parent!
            {
                addNode(new DXEntry(new DXAttributes(new DXAttribute("structuralTreeNode", "true")), nodeDN.parentDN()));  // add a 'fake' node to pad the tree out...
                P = (Node)nodes.get(nodeDN.parentDN().toString());    
            }   
        }
        log.fine("parent = " + ((P==null)?"null":P.toString()));
        if (P != null) P.addChild(N);
        return true;      // not very discriminating
    }
    
   /**
    *    Update an entry with the designated DN.
    *    @param oldSet the old set of attributes of the object.
    *    @param newSet the replacement set of attributes..
    *    @return the operation's success status
    */
   
    public boolean updateNode(DXEntry oldSet, DXEntry newSet)
    {
        log.fine("offline cache updating " + oldSet.getDN().toString());
        Node N = (Node)nodes.get(oldSet.getDN().toString());
        if (N==null) return false;
        N.updateAttributes(newSet);
        return true;    
    }
    
   /**
    *    deletes an entry with the given DN.  If the entry has subordinates,
    *    they are also deleted.
    *
    *    @param nodeDN the DN of the tree root to delete (may be a single entry).
    *    @return the operation's success status

⌨️ 快捷键说明

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