users@glassfish.java.net

Re: Problem on configuring Java Mail Session

From: Sahoo <sahoo_at_sun.com>
Date: Thu, 20 Mar 2008 13:03:01 +0530

The correct way to look up resource in Java EE is to use the local name
(a.k.a. logical name) of the resource rather than the actual JNDI name
of the resource. You can run the following sample and see the difference:

@Stateless()
@WebService()
public class VisitorRegistrationService {

    // The actual JNDI name is mail/FossDemoMailSession
   // where as the local name is mail/MailSession.
    @Resource(name="mail/MailSession",
mappedName="mail/FossDemoMailSession")
    Session mailSession; // inject a Mail Session
    /**
     * Web service operation
     */
    @WebMethod
    public int registerVisitor(
            String name,
            String email) {
        try {
        InitialContext ctx = new InitialContext();
        Object a = ctx.lookup("mail/FossDemoMailSession");
        Object b = ctx.lookup("java:comp/env/mail/MailSession");
        System.out.println(a);
        System.out.println(b);
        } catch(Exception e) {
        e.printStackTrace();
        }
        return 0;
  }
}

Looking up the resource using the actual JNDI name
(mail/FossDemoMailSession) returns
com.sun.enterprise.deployment.MailConfiguration_at_49d560 object, where as
if you use java:comp/env/mail/MailSession, it returns
javax.mail.Session_at_157bbd4.

To try out yourself, just compile, jar and deploy. Create a MailSession
using admin gui and give it a name mail/FossDemoMailSession. You can
test the program by using the web service test client that is available
in the admin gui.

Thanks,
Sahoo

glassfish_at_javadesktop.org wrote:
> Hi Sahoo,
> Below post from 'Frenchdrip' pointed out what my doubt was. Actually I tried followig different ways,
> 1. directly using resource injection on setter or instance variable
> 2. inject resource on class level and lookup it from sessionContext
> 3. inject resource on class level and lookup it from JNDI initial context.
>
> Only 1 gets mailSession object. Both 2 and 3 return mail configuration which requires casting to session. I had never experienced such as well known 'NameNotFoundException' so I don't believe that's a JNDI problem.
>
> Thanks,
> Ken
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=264257
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>