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

📄 jndibroker.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            catch (NamingException e)    // annoying, but not fatal, error.
            {
                log.log(Level.WARNING, "Failed to connect to schemaOps: " + url, e);
            }
        }
        else
        {
            log.info("skipped schemaOps stuff : " + cData.protocol);
        }

        if (cData.protocol.equalsIgnoreCase("dsml"))    //TODO: some day we may support a different version of DSML. If so the version variable in ConnectionData should be called ldapVersion and a new variable called dsmlVersion added.
            log.info("Successfully connected to " + url + " using " + cData.protocol + " version 2");
        else
            log.info("Successfully connected to " + url + " using " + cData.protocol + " version " + cData.version);

        request.setStatus(true); 		// success!
        request.finish();
        return request;
 	}
	
	

   /**
    *   Warning: ETrust Directory Specific Code!  Currently unused.
    *
	*	This is a bit of a hack to verify the base DN.  It basically takes a DN and does a base object
	*	search and returns the DN of the results.  This method is needed as a bit of a hack for when the
	*	the user puts in a base DN that is of a different case to what is stored in the dsa.  If this happens
	*	the tree remembers the wrong DN and throws some errors when the user tries to modify the base DN
	*	entry (bug 2363).  However, currently (20 June 02) the dsa gives the DN back the way it was put in.  I've
	*	assigned the bug to them to fix...  
	*	@param dn the DN to verify (or do the base object search on).
	*	@return the DN that the search returns (one day this will be what is actually stored in the dsa).  If
	*		there is an exception thrown, the DN will be returned unchanged.
	*	.
	*/
	//TE: NOTE: WAITING IN DSA CHANGE BEFORE THIS CAN BE USED...BUG 2363
	public String getActualDN(String dn)
	{
		if(true)
			return dn;
			
		try
		{
            NamingEnumeration en = dirOps.searchBaseEntry(new DN(dn), "objectClass=*",0,0, null);
			String temp = "";
			while (en.hasMoreElements())
			{
				temp = (en.nextElement()).toString();
				temp = temp.substring(0, temp.indexOf(":"));				
			}
			return temp;
		}
		catch(Exception e)
		{
			return dn;
		}		
	}



   /**
	*  	The attributeNames hash provides a quick look up mapping
	*  	between numericoids and attribute names.  This initialises
	*  	that hashtable.
	*/

    // XXX probably neater ways of doing this now jndi 1.2 is out...
    // TODO: ... and why is it being done here, and not in schema ops?
    protected void initAttributeNamesHash()
    {
        attributeNames = new Hashtable(100);

        /*
         *   XXX This tries to parse efficiently the syntax line ... not very happy with it though, it seems a bit ad-hoc.
         *   we may want to rewrite this more generically some time...
         */
        try
        {
            Attribute attDefs = schemaOps.getAttributeTypes();
            if (attDefs==null)
            {
                log.warning("unable to read schema attributes in JNDIBroker:initAttributeNamesHash");
                return;
            }
            StringTokenizer tokenizer;
            for (int i=0; i<attDefs.size(); i++)
            {
                String parseMe = attDefs.get(i).toString();  // response something like: ( 2.5.4.4 NAME ( 'sn' 'surname' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
                int namePos = parseMe.indexOf("NAME");
                if (namePos == -1)
                    throw new NamingException("unable to parse ldap syntax '" + parseMe + "'");
                String oid = parseMe.substring(1, namePos).trim();
                String names = "";
                if (parseMe.indexOf("SYNTAX")>-1)
                    names = parseMe.substring(parseMe.indexOf("'", namePos), parseMe.indexOf("SYNTAX")-2); // trim off final bracket, if it exists...

                tokenizer = new StringTokenizer(names, "'");
                while (tokenizer.hasMoreElements())
                {
                    String name = tokenizer.nextToken().trim();
                    if (name.length() > 0)
                    {
                        attributeNames.put(oid, name);
                    }
                }

            }
        }
        catch (NamingException e)
        {
            log.warning("unable to parse schemaOps at JndiBroker:initAttributeNamesHash " + e + "\n");
        }
        catch (Exception e2)
        {
            log.warning("Unexpected exception parsing schemaOps at JndiBroker:initAttributeNamesHash " + e2 + "\n");
            e2.printStackTrace();
        }

    }



   /**
	*	@param attributeoid the attributes OID.
	*	@return the attribute description.  If attributeNames is null "(schemaOps not correctly read)"
	*		is returned, if attributeNames are still null after trying to get the description
	*		"(attribute not listed in schemaOps)" is returned.
	*/

    public String getAttributeDescription(String attributeoid)
    {
        if (attributeNames == null) return "(schemaOps not correctly read)";
        String attributeName = (String)attributeNames.get(attributeoid);
        if (attributeName == null) attributeName = "(attribute not listed in schemaOps)";
        return attributeName;
    }


   /**
	*  	This returns the version of ldap currently in use.
	*  	@return current ldap version ('2' or '3')
	*/

    public int getVersion()
    {
        return dirOps.getLdapVersion();
    }



   /**
	*  	Used for debugging; prints out the first level of a dn from
	*  	a particular context, headlined with
	*  	with the supplied message.
	*  	@param C the context to print out info for.
	*  	@param dn the DN to print the children of.
	*  	@param message text to print out along with the context data.
	*/

    public void printContextList(Context C, DN dn, String message)
    {
        System.out.println("******* " + message + " ******\nPrinting context '" + dn + "'");
        try
        {
            NamingEnumeration debug = C.list(dn);
            while (debug.hasMore())
            {
                System.out.println(((NameClassPair)(debug.next())).getName());
            }
        }
        catch (NamingException e)
        {
            System.out.println("error printing context " + dn + "( " + message + " )" +e);
        }
    }



   /**
	* 	Disconnects from the current context, freeing both context and
	*  	context environment parameters list.
	*/

 	public void disconnect()
 	{
        attributeNames = null;
        ctx = null;
        schemaOps = null;

        if (dirOps == null)
            return;   // no context open (not even a BasicOps object in existance!).
         try
         {
             dirOps.close();  // closes ctx...
         }
         catch (NamingException e)
         {
             e.printStackTrace();  //To change body of catch statement use Options | File Templates.
         }
         dirOps = null;

        Runtime.getRuntime().gc();
        Runtime.getRuntime().runFinalization();
 	}



   /**
	*   Sets the maximum number of objects returned by a search
	*   @param maxResponses the maximum returned objects
	*/

    public void setLimit(int maxResponses) { limit = maxResponses; }



   /**
	*   Sets the timeout period before the connection gives up trying
	*   to fetch a given data request.
	*   @param maxTime the maximum time allowed for a query.
	*/

    public void setTimeout(int maxTime) { timeout = maxTime; }



   /**
	*   returns the next level of a directory tree, returning
	*   a Enumeration of the results
	*   @param searchbase the node in the tree to expand
	*   @return list of results (NameClassPair); the next layer of the tree...
	*/

    public DXNamingEnumeration unthreadedList(DN searchbase)
    {
        SetContextToBrowsingAliases();

        try
        {
            return new DXNamingEnumeration(dirOps.list(searchbase));
        }
        catch (NamingException e)
        {
            error("unable to list " + searchbase, e);
            return new DXNamingEnumeration();  // return empty list.
        }
    }



   /**
	*   Performs a directory search.
	*   @param dn the DN (relative to initial context in ldap) to seach from.
	*   @param filter the non-null filter to use for the search
	*   @param search_level whether to search the base object, the next level or the whole subtree.
	*   @param returnAttributes - a list of attributes to return.  If set to null,
	*          only the objectClass is returned.
	*   @return list of results ('SearchResult's); the next layer of the tree...
	*/

    public DXNamingEnumeration unthreadedSearch(DN dn, String filter, int search_level, String[] returnAttributes)
    {
        SetContextToSearchingAliases();

        DXNamingEnumeration ret;

        // CB - use constants for search levels.

        try
        {
            if (search_level == SEARCH_BASE_OBJECT)
                ret = new DXNamingEnumeration(dirOps.searchBaseEntry(dn, filter, limit, timeout, returnAttributes));
            else if (search_level == SEARCH_ONE_LEVEL)
                ret = new DXNamingEnumeration(dirOps.searchOneLevel(dn, filter, limit, timeout, returnAttributes));
            else if (search_level == SEARCH_SUB_TREE)
                ret = new DXNamingEnumeration(dirOps.searchSubTree(dn, filter, limit, timeout, returnAttributes));
            else
                return null;
        }
        catch (NamingException e)
        {
            error("unable to search " + dn, e);
            return new DXNamingEnumeration();
        }

        SetContextToBrowsingAliases();

        return ret;
    }



   /**
	* 	Reads all the attribute type and values for the given entry.
	* 	Converts utf-8 to unicode if necessary.
	* 	@param dn the ldap string distinguished name of entry to be read
	* 	@return an 'Attributes' object containing a list of all Attribute
	*   	objects.
	*/

    public synchronized Attributes read(DN dn)
    {
        Attributes atts = null;
        try
        {
            atts = dirOps.read(dn);
        }
        catch (NamingException e)
        {
            error("unable to read " + dn, e);

        }
        return new DXAttributes(atts);
    }



   /**
	*	Deletes a subtree by recursively deleting sub-sub trees from the given DN.
	*	@param nodeDN the DN of the node where to do the recursive delete.
	*/

    public void deleteTree(DN nodeDN)
        throws NamingException
    {
         dirOps.deleteTree(nodeDN);
    }



   /**
    *  	Moves a DN to a new DN, including all subordinate entries.
    *  	(nb it is up to the implementer how this is done; e.g. if it is an
    *  	ldap broker, it may choose rename, or copy-and-delete, as appropriate)
    * 	@param oldNodeDN the original DN of the sub tree root (may be a single
    *  		entry).
    *  	@param newNodeDN the target DN for the tree to be moved to.
    */

    public void moveTree(DN oldNodeDN, DN newNodeDN)       // may be a single node.
        throws NamingException
    {
        dirOps.moveTree(oldNodeDN, newNodeDN);
    }



   /**
    *  	Copies a DN representing a subtree to a new subtree, including
    *  	copying all subordinate entries.
    *  	@param oldNodeDN the original DN of the sub tree root
    *  		to be copied (may be a single entry).
    *  	@param newNodeDN the target DN for the tree to be moved to.
    */

    public void unthreadedCopy(DN oldNodeDN, DN newNodeDN)       // may be a single node.
        throws NamingException
    {
        dirOps.copyTree(oldNodeDN, newNodeDN);
    }



   /**
	*  	Checks the existance of a given entry.
	*	@param checkMe the DN of the entry to check for existance.
	*  	@return whether the entry could be found in the directory.
	*/

    public boolean unthreadedExists(DN checkMe)
           throws NamingException
    {
        return dirOps.exists(checkMe);
    }



   /**
    *	@return
	*/

    public boolean processQueue()
    {
        if (dirOps != null) dirOps.setQuietMode(true);
        boolean ret = super.processQueue();


        // WARNING - THREAD PROBLEMS POSSIBLE
        // If this thread is cancelled, we run the risk of interfering
        // with dirOps quiet mode status.  However, since only jndi brokers
        // should be accessing it, it's probably o.k. to leave it in
        // quiet mode = true.  Setting quiet mode = false while something
        // else was using it could be very bad howerver, hence the
        // immediate bail out.
        if (ret == false) return false;

        if (dirOps != null) dirOps.setQuietMode(false);
        return true;
    }


   /**
	*    Utility method for extended queries - returns whether
	*    a 'masked' exception has occured.
	*/

    public Exception getException()
    {
        return dirOps.quietException;
    }



   /**
	*  	Utility method for extended queries - allows
	*  	a 'masked' exception to be cleared.
	*/

    public void clearException()
    {
        dirOps.quietException = null;
    }



   /**
    *	@param request
	*	@return the query.
	*/

    protected DataQuery finish(DataQuery request)
    {
        request.setException(dirOps.quietException);  // probably null
        request.finish();
        return request;
    }


   /**
	*    whether the data source is currently on-line.
	*    @return on-line status
	*/

⌨️ 快捷键说明

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