Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] How to bind / lookup DataSource via JNDI without container

I’m working on a unit test which talks to MySQL. I’m trying to bind/lookup a datasource via JNDI and the code
 below works with Tomcat, note it makes reference to org.apache.naming.java.javaURLContextFactory, which is
a tomcat class.  I’m able to bind the datasource without issues.

 How can I do this with Jetty factory class so I don’t have to have any tomcat dependencies, I’ve switched everything
over to Jetty but this.

I tried using “org.eclipse.jetty.jndi.java.javaURLContextFactory" thinking it might be similar for the INITIAL_CONTEXT_FACTORY,
but I get a:
Cannot instantiate class: org.eclipse.jetty.jndi.java.javaURLContextFactory
So It doesn’t appear quite as simple as swapping one out for the other.

Any help here would be greatly appreciated.
Thanks,
Chris


package test

import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;


public class DataStoreSetup {

    public static void main(String argv[]) {
        DataStoreSetup.bindDataStore();
    }
    public static void bindDataStore() {
        // setup the jndi context and the datasource
        try {
            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    "org.eclipse.jetty.jndi.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES,
                    "org.apache.naming");

            InitialContext ic = new InitialContext();

            try {
                ic.lookup("java:comp/env/jdbc/myDB");
            } catch (javax.naming.NameNotFoundException e) {
                ic.createSubcontext("java:");
                ic.createSubcontext("java:comp");
                ic.createSubcontext("java:comp/env");
                ic.createSubcontext("java:comp/env/jdbc");

                String driverClassName = System.getenv("JDBC_DRIVER_CLASSNAME");
                String jdbcUrl = System.getenv("JDBC_URL");
                String jdbcUserName = System.getenv("JDBC_USERNAME");
                String jdbcPassword = System.getenv("JDBC_PASSWORD");
                BasicDataSource bds = new BasicDataSource();
                bds.setDriverClassName(driveClassName);
                bds.setUrl(jdbcUrl);
                bds.setUsername(jdbcUserName);
                bds.setPassword(jdbcPassword);
                Connection conn = bds.getConnection();
                System.out.println("Connection = "+conn);
                ic.bind("java:comp/env/jdbc/myDB", bds);
            }
        } catch (Exception ex)
        {
            System.err.println(ex.getMessage());
        }
    }
}




--
 

Back to the top