java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4086
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Using reflection with an EJB E-mail
User Rating: / 5
PoorBest 

EJB allows us to use meta-data in order to examine an EJB using reflection and to determine its methods, method parameters, and other information. In your EJB application, you would like to examine an EJB reference using reflection. However, since an EJB reference is actually a reference to a stub object in the EJB container (pointing to an EJB), you cannot examine the actual EJB itself. Or you might only have a reference to the EJB home object and you want to determine the EJB's available business methods before looking up or creating one. Using reflection on an EJB reference will not work because the reference points to an EJB stub object inside the EJB container, not to the EJB itself.

Here we define an application printing meta-information about a bean specified.


public class DescribeEJB {
    
    public static final void main(String[] args) {
        
        try {
            
            // prepare context:
            Context context = getInitialContext();
            
            // get home handle:
            EJBHome home = (EJBHomePortableRemoteObject.narrow(
                    (EJBHomecontext.lookup("test.someBean")
                    EJBHome.class);
            
            EJBMetaData metaData = home.getEJBMetaData();
            Class homeClass = metaData.getHomeInterfaceClass();
            Class remoteClass = metaData.getRemoteInterfaceClass();
            
            System.out.println("Meta data:");
            System.out.println("  home class:" + homeClass.getName());
            System.out.println("  remote class:" + remoteClass.getName());
            
            // convert handle to real home type:
            home = (EJBHomejavax.rmi.PortableRemoteObject.narrow(home, 
                    homeClass);
            
            System.out.println("  primary-key class:" 
                    metaData.getPrimaryKeyClass().getName());
            
            System.out.println("  is session: " + metaData.isSession());
            
            if (metaData.isSession()) {
                System.out.println("  is stateless:" 
                        metaData.isStatelessSession());
            }
            
            System.out.println("Home interface:");
            Method[] methods = homeClass.getDeclaredMethods();
            
            for (int i = 0; i < methods.length; i++) {
                
                Class[] params = remoteMethods[i].getParameterTypes();
                
                System.out.println("  " + methods[i].getName() ", " 
                        params.length + " parameters:");
                
                for (int j = 0; j < params.length; j++) {
                    System.out.println("    " + params[j].getName());
                }
            }
            
            System.out.println("Remote interface:");
            Method[] remoteMethods = remoteClass.getDeclaredMethods();
            
            for (int i = 0; i < remoteMethods.length; i++) {
                
                Class[] params = remoteMethods[i].getParameterTypes();
                System.out.println("  " + methods[i].getName() ", " 
                        params.length + " parameters:");
                
                Class[] params = remoteMethods[i].getParameterTypes();
                for (int j = 0; j < params.length; j++) {
                    System.out.println("    " + params[j].getName());
                }
            }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.