Example usage for javax.naming NamingException NamingException

List of usage examples for javax.naming NamingException NamingException

Introduction

In this page you can find the example usage for javax.naming NamingException NamingException.

Prototype

public NamingException(String explanation) 

Source Link

Document

Constructs a new NamingException with an explanation.

Usage

From source file:org.springframework.ldap.pool.DelegatingContext.java

/**
 * @see javax.naming.Context#close()/*  w  w  w.  ja v  a2s.c  o  m*/
 */
public void close() throws NamingException {
    final Context context = this.getInnermostDelegateContext();
    if (context == null) {
        return;
    }

    //Get a local reference so the member can be nulled earlier
    this.delegateContext = null;

    //Return the object to the Pool and then null the pool reference
    try {
        boolean valid = true;

        if (context instanceof FailureAwareContext) {
            FailureAwareContext failureAwareContext = (FailureAwareContext) context;
            if (failureAwareContext.hasFailed()) {
                valid = false;
            }
        }

        if (valid) {
            this.keyedObjectPool.returnObject(this.dirContextType, context);
        } else {
            this.keyedObjectPool.invalidateObject(this.dirContextType, context);
        }
    } catch (Exception e) {
        final NamingException namingException = new NamingException(
                "Failed to return delegate Context to pool.");
        namingException.setRootCause(e);
        throw namingException;
    } finally {
        this.keyedObjectPool = null;
    }
}

From source file:org.springframework.ldap.pool.DelegatingDirContext.java

protected void assertOpen() throws NamingException {
    if (this.delegateDirContext == null) {
        throw new NamingException("DirContext is closed.");
    }/*from  ww w. jav a2  s . co  m*/

    super.assertOpen();
}

From source file:org.springframework.ldap.pool.DelegatingLdapContext.java

protected void assertOpen() throws NamingException {
    if (this.delegateLdapContext == null) {
        throw new NamingException("LdapContext is closed.");
    }//from w w  w . j  a v a 2s . com

    super.assertOpen();
}

From source file:org.wso2.appcloud.core.DomainMappingManager.java

/**
 * Resolve CNAME and A records for the given {@code hostname}.
 *
 * @param domain             hostname to be resolved.
 * @param environmentConfigs environment configuration
 * @return {@link com.google.common.collect.Multimap} of resolved dns entries. This {@link com.google.common.collect.Multimap} will contain the resolved
 * "CNAME" and "A" records from the given {@code hostname}
 * @throws AppCloudException if error occurred while the operation
 *///from w ww . java  2  s .  c  o  m
public Multimap<String, String> resolveDNS(String domain, Hashtable<String, String> environmentConfigs)
        throws AppCloudException, NamingException {
    // result mutimap of dns records. Contains the cname and records resolved by the given hostname
    // ex:  CNAME   => foo.com,bar.com
    //      A       => 192.1.2.3 , 192.3.4.5
    Multimap<String, String> dnsRecordsResult = ArrayListMultimap.create();
    Attributes dnsRecords;
    boolean isARecordFound = false;
    boolean isCNAMEFound = false;

    try {
        if (log.isDebugEnabled()) {
            log.debug("DNS validation: resolving DNS for " + domain + " " + "(A/CNAME)");
        }
        DirContext context = new InitialDirContext(environmentConfigs);
        String[] dnsRecordsToCheck = new String[] { DNS_A_RECORD, DNS_CNAME_RECORD };
        dnsRecords = context.getAttributes(domain, dnsRecordsToCheck);
    } catch (NamingException e) {
        String msg = "DNS validation: DNS query failed for: " + domain + ". Error occurred while configuring "
                + "directory context.";
        log.error(msg, e);
        throw new AppCloudException(msg, e);
    }

    try {
        // looking for for A records
        Attribute aRecords = dnsRecords.get(DNS_A_RECORD);
        if (aRecords != null && aRecords.size() > 0) { // if an A record exists
            NamingEnumeration aRecordHosts = aRecords.getAll(); // get all resolved A entries
            String aHost;
            while (aRecordHosts.hasMore()) {
                isARecordFound = true;
                aHost = (String) aRecordHosts.next();
                dnsRecordsResult.put(DNS_A_RECORD, aHost);
                if (log.isDebugEnabled()) {
                    log.debug("DNS validation: A record found: " + aHost);
                }
            }
        }

        // looking for CNAME records
        Attribute cnameRecords = dnsRecords.get(DNS_CNAME_RECORD);
        if (cnameRecords != null && cnameRecords.size() > 0) { // if CNAME record exists
            NamingEnumeration cnameRecordHosts = cnameRecords.getAll(); // get all resolved CNAME entries for hostname
            String cnameHost;
            while (cnameRecordHosts.hasMore()) {
                isCNAMEFound = true;
                cnameHost = (String) cnameRecordHosts.next();
                if (cnameHost.endsWith(".")) {
                    // Since DNS records are end with "." we are removing it.
                    // For example real dns entry for www.google.com is www.google.com.
                    cnameHost = cnameHost.substring(0, cnameHost.lastIndexOf('.'));
                }
                dnsRecordsResult.put(DNS_CNAME_RECORD, cnameHost);
                if (log.isDebugEnabled()) {
                    log.debug("DNS validation: recurring on CNAME record towards host " + cnameHost);
                }
                dnsRecordsResult.putAll(resolveDNS(cnameHost, environmentConfigs)); // recursively resolve cnameHost
            }
        }

        if (!isARecordFound && !isCNAMEFound && log.isDebugEnabled()) {
            log.debug("DNS validation: No CNAME or A record found for domain: '" + domain);
        }
        return dnsRecordsResult;
    } catch (NamingException ne) {
        String msg = "DNS validation: DNS query failed for: " + domain + ". Provided domain: " + domain
                + " might be a " + "non existing domain.";
        // we are logging this as warn messages since this is caused, due to an user error. For example if the
        // user entered a rubbish custom url(Or a url which is, CNAME record is not propagated at the
        // time of adding the url), then url validation will fail but it is not an system error
        log.warn(msg, ne);
        throw new NamingException(msg);
    }
}

From source file:org.wso2.carbon.appfactory.ext.jndi.ApplicationAwareCarbonInitialJNDIContext.java

public Hashtable<?, ?> getEnvironment() throws NamingException {
    if (isSubTenant(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId())) {
        throw new NamingException("Tenants cannot retrieve the environment.");
    }/*from   ww  w  .  j  a va  2 s .co m*/
    return getInitialContext().getEnvironment();
}

From source file:org.wso2.carbon.appfactory.ext.jndi.ApplicationAwareCarbonInitialJNDIContext.java

private DirContext getDirectoryContext(String name) throws NamingException {
    Context initialContext = getInitialContext(name);
    if (initialContext instanceof DirContext) {
        return (DirContext) initialContext;
    }/*from   w w w. j av a 2 s.c  om*/
    throw new NamingException("The given Context is not an instance of " + DirContext.class.getName());
}

From source file:org.wso2.carbon.appfactory.ext.jndi.ApplicationAwareCarbonInitialJNDIContext.java

private LdapContext getLdapContext() throws NamingException {
    DirContext dirContext = getDirectoryContext();
    if (dirContext instanceof EventContext) {
        return (LdapContext) dirContext;
    }//  www  . j av  a 2  s.  c o m
    throw new NamingException("The given Context is not an instance of " + LdapContext.class.getName());
}

From source file:org.wso2.carbon.appfactory.ext.jndi.ApplicationAwareCarbonInitialJNDIContext.java

private EventContext getEventContext(String name) throws NamingException {
    Context initialContext = getInitialContext(name);
    if (initialContext instanceof EventContext) {
        return (EventContext) initialContext;
    }/*from w ww.ja  v  a2s  . co m*/
    throw new NamingException("The given Context is not an instance of " + EventContext.class.getName());
}

From source file:org.wso2.carbon.appfactory.ext.jndi.ApplicationAwareCarbonInitialJNDIContext.java

private EventDirContext getEventDirContext(String name) throws NamingException {
    EventContext eventContext = getEventContext(name);
    if (eventContext instanceof EventDirContext) {
        return (EventDirContext) eventContext;
    }/*from   www. jav a 2  s . c  o  m*/
    throw new NamingException("The given Context is not an instance of " + EventDirContext.class.getName());
}

From source file:org.wso2.carbon.connector.ldap.SearchEntry.java

@Override
public void connect(MessageContext messageContext) throws ConnectException {
    String objectClass = (String) getParameter(messageContext, LDAPConstants.OBJECT_CLASS);
    String filter = (String) getParameter(messageContext, LDAPConstants.FILTERS);
    String dn = (String) getParameter(messageContext, LDAPConstants.DN);
    String returnAttributes[] = ((String) getParameter(messageContext, LDAPConstants.ATTRIBUTES)).split(",");
    boolean onlyOneReference = Boolean
            .valueOf((String) getParameter(messageContext, LDAPConstants.ONLY_ONE_REFERENCE));

    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, LDAPConstants.NAMESPACE);
    OMElement result = factory.createOMElement(LDAPConstants.RESULT, ns);

    try {/*from   w w w . j  ava 2  s .  c  o  m*/
        DirContext context = LDAPUtils.getDirectoryContext(messageContext);

        String attrFilter = generateAttrFilter(filter);
        String searchFilter = generateSearchFilter(objectClass, attrFilter);
        NamingEnumeration<SearchResult> results = null;
        try {
            results = searchInUserBase(dn, searchFilter, returnAttributes, SearchControls.SUBTREE_SCOPE,
                    context);
            SearchResult entityResult = null;

            if (!onlyOneReference) {
                if (results != null && results.hasMore()) {
                    while (results.hasMore()) {
                        entityResult = results.next();
                        result.addChild(prepareNode(entityResult, factory, ns, returnAttributes));
                    }
                }
            } else {
                entityResult = makeSureOnlyOneMatch(results);
                if (entityResult == null)
                    throw new NamingException(
                            "Multiple objects for the searched target have been found. Try to "
                                    + "change onlyOneReference option");
                result.addChild(prepareNode(entityResult, factory, ns, returnAttributes));
            }

            LDAPUtils.preparePayload(messageContext, result);

            if (context != null) {
                context.close();
            }

        } catch (NamingException e) { //LDAP Errors are catched
            LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.SEARCH_ERROR, e);
            throw new SynapseException(e);
        }

    } catch (NamingException e) { //Authentication failures are catched
        LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS, e);
        throw new SynapseException(e);
    }
}