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:alpine.auth.LdapConnectionWrapper.java

/**
 * Performs a search for the specified username. Internally, this method queries on
 * the attribute defined by {@link Config.AlpineKey#LDAP_ATTRIBUTE_NAME}.
 * @param ctx the DirContext to use/*from  www  .  j a  va2s  . c o m*/
 * @param username the username to query on
 * @return a list of SearchResult objects. If the username is found, the list should typically only contain one result.
 * @throws NamingException if an exception is thrown
 * @since 1.4.0
 */
public SearchResult searchForSingleUsername(DirContext ctx, String username) throws NamingException {
    final List<SearchResult> results = searchForUsername(ctx, username);
    if (results == null || results.size() == 0) {
        return null;
    } else if (results.size() == 1) {
        return results.get(0);
    } else {
        throw new NamingException(
                "Multiple entries in the directory contain the same username. This scenario is not supported");
    }
}

From source file:com.netspective.axiom.connection.JakartaCommonsDbcpConnectionProvider.java

protected DataSource createDataSource(ValueContext vc, String dataSourceId) throws NamingException {

    DataSourceInfo dataSourceInfo = (DataSourceInfo) dataSourcesInfo.get(dataSourceId);

    if (dataSourceInfo == null)
        throw new NamingException("Data Source: '" + dataSourceId
                + "' not defined as a data source for Jakarta Commons DBCP provider.");

    String driverClassName = dataSourceInfo.driverClass.getTextValueOrBlank(vc);
    try {//from  w  w  w .j av  a 2 s  .  c  om
        Class.forName(driverClassName);
    } catch (ClassNotFoundException cnfe) {
        log.error("Driver '" + driverClassName + "' not found for name '" + dataSourceId + "'");
        throw new NamingException("Driver '" + driverClassName + "' not found for name '" + dataSourceId + "'");
    }

    if (log.isDebugEnabled()) {
        log.debug("Initializing data source: '" + dataSourceInfo.getName() + "'\n"
                + "                  driver: '" + driverClassName + "'\n" + "                     url: '"
                + dataSourceInfo.url.getTextValueOrBlank(vc) + "'\n" + "                    user: '"
                + dataSourceInfo.user.getTextValueOrBlank(vc) + "'\n" + "                password: '"
                + dataSourceInfo.password.getTextValueOrBlank(vc) + "'");
    }

    ObjectPool connectionPool = new GenericObjectPool(null, dataSourceInfo.getPoolConfig());
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            dataSourceInfo.url.getTextValueOrBlank(vc), dataSourceInfo.user.getTextValueOrBlank(vc),
            dataSourceInfo.password.getTextValueOrBlank(vc));
    try {
        //The reference to this object is not used within this method.  It's constuctor sets a reference of itself
        //in the conectionPool object we pass as a parameter.
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);
    } catch (IllegalStateException e) {
        log.error("Trying to reset the pool factory for data source: '" + dataSourceInfo.name
                + "' when the pool objects are already in use, thus the pool is active.");
        return null;
    }

    catch (Exception e) //Generic Exception being caught here because Constructor of PoolableConnectionFactory is declared that way
    {
        log.error(
                "An Exception was encountered when creating the pool factory in the Jakarta Commons DBCP framework.",
                e);
        return null;
    }

    DbcpPoolingDataSource dataSource = new DbcpPoolingDataSource(connectionPool);

    return dataSource;
}

From source file:net.sf.ehcache.distribution.JNDIManualRMICacheManagerPeerProvider.java

/**
 * Get the CachePeer from a local cache.  Test isStale.
 * If it is Stale, look it up in JNDI again and test isStale again.
 *
 * @param jndiProviderUrl// w w w .  j a v a 2s. c o m
 * @return CachePeer
 * @throws NamingException when JNDI lookup fails or when the CachePeer is stale and cannot be reestablished.
 */
private CachePeer lookupCachePeer(String jndiProviderUrl) throws NamingException {
    CachePeer cachePeer = getCachePeer(jndiProviderUrl);
    boolean isAlreadyLookedupRemoteCachePeer = false;

    // The last lookup and test isStale may have caused
    // cachePeer to be null for jndiProviderUrl.
    if (cachePeer == null) {
        cachePeer = lookupRemoteCachePeer(jndiProviderUrl);
        if (cachePeer == null) {
            String msg = "cachePeer null after lookup " + jndiProviderUrl;
            LOG.debug(msg);
            throw new NamingException(msg);
        }
        isAlreadyLookedupRemoteCachePeer = true;
    }
    cachePeer = getNonStaleCachePeer(jndiProviderUrl, cachePeer, isAlreadyLookedupRemoteCachePeer);
    registerCachePeer(jndiProviderUrl, cachePeer);
    return cachePeer;
}

From source file:ldap.LdapApi.java

/**
 * open the directory connection.//from  w  w w. j av a  2  s  . co  m
 * @param url
 * @param dn
 * @param password
 * @param tracing
 * @return DirContext - context
 * @throws NamingException
 */
private DirContext setupJNDIConnection(String url, String userDN, String password, boolean tracing)
        throws NamingException {
    /*
    *  setup  environment variables to sensible default valuse
    */
    Hashtable env = new Hashtable();
    // sanity check
    if (url == null) {
        throw new LdapException("URL not specified in openContext()!");
    }

    // tracing on/off, since it can't be set once the connection is open.
    if (tracing) {
        env.put("com.sun.jndi.ldap.trace.ber", System.err); // echo trace to standard error output
    }

    //env.put("java.naming.ldap.version", "3");               // always use ldap v3 - v2 too limited
    env.put(LdapConstants.ldapVersionStr, LdapConstants.ldapVersion); // always use ldap v3 - v2 too limited
    env.put(Context.INITIAL_CONTEXT_FACTORY, LdapConstants.ldapContext); // use default jndi provider
    env.put(LdapConstants.ldapDeleteRdn, LdapConstants.ldapDeleteRdnValue); // usually what we want
    //env.put(Context.REFERRAL, "ignore");                    //could be: follow, ignore, throw
    env.put(Context.REFERRAL, LdapConstants.ldapIgnore); //could be: follow, ignore, throw
    // env.put("java.naming.ldap.derefAliases", "finding");    // could be: finding, searching, etc.
    env.put(LdapConstants.ldapFindingAliases, LdapConstants.ldapFindingStr); // could be: finding, searching, etc.

    //env.put(Context.SECURITY_AUTHENTICATION, "simple");         // 'simple' = username + password
    env.put(Context.SECURITY_AUTHENTICATION, LdapConstants.ldapSecurityAuth); // 'simple' = username + password

    env.put(Context.SECURITY_PRINCIPAL, userDN); // add the full user dn
    env.put(Context.SECURITY_CREDENTIALS, password); // stupid jndi requires us to cast this to a string-
    env.put(Context.PROVIDER_URL, url); // the ldap url to connect to; e.g. "ldap://ca.com:389"

    /*
     *  Open the actual LDAP session using the above environment variables
     */
    context = new InitialDirContext(env);
    if (context == null) {
        throw new NamingException(
                "Internal Error with jndi connection: No Context was returned, however no exception was reported by jndi.");
    } else {
        logger.info("context is not null");
    }
    return context;
}

From source file:com.dattack.naming.AbstractContext.java

@Override
public NamingEnumeration<Binding> listBindings(final Name name) throws NamingException {

    ensureContextNotClosed();/*from  w w  w  . ja va2 s  .c o m*/

    if (name == null || name.isEmpty()) {
        final Map<Name, Object> enumStore = new HashMap<Name, Object>();
        enumStore.putAll(objectTable);
        enumStore.putAll(subContexts);
        return new BindingNamingEnumeration(enumStore);
    }

    final Name subName = name.getPrefix(1);

    if (objectTable.containsKey(subName)) {
        throw new NotContextException(String.format("%s cannot be listed", name));
    }

    if (subContexts.containsKey(subName)) {
        return subContexts.get(subName).listBindings(name.getSuffix(1));
    }

    throw new NamingException(String.format("The named context '%s' can't be found", name));
}

From source file:com.enioka.jqm.tools.JndiContext.java

@Override
public void bind(String name, Object obj) throws NamingException {
    jqmlogger.debug("binding [" + name + "] to a [" + obj.getClass().getCanonicalName() + "]");
    if (r != null && name.startsWith("rmi://")) {
        try {/*  w  ww .  ja  v  a 2s. co  m*/
            jqmlogger.debug(
                    "binding [" + name.split("/")[3] + "] to a [" + obj.getClass().getCanonicalName() + "]");
            this.r.bind(name.split("/")[3], (Remote) obj);
        } catch (Exception e) {
            NamingException e1 = new NamingException("could not bind RMI object");
            e1.setRootCause(e);
            throw e1;
        }
    } else {
        this.singletons.put(name, obj);
    }
}

From source file:net.sf.ehcache.distribution.JNDIManualRMICacheManagerPeerProvider.java

/**
 * @param jndiProviderUrl//from ww  w.  j  a  v a  2 s.c o  m
 * @param cachePeer       - may not be null
 * @param isAlreadyLookedupRemoteCachePeer
 *
 * @return a CachePeer that is not stale
 * @throws NamingException
 */
private CachePeer getNonStaleCachePeer(final String jndiProviderUrl, final CachePeer cachePeer,
        final boolean isAlreadyLookedupRemoteCachePeer) throws NamingException {
    boolean isStale = isStale(cachePeer);
    CachePeer localCachePeer = null;
    if (isStale) {
        if (!isAlreadyLookedupRemoteCachePeer) {
            LOG.debug("CachePeer is stale, looking it up again " + jndiProviderUrl);

            // The cachePeer is stale.  Look it up again.
            localCachePeer = lookupRemoteCachePeer(jndiProviderUrl);
            if (!isStale(localCachePeer)) {
                isStale = false;
            }
        }
    } else {
        localCachePeer = cachePeer;
    }

    if (isStale) {
        String msg = "After lookup CachePeer is stale " + jndiProviderUrl;
        LOG.info(msg);
        throw new NamingException(msg);
    }

    return localCachePeer;
}

From source file:com.dattack.naming.AbstractContext.java

@Override
public Object lookup(final Name name) throws NamingException {

    ensureContextNotClosed();//  w w w.java 2s .  c o m

    /*
     * Extract from Context Javadoc: If name is empty, returns a new instance of this context (which represents the
     * same naming context as this context, but its environment may be modified independently and it may be accessed
     * concurrently).
     */
    if (name.isEmpty()) {
        try {
            return this.clone();
        } catch (final CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw (NamingException) new OperationNotSupportedException(e.getMessage()).initCause(e);
        }
    }

    if (name.size() > 1) {
        if (subContexts.containsKey(name.getPrefix(1))) {
            return subContexts.get(name.getPrefix(1)).lookup(name.getSuffix(1));
        }
        throw new NamingException(
                String.format("Invalid subcontext '%s' in context '%s'", name.getPrefix(1).toString(),
                        StringUtils.isBlank(getNameInNamespace()) ? "/" : getNameInNamespace()));
    }

    Object result = null; // not found
    if (objectTable.containsKey(name)) {
        result = objectTable.get(name);
    } else if (subContexts.containsKey(name)) {
        result = subContexts.get(name);
    } else if (env.containsKey(name.toString())) {
        result = env.get(name.toString());
    }

    return result;
}

From source file:com.enioka.jqm.tools.JndiContext.java

@Override
public void unbind(String name) throws NamingException {
    if (r != null && name.startsWith("rmi://")) {
        try {/*from w  ww .  ja  v  a 2s . c o m*/
            jqmlogger.debug("unbinding RMI name " + name);
            this.r.unbind(name.split("/")[3]);
        } catch (Exception e) {
            NamingException e1 = new NamingException("could not unbind RMI name");
            e1.setRootCause(e);
            throw e1;
        }
    } else {
        this.singletons.remove(name);
    }
}

From source file:ldap.ActiveLoginImpl.java

/**
 * open the directory connection./*ww  w  . j  ava2s.  c o m*/
 * @param url
 * @param tracing
 * @return
 * @throws NamingException
 */
private DirContext setupJNDIConnection(String url, String userDN, String password, boolean tracing)
        throws NamingException {
    /*
     * First, set up a large number of environment variables to sensible default valuse
     */

    Hashtable env = new Hashtable();
    // sanity check
    if (url == null)
        throw new NamingException("URL not specified in openContext()!");

    // set the tracing level now, since it can't be set once the connection is open.
    if (tracing)
        env.put("com.sun.jndi.ldap.trace.ber", System.err); // echo trace to standard error output

    //env.put("java.naming.ldap.version", "3");               // always use ldap v3 - v2 too limited
    env.put(LdapConstants.ldapVersionStr, LdapConstants.ldapVersion); // always use ldap v3 - v2 too limited

    //env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");  // use default jndi provider
    env.put(Context.INITIAL_CONTEXT_FACTORY, LdapConstants.ldapContext); // use default jndi provider

    //env.put("java.naming.ldap.deleteRDN", "false");         // usually what we want
    env.put(LdapConstants.ldapDeleteRdn, LdapConstants.ldapDeleteRdnValue); // usually what we want

    //env.put(Context.REFERRAL, "ignore");                    //could be: follow, ignore, throw
    env.put(Context.REFERRAL, LdapConstants.ldapIgnore); //could be: follow, ignore, throw

    // env.put("java.naming.ldap.derefAliases", "finding");    // could be: finding, searching, etc.
    env.put(LdapConstants.ldapFindingAliases, LdapConstants.ldapFindingStr); // could be: finding, searching, etc.

    //env.put(Context.SECURITY_AUTHENTICATION, "simple");         // 'simple' = username + password
    env.put(Context.SECURITY_AUTHENTICATION, LdapConstants.ldapSecurityAuth); // 'simple' = username + password

    env.put(Context.SECURITY_PRINCIPAL, userDN); // add the full user dn

    env.put(Context.SECURITY_CREDENTIALS, password); // stupid jndi requires us to cast this to a string-

    env.put(Context.PROVIDER_URL, url); // the ldap url to connect to; e.g. "ldap://ca.com:389"

    /*
     *  Open the actual LDAP session using the above environment variables
     */

    DirContext newContext = new InitialDirContext(env);

    if (newContext == null)
        throw new NamingException(
                "Internal Error with jndi connection: No Context was returned, however no exception was reported by jndi.");

    return newContext;

}