Search in sources :

Example 6 with InitialContext

use of javax.naming.InitialContext in project camel by apache.

the class FileEndpointReferenceRouteTest method runTest.

@Test
public void runTest() throws Exception {
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.PROVIDER_URL, GuiceInitialContextFactory.class.getName());
    env.put(Injectors.MODULE_CLASS_NAMES, MyModule.class.getName());
    InitialContext context = new InitialContext(env);
    Injector injector = (Injector) context.lookup(Injector.class.getName());
    assertNotNull("Found injector", injector);
    Object value = context.lookup("fileFilter");
    assertNotNull("Should have found a value for foo!", value);
    assertCamelContextRunningThenCloseInjector(injector);
}
Also used : Hashtable(java.util.Hashtable) Injector(com.google.inject.Injector) GuiceInitialContextFactory(org.apache.camel.guice.jndi.GuiceInitialContextFactory) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 7 with InitialContext

use of javax.naming.InitialContext in project tomcat by apache.

the class InstanceKeyDataSource method testCPDS.

protected ConnectionPoolDataSource testCPDS(final String username, final String password) throws javax.naming.NamingException, SQLException {
    // The source of physical db connections
    ConnectionPoolDataSource cpds = this.dataSource;
    if (cpds == null) {
        Context ctx = null;
        if (jndiEnvironment == null) {
            ctx = new InitialContext();
        } else {
            ctx = new InitialContext(jndiEnvironment);
        }
        final Object ds = ctx.lookup(dataSourceName);
        if (ds instanceof ConnectionPoolDataSource) {
            cpds = (ConnectionPoolDataSource) ds;
        } else {
            throw new SQLException("Illegal configuration: " + "DataSource " + dataSourceName + " (" + ds.getClass().getName() + ")" + " doesn't implement javax.sql.ConnectionPoolDataSource");
        }
    }
    // try to get a connection with the supplied username/password
    PooledConnection conn = null;
    try {
        if (username != null) {
            conn = cpds.getPooledConnection(username, password);
        } else {
            conn = cpds.getPooledConnection();
        }
        if (conn == null) {
            throw new SQLException("Cannot connect using the supplied username/password");
        }
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (final SQLException e) {
            // at least we could connect
            }
        }
    }
    return cpds;
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) InitialContext(javax.naming.InitialContext)

Example 8 with InitialContext

use of javax.naming.InitialContext in project tomcat by apache.

the class EjbFactory method getLinked.

@Override
protected Object getLinked(Reference ref) throws NamingException {
    // If ejb-link has been specified, resolving the link using JNDI
    RefAddr linkRefAddr = ref.get(EjbRef.LINK);
    if (linkRefAddr != null) {
        // Retrieving the EJB link
        String ejbLink = linkRefAddr.getContent().toString();
        Object beanObj = (new InitialContext()).lookup(ejbLink);
        return beanObj;
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) InitialContext(javax.naming.InitialContext)

Example 9 with InitialContext

use of javax.naming.InitialContext in project tomcat by apache.

the class OpenEjbFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
     * Create a new EJB instance using OpenEJB.
     *
     * @param obj The reference object describing the DataSource
     */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Object beanObj = null;
    if (obj instanceof EjbRef) {
        Reference ref = (Reference) obj;
        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }
    }
    return beanObj;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext)

Example 10 with InitialContext

use of javax.naming.InitialContext in project jetty.project by eclipse.

the class PlusDescriptorProcessor method bindEnvEntry.

/**
     * @param name the jndi name
     * @param value the value
     * @throws Exception if unable to bind entry
     */
public void bindEnvEntry(String name, Object value) throws Exception {
    InitialContext ic = null;
    boolean bound = false;
    //check to see if we bound a value and an EnvEntry with this name already
    //when we processed the server and the webapp's naming environment
    //@see EnvConfiguration.bindEnvEntries()
    ic = new InitialContext();
    try {
        NamingEntry ne = (NamingEntry) ic.lookup("java:comp/env/" + NamingEntryUtil.makeNamingEntryName(ic.getNameParser(""), name));
        if (ne != null && ne instanceof EnvEntry) {
            EnvEntry ee = (EnvEntry) ne;
            bound = ee.isOverrideWebXml();
        }
    } catch (NameNotFoundException e) {
        bound = false;
    }
    if (!bound) {
        //either nothing was bound or the value from web.xml should override
        Context envCtx = (Context) ic.lookup("java:comp/env");
        NamingUtil.bind(envCtx, name, value);
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NameNotFoundException(javax.naming.NameNotFoundException) NamingEntry(org.eclipse.jetty.plus.jndi.NamingEntry) InitialContext(javax.naming.InitialContext) EnvEntry(org.eclipse.jetty.plus.jndi.EnvEntry)

Aggregations

InitialContext (javax.naming.InitialContext)1068 Test (org.junit.Test)325 EJBException (javax.ejb.EJBException)228 Properties (java.util.Properties)213 Context (javax.naming.Context)194 RemoteException (java.rmi.RemoteException)173 TestFailureException (org.apache.openejb.test.TestFailureException)172 NamingException (javax.naming.NamingException)168 AssertionFailedError (junit.framework.AssertionFailedError)168 JMSException (javax.jms.JMSException)167 RemoveException (javax.ejb.RemoveException)66 CreateException (javax.ejb.CreateException)57 DataSource (javax.sql.DataSource)55 Hashtable (java.util.Hashtable)54 Assembler (org.apache.openejb.assembler.classic.Assembler)47 EjbJar (org.apache.openejb.jee.EjbJar)41 NameNotFoundException (javax.naming.NameNotFoundException)40 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)38 Connection (java.sql.Connection)37 StatelessBean (org.apache.openejb.jee.StatelessBean)30