NameNotfoundException in Jetty 9 at mail session r

2019-08-29 06:13发布

问题:

I'm writing a REST API backend servlet with Jersey, and I have some problems using jndi to configure mail sending.

Here's my WEB-INF/jetty-env.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="mail" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>mail/Session</Arg>
    <Arg>
      <New class="org.eclipse.jetty.jndi.factories.MailSessionReference">
        <Set name="user">MY_USER</Set>
        <Set name="password">MY_PASS</Set>
        <Set name="properties">
          <New class="java.util.Properties">
            <Put name="mail.smtp.host">MY_HOST</Put>
            <Put name="mail.from">MY_SENDER</Put>
            <Put name="mail.debug">true</Put>
          </New>
        </Set>
      </New>
    </Arg>
  </New>
</Configure>

And here's how I try to look it up:

Session session = (Session) new InitialContext().lookup("java:comp/env/mail/Session");

However, I get:

 javax.naming.NameNotFoundException; remaining name 'mail/Session'
at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:487)

From the DEBUG logs after 'service jetty restart':

20-01-2015 10:10:39 DEBUG jndi:183 - SAVE mail/Session in o.e.j.w.WebAppContext@4b9e13df{/finalcountdown.backend,file:/tmp/jetty-0.0.0.0-8085-finalcountdown.backend.war-_finalcountdown.backend-any-2063340985085206547.dir/webapp/,STARTING}{/finalcountdown.backend.war}
20-01-2015 10:10:39 DEBUG jndi:79 - InitialContextFactory.getInitialContext()
20-01-2015 10:10:39 DEBUG jndi:82 - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@6b9651f3
20-01-2015 10:10:39 DEBUG jndi:79 - InitialContextFactory.getInitialContext()
20-01-2015 10:10:39 DEBUG jndi:82 - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@23d2a7e8
20-01-2015 10:10:39 DEBUG jndi:411 - Looking up name="org.eclipse.jetty.webapp.WebAppContext@4b9e13df"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=org.eclipse.jetty.webapp.WebAppContext@4b9e13df obj=org.eclipse.jetty.jndi.NamingContext@4abdb505[name=org.eclipse.jetty.webapp.WebAppContext@4b9e13df,parent=org.eclipse.jetty.jndi.local.localContextRoot$NamingRoot@7ce6a65d,bindings.size=0] for context=null as org.eclipse.jetty.webapp.WebAppContext@4b9e13df: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@4abdb505[name=org.eclipse.jetty.webapp.WebAppContext@4b9e13df,parent=org.eclipse.jetty.jndi.local.localContextRoot$NamingRoot@7ce6a65d,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext org.eclipse.jetty.webapp.WebAppContext@4b9e13df created
20-01-2015 10:10:39 DEBUG jndi:464 - Looking up name="__"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=__ obj=org.eclipse.jetty.jndi.NamingContext@1500955a[name=__,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0] for context=org.eclipse.jetty.webapp.WebAppContext@4b9e13df as __: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@1500955a[name=__,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext __ created
20-01-2015 10:10:39 DEBUG jndi:464 - Looking up name="mail"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=mail obj=org.eclipse.jetty.jndi.NamingContext@e874448[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@1500955a,bindings.size=0] for context=__ as mail: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@e874448[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@1500955a,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext mail created
20-01-2015 10:10:39 DEBUG jndi:1327 - Removing binding with key=Session
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=Session obj=mail/Session for context=mail as Session: org.eclipse.jetty.plus.jndi.Resource:mail/Session
20-01-2015 10:10:39 DEBUG jndi:88 - Bound object to Session
20-01-2015 10:10:39 DEBUG jndi:411 - Looking up name="org.eclipse.jetty.webapp.WebAppContext@4b9e13df"
20-01-2015 10:10:39 DEBUG jndi:76 - Subcontext org.eclipse.jetty.webapp.WebAppContext@4b9e13df already exists
20-01-2015 10:10:39 DEBUG jndi:464 - Looking up name="mail"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=mail obj=org.eclipse.jetty.jndi.NamingContext@29b5cd00[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0] for context=org.eclipse.jetty.webapp.WebAppContext@4b9e13df as mail: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@29b5cd00[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext mail created
20-01-2015 10:10:39 DEBUG jndi:1327 - Removing binding with key=Session
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=Session obj=Reference Class Name: javax.mail.Session

So it should be bound. What am I missing here?

回答1:

There's 2 things for you to fix.

First, Your DOCTYPE is bad.

It should be

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

Second, you seem to be missing the mandatory WEB-INF/web.xml reference to your JNDI object.

Add this:

<resource-ref>
  <res-ref-name>mail/Session</res-ref-name>
  <res-type>javax.mail.Session</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

That should make your environment happy

Also note, that for this to work on Jetty 9.2+ you'll want the the annotations and jndi modules enabled in your ${jetty.base}/start.ini