1 Reply Latest reply on Sep 21, 2006 5:00 AM by onglaochancuu

    JNDI and java: context problem

    fla83tn

      Hi,
      In my project I'd like to send an e-mail from my message-driven bean but I have 2 problem.
      First of all this is my problematic code:

      
       private void send(Context c,String to){
       javax.mail.Session session = null;
      
       try {
      
       session = ---- see after -----
      
       MimeMessage m = new MimeMessage(session);
       m.setFrom();
      
       m.setRecipients(javax.mail.Message.RecipientType.TO,
       InternetAddress.parse(to, false));
       m.setSubject("Pinestore Order confirmation");
       m.setSentDate(new Date());
       m.setContent("The Order has been processed",
       "text/plain"); //equivalent to setText..
       Store s = session.getStore();
       s.connect(); // POP authentication
       Transport.send(m);
      
       System.out.println("Message sent OK.");
       } catch (javax.mail.MessagingException e) {
       e.printStackTrace();
       } catch (javax.naming.NamingException e) {
       e.printStackTrace();
       }
       }
      
       public static void main(String args[]){
       Context c = null;
       try {
       Hashtable environment = new Hashtable();
       environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
       environment.put(Context.PROVIDER_URL, "jnp://localhost:1099");
       c =new InitialContext(environment);
      
       }
       catch (NamingException ex) {
       }
       send(c,"xxx@domain");
       }
      

      This are my question:

      this is only because I have some confusion:
      Why couldn't I access to resources in jndi at java:comp/env?
      If I do
      Context c = b.getInitialContext();
      Context compEnv = (Context) c.lookup("java:comp/env");
      

      compEnv is null..the only things that work is c.lookup("") or lookup of everything that is located under Global JNDI Namespace..


      My second question is:
      If I leave the default
      <attribute name="JNDIName">java:/Mail</attribute>
      in file mail-service.xml I can't manage to lookup via jndi to the MBean...this because the java: context seems to be invisible..only names that are in Global JNDI Namespace are visible..



      So I try to get around the problem modifying the file mail-service.xml as
      <attribute name="JNDIName">Mail</attribute>
      so now I can do in my code:
       session = (javax.mail.Session)PortableRemoteObject.narrow(
       c.lookup("Mail"), javax.mail.Session.class);
      

      In this case the lookup works, but now I get still error because javax.mail.Session isn't a remote interface and then I can not do a PortableRemoteObject.narrow...



      Somewhere I read that:

      "objects referenced behind the java: root are not reachable by external JNDI calls, they are private to the VM of the server."

      Is it true? How can I solve the problem?

      Instead for the last error I don't know how to bind the result of the lookup..

      Please somebody help me!!!!!