Search code examples
javajettyjndi

Embedded Jetty with jetty env file not working


I have been trying to set up an embedded jetty server (Jetty version 9.3.7), and have been having some difficulties with the jetty-env file.

My application has been successfully deployed on multiple servers, so I know the problem must be related to the jetty config specifically.

I have looked at this question, and have based my current implementation on the answer, but it is not working.

I have the following src/main/resources/WEB-INF/jetty-env.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="datasource" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/datasource</Arg>
    <Arg>
      <New class="org.apache.commons.dbcp.BasicDataSource">
        <Set name="url">jdbc:h2:tcp://localhost:9092/build/db/testdb;USER=sa</Set>
      </New>
    </Arg>
  </New>
</Configure>

I also have a src/main/resources/WEB-INF/web.xml file, which contains the following segment:

<resource-ref>
    <description>TESTDB</description>
    <res-ref-name>jdbc/datasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

I am able to use the Gradle jetty plugin to start a jetty server, and so know that this configuration is valid.

Below is the class that I am trying to use to create a jetty server:

public class JettyServer {

    private static final Logger LOGGER = LoggerFactory.getLogger(JettyServer.class);

    public static void main(final String[] args) throws Exception {
        LOGGER.debug("Starting jetty server");
        int port = 8083;
        LOGGER.debug("Setting the port to {}", port);
        final Server server = new Server(port);
        Configuration.ClassList classList = Configuration.ClassList.serverDefault(server);
        classList.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration",
            "org.eclipse.jetty.plus.webapp.PlusConfiguration");
        final WebAppContext root = new WebAppContext();
        root.setContextPath("/");
        root.setResourceBase(".");
        root.setDescriptor(JettyServer.class.getResource("/WEB-INF/web.xml").toString());
        server.setHandler(root);
        server.start();
        server.join();
    }
}

The JNDI name is referenced in a spring configuration java class:

@Bean
public DataSource dataSource() throws NamingException {
    final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("java:comp/env/jdbc/datasource");
    jndiObjectFactoryBean.setResourceRef(true);
    jndiObjectFactoryBean.setProxyInterface(DataSource.class);
    jndiObjectFactoryBean.afterPropertiesSet();
    return (DataSource)jndiObjectFactoryBean.getObject();
}

When I try to start my application, I get the following error:

Caused by: javax.naming.NameNotFoundException: null
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:532) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:563) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:578) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:106) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at javax.naming.InitialContext.lookup(InitialContext.java:417) ~[na:1.8.0_73]
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectTargetSource.afterPropertiesSet(JndiObjectTargetSource.java:97) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.createJndiObjectProxy(JndiObjectFactoryBean.java:315) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.access$000(JndiObjectFactoryBean.java:304) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:200) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]

As I say, if I use the same config to start a jetty server from the config, there are no issues and I get a server, so I assume the problem must relate to my JettyServer class, but I can't find anything online to indicate what I'm doing wrong.

Can anybody spot anything obvious?


Solution

  • In the end, I took a look at the source code for the gradle jetty plugin and noticed that they were adding the configurations a little differently. This worked for me, and my server is now running as expected:

    public class JettyServer {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(JettyServer.class);
    
        public static void main(final String[] args) throws Exception {
            LOGGER.debug("Starting jetty server");
            int port = 8083;
            LOGGER.debug("Setting the port to {}", port);
            final Server server = new Server(port);
            final WebAppContext root = new WebAppContext();
            root.setContextPath("/");
            root.setResourceBase(".");
            EnvConfiguration envConfiguration = new EnvConfiguration();
            envConfiguration.setJettyEnvXml(JettyServer.class.getResource("/WEB-INF/jetty-env.xml").toURI().toURL());
            //Adding the actual classes instead of just the class names
            root.setConfigurations(new Configuration[]{envConfiguration, new PlusConfiguration(), new WebXmlConfiguration()});
            root.setDescriptor(JettyServer.class.getResource("/WEB-INF/web.xml").toString());
            server.setHandler(root);
            server.start();
            server.join();
        }
    }
    

    I guess I must have my project set up slightly incorrectly, which means that the default configurations weren't working. I will not accept this answer for now in case someone can tell me any other alternatives.