Search in sources :

Example 51 with Context

use of javax.naming.Context in project hibernate-orm by hibernate.

the class JndiRegionFactoryTest method afterStandardServiceRegistryBuilt.

@Override
protected void afterStandardServiceRegistryBuilt(StandardServiceRegistry ssr) {
    if (bindToJndi) {
        try {
            // Create an in-memory jndi
            namingServer = new SingletonNamingServer();
            namingMain = new Main();
            namingMain.setInstallGlobalService(true);
            namingMain.setPort(-1);
            namingMain.start();
            props = new Properties();
            props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            final String cfgFileName = (String) ssr.getService(ConfigurationService.class).getSettings().get(InfinispanRegionFactory.INFINISPAN_CONFIG_RESOURCE_PROP);
            manager = new DefaultCacheManager(cfgFileName == null ? InfinispanRegionFactory.DEF_INFINISPAN_CONFIG_RESOURCE : cfgFileName, false);
            Context ctx = new InitialContext(props);
            bind(JNDI_NAME, manager, EmbeddedCacheManager.class, ctx);
        } catch (Exception e) {
            throw new RuntimeException("Failure to set up JNDI", e);
        }
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) SingletonNamingServer(org.jnp.server.SingletonNamingServer) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Properties(java.util.Properties) Main(org.jnp.server.Main) InitialContext(javax.naming.InitialContext) NameNotFoundException(javax.naming.NameNotFoundException)

Example 52 with Context

use of javax.naming.Context in project jersey by jersey.

the class ThreadLocalNamedInvoker method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // if no instance yet exists for the current thread then look one up and stash it
    if (this.get() == null) {
        Context ctx = new InitialContext();
        T t = (T) ctx.lookup(name);
        this.set(t);
    }
    return super.invoke(proxy, method, args);
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext)

Example 53 with Context

use of javax.naming.Context in project javaee7-samples by javaee-samples.

the class UserTransactionTest method should_work_with_jndi.

@Test
public void should_work_with_jndi() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException, NamingException {
    Context context = new InitialContext();
    UserTransaction ut = (UserTransaction) context.lookup("java:comp/UserTransaction");
    ut.begin();
    ut.commit();
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 54 with Context

use of javax.naming.Context in project javaee7-samples by javaee-samples.

the class EmployeeBean method postConstruct.

@PostConstruct
public void postConstruct() {
    try {
        Context context = new InitialContext();
        em = (EntityManager) context.lookup("java:comp/env/persistence/myJNDI");
    } catch (NamingException ex) {
        Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) PersistenceContext(javax.persistence.PersistenceContext) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) PostConstruct(javax.annotation.PostConstruct)

Example 55 with Context

use of javax.naming.Context in project hudson-2.x by hudson.

the class AbstractModelObject method getConfiguredHudsonProperty.

/**
     * Checks jndi,environment, hudson environment and system properties for specified key.
     * Property is checked in direct order:
     * <ol>
     * <li>JNDI ({@link InitialContext#lookup(String)})</li>
     * <li>Hudson environment ({@link EnvVars#masterEnvVars})</li>
     * <li>System properties ({@link System#getProperty(String)})</li>
     * </ol>
     * @param key - the name of the configured property.
     * @return the string value of the configured property, or null if there is no property with that key.
     */
protected String getConfiguredHudsonProperty(String key) {
    if (StringUtils.isNotBlank(key)) {
        String resultValue;
        try {
            InitialContext iniCtxt = new InitialContext();
            Context env = (Context) iniCtxt.lookup("java:comp/env");
            resultValue = StringUtils.trimToNull((String) env.lookup(key));
            if (null != resultValue) {
                return resultValue;
            }
            // look at one more place. See http://issues.hudson-ci.org/browse/HUDSON-1314
            resultValue = StringUtils.trimToNull((String) iniCtxt.lookup(key));
            if (null != resultValue) {
                return resultValue;
            }
        } catch (NamingException e) {
        // ignore
        }
        // look at the env var next
        resultValue = StringUtils.trimToNull(EnvVars.masterEnvVars.get(key));
        if (null != resultValue) {
            return resultValue;
        }
        // finally check the system property
        resultValue = StringUtils.trimToNull(System.getProperty(key));
        if (null != resultValue) {
            return resultValue;
        }
    }
    return null;
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Aggregations

Context (javax.naming.Context)507 InitialContext (javax.naming.InitialContext)250 Test (org.junit.Test)173 NamingException (javax.naming.NamingException)156 DataSource (javax.sql.DataSource)72 Properties (java.util.Properties)67 Connection (java.sql.Connection)62 NameNotFoundException (javax.naming.NameNotFoundException)52 UserTransaction (javax.transaction.UserTransaction)48 SQLException (java.sql.SQLException)46 IOException (java.io.IOException)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)44 Statement (java.sql.Statement)42 Name (javax.naming.Name)38 Hashtable (java.util.Hashtable)35 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)27 BeanContext (org.apache.openejb.BeanContext)27 Binding (javax.naming.Binding)25 Reference (javax.naming.Reference)25 NotContextException (javax.naming.NotContextException)23