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:com.netspective.axiom.connection.JakartaCommonsDbcpConnectionProvider.java

public final Connection getConnection(ValueContext vc, String dataSourceId)
        throws NamingException, SQLException {
    if (dataSourceId == null)
        throw new NamingException("name is NULL in " + this.getClass().getName() + ".getConnection(String)");

    DataSource source = getDataSource(vc, dataSourceId);

    if (source == null) {
        if (log.isDebugEnabled())
            log.debug("name not found in " + JakartaCommonsDbcpConnectionProvider.class.getName()
                    + ".getConnection('" + dataSourceId + "'). Available: " + getAvailableDataSources());
        throw new NamingException(
                "Data source '" + dataSourceId + "' not found in Jakarta Commons DBCP provider.");
    }//from   ww w  . ja  v  a 2s  .co m

    return source.getConnection();
}

From source file:com.springsource.insight.plugin.ldap.TestLdapContext.java

private void ensureOpen() throws NamingException {
    if (closed) {
        throw new NamingException("Context marked as closed");
    }/*from w w  w  .jav a2 s .  com*/
}

From source file:com.flexive.core.Database.java

private static DataSource getDataSource(String dataSourceName, boolean useDefaultDataSource)
        throws NamingException, SQLException {
    if (dataSourcesByName.containsKey(dataSourceName)) {
        return dataSourcesByName.get(dataSourceName);
    }//from w ww.  j  a  va2 s.  c o  m
    final Context c = EJBLookup.getInitialContext();
    DataSource dataSource = null;
    for (String path : getPossibleJndiNames(dataSourceName)) {
        try {
            dataSource = (DataSource) c.lookup(path);
            break;
        } catch (NamingException e) {
            // pass
        }
    }
    if (dataSource == null) {
        // Geronimo
        String name = dataSourceName;
        if (name.startsWith("jdbc/")) {
            name = name.substring(5);
        }
        Object o = null;
        try {
            o = c.lookup("jca:/console.dbpool/" + name + "/JCAManagedConnectionFactory/" + name);
            dataSource = (DataSource) o.getClass().getMethod("$getResource").invoke(o);
        } catch (NamingException e) {
            // pass
        } catch (NoSuchMethodException e) {
            if (o instanceof DataSource) {
                return (DataSource) o;
            }
            String sErr = "Unable to retrieve Connection to [" + dataSourceName
                    + "]: JNDI resource is no DataSource and method $getResource not found!";
            LOG.error(sErr);
            throw new SQLException(sErr);
        } catch (Exception ex) {
            String sErr = "Unable to retrieve Connection to [" + dataSourceName + "]: " + ex.getMessage();
            LOG.error(sErr);
            throw new SQLException(sErr);
        }
    }
    if (dataSource == null && useDefaultDataSource && !dataSourceName.contains("/flexiveTest")) {
        dataSource = tryGetDefaultDataSource(c,
                GlobalConfigurationEngineBean.DEFAULT_DS
                        + (dataSourceName.endsWith(NO_TX_SUFFIX) ? NO_TX_SUFFIX : ""),
                new DefaultDivisionDataSourceInitializer());
    }
    if (dataSource == null) {
        // throw exception with the base datasource name, not the last looked-up path
        throw new NamingException("JNDI data source not found: " + dataSourceName);
    }
    dataSourcesByName.put(dataSourceName, dataSource);
    return dataSource;
}

From source file:io.apiman.gateway.engine.policies.BasicAuthLDAPTest.java

private static void injectEntry(LdifEntry entry) throws Exception {
    if (entry.isChangeAdd()) {
        service.getAdminSession().add(new DefaultServerEntry(service.getSchemaManager(), entry.getEntry()));
    } else if (entry.isChangeModify()) {
        service.getAdminSession().modify(entry.getDn(), entry.getModificationItems());
    } else {//from  w  ww  .  java 2s. com
        String message = I18n.err(I18n.ERR_117, entry.getChangeType());
        throw new NamingException(message);
    }
}

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

@Override
public void rename(final Name oldName, final Name newName) throws NamingException {

    ensureContextNotClosed();//from  w ww  . j  a va2  s.  com

    if (newName.isEmpty()) {
        throw new InvalidNameException("Cannot bind to empty name");
    }

    final Object oldValue = lookup(oldName);
    if (oldValue == null) {
        throw new NamingException(String.format("Cannot rename object: name not found (%s)", oldName));
    }

    if (lookup(newName) != null) {
        throw new NameAlreadyBoundException(
                String.format("Cannot rename object: name already bound (%s)", newName));
    }

    unbind(oldName);
    unbind(newName);
    bind(newName, oldValue);
}

From source file:edu.vt.middleware.ldap.AbstractLdap.java

/**
 * This will query the LDAP with the supplied dn, filter, filter arguments,
 * and search controls. See {@link #search(String, String, Object[],
 * SearchControls, SearchResultHandler...)}. The PagedResultsControl is used
 * in conjunction with {@link LdapConfig#getPagedResultsSize()} to produce the
 * results.//from   w ww.  j  av a2  s .c o  m
 *
 * @param  dn  <code>String</code> name to begin search at
 * @param  filter  <code>String</code> expression to use for the search
 * @param  filterArgs  <code>Object[]</code> to substitute for variables in
 * the filter
 * @param  searchControls  <code>SearchControls</code> to perform search with
 * @param  handler  <code>SearchResultHandler[]</code> to post process results
 *
 * @return  <code>Iterator</code> - of LDAP search results
 *
 * @throws  NamingException  if the LDAP returns an error
 */
protected Iterator<SearchResult> pagedSearch(final String dn, final String filter, final Object[] filterArgs,
        final SearchControls searchControls, final SearchResultHandler... handler) throws NamingException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Paginated search with the following parameters:");
        this.logger.debug("  dn = " + dn);
        this.logger.debug("  filter = " + filter);
        this.logger.debug("  filterArgs = " + Arrays.toString(filterArgs));
        this.logger.debug("  searchControls = " + searchControls);
        this.logger.debug("  handler = " + Arrays.toString(handler));
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("  config = " + this.config.getEnvironment());
        }
    }

    final List<SearchResult> results = new ArrayList<SearchResult>();
    LdapContext ctx = null;
    NamingEnumeration<SearchResult> en = null;
    try {
        for (int i = 0; i <= this.config.getOperationRetry() || this.config.getOperationRetry() == -1; i++) {
            try {
                byte[] cookie = null;
                ctx = this.getContext();
                ctx.setRequestControls(new Control[] {
                        new PagedResultsControl(this.config.getPagedResultsSize(), Control.CRITICAL), });
                do {
                    List<SearchResult> pagedResults = null;
                    en = ctx.search(dn, filter, filterArgs, searchControls);

                    if (handler != null && handler.length > 0) {
                        final SearchCriteria sc = new SearchCriteria();
                        if (ctx != null && !"".equals(ctx.getNameInNamespace())) {
                            sc.setDn(ctx.getNameInNamespace());
                        } else {
                            sc.setDn(dn);
                        }
                        sc.setFilter(filter);
                        sc.setFilterArgs(filterArgs);
                        if (searchControls != null) {
                            sc.setReturnAttrs(searchControls.getReturningAttributes());
                        }
                        for (int j = 0; j < handler.length; j++) {
                            if (j == 0) {
                                pagedResults = handler[j].process(sc, en,
                                        this.config.getHandlerIgnoreExceptions());
                            } else {
                                pagedResults = handler[j].process(sc, pagedResults);
                            }
                        }
                    } else {
                        pagedResults = SR_COPY_RESULT_HANDLER.process(null, en,
                                this.config.getHandlerIgnoreExceptions());
                    }

                    results.addAll(pagedResults);

                    final Control[] controls = ctx.getResponseControls();
                    if (controls != null) {
                        for (int j = 0; j < controls.length; j++) {
                            if (controls[j] instanceof PagedResultsResponseControl) {
                                final PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[j];
                                cookie = prrc.getCookie();
                            }
                        }
                    }

                    // re-activate paged results
                    ctx.setRequestControls(
                            new Control[] { new PagedResultsControl(this.config.getPagedResultsSize(), cookie,
                                    Control.CRITICAL), });

                } while (cookie != null);

                break;
            } catch (NamingException e) {
                this.operationRetry(ctx, e, i);
            } catch (IOException e) {
                if (this.logger.isErrorEnabled()) {
                    this.logger.error("Could not encode page size into control", e);
                }
                throw new NamingException(e.getMessage());
            }
        }
    } finally {
        if (en != null) {
            en.close();
        }
        if (ctx != null) {
            ctx.close();
        }
    }
    return results.iterator();
}

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

/**
 * Sets the full name of this context within its own namespace.
 *
 * @param name/*from   ww w .  j a va  2 s .c o  m*/
 *            the context's name
 * @throws NamingException
 *             if a name already set
 */
protected void setNameInNamespace(final Name name) throws NamingException {
    if (nameInNamespace != null && !nameInNamespace.isEmpty()) {
        throw new NamingException("Name already set.");
    }
    nameInNamespace = name;
}

From source file:fedora.server.security.servletfilters.ldap.FilterLdap.java

private NamingEnumeration getBasicNamingEnumeration(String userid, String password, String filter,
        SearchControls searchControls, Hashtable env) throws NamingException, Exception {
    String m = FilterSetup.getFilterNameAbbrev(FILTER_NAME) + " getNamingEnumeration() ";
    log.debug(m + ">");
    NamingEnumeration ne = null;/*from   w w w .ja v a 2 s  .  com*/
    try {
        DirContext ctx;
        try {
            ctx = new InitialDirContext(env);
        } catch (NamingException th) {
            String msg = "exception getting ldap context";
            if (LOG_STACK_TRACES) {
                log.error(m + msg, th);
            } else {
                log.error(m + msg + " " + th.getMessage());
            }
            throw th;
        }
        if (ctx == null) {
            log.error(m + "unexpected null ldap context");
            throw new NamingException("");
        }
        try {
            ne = ctx.search(BASE, filter, searchControls);
        } catch (NamingException th) {
            String msg = "exception getting ldap enumeration";
            if (LOG_STACK_TRACES) {
                log.error(m + msg, th);
            } else {
                log.error(m + msg + " " + th.getMessage());
            }
            throw th;
        }
        if (ne == null) {
            log.error(m + "unexpected null ldap enumeration");
            throw new NamingException("");
        }
    } finally {
        log.debug(m + "< " + ne);
    }
    return ne;
}

From source file:ldap.ActiveLoginImpl.java

public Attributes hashPasswordAttribute(Attributes account) throws NamingException {
    Attribute pwdAtt = account.get(LdapConstants.ldapAttrUserPassword);
    if (pwdAtt == null || pwdAtt.get() == null)
        throw new NamingException("user password attribute missing!");

    logger.info("entered hashPassword()" + pwdAtt);
    Object o = pwdAtt.get();/*from  w ww. j a va 2s .  c  om*/
    logger.info("entered hashPassword()");
    byte[] hash = hashPassword(o);
    logger.info("completed hashPassword()");

    account.remove(LdapConstants.ldapAttrUserPassword);
    logger.info("adding the ldapAttrUserPassword, " + hash);
    account.put(LdapConstants.ldapAttrUserPassword, hash);
    byte[] pwd = (byte[]) account.get("userPassword").get();
    if (pwd != null) {
        logger.info("getting the ldapAttrUserPassword, " + pwd);
    } else {
        logger.info("hash pwd is null when tried to retrieve it");
    }
    return account;
}

From source file:fedora.server.security.servletfilters.ldap.FilterLdap.java

private NamingEnumeration getNamingEnumeration(String userid, String password, String filter,
        SearchControls searchControls, Hashtable env) throws NamingException, Exception {
    String m = FilterSetup.getFilterNameAbbrev(FILTER_NAME) + " getNamingEnumeration() ";
    log.debug(m + ">");
    // this condition is to -further- protect against behavior suggested by
    // log from hull (see below for first-line protection)
    // the idea here is to steer clear of possible trouble in underlying
    // code and avoid calling ldap w/o a needed and practical password

    String msg = "[LDAP: error code 49 - Bind failed: ";

    if (!individualUserBind()) {
        log.info(m + "-not- binding individual user");
    } else {/* ww w. j  a v  a  2  s .  c o  m*/
        log.info(m + "-binding- individual user");
        if (password == null) {
            log.debug(m + "null password");
            if (USE_FILTER.equalsIgnoreCase(PW_NULL)) {
                log.debug(m + "-no- pre null password handling");
            } else {
                if (AUTHENTICATE) {
                    log.info(m + "-doing- pre null password handling");
                    if (UNAUTHENTICATE_USER_UNCONDITIONALLY.equalsIgnoreCase(PW_NULL)) {
                        log.info(m + "pre unauthenticating for null password");
                        throw new NamingException(msg + "null password]");
                    } else if (SKIP_FILTER.equalsIgnoreCase(PW_NULL)) {
                        log.info(m + "pre ignoring for null passwd");
                        throw new Exception(msg + "null password]");
                    } else {
                        assert true : "bad value for PW_NULL==" + PW_NULL;
                    }
                }
            }
        } else if ("".equals(password)) {
            log.debug(m + "0-length password");
            if (USE_FILTER.equalsIgnoreCase(PW_0)) {
                log.debug(m + "-no- pre 0-length password handling");
            } else {
                if (AUTHENTICATE) {
                    log.info(m + "-doing- pre 0-length password handling");
                    if (UNAUTHENTICATE_USER_UNCONDITIONALLY.equalsIgnoreCase(PW_0)) {
                        log.info(m + "pre unauthenticating for 0-length password");
                        throw new NamingException(msg + "0-length password]");
                    } else if (SKIP_FILTER.equalsIgnoreCase(PW_0)) {
                        log.info(m + "pre ignoring for 0-length passwd");
                        throw new Exception(msg + "0-length password]");
                    } else {
                        assert true : "bad value for PW_0==" + PW_0;
                    }
                }
            }
        } else {
            assert password.length() > 0;
        }
    }

    NamingEnumeration ne = null;
    try {
        ne = getBasicNamingEnumeration(userid, password, filter, searchControls, env);
        assert ne != null;
        if (ne.hasMoreElements()) {
            log.debug(m + "enumeration has elements");
        } else {
            log.debug(m + "enumeration has no elements, yet no exceptions");
            if (bindRequired() && !individualUserBind()) {
                log.debug(m + "failed security bind");
                throw new NamingException(msg + "failed security bind]");
            }
            if (!AUTHENTICATE) {
                log.debug(m + "user authentication -not- done by this filter");
            } else {
                log.debug(m + "user authentication -done- by this filter");
                if (!bindRequired()) {
                    log.debug(m + "but -not- binding");
                } else {
                    log.debug(m + "-and- binding");
                    if (SKIP_FILTER.equalsIgnoreCase(EMPTY_RESULTS)) {
                        log.debug(m + "passing thru for EMPTY_RESULTS");
                        throw new Exception(msg + "null password]");
                    } else if (UNAUTHENTICATE_USER_UNCONDITIONALLY.equalsIgnoreCase(EMPTY_RESULTS)) {
                        log.debug(m + "failing for EMPTY_RESULTS");
                        throw new NamingException(msg + "null password]");
                    } else if (USE_FILTER.equalsIgnoreCase(EMPTY_RESULTS)) {
                        log.debug(m + "passing for EMPTY_RESULTS");
                        //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                    } else if (UNAUTHENTICATE_USER_CONDITIONALLY.equalsIgnoreCase(EMPTY_RESULTS)) {
                        if (ATTRIBUTES2RETURN == null || ATTRIBUTES2RETURN.length < 1) {
                            log.debug(m + "fair enough");
                        } else {
                            throw new NamingException(msg + "expected some");
                        }
                    } else {
                        assert true : "bad value for EMPTY_RESULTS==" + EMPTY_RESULTS;
                    }
                }
            }
        }
    } finally {
        log.debug(m + "< " + ne);
    }
    return ne;
}