Example usage for javax.naming InitialContext lookup

List of usage examples for javax.naming InitialContext lookup

Introduction

In this page you can find the example usage for javax.naming InitialContext lookup.

Prototype

public Object lookup(Name name) throws NamingException 

Source Link

Usage

From source file:de.mpg.escidoc.services.edoc.PubManImport.java

public PubManImport(String fileName, String username, String password) throws Exception {
    userHandle = AdminHelper.loginUser(username, password);
    this.fileName = fileName;
    InitialContext context = new InitialContext();
    validating = (ItemValidating) context.lookup(ItemValidating.SERVICE_NAME);
}

From source file:com.funambol.server.db.DataSourceContextHelperTest.java

/**
 * Looks up a resource in the context//  www  . j  a  va  2  s.c o  m
 * @param jndiName the jndi name
 * @return the resource
 * @throws java.lang.Exception if an error occurs
 */
private Object lookup(String jndiName) throws Exception {
    InitialContext context = new InitialContext();
    return context.lookup(jndiName);
}

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

@Test
public void testLookupInvalidObjectName() throws NamingException {
    final InitialContext context = new InitialContext();
    final String name = getCompositeName(VALID_CONTEXT, INVALID_OBJECT_NAME);
    final Object obj = context.lookup(name);
    assertNull(obj);/*from   w  w  w . java  2  s .com*/
}

From source file:be.fedict.trust.service.bean.TrustServiceTrustLinker.java

private void logAudit(String message) {

    try {/* www  .jav a2 s  . com*/
        InitialContext initialContext = new InitialContext();
        AuditDAO auditDAO = (AuditDAO) initialContext.lookup(AuditDAO.JNDI_BINDING);
        auditDAO.logAudit(message);
    } catch (NamingException e) {
        LOG.error("Failed to log audit message: " + message, e);
    }

}

From source file:org.wso2.mb.integration.common.clients.operations.queue.QueueMessageBrowser.java

public QueueMessageBrowser(String host, String port, String userName, String password, String destination,
        int printNumberOfMessagesPer, boolean isToPrintMessage) {
    this.hostName = host;
    this.port = port;
    this.queueName = destination;
    this.printNumberOfMessagesPer = printNumberOfMessagesPer;
    this.isToPrintEachMessage = isToPrintMessage;

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password));
    properties.put("queue." + queueName, queueName);

    log.info("getTCPConnectionURL(userName,password) = " + getTCPConnectionURL(userName, password));

    try {/*from w w  w. j  ava 2  s.  c  o  m*/
        InitialContext ctx = new InitialContext(properties);
        // Lookup connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        queueConnection.start();
        queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        Queue queue = (Queue) ctx.lookup(queueName);
        queueBrowser = queueSession.createBrowser(queue);

    } catch (NamingException e) {
        log.error("Error while looking up for queue", e);
    } catch (JMSException e) {
        log.error("Error while initializing queue connection", e);
    }

}

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

@Test
public void testLookupInvalidContext() throws NamingException {

    exception.expect(NamingException.class);
    exception.expectMessage(String.format("Invalid subcontext '%s' in context '/'", INVALID_CONTEXT));
    final InitialContext context = new InitialContext();
    final String name = getCompositeName(INVALID_CONTEXT, VALID_OBJECT_NAME);
    final Object obj = context.lookup(name);
    fail(String.format("This test must fail because the name '%s' not exists in context '/' (object: %s)",
            INVALID_CONTEXT, ObjectUtils.toString(obj)));
}

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

@Test
public void testLookupInvalidContextAndName() throws NamingException {

    exception.expect(NamingException.class);
    exception.expectMessage(String.format("Invalid subcontext '%s' in context '/'", INVALID_CONTEXT));
    final InitialContext context = new InitialContext();
    final String name = getCompositeName(INVALID_CONTEXT, INVALID_OBJECT_NAME);
    final Object obj = context.lookup(name);
    fail(String.format("This test must fail because the name '%s' not exists (object: %s)", INVALID_CONTEXT,
            ObjectUtils.toString(obj)));
}

From source file:org.pepstock.jem.junit.test.http.java.HttpConsumeSbTasklet.java

@Override
public RepeatStatus run(StepContribution stepContribution, ChunkContext chuckContext) throws TaskletException {

    try {/*  ww w  .j  av  a2  s . c  o m*/
        Hashtable<String, String> env = this.createEnvironment();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.pepstock.jem.node.tasks.jndi.JemContextFactory");
        InitialContext context = new InitialContext(env);
        // get http resource, note that jem-http is the name of the resource
        // present in the JCL
        InputStream httpStream = (InputStream) context.lookup("jem-http");
        StringWriter writer = new StringWriter();
        IOUtils.copy(httpStream, writer);
        String theString = writer.toString();
        System.out.println("Read Http: " + theString);
        httpStream.close();
    } catch (Exception e) {
        throw new TaskletException(e);
    }
    return RepeatStatus.FINISHED;
}

From source file:org.wso2.carbon.registry.caching.invalidator.connection.JMSNotification.java

@Override
public void createConnection(Properties config) {
    try {//from  w  ww  .j  ava2 s . c  o m
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, config.getProperty("initialContextFactory"));
        props.put(Context.PROVIDER_URL, config.getProperty("providerUrl"));
        props.put(Context.SECURITY_PRINCIPAL, config.getProperty("securityPrincipal"));
        props.put(Context.SECURITY_CREDENTIALS, config.getProperty("securityCredentials"));
        props.put("topic.cacheInvalidateTopic", config.getProperty("cacheInvalidateTopic"));
        InitialContext jndi = new InitialContext(props);
        ConnectionFactory connectionFactory = (ConnectionFactory) jndi.lookup("ConnectionFactory");
        destination = (Destination) jndi.lookup("cacheInvalidateTopic");

        connection = connectionFactory.createConnection(config.getProperty("securityPrincipal"),
                config.getProperty("securityCredentials"));
        connection.start();
    } catch (NamingException | JMSException e) {
        log.error("Global cache invalidation: Error message broker initialization", e);
    }
}

From source file:org.jetbrains.webdemo.backend.BackendHttpServlet.java

private boolean loadTomcatParameters() {
    InitialContext initCtx = null;
    try {//  w w w .j a v  a 2  s .  co m
        initCtx = new InitialContext();
        NamingContext envCtx = (NamingContext) initCtx.lookup("java:comp/env");
        try {
            CommandRunner.setServerSettingFromTomcatConfig("java_home", (String) envCtx.lookup("java_home"));
        } catch (NamingException e) {
            CommandRunner.setServerSettingFromTomcatConfig("java_home", System.getenv("JAVA_HOME"));
        }
        try {
            CommandRunner.setServerSettingFromTomcatConfig("java_execute",
                    (String) envCtx.lookup("java_execute"));
        } catch (NamingException e) {
            String executable = isWindows() ? "java.exe" : "java";
            CommandRunner.setServerSettingFromTomcatConfig("java_execute",
                    BackendSettings.JAVA_HOME + File.separator + "bin" + File.separator + executable);
        }
        try {
            CommandRunner.setServerSettingFromTomcatConfig("app_output_dir",
                    (String) envCtx.lookup("app_output_dir"));
        } catch (NamingException e) {
            File rootFolder = new File(BackendSettings.WEBAPP_ROOT_DIRECTORY);
            String appHome = rootFolder.getParentFile().getParentFile().getParent();
            CommandRunner.setServerSettingFromTomcatConfig("app_output_dir", appHome);
        }

        try {
            CommandRunner.setServerSettingFromTomcatConfig("is_test_version",
                    (String) envCtx.lookup("is_test_version"));
        } catch (NameNotFoundException e) {
            //Absent is_test_version variable in context.xml
            CommandRunner.setServerSettingFromTomcatConfig("is_test_version", "false");
        }
        try {
            CommandRunner.setServerSettingFromTomcatConfig("timeout", (String) envCtx.lookup("timeout"));
        } catch (NameNotFoundException e) {
            //Absent timeout variable in context.xml
        }

        return true;
    } catch (Throwable e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        return false;
    }

}