You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2001/03/20 02:21:07 UTC

cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory EjbFactory.java

remm        01/03/19 17:21:07

  Modified:    catalina/src/share/org/apache/naming/factory EjbFactory.java
  Log:
  - Use the specified ejb-link to resolve the EJB reference.
  - Will do a JNDI lookup using the URL specified.
  - Will also check the type of the bean returned, and check if it implements
    the home interface specified in the reference.
  
  Revision  Changes    Path
  1.3       +27 -3     jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java
  
  Index: EjbFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- EjbFactory.java	2001/01/23 03:43:53	1.2
  +++ EjbFactory.java	2001/03/20 01:21:05	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java,v 1.2 2001/01/23 03:43:53 remm Exp $
  - * $Revision: 1.2 $
  - * $Date: 2001/01/23 03:43:53 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java,v 1.3 2001/03/20 01:21:05 remm Exp $
  + * $Revision: 1.3 $
  + * $Date: 2001/03/20 01:21:05 $
    *
    * ====================================================================
    *
  @@ -67,6 +67,7 @@
   import java.util.Hashtable;
   import javax.naming.Name;
   import javax.naming.Context;
  +import javax.naming.InitialContext;
   import javax.naming.NamingException;
   import javax.naming.Reference;
   import javax.naming.RefAddr;
  @@ -77,7 +78,7 @@
    * Object factory for EJBs.
    * 
    * @author Remy Maucherat
  - * @version $Revision: 1.2 $ $Date: 2001/01/23 03:43:53 $
  + * @version $Revision: 1.3 $ $Date: 2001/03/20 01:21:05 $
    */
   
   public class EjbFactory
  @@ -110,6 +111,27 @@
           
           if (obj instanceof EjbRef) {
               Reference ref = (Reference) obj;
  +
  +            // If ejb-link has been specified, resolving the link using JNDI
  +            RefAddr linkRefAddr = ref.get(EjbRef.LINK);
  +            if (linkRefAddr != null) {
  +                // Retrieving the EJB link
  +                String ejbLink = linkRefAddr.getContent().toString();
  +                Object beanObj = (new InitialContext()).lookup(ejbLink);
  +                // Load home interface and checking if bean correctly
  +                // implements specified home interface
  +                String homeClassName = ref.getClassName();
  +                Class home = Class.forName(homeClassName);
  +                if (home.isInstance(beanObj)) {
  +                    return beanObj;
  +                } else {
  +                    throw new NamingException
  +                        ("Bean of type " + beanObj.getClass().getName() 
  +                         + " doesn't implement home interface " 
  +                         + home.getName());
  +                }
  +            }
  +            
               ObjectFactory factory = null;
               RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
               if (factoryRefAddr != null) {
  @@ -138,6 +160,7 @@
                       }
                   }
               }
  +
               // Note: No defaults here
               if (factory != null) {
                   return factory.getObjectInstance
  @@ -146,6 +169,7 @@
                   throw new NamingException
                       ("Cannot create resource instance");
               }
  +
           }
   
           return null;
  
  
  

Re: cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory EjbFactory.java

Posted by Remy Maucherat <re...@apache.org>.
> This code does not work because the classloader is different than the
> servlet class loader.  The "Class home = Class.forName(homeClassName);"
> line fails with "Class.forName() failed: java.lang.ClassNotFoundException:
> cool.models.coop07.java.S_STRESS_31KpsHome".
> This same line works in the servlet.

I thought this would be equivalent, but Class.forName doesn't use the
context class loader. Instead, I should directly use the context class
loader to load the home interface ...

I re-read the J2EE 1.3 PFD spec, and I mistakenly interpreted paragraph
5.3.3 as things I had to enforce.

> Actually, I do not think the test of class type returned is necessary.
> When it is eliminated, the servlet will get an exception when casting the
> remote object to the home interface anyway (assuming some type of
problem).
>  Otherwise, it works and less code is executed.
> Also, if this were an LDAP/IIOP class, ordinary class casting will not
> work, the servlet would be using "PortableRemoteObject.narrow(...)"
> instead.

True.

> So, my suggestion is to erase lines:
> >   +                String homeClassName = ref.getClassName();
> >   +                Class home = Class.forName(homeClassName);
> >   +                if (home.isInstance(beanObj)) {
> >   +                } else {
> >   +                    throw new NamingException
> >   +                        ("Bean of type " +
beanObj.getClass().getName()
> >   +                         + " doesn't implement home interface "
> >   +                         + home.getName());
> >   +                }
> And leave only line:
> >   +                return beanObj;

Ok.

Remy


Re: cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory EjbFactory.java

Posted by tt...@ticnet.com.
This code does not work because the classloader is different than the
servlet class loader.  The "Class home = Class.forName(homeClassName);"
line fails with "Class.forName() failed: java.lang.ClassNotFoundException:
cool.models.coop07.java.S_STRESS_31KpsHome".
This same line works in the servlet.

Actually, I do not think the test of class type returned is necessary.  
When it is eliminated, the servlet will get an exception when casting the
remote object to the home interface anyway (assuming some type of problem).
 Otherwise, it works and less code is executed.  
Also, if this were an LDAP/IIOP class, ordinary class casting will not
work, the servlet would be using "PortableRemoteObject.narrow(...)"
instead.

So, my suggestion is to erase lines:
>   +                String homeClassName = ref.getClassName();
>   +                Class home = Class.forName(homeClassName);
>   +                if (home.isInstance(beanObj)) {
>   +                } else {
>   +                    throw new NamingException
>   +                        ("Bean of type " + beanObj.getClass().getName() 
>   +                         + " doesn't implement home interface " 
>   +                         + home.getName());
>   +                }
And leave only line:
>   +                return beanObj;

Tim

remm@apache.org writes:

> remm        01/03/19 17:21:07
> 
>   Modified:    catalina/src/share/org/apache/naming/factory EjbFactory.java
>   Log:
>   - Use the specified ejb-link to resolve the EJB reference.
>   - Will do a JNDI lookup using the URL specified.
>   - Will also check the type of the bean returned, and check if it implements
>     the home interface specified in the reference.
>   
>   Revision  Changes    Path
>   1.3       +27 -3     jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java
>   
>   Index: EjbFactory.java
>   ===================================================================
>   RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java,v
>   retrieving revision 1.2
>   retrieving revision 1.3
>   diff -u -r1.2 -r1.3
>   --- EjbFactory.java	2001/01/23 03:43:53	1.2
>   +++ EjbFactory.java	2001/03/20 01:21:05	1.3
>   @@ -1,7 +1,7 @@
>    /*
>   - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java,v 1.2 2001/01/23 03:43:53 remm Exp $
>   - * $Revision: 1.2 $
>   - * $Date: 2001/01/23 03:43:53 $
>   + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/EjbFactory.java,v 1.3 2001/03/20 01:21:05 remm Exp $
>   + * $Revision: 1.3 $
>   + * $Date: 2001/03/20 01:21:05 $
>     *
>     * ====================================================================
>     *
>   @@ -67,6 +67,7 @@
>    import java.util.Hashtable;
>    import javax.naming.Name;
>    import javax.naming.Context;
>   +import javax.naming.InitialContext;
>    import javax.naming.NamingException;
>    import javax.naming.Reference;
>    import javax.naming.RefAddr;
>   @@ -77,7 +78,7 @@
>     * Object factory for EJBs.
>     * 
>     * @author Remy Maucherat
>   - * @version $Revision: 1.2 $ $Date: 2001/01/23 03:43:53 $
>   + * @version $Revision: 1.3 $ $Date: 2001/03/20 01:21:05 $
>     */
>    
>    public class EjbFactory
>   @@ -110,6 +111,27 @@
>            
>            if (obj instanceof EjbRef) {
>                Reference ref = (Reference) obj;
>   +
>   +            // If ejb-link has been specified, resolving the link using JNDI
>   +            RefAddr linkRefAddr = ref.get(EjbRef.LINK);
>   +            if (linkRefAddr != null) {
>   +                // Retrieving the EJB link
>   +                String ejbLink = linkRefAddr.getContent().toString();
>   +                Object beanObj = (new InitialContext()).lookup(ejbLink);
>   +                // Load home interface and checking if bean correctly
>   +                // implements specified home interface
>   +                String homeClassName = ref.getClassName();
>   +                Class home = Class.forName(homeClassName);
>   +                if (home.isInstance(beanObj)) {
>   +                    return beanObj;
>   +                } else {
>   +                    throw new NamingException
>   +                        ("Bean of type " + beanObj.getClass().getName() 
>   +                         + " doesn't implement home interface " 
>   +                         + home.getName());
>   +                }
>   +            }
>   +            
>                ObjectFactory factory = null;
>                RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
>                if (factoryRefAddr != null) {
>   @@ -138,6 +160,7 @@
>                        }
>                    }
>                }
>   +
>                // Note: No defaults here
>                if (factory != null) {
>                    return factory.getObjectInstance
>   @@ -146,6 +169,7 @@
>                    throw new NamingException
>                        ("Cannot create resource instance");
>                }
>   +
>            }
>    
>            return null;
>   
>   
>