Search in sources :

Example 21 with NamingException

use of javax.naming.NamingException in project jetty.project by eclipse.

the class NamingEntry method unbindENC.

/**
     * Unbind this NamingEntry from a java:comp/env
     */
public void unbindENC() {
    try {
        InitialContext ic = new InitialContext();
        Context env = (Context) ic.lookup("java:comp/env");
        __log.debug("Unbinding java:comp/env/" + getJndiName());
        env.unbind(getJndiName());
    } catch (NamingException e) {
        __log.warn(e);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 22 with NamingException

use of javax.naming.NamingException in project jetty.project by eclipse.

the class NamingEntryUtil method bindToENC.

/**
     * Link a name in a webapp's java:/comp/evn namespace to a pre-existing
     * resource. The pre-existing resource can be either in the webapp's
     * naming environment, or in the container's naming environment. Webapp's
     * environment takes precedence over the server's namespace.
     *
     * @param scope the scope of the lookup
     * @param asName the name to bind as
     * @param mappedName the name from the environment to link to asName
     * @return true if bind success, false if not bound
     * @throws NamingException if unable to bind
     */
public static boolean bindToENC(Object scope, String asName, String mappedName) throws NamingException {
    if (asName == null || asName.trim().equals(""))
        throw new NamingException("No name for NamingEntry");
    if (mappedName == null || "".equals(mappedName))
        mappedName = asName;
    NamingEntry entry = lookupNamingEntry(scope, mappedName);
    if (entry == null)
        return false;
    entry.bindToENC(asName);
    return true;
}
Also used : NamingException(javax.naming.NamingException)

Example 23 with NamingException

use of javax.naming.NamingException in project tomcat by apache.

the class NamingContext method bind.

/**
     * Binds a name to an object. All intermediate contexts and the target
     * context (that named by all but terminal atomic component of the name)
     * must already exist.
     *
     * @param name the name to bind; may not be empty
     * @param obj the object to bind; possibly null
     * @param rebind if true, then perform a rebind (ie, overwrite)
     * @exception NameAlreadyBoundException if name is already bound
     * @exception javax.naming.directory.InvalidAttributesException if object
     * did not supply all mandatory attributes
     * @exception NamingException if a naming exception is encountered
     */
protected void bind(Name name, Object obj, boolean rebind) throws NamingException {
    if (!checkWritable()) {
        return;
    }
    while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException(sm.getString("namingContext.invalidName"));
    NamingEntry entry = bindings.get(name.get(0));
    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0)));
        }
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
            }
        } else {
            throw new NamingException(sm.getString("namingContext.contextExpected"));
        }
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException(sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind = NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
            }
            bindings.put(name.get(0), entry);
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) Referenceable(javax.naming.Referenceable) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Example 24 with NamingException

use of javax.naming.NamingException in project hadoop by apache.

the class DNS method getHosts.

/**
   * Returns all the host names associated by the provided nameserver with the
   * address bound to the specified network interface
   *
   * @param strInterface
   *            The name of the network interface or subinterface to query
   *            (e.g. eth0 or eth0:0)
   * @param nameserver
   *            The DNS host name
   * @param tryfallbackResolution
   *            if true and if reverse DNS resolution fails then attempt to
   *            resolve the hostname with
   *            {@link InetAddress#getCanonicalHostName()} which includes
   *            hosts file resolution.
   * @return A string vector of all host names associated with the IPs tied to
   *         the specified interface
   * @throws UnknownHostException if the given interface is invalid
   */
public static String[] getHosts(String strInterface, @Nullable String nameserver, boolean tryfallbackResolution) throws UnknownHostException {
    final List<String> hosts = new Vector<String>();
    final List<InetAddress> addresses = getIPsAsInetAddressList(strInterface, true);
    for (InetAddress address : addresses) {
        try {
            hosts.add(reverseDns(address, nameserver));
        } catch (NamingException ignored) {
        }
    }
    if (hosts.isEmpty() && tryfallbackResolution) {
        for (InetAddress address : addresses) {
            final String canonicalHostName = address.getCanonicalHostName();
            // Don't use the result if it looks like an IP address.
            if (!InetAddresses.isInetAddress(canonicalHostName)) {
                hosts.add(canonicalHostName);
            }
        }
    }
    if (hosts.isEmpty()) {
        LOG.warn("Unable to determine hostname for interface " + strInterface);
        hosts.add(cachedHostname);
    }
    return hosts.toArray(new String[hosts.size()]);
}
Also used : NamingException(javax.naming.NamingException) Vector(java.util.Vector) InetAddress(java.net.InetAddress)

Example 25 with NamingException

use of javax.naming.NamingException in project hadoop by apache.

the class LdapGroupsMapping method getRelativeDistinguishedName.

/**
   * A helper method to get the Relative Distinguished Name (RDN) from
   * Distinguished name (DN). According to Active Directory documentation,
   * a group object's RDN is a CN.
   *
   * @param distinguishedName A string representing a distinguished name.
   * @throws NamingException if the DN is malformed.
   * @return a string which represents the RDN
   */
private String getRelativeDistinguishedName(String distinguishedName) throws NamingException {
    LdapName ldn = new LdapName(distinguishedName);
    List<Rdn> rdns = ldn.getRdns();
    if (rdns.isEmpty()) {
        throw new NamingException("DN is empty");
    }
    Rdn rdn = rdns.get(rdns.size() - 1);
    if (rdn.getType().equalsIgnoreCase(groupNameAttr)) {
        String groupName = (String) rdn.getValue();
        return groupName;
    }
    throw new NamingException("Unable to find RDN: The DN " + distinguishedName + " is malformed.");
}
Also used : NamingException(javax.naming.NamingException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Aggregations

NamingException (javax.naming.NamingException)1246 InitialContext (javax.naming.InitialContext)417 Context (javax.naming.Context)259 IOException (java.io.IOException)163 Attribute (javax.naming.directory.Attribute)111 DirContext (javax.naming.directory.DirContext)100 SearchResult (javax.naming.directory.SearchResult)98 ArrayList (java.util.ArrayList)95 SQLException (java.sql.SQLException)93 NameNotFoundException (javax.naming.NameNotFoundException)88 Attributes (javax.naming.directory.Attributes)85 DataSource (javax.sql.DataSource)84 Properties (java.util.Properties)77 Reference (javax.naming.Reference)77 InitialDirContext (javax.naming.directory.InitialDirContext)77 Test (org.junit.Test)75 Hashtable (java.util.Hashtable)73 SearchControls (javax.naming.directory.SearchControls)73 HashMap (java.util.HashMap)55 LdapContext (javax.naming.ldap.LdapContext)55