Example usage for javax.naming Context PROVIDER_URL

List of usage examples for javax.naming Context PROVIDER_URL

Introduction

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

Prototype

String PROVIDER_URL

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

Click Source Link

Document

Constant that holds the name of the environment property for specifying configuration information for the service provider to use.

Usage

From source file:org.mule.config.spring.jndi.SpringInitialContextFactory.java

public Context getInitialContext(Hashtable environment) throws NamingException {
    if (singleton != null) {
        return singleton;
    }//  w  w  w.  j  av a2 s . c  o  m
    Resource resource = null;
    Object value = environment.get(Context.PROVIDER_URL);
    String key = "jndi.xml";
    if (value == null) {
        resource = new ClassPathResource(key);
    } else {
        if (value instanceof Resource) {
            resource = (Resource) value;
        } else {
            ResourceEditor editor = new ResourceEditor();
            key = value.toString();
            editor.setAsText(key);
            resource = (Resource) editor.getValue();
        }
    }
    BeanFactory context = loadContext(resource, key);
    Context answer = (Context) context.getBean("jndi");
    if (answer == null) {
        log.warn("No JNDI context available in JNDI resource: " + resource);
        answer = new DefaultSpringJndiContext(environment, new ConcurrentHashMap());
    }
    return answer;
}

From source file:org.wso2.carbon.connector.integration.test.ejb2.Ejb2ConnectorIntegrationTest.java

/**
 * Set up the environment./*from ww  w.ja  va2s  . c  o  m*/
 */
@BeforeClass(alwaysRun = true)
public void setEnvironment() throws Exception {
    init("ejb2-connector-1.0.1-SNAPSHOT");
    esbRequestHeadersMap.put("Accept-Charset", "UTF-8");
    esbRequestHeadersMap.put("Content-Type", "application/json");
    esbRequestHeadersMap.put("Accept", "application/json");
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    p.put(Context.PROVIDER_URL, "localhost");
    ctx = new InitialContext(p);
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapSecurityService.java

@Override
public boolean authenticate(String id, char[] password) {
    String cachedPassword = credentialCache.get(id);
    String encodedPassword = null;

    try {//from   w ww .  jav a 2s. c  o m
        encodedPassword = codec.encode(new String(password));
    } catch (EncoderException e1) {
    }

    if (cachedPassword != null && encodedPassword != null && cachedPassword.equals(encodedPassword))
        return true;

    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, url);
    environment.put(Context.SECURITY_AUTHENTICATION, "simple");
    environment.put(Context.SECURITY_PRINCIPAL, id);
    environment.put(Context.SECURITY_CREDENTIALS, new String(password));

    try {
        InitialDirContext context = new InitialDirContext(environment);
        context.close();

        if (encodedPassword != null)
            credentialCache.put(id, encodedPassword);

        return true;
    } catch (NamingException e) {
        return false;
    }
}

From source file:org.zanata.util.CleanDocumentStorageRule.java

public static String getDocumentStoragePath() {
    if (storagePath == null) {
        final Properties env = new Properties();

        // env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                org.jboss.naming.remote.client.InitialContextFactory.class.getName());
        long portOffset = Integer.parseInt(PropertiesHolder.getProperty("cargo.port.offset", "0"));
        String rmiPort = System.getenv("JBOSS_REMOTING_PORT");
        int rmiPortNum = rmiPort != null ? parseInt(rmiPort) : 4547;
        long realRmiPort = portOffset + rmiPortNum;
        String remoteUrl = "remote://localhost:" + realRmiPort;
        env.put(Context.PROVIDER_URL, remoteUrl);
        InitialContext remoteContext = null;
        try {/*from   w  ww .j a  va 2s.  c om*/
            remoteContext = new InitialContext(env);
            storagePath = (String) remoteContext.lookup("zanata/files/document-storage-directory");
        } catch (NamingException e) {
            // wildfly uses 'http-remoting:' not 'remote:'
            String httpPort = System.getenv("JBOSS_HTTP_PORT");
            int httpPortNum = httpPort != null ? parseInt(httpPort) : 8180;
            long realHttpPort = httpPortNum + portOffset;
            String httpRemotingUrl = "http-remoting://localhost:" + realHttpPort;
            log.warn("Unable to access {}: {}; trying {}", remoteUrl, e.toString(), httpRemotingUrl);
            try {
                env.put(Context.PROVIDER_URL, httpRemotingUrl);
                remoteContext = new InitialContext(env);
                storagePath = (String) remoteContext.lookup("zanata/files/document-storage-directory");
            } catch (NamingException e1) {
                // fall back option:
                log.warn("Unable to access {}: {}", httpRemotingUrl, e.toString());
                URL testClassRoot = Thread.currentThread().getContextClassLoader()
                        .getResource("setup.properties");
                File targetDir = new File(testClassRoot.getPath()).getParentFile();
                storagePath = new File(targetDir, "zanata-documents").getAbsolutePath();
            }
        }
    }
    return storagePath;
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapService.java

@Override
public boolean authenticate(String id, char[] password) {
    if (id == null || id.isEmpty())
        return false;

    if (idSuffix != null)
        id = id + idSuffix;/* w ww.  j  a v  a2s  .  c  o m*/

    String cachedPassword = credentialCache.get(id);
    String encodedPassword = null;

    try {
        encodedPassword = codec.encode(new String(password));
    } catch (EncoderException e1) {
    }

    if (cachedPassword != null && encodedPassword != null && cachedPassword.equals(encodedPassword))
        return true;

    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, url);
    environment.put(Context.SECURITY_AUTHENTICATION, "simple");
    environment.put(Context.SECURITY_PRINCIPAL, id);
    environment.put(Context.SECURITY_CREDENTIALS, new String(password));

    try {
        InitialDirContext context = new InitialDirContext(environment);
        context.close();

        if (encodedPassword != null)
            credentialCache.put(id, encodedPassword);

        return true;
    } catch (NamingException e) {
        return false;
    }
}

From source file:org.dhatim.routing.jms.activemq.ActiveMQProvider.java

public ActiveMQProvider() {
    jndiProperties = new Properties();
    jndiProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
    jndiProperties.setProperty(Context.PROVIDER_URL, DEFAULT_PROVIDER_URL);
}

From source file:br.com.upic.camel.ldap.LdapEndpoint.java

@Override
protected void onExchange(final Exchange exchange) throws Exception {
    LOG.info("Setting up the context");

    final Hashtable<String, String> conf = new Hashtable<String, String>();

    LOG.debug("Initial Context Factory = " + initialContextFactory);

    conf.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);

    LOG.debug("Provider URL = " + providerUrl);

    conf.put(Context.PROVIDER_URL, providerUrl);

    LOG.debug("Security Authentication = " + securityAuthentication);

    conf.put(Context.SECURITY_AUTHENTICATION, securityAuthentication);

    final Message in = exchange.getIn();

    final String user = in.getHeader(HEADER_USER, String.class);

    LOG.debug("User = " + user);

    conf.put(Context.SECURITY_PRINCIPAL, user);

    final String password = in.getHeader(HEADER_PASSWORD, String.class);

    LOG.debug("Password = " + password);

    conf.put(Context.SECURITY_CREDENTIALS, password);

    LOG.info("Authenticating in directory");

    final Message out = exchange.getOut();

    try {/*ww w.jav a 2s. c  o m*/
        new InitialContext(conf);

        out.setBody(true);
    } catch (final AuthenticationException e) {
        LOG.error(e.getMessage(), e);

        out.setBody(false);
    }

}

From source file:org.malaguna.cmdit.service.ldap.LDAPBase.java

public Attributes loadUser(String uid, String[] attrs) {

    // Preparar las variables de entorno para la conexin JNDI
    Hashtable<String, String> entorno = new Hashtable<String, String>();

    // Credenciales del usuario para realizar la bsqueda
    String cadena = "uid=" + user + "," + context;

    entorno.put(Context.PROVIDER_URL, server);
    entorno.put(Context.INITIAL_CONTEXT_FACTORY, initContext);
    if (password != null && user != null) {
        entorno.put(Context.SECURITY_PRINCIPAL, cadena);
        entorno.put(Context.SECURITY_CREDENTIALS, password);
    }//from w ww .  j a  v  a 2  s .  c  o m

    Attributes atributos = null;

    try {
        // Crear contexto de directorio inicial
        DirContext ctx = new InitialDirContext(entorno);

        // Recuperar atributos del usuario que se est buscando
        if (attrs != null)
            atributos = ctx.getAttributes("uid=" + uid + "," + context, attrs);
        else
            atributos = ctx.getAttributes("uid=" + uid + "," + context);

        // Cerrar la conexion
        ctx.close();
    } catch (NamingException e) {
        logger.error(messages.getMessage("err.ldap.attribute", new Object[] { e }, Locale.getDefault()));
    }

    return atributos;

}

From source file:org.openhie.openempi.ejb.util.ServiceLocator.java

/**
 * Get the initial naming context// www .ja v  a 2 s .  c  om
 */
protected static Context getInitialContext() throws NamingException {
    if (context == null) {
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        props.put(Context.PROVIDER_URL, "localhost:1099");
        context = new InitialContext(props);
    }
    return context;
}

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

public ActiveDirectory(String username, String password, String domain) throws NamingException {
    if (StringUtils.isEmpty(domain))
        throw new NamingException("The domain is empty");
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    properties.put(Context.PROVIDER_URL, StringUtils.fastConcat("LDAP://", domain));
    properties.put(Context.SECURITY_PRINCIPAL, StringUtils.fastConcat(username, "@", domain));
    properties.put(Context.SECURITY_CREDENTIALS, password);
    properties.put("java.naming.ldap.attributes.binary", "objectSID");
    properties.put(Context.REFERRAL, "follow");
    dirContext = new InitialDirContext(properties);
    domainSearchName = getDomainSearch(domain);
}