Example usage for javax.naming Context lookup

List of usage examples for javax.naming Context lookup

Introduction

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

Prototype

public Object lookup(String name) throws NamingException;

Source Link

Document

Retrieves the named object.

Usage

From source file:org.apache.cayenne.configuration.server.JNDIDataSourceFactory.java

DataSource lookupViaJNDI(String location) throws NamingException {

    LOGGER.info("Connecting. JNDI path: " + location);

    Context context = new InitialContext();
    DataSource dataSource;//from   w w  w. j a v  a 2 s  .c  om
    try {
        Context envContext = (Context) context.lookup("java:comp/env");
        dataSource = (DataSource) envContext.lookup(location);
    } catch (NamingException namingEx) {
        // try looking up the location directly...
        dataSource = (DataSource) context.lookup(location);
    }

    LOGGER.info("Found JNDI DataSource at location: " + location);

    return dataSource;
}

From source file:ca.n4dev.dev.worktime.config.SpringHibernateJPAConfig.java

@Bean
public DataSource dataSource() {
    try {/* ww w  . ja  v  a 2s .  co m*/
        // Obtain our environment naming context
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");

        // Look up our data source
        DataSource ds = (DataSource) envCtx.lookup("jdbc/WorktimeDB");

        //         if (ds == null) {
        //
        //            ds = new DataSource();
        //            PoolProperties p = new PoolProperties();
        //            
        //            p.setUrl(env.getRequiredProperty("")); // "jdbc:mysql://localhost:3306/mysql"
        //            p.setDriverClassName(env.getRequiredProperty("")); // "com.mysql.jdbc.Driver"
        //            p.setUsername(env.getRequiredProperty("")); // "root"
        //            p.setPassword(env.getRequiredProperty("")); // "password"
        //            p.setJmxEnabled(Boolean.valueOf(env.getRequiredProperty(""))); // false
        //            p.setTestWhileIdle(Boolean.valueOf(env.getRequiredProperty(""))); // false
        //            p.setTestOnBorrow(Boolean.valueOf(env.getRequiredProperty(""))); // true
        //            p.setValidationQuery(env.getRequiredProperty("")); // "SELECT 1"
        //            p.setTestOnReturn(Boolean.valueOf(env.getRequiredProperty(""))); // false
        //            p.setValidationInterval(Integer.parseInt(env.getRequiredProperty(""))); // 30000
        //            p.setTimeBetweenEvictionRunsMillis(Integer.parseInt(env.getRequiredProperty(""))); // 30000
        //            p.setMaxActive(Integer.parseInt(env.getRequiredProperty(""))); // 100
        //            p.setInitialSize(Integer.parseInt(env.getRequiredProperty(""))); // 10
        //            p.setMaxWait(Integer.parseInt(env.getRequiredProperty(""))); // 10000
        //            p.setRemoveAbandonedTimeout(Integer.parseInt(env.getRequiredProperty(""))); // 60
        //            p.setMinEvictableIdleTimeMillis(Integer.parseInt(env.getRequiredProperty(""))); // 30000
        //            p.setMinIdle(Integer.parseInt(env.getRequiredProperty(""))); // 10
        //            p.setLogAbandoned(Boolean.valueOf(env.getRequiredProperty(""))); // true
        //            p.setRemoveAbandoned(Boolean.valueOf(env.getRequiredProperty(""))); // true
        //            p.setJdbcInterceptors(env.getRequiredProperty("")); // "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
        //
        //            
        //            ds.setPoolProperties(p);
        //            
        //         }

        return ds;

    } catch (Exception e) {
        throw new RuntimeException("Unable to get or create the datasource!");
    }
}

From source file:org.castor.cpa.persistence.sql.connection.JNDIConnectionFactory.java

/**
 * {@inheritDoc}/*from w w  w  .j a v  a 2 s  . c o  m*/
 */
public void initializeFactory(final PersistenceFactory factory) throws MappingException {
    _factory = factory;
    String name = _jndi.getName();

    Object dataSource;
    try {
        Context initialContext = new InitialContext();
        dataSource = initialContext.lookup(name);
    } catch (NameNotFoundException e) {
        String msg = Messages.format("jdo.jndiNameNotFound", name);
        LOG.error(msg, e);
        throw new MappingException(msg, e);
    } catch (NamingException e) {
        throw new MappingException(e);
    }

    if (!(dataSource instanceof DataSource)) {
        String msg = Messages.format("jdo.jndiNameNotFound", name);
        LOG.error(msg);
        throw new MappingException(msg);
    }

    _dataSource = (DataSource) dataSource;

    if (LOG.isDebugEnabled()) {
        LOG.debug("Using DataSource from JNDI ENC: " + name);
    }
}

From source file:sequrity.LoginService.java

public Korisnik loadUserByEmail(String email) throws Exception {
    Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
    EntityManagerFactory emf = (EntityManagerFactory) envCtx.lookup("ime");
    EntityManager em = emf.createEntityManager();
    try {//from w  w w.  j a  v  a  2s  .com
        return (Korisnik) em.createNamedQuery("Profesor.findByEmail").setParameter("email", email)
                .getSingleResult();
    } catch (NoResultException e) {
        try {
            return (Korisnik) em.createNamedQuery("Student.findByEmail").setParameter("email", email)
                    .getSingleResult();
        } catch (NoResultException ex) {
            throw new Exception("Korisnik ne postoji u sistemu.");
        }
    }
}

From source file:minor.commodity.CommodityQuote.java

private CommodityDetailsFacadeLocal lookupCommodityDetailsFacadeLocal() {
    try {/*from w w  w.  ja  v a  2 s  . com*/
        Context c = new InitialContext();
        return (CommodityDetailsFacadeLocal) c.lookup(
                "java:global/MinorTest/CommodityDetailsFacade!minor.session.CommodityDetailsFacadeLocal");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:com.vnet.demo.service.azure.servicebus.AzureServiceBusServiceFactory.java

public AzureServiceBusService create() {
    if (this.azureServiceBusService == null) {
        try {/*from   w  w w  . jav  a 2 s .  c  o m*/
            PropertiesFactoryBean properties = SpringUtil.getBean(PropertiesFactoryBean.class);
            this.username = (String) properties.getObject().get("azure.servicebus.username");
            this.password = (String) properties.getObject().get("azure.servicebus.password");
            this.host = (String) properties.getObject().get("azure.servicebus.host");
            String defaultQueue = (String) properties.getObject().get("azure.servicebus.queue");

            String connectionString = "amqps://" + username + ":" + encode(password) + "@" + host;
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory");
            env.put("connectionfactory.ServiceBusConnectionFactory", connectionString);

            Context context = new InitialContext(env);
            ConnectionFactory connectionFactory = (ConnectionFactory) context
                    .lookup("ServiceBusConnectionFactory");
            azureServiceBusService = new AzureServiceBusService(connectionFactory, defaultQueue);
        } catch (NamingException | IOException e) {
            e.printStackTrace();
        }
        ;
    }
    return this.azureServiceBusService;
}

From source file:eu.planets_project.tb.impl.services.util.wee.WeeRemoteUtil.java

private void getWeeRemoteObjects() {
    // get an instance of the Workflow Template Registry Service
    // and the Workflow Execution Service
    try {//from  w w  w .ja v a  2s .  c om
        Context ctx = new javax.naming.InitialContext();
        wftRegImp = (WftRegistryService) PortableRemoteObject
                .narrow(ctx.lookup("planets-project.eu/WftRegistryService/remote"), WftRegistryService.class);
        weeService = (WeeService) PortableRemoteObject
                .narrow(ctx.lookup("planets-project.eu/WeeService/remote"), WeeService.class);
    } catch (NamingException e) {
        log.error("Could not retrieve the WeeService or WftRegistryService object" + e);
    }

    //get an instance of the ServiceRegistry
    registry = ServiceRegistryFactory.getServiceRegistry();
}

From source file:org.aksw.rest.NIFoggd.java

private void init() {

    if (index == null) {

        try {//from  w  w  w  .java 2s . c  om

            Context context = (Context) new InitialContext().lookup("java:comp/env");
            index = (String) context.lookup("LUCENE_INDEX");

        } catch (NamingException e) {

            index = "/tmp/lucene";

            LOG.error(
                    "Please, check your web.xml file. I can not retrieve the Lucene index path from the env variable ");
            LOG.error("E.g:");
            LOG.error("    <env-entry>\n" + "        <env-entry-name>LUCENE_INDEX</env-entry-name>\n"
                    + "        <env-entry-value>/tmp/lucene/</env-entry-value>\n"
                    + "        <env-entry-type>java.lang.String</env-entry-type>\n" + "    </env-entry>");

            index = "/tmp/lucene/";

        }
    }

}

From source file:com.heliumv.factory.BaseCall.java

private Context getInitialContext() throws NamingException {
    Context env = (Context) new InitialContext().lookup("java:comp/env");
    String namingFactory = (String) env.lookup(Context.INITIAL_CONTEXT_FACTORY);
    String urlProvider = (String) env.lookup(Context.PROVIDER_URL);

    log.debug("namingFactory = {" + namingFactory + "}");
    log.debug("urlProvider = {" + urlProvider + "}");

    Hashtable<String, String> environment = new Hashtable<String, String>();

    environment.put(Context.INITIAL_CONTEXT_FACTORY, namingFactory);
    environment.put(Context.PROVIDER_URL, urlProvider);
    return new InitialContext(environment);
}

From source file:org.apache.axis2.transaction.TransactionConfiguration.java

private synchronized TransactionManager lookupTransactionManager() throws AxisFault {

    try {//from   w  ww .  j a  v a2  s .com

        Context context = new InitialContext(jndiProperties);
        Object transactionManager = context.lookup(transactionManagerJNDIName);
        if (transactionManager != null && transactionManager instanceof TransactionManager) {
            return (TransactionManager) transactionManager;
        } else {
            log.error("TransactionManager : " + transactionManagerJNDIName + " not found when looking up"
                    + " using JNDI properties : " + context.getEnvironment());
            throw new AxisFault("TransactionManager : " + transactionManagerJNDIName
                    + " not found when looking up" + " using JNDI properties : " + context.getEnvironment());
        }

    } catch (NamingException e) {

        log.error(new StringBuilder().append("Error looking up TransactionManager ")
                .append(" using JNDI properties : ").append(jndiProperties));
        throw new AxisFault(
                "TransactionManager not found when looking up" + " using JNDI properties : " + jndiProperties);
    }
}