Search in sources :

Example 76 with NamingException

use of javax.naming.NamingException in project yyl_example by Relucent.

the class LdapDaoHelper method find.

/**
	 * 查找条目
	 * @param base 基准,意为从某个条目下搜索
	 * @param filter 过滤条件
	 * @param searchAttrSet 查找的字段
	 * @param ctx LDAP上下文连接
	 * @return 条目列表
	 * @deprecated 该方法未经过严谨测试
	 */
public static List<LdapEntry> find(String base, String filter, Set<String> searchAttrSet, LdapContext ctx) throws NamingException {
    try {
        List<LdapEntry> entries = new ArrayList<LdapEntry>();
        SearchControls searchcontrols = new SearchControls();
        searchcontrols.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchcontrols.setReturningAttributes(searchAttrSet.toArray(new String[searchAttrSet.size()]));
        NamingEnumeration<SearchResult> namingenumeration = ctx.search(base, filter, searchcontrols);
        // 得到所有符合条件的条目
        if (namingenumeration != null) {
            while (namingenumeration.hasMore()) {
                SearchResult searchresult = (SearchResult) namingenumeration.next();
                String dn = base.length() > 0 ? searchresult.getName() + "," + base : searchresult.getName();
                entries.add(toLdapEntry(searchresult.getAttributes(), dn));
            }
        }
        return entries;
    } catch (NamingException e) {
        throw e;
    }
}
Also used : ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 77 with NamingException

use of javax.naming.NamingException in project yyl_example by Relucent.

the class LdapDaoHelper method get.

/**
	 * 查找条目
	 * @param base 基准,意为从某个条目下搜索
	 * @param filter 过滤条件
	 * @param searchAttrSet 查找的字段
	 * @param ctx LDAP上下文连接
	 * @return 条目列表
	 * @deprecated 该方法未经过严谨测试
	 */
public static LdapEntry get(String base, String filter, Set<String> searchAttrSet, LdapContext ctx) throws NamingException {
    try {
        SearchControls searchcontrols = new SearchControls();
        searchcontrols.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchcontrols.setReturningAttributes(searchAttrSet.toArray(new String[searchAttrSet.size()]));
        NamingEnumeration<SearchResult> namingenumeration = ctx.search(base, filter, searchcontrols);
        // 得到所有符合条件的条目
        if (namingenumeration != null) {
            while (namingenumeration.hasMore()) {
                SearchResult searchresult = (SearchResult) namingenumeration.next();
                String dn = base.length() > 0 ? searchresult.getName() + "," + base : searchresult.getName();
                return toLdapEntry(searchresult.getAttributes(), dn);
            }
        }
        return null;
    } catch (NamingException e) {
        throw e;
    }
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 78 with NamingException

use of javax.naming.NamingException in project yyl_example by Relucent.

the class LdapDaoHelper method create.

/**
	 * 创建新条目
	 * @param entry 新条目
	 * @param ctx LDAP上下文连接
	 * @deprecated 该方法未经过严谨测试
	 */
public static void create(LdapEntry entry, LdapContext ctx) throws NamingException {
    try {
        String dn = entry.getDn();
        Attributes attrs = new BasicAttributes(true);
        if (entry != null && !entry.isEmpty()) {
            Iterator<String> iterator = entry.keySet().iterator();
            while (iterator.hasNext()) {
                String id = iterator.next();
                Attribute attr = new BasicAttribute(id);
                List<?> values = entry.getAll(id);
                if (values != null) {
                    for (Object value : values) {
                        attr.add(value);
                    }
                }
                attrs.put(attr);
            }
        }
        ctx.createSubcontext(dn, attrs);
    } catch (NamingException e) {
        throw e;
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException)

Example 79 with NamingException

use of javax.naming.NamingException in project ACS by ACS-Community.

the class ManagerImpl method bind.

/**
	 * Bind object to remote directory.
	 *
	 * Use INS syntax specified in the INS specification.
	 * In short, the syntax is that components are left-to-right slash ('/') separated and case-sensitive.
	 * The id and kind of each component are separated by the period character ('.').
	 *
	 * @param	remoteDirectory	remote directory context to be used.
	 * @param	name	name of the object, code non-<code>null</code>
	 * @param	object	object to be binded
	 */
private void bind(Context remoteDirectory, String name, String type, Object object) {
    assert (name != null);
    // do not bind interdomain names
    if (name.startsWith(CURL_URI_SCHEMA))
        return;
    if (remoteDirectory != null) {
        try {
            // hierarchical name check
            int pos = name.indexOf('/');
            if (pos != -1) {
                if (pos == 0 || pos == name.length())
                    throw new IllegalArgumentException("Invalid hierarchical name '" + name + "'.");
                String parent = name.substring(0, pos);
                String child = name.substring(pos + 1);
                // lookup for subcontext, if not found create one
                Context parentContext = null;
                try {
                    parentContext = (Context) remoteDirectory.lookup(parent);
                } catch (NameNotFoundException nnfe) {
                // noop
                }
                if (parentContext == null) {
                    parentContext = remoteDirectory.createSubcontext(parent);
                /// @todo temp. commented out
                //generateHiearchyContexts(remoteDirectory, parent, parentContext);
                }
                // delegate
                bind(parentContext, child, type, object);
                return;
            }
            NameParser parser = remoteDirectory.getNameParser("");
            Name n;
            if (type != null)
                n = parser.parse(name + "." + type);
            else
                n = parser.parse(name);
            // special case
            if (name.endsWith(".D")) {
                remoteDirectory.rebind(n, object);
            /// @todo temp. commented out
            //generateHiearchyContexts(remoteDirectory, name, (Context)remoteDirectory.lookup(name));
            } else
                remoteDirectory.bind(n, object);
        } catch (NameAlreadyBoundException nabe) {
            rebind(remoteDirectory, name, type, object);
        } catch (NamingException ne) {
            CoreException ce = new CoreException("Failed to bind name '" + name + "' to the remote directory.", ne);
            logger.log(Level.FINE, ce.getMessage(), ce);
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) CoreException(com.cosylab.acs.maci.CoreException) NameNotFoundException(javax.naming.NameNotFoundException) NamingException(javax.naming.NamingException) NameParser(javax.naming.NameParser) Name(javax.naming.Name)

Example 80 with NamingException

use of javax.naming.NamingException in project ACS by ACS-Community.

the class ManagerImpl method lookup.

/**
	 * Lookups for an object in remote directory.
	 *
	 * Use INS syntax specified in the INS specification.
	 * In short, the syntax is that components are left-to-right slash ('/') separated and case-sensitive.
	 * The id and kind of each component are separated by the period character ('.').
	 *
	 * @param	remoteDirectory	remote directory context to be used.
	 * @param	name	name of the object, code non-<code>null</code>
	 * @param	type	type of the object
	 * @return	object	object found in the remote directory, <code>null<code> if nout found
	 */
private Object lookup(Context remoteDirectory, String name, String type) {
    assert (name != null);
    // do not look for interdomain names
    if (name.startsWith(CURL_URI_SCHEMA))
        return null;
    Object resolved = null;
    if (remoteDirectory != null) {
        try {
            NameParser parser = remoteDirectory.getNameParser("");
            Name n;
            if (type != null)
                n = parser.parse(name + "." + type);
            else
                n = parser.parse(name);
            resolved = remoteDirectory.lookup(n);
        } catch (NamingException ne) {
            CoreException ce = new CoreException("Failed to lookup name '" + name + "' in the remote directory.", ne);
            logger.log(Level.FINE, ce.getMessage(), ce);
        }
    }
    return resolved;
}
Also used : CoreException(com.cosylab.acs.maci.CoreException) NamingException(javax.naming.NamingException) NameParser(javax.naming.NameParser) Name(javax.naming.Name)

Aggregations

NamingException (javax.naming.NamingException)698 InitialContext (javax.naming.InitialContext)234 Context (javax.naming.Context)169 IOException (java.io.IOException)82 NameNotFoundException (javax.naming.NameNotFoundException)67 SQLException (java.sql.SQLException)58 Reference (javax.naming.Reference)53 DataSource (javax.sql.DataSource)52 Test (org.junit.Test)51 Attribute (javax.naming.directory.Attribute)49 DirContext (javax.naming.directory.DirContext)48 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)41 Name (javax.naming.Name)36 SearchResult (javax.naming.directory.SearchResult)35 Hashtable (java.util.Hashtable)34 InitialDirContext (javax.naming.directory.InitialDirContext)34 Connection (java.sql.Connection)33 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)32 Attributes (javax.naming.directory.Attributes)30