Search code examples
datasourcejndijaasjetty-9

"javax.naming.NameNotFoundException; remaining name 'env/jdbc/myds' " appear when I using jaas to get the DataSource


I use jetty 9.2.2.v20140723 as the server. I use jetty-maven-plugin, the pom.xml like this:

    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.2.2.v20140723</version>
        <configuration>
            <systemProperties>
                <systemProperty>
                     <name>java.security.auth.login.config</name>
                     <value>props.conf</value>
                 </systemProperty>
            </systemProperties>
            <!-- <scanIntervalSeconds>3</scanIntervalSeconds> -->
            <connectors>
                <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                    <port>8080</port>
                </connector>
                <connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">
                    <port>8443</port>
                    <keystore>keystore.jks</keystore>
                    <keyPassword>password</keyPassword>
                    <password>password</password>
                    <needClientAuth>false</needClientAuth>
                    <wantClientAuth>false</wantClientAuth>
                </connector>
            </connectors>
        </configuration>
    </plugin>

jetty-env.xml like this:

  <Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="myds" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg><Ref refid="wac"/></Arg>
     <Arg>jdbc/myds</Arg>
     <Arg>

        <New class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
           <Set name="Url">jdbc:mysql://localhost:3306/cp</Set>
           <Set name="User">root</Set>
           <Set name="Password">1234</Set>
        </New>
     </Arg>
 </New>
</Configure>

web.xml like this:

<resource-ref>
    <description>My DataSource Reference</description>
    <res-ref-name>jdbc/myds</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

When I login in like this:

ctx = new LoginContext("props", new TestCallBackHandler(username, password));
ctx.login();

I get the error:

javax.security.auth.login.LoginException: java.lang.IllegalStateException: javax.naming.NameNotFoundException; remaining name 'env/jdbc/myds'

But I can get the datasource like this:

InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/myds");

Solution

  • Is the Vaadin problem, I changed the "@Push"