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.JndiConnectionProvider.java

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

    Context env = getRootContext();
    return (DataSource) env.lookup(dataSourceId);
}

From source file:ldap.SearchUtility.java

/**
 * open the directory connection.//ww  w .  java2s.  co m
 *
 * @param url
 * @param tracing
 * @return
 * @throws javax.naming.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(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // use default jndi provider
    env.put("java.naming.ldap.deleteRDN", "false"); // usually what we want
    env.put(Context.REFERRAL, "ignore"); //could be: follow, ignore, throw
    env.put("java.naming.ldap.derefAliases", "finding"); // could be: finding, searching, etc.
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); // '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;

}

From source file:com.googlecode.psiprobe.beans.JBossResourceResolverBean.java

public boolean resetResource(Context context, String resourceName, ContainerWrapperBean containerWrapper)
        throws NamingException {
    try {//w w w  .j a v a 2 s  .com
        ObjectName poolOName = new ObjectName("jboss.jca:service=ManagedConnectionPool,name=" + resourceName);
        MBeanServer server = getMBeanServer();
        if (server != null) {
            try {
                server.invoke(poolOName, "stop", null, null);
                server.invoke(poolOName, "start", null, null);
                return true;
            } catch (Exception e) {
                logger.error("Could not reset resource \"" + resourceName + "\"", e);
            }
        }
        return false;
    } catch (MalformedObjectNameException e) {
        throw new NamingException("Resource name: \"" + resourceName + "\" makes a malformed ObjectName");
    }
}

From source file:ldap.ActiveLoginImpl.java

/**
 * This updates the UserAccount./*from  www  .j  a  v a  2  s  .com*/
 * It requires at a minimum a name;
 * and optionally any other attributes.
 *
 * Note that this will REPLACE any attributes passed, deleting any existing values
 * for the specified attribute (e.g. if the attribute is userPassword, the old userPassword will
 * be discarded, rather than there being two userPasswords in the entry).
 *
 * Modifying the naming attribute will probably result in an error (depending on the directory).
 *
 * @param account
 * @throws Exception
 */
public void updateAccount(UserAccount account, DirContext context, String userDN) throws Exception {

    //if (account.get(Config.USER_NAMING_ATT) == null)
    if (account.get(LdapConstants.ldapDnAttrType) == null)
        throw new NamingException("UpdateAccount(), UserAccount has no naming Attribute");

    // should not be used
    //logger.info("Updating: \n" + account.getUserDN() + "\n" + account.toString());
    logger.info("Updating: \n" + userDN + "\n" + account.toString());

    // remove the naming attribute from the account before adding

    Attributes atts = copyAttributes(account); // create a local copy

    //atts.remove(Config.USER_NAMING_ATT);  // we can't modify the naming attribute this way, so don't try...
    //atts.remove(LdapConstants.ldapAttrUid);  // we can't modify the naming attribute this way, so don't try...
    atts.remove(LdapConstants.ldapDnAttrType); // we can't modify the naming attribute this way, so don't try...
    atts = hashPasswordAttribute(atts);
    // context.modifyAttributes(account.getUserDN(), DirContext.REPLACE_ATTRIBUTE, atts);
    context.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, atts);
}

From source file:net.e2.bw.servicereg.ldap.ServiceInstanceLdapService.java

/** {@inheritDoc} */
@Override//w  w w  . ja  v a2 s  .c o m
public void assignServiceInstanceRole(String serviceInstanceId, String userId, String role) {
    Objects.requireNonNull(role);
    Objects.requireNonNull(userService.getUser(userId));
    CachedServiceInstance service = Objects.requireNonNull(getCachedServiceInstance(serviceInstanceId));
    String userDN = getUserDN(userId);
    String serviceDN = getServiceInstanceDN(service.getServiceInstanceId());

    // Check if the user already holds the role
    if (service.userHasRole(userId, role)) {
        return;
    }

    try {
        List<String> attrs = Collections.singletonList(getConfig().getRoleMemberAttribute());
        // NB: Create the role entry if it does not exist
        SearchResult roleEntry = searchRoleEntry(serviceDN, role, attrs, true);

        if (roleEntry == null) {
            // Failed looking up or creating the role entry
            throw new NamingException("Error getting role entry " + role + " for service " + serviceDN);
        }

        // Add the user as a role member
        ldapServerService.addUniqueAttributeValue(roleEntry.getNameInNamespace(),
                getConfig().getRoleMemberAttribute(), userDN);
        log.info("Added " + userId + " as " + role + " for service " + serviceDN);

        // Un-cache the organization
        ldapCache.getServiceInstanceCache().evict(serviceInstanceId);

    } catch (NamingException e) {
        throw new RuntimeException("Error assigning service role ", e);
    }
}

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

public Connection getConnection(ValueContext vc, String dataSourceId) throws NamingException, SQLException {
    DataSourceInfo dsInfo = getDataSourceInfo(dataSourceId);
    if (dsInfo != null) {
        try {/* w ww  .  ja v a  2 s.c om*/
            Class.forName(dsInfo.getDriverName());
        } catch (ClassNotFoundException cnfe) {
            throw new NamingException("Driver '" + dsInfo.getDriverName() + "' not found for dataSourceId '"
                    + dataSourceId + "'");
        }
        return DriverManager.getConnection(dsInfo.getConnUrl(), dsInfo.getConnUser(), dsInfo.getConnPassword());
    } else
        throw new NamingException("Information for DataSource '" + dataSourceId + "' not found.");
}

From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java

public static String getObjectSID(Attributes attrs) throws NamingException {
    Attribute attr = attrs.get("objectsid");
    if (attr == null)
        throw new NamingException("No ObjectSID attribute");
    Object attrObject = attr.get();
    if (attrObject == null)
        throw new NamingException("ObjectSID is empty");
    if (attrObject instanceof String) {
        String attrString = (String) attrObject;
        if (attrString.startsWith("S-"))
            return attrString;
        return decodeSID(attrString.getBytes());
    } else if (attrObject instanceof byte[]) {
        return decodeSID((byte[]) attrObject);
    } else//w w w  .  ja v a2 s .com
        throw new NamingException("Unknown attribute type: " + attrObject.getClass().getName());
}

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

private Context getParentContext(final Name name) throws NamingException {

    final Object context = lookup(name.getPrefix(name.size() - 1));
    if (context instanceof Context) {
        return (Context) context;
    }/*w ww . j  av a  2 s . c o  m*/
    throw new NamingException(String.format("Cannot unbind object. Target context does not exist (%s)",
            name.getPrefix(name.size() - 1)));
}

From source file:ldap.ActiveLoginImpl.java

/**
 * This adds a new user.  It requires at the minimum a name, it should also
 * usually have a surname and a password at a minimum.
 *
 * @param account/*ww  w  . ja v  a  2s  .co  m*/
 * @throws Exception
 */
public void addAccount(UserAccount account, DirContext context, String userBaseDN) throws Exception {
    // set some default values for the user entry if they haven't been manually added.

    //if (account.get(Config.USER_NAMING_ATT) == null)
    if (account.get(LdapConstants.ldapAttrCn) == null)
        throw new NamingException("addAccount(), UserAccount has no naming Attribute");

    if (account.get(LdapConstants.ldapObjectClass) == null) {
        //Attribute oc = new BasicAttribute("objectClass");
        Attribute oc = new BasicAttribute(LdapConstants.ldapObjectClass);

        if (LdapConstants.ldapObjectClassEmployeeEnable) {
            //oc.add("employee");
            oc.add(LdapConstants.ldapObjectClassEmployee);
        }

        //old redbasin stuff   
        /*   
               if (LdapConstants.ldapAttrTopEnable) {
                       oc.add(LdapConstants.ldapAttrTop); 
               }
               if (LdapConstants.ldapAttrPersonEnable) {
                       oc.add(LdapConstants.ldapAttrPerson); 
               }
               if (LdapConstants.ldapAttrOrgPersonEnable) {
                       oc.add(LdapConstants.ldapAttrOrgPerson); 
               }
               if (LdapConstants.ldapAttrInetOrgPersonEnable) {
                       oc.add(LdapConstants.ldapAttrInetOrgPerson); 
               }
        */
        account.put(oc);
    }

    /*  made changes  */
    /*
            if (account.get("cn") == null)
    account.put("cn", account.getUserID());
            
            if (account.get("sn") == null)
    account.put("sn", "xxx");  // put in default value for required attribute
    */
    if (account.get(LdapConstants.ldapAttrCn) == null)
        account.put(LdapConstants.ldapAttrCn, account.getUserID());

    if (account.get(LdapConstants.ldapAttrSn) == null)
        account.put(LdapConstants.ldapAttrSn, "xxx"); // put in default value for required attribute
    //logger.info("ADDING: \n" + account.getUserDN() + "\n" + account.toString());
    logger.info("ADDING: \n" + userBaseDN + "\n" + account.toString());

    /**
    * deal with the password adding later 
    */
    /*
       Attributes attributes = copyAttributes(account);
       UserAccount myaccount = hashPasswordAttribute(attributes);
    */
    // use this only when we add the user
    //context.createSubcontext(account.getUserDN(), account);
    context.createSubcontext(userBaseDN, account);
}

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

@Override
public NamingEnumeration<NameClassPair> list(final Name name) throws NamingException {

    ensureContextNotClosed();//from   w w w.j  a  va  2 s  .  c  o  m

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

    final Name prefixName = name.getPrefix(1);
    if (objectTable.containsKey(prefixName)) {
        throw new NotContextException(String.format("%s cannot be listed", name));
    }

    if (subContexts.containsKey(prefixName)) {
        return subContexts.get(prefixName).list(name.getSuffix(1));
    }

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