The jetty-jndi test cases might prove useful for you.
- https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/jetty-jndi/src/test/java/org/eclipse/jetty/jndi/factories/TestMailSessionReference.java - https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/jetty-jndi/src/test/java/org/eclipse/jetty/jndi/java/TestLocalJNDI.java - https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/jetty-jndi/src/test/java/org/eclipse/jetty/jndi/java/TestJNDI.java#L201-L215 Joakim Erdfelt / [email protected] On Thu, Dec 21, 2017 at 11:25 AM, Christopher Gokey <[email protected]> wrote: > 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.ja > vaURLContextFactory > 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_CLA > SSNAME"); > 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()); > } > } > } > > > > > -- > > > _______________________________________________ > jetty-users mailing list > [email protected] > To change your delivery options, retrieve your password, or unsubscribe > from this list, visit > https://dev.eclipse.org/mailman/listinfo/jetty-users >
_______________________________________________ jetty-users mailing list [email protected] To change your delivery options, retrieve your password, or unsubscribe from this list, visit https://dev.eclipse.org/mailman/listinfo/jetty-users
