Example usage for javax.naming Context INITIAL_CONTEXT_FACTORY

List of usage examples for javax.naming Context INITIAL_CONTEXT_FACTORY

Introduction

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

Prototype

String INITIAL_CONTEXT_FACTORY

To view the source code for javax.naming Context INITIAL_CONTEXT_FACTORY.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the initial context factory to use.

Usage

From source file:org.projectforge.ldap.LdapConnector.java

private Hashtable<String, String> createEnv(final String user, final String password) {
    // Set up the environment for creating the initial context
    final Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapConfig.getCompleteServerUrl());
    final String authentication = ldapConfig.getAuthentication();
    if (StringUtils.isNotBlank(authentication) == true) {
        env.put(Context.SECURITY_AUTHENTICATION, ldapConfig.getAuthentication());
        if ("none".equals(authentication) == false || user != null || password != null) {
            env.put(Context.SECURITY_PRINCIPAL, user);
            env.put(Context.SECURITY_CREDENTIALS, password);
        }/*from www .ja va  2 s.  co m*/
    }
    if (ldapConfig != null && StringUtils.isNotBlank(ldapConfig.getSslCertificateFile()) == true) {
        env.put("java.naming.ldap.factory.socket", "org.projectforge.ldap.MySSLSocketFactory");
    }
    log.info("Trying to connect the LDAP server: url=[" + ldapConfig.getCompleteServerUrl()
            + "], authentication=[" + ldapConfig.getAuthentication() + "], principal=[" + user + "]");
    return env;
}

From source file:org.wso2.carbon.deployment.notifier.internal.JMSUtils.java

/**
 * Return the JMS destination with the given destination name looked up from the context
 * Borrowed generiously from axis2 jms transport implementation
 *
 * @param context the Context to lookup/*from w  w  w. ja  v a  2 s.c o  m*/
 * @param destinationName name of the destination to be looked up
 * @param destinationType type of the destination to be looked up
 * @return the JMS destination, or null if it does not exist
 */
public static Destination lookupDestination(Context context, String destinationName, String destinationType)
        throws NamingException {
    try {
        return lookup(context, Destination.class, destinationName);
    } catch (NameNotFoundException e) {
        Properties initialContextProperties = new Properties();
        if (context.getEnvironment() != null) {
            if (context.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY) != null) {
                initialContextProperties.put(Context.INITIAL_CONTEXT_FACTORY,
                        context.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY));
            }
            if (context.getEnvironment().get(Context.PROVIDER_URL) != null) {
                initialContextProperties.put(Context.PROVIDER_URL,
                        context.getEnvironment().get(Context.PROVIDER_URL));
            }
        }
        if (Constants.DESTINATION_TYPE_TOPIC.equalsIgnoreCase(destinationType)) {
            initialContextProperties.put(Constants.TOPIC_PREFIX + destinationName, destinationName);
        } else {
            initialContextProperties.put(Constants.QUEUE_PREFIX + destinationName, destinationName);
        }
        InitialContext initialContext = new InitialContext(initialContextProperties);
        try {
            return lookup(initialContext, Destination.class, destinationName);
        } catch (NamingException e1) {
            return lookup(context, Destination.class,
                    (Constants.DESTINATION_TYPE_TOPIC.equalsIgnoreCase(destinationType) ? "dynamicTopics/"
                            : "dynamicQueues/") + destinationName);
        }
    }
}

From source file:org.mule.transport.rmi.RmiMessageReceiverTestCase.java

private void registerRmi() throws Exception {
    if (null == rmiRegistry) {
        rmiRegistry = LocateRegistry.createRegistry(11099);
        Naming.rebind("//localhost:11099/TestMatchingMethodsComponent",
                new SerializedMatchingMethodsComponent());

        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");

        Context context = new InitialContext(env);
        SerializedMatchingMethodsComponent obj = (SerializedMatchingMethodsComponent) context
                .lookup("rmi://localhost:11099/TestMatchingMethodsComponent");

        if (obj == null) {
            throw new RuntimeException("Could not start RMI properly");
        }/*  w ww  .  j  ava  2  s  .c  o m*/
    }
}

From source file:org.jkcsoft.java.util.JndiHelper.java

public static Context getContext(String url, String contextFactoryName) throws NamingException {
    log.debug("Getting initial context from jndi url [" + url + "]");
    Context ctx = null;//from   ww  w  . ja  va2  s.  co  m
    Hashtable properties = new Hashtable();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, contextFactoryName);

    properties.put(Context.PROVIDER_URL, url);

    try {
        // Creating the JNDI directory context (with LDAP context
        // factory), performs an LDAP bind to the LDAP provider thereby
        // authenticating the username/pw.
        ctx = new InitialContext(properties);
    } catch (NamingException ex) {
        throw ex;
    }

    return ctx;
}

From source file:org.wso2.carbon.andes.ui.client.QueueBrowserClient.java

/***
 * Constructor - create a new browser instance for the given queue as given user.
 * @param nameOfQueue name of queue/*from  www.java 2s .  c  om*/
 * @param userName User's username as per user store
 * @param accessKey as generated by andes authentication service for the user.
 * @throws FileNotFoundException
 * @throws XMLStreamException
 * @throws AndesException
 */
public QueueBrowserClient(String nameOfQueue, String userName, String accessKey)
        throws FileNotFoundException, XMLStreamException, AndesException {
    this.nameOfQueue = nameOfQueue;
    this.properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, UIUtils.getTCPConnectionURL(userName, accessKey));
    properties.put(QUEUE_NAME_PREFIX + nameOfQueue, nameOfQueue);
    properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true");

}

From source file:org.smartfrog.avalanche.shared.jms.MessageListener.java

public void init() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.mom4j.jndi.InitialCtxFactory");

    p.put(Context.PROVIDER_URL, "xcp://" + serverName + ":" + port);

    Context ctx = null;//from   ww w . ja v a2  s. c  o m
    try {
        ctx = new InitialContext(p);
        QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
        queue = (Queue) ctx.lookup(queueName);

        qc = qcf.createQueueConnection(userName, password);
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:org.web4thejob.security.ADAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    if (authentication.getName() == null || (String) authentication.getCredentials() == null) {
        throw new BadCredentialsException("");
    }//from   w  ww  . j ava  2 s .c o m

    String principal = getPrincipal(authentication.getName());
    String passwd = (String) authentication.getCredentials();

    LdapContext ctx = null;
    try {
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, LdapCtxFactory.class.getCanonicalName());
        env.put(Context.SECURITY_AUTHENTICATION, "Simple");
        env.put(Context.SECURITY_PRINCIPAL, principal);
        env.put(Context.SECURITY_CREDENTIALS, passwd);
        env.put(Context.PROVIDER_URL, url);
        ctx = new InitialLdapContext(env, null);
        //LDAP Connection Successful

        UserDetails userDetails = userDetailsService.loadUserByUsername(principal);
        return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
    } catch (NamingException nex) {
        throw new BadCredentialsException("LDAP authentication failed.", nex);
    } catch (UsernameNotFoundException e) {
        throw new BadCredentialsException("UserDetails did not find a valid user for name: " + principal, e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:org.pegadi.server.user.LDAPUserServerImpl.java

public void init() {
    env.put("java.naming.ldap.version", "3");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url + "/" + ldapBaseDN);
    env.put(Context.SECURITY_AUTHENTICATION, auth);
    env.put(Context.SECURITY_PRINCIPAL, ldapLoginDN);
    env.put(Context.SECURITY_CREDENTIALS, ldapPassword);

    try {//from  w  ww  .  j  a va  2  s.  c  o m
        ctx = new InitialDirContext(env);
        log.info("Successfully created a Context");
    } catch (NamingException e) {
        log.error("Unable to create a Context", e);
    } catch (Exception e) {
        log.error("This should never come", e);
    }
}

From source file:py.una.pol.karaku.util.LDAPUtil.java

private DirContext createInitialDirContext() {

    Map<Object, String> env = new HashMap<Object, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, propertiesUtil.get(LDAP_SERVER_KEY) + "/" + propertiesUtil.get(LDAP_DN_KEY));
    env.put(Context.SECURITY_PRINCIPAL, propertiesUtil.get(LDAP_ADMIN_KEY));
    env.put(Context.SECURITY_CREDENTIALS, propertiesUtil.get(LDAP_ADMIN_PASS_KEY));

    try {/*w ww. j  av a 2 s  .  c om*/
        return new InitialDirContext(new Hashtable<Object, String>(env));

    } catch (NamingException e) {
        throw new KarakuRuntimeException(e.getMessage(), e);
    }

}

From source file:com.marklogic.samplestack.integration.web.LDAPIT.java

@Before
public void setup() throws NamingException {
    env = new Hashtable<String, Object>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if (ldapUsername != null) {
        env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
    }//  w w  w . j a  v  a 2s . c om
    if (ldapPassword != null) {
        env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
    }
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapServer);

    // ensures that objectSID attribute values
    // will be returned as a byte[] instead of a String
    // env.put("java.naming.ldap.attributes.binary", "uid");

    // the following is helpful in debugging errors
    //env.put("com.sun.jndi.ldap.trace.ber", System.err);

    ctx = new InitialLdapContext(env, null);

}