Jetty 7: configuring JNDI for Start.java

给你一囗甜甜゛ 提交于 2019-12-09 05:39:45

问题


Following Wicket 1.5's lead, I'm converting a project from Jetty 6.1.25 to 7.5.0.v20110901. My existing Start.java contains the following setup, which I use to configure JNDI:

    EnvConfiguration envConfiguration = new EnvConfiguration();
    URL url = new File("src/main/webapp/WEB-INF/jetty-env.xml").toURI().toURL();
    envConfiguration.setJettyEnvXml(url);

    bb.setConfigurations(new Configuration[]{new WebInfConfiguration(),
                         envConfiguration,
                         new org.mortbay.jetty.plus.webapp.Configuration(), new JettyWebXmlConfiguration(),
                         new TagLibConfiguration()});

Then my jetty-env.xml has the following:

<Configure class="org.mortbay.jetty.webapp.WebAppContext">

    <New class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>jdbc/myapp</Arg>
        <Arg>
            <New class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
                <Set name="url">jdbc:mysql://localhost/myapp?characterEncoding=utf8</Set>
                <Set name="username">username</Set>
                <Set name="password">password</Set>
            </New>
        </Arg>
    </New>

</Configure>

This has worked great in Jetty 6, but in 7, org.mortbay.jetty.plus.webapp.Configuration does not seem to exist (or perhaps I'm missing a Jar).

Can someone give me some guidance on how to configure JNDI with Jetty 7?


回答1:


Put the following into src/test/jetty/jetty-env.xml:

<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
    <New class="org.eclipse.jetty.plus.jndi.EnvEntry">
    <Arg>jdbc/mydatasource</Arg>
    <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
            <Set name="Url">jdbc:mysql://localhost/mydatabase?characterEncoding=utf8</Set>
            <Set name="User">username</Set>
            <Set name="Password">password</Set>
        </New>
    </Arg>
    </New>
</Configure>

Then modify Start.java to define the following properties:

System.setProperty("java.naming.factory.url.pkgs", "org.eclipse.jetty.jndi");
System.setProperty("java.naming.factory.initial", "org.eclipse.jetty.jndi.InitialContextFactory");

And add the following configuration to the WebAppContext:

EnvConfiguration envConfiguration = new EnvConfiguration();
URL url = new File("src/test/jetty/jetty-env.xml").toURI().toURL();
envConfiguration.setJettyEnvXml(url);

bb.setConfigurations(new Configuration[]{ new WebInfConfiguration(), envConfiguration, new WebXmlConfiguration() });

Full details on my blog.




回答2:


Starting with Jetty 7, the package name was changed from org.mortbay.jetty to org.eclipse.jetty.

In addition, org.eclipse.jetty.plus.webapp.Configuration was renamed in version 7.2.0 and the new name is PlusConfiguration. I'm guessing this was done to avoid a name clash with org.eclipse.jetty.webapp.Configuration.



来源:https://stackoverflow.com/questions/7923980/jetty-7-configuring-jndi-for-start-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!