Example EJB example

You can write EJB code fragments that serve as query engines on an EJB server.

The code fragment below includes an EJB called XmlBean. XmlBean includes the query() method, which allows you to query any XML document on the Web. In this component, query() first creates an XmlDoc object, then queries the document.

The remote interface looks like:

public interface XmlBean extends javax.ejb.EJBObject
{
      /**
        * XQL Method	*/	
     public String XQL(String query, URI location)
     throwsjava.rmi.RemoteException;}

The Bean implementation looks like:

public class XmlBean extends java.lang.Object implements
javax.ejb.SessionBean
{
      ....
      /***
      * XQL Method	
      */	
     public String XQL(String query, java.net.URI location) throws
         java.rmi.RemoteException
{
try {
      String result;
      if((result = 
       Xql.query(query, location.openStream())) !=						
       Xql.EmptyResult)
{
       return (result);	
       }else{
return (null);
       }					
       }catch(Exception e){
            throw new java.rmi.RemoteException(e.getMessage()));
					}
....}
}
And the client code looks like:....Context ctx = getInitialContext();
// make the instance of the class in Jaguar
XmlBeanHome -beanHome =
(XmlBeanHome)ctx.lookup(“XmlBean”);
_xmlBean = (XmlBean)_beanHome.create();
URI u = new URI("http://mywebsite/bookstore.xml");
String res= xmlBean.XQL("/bookstore/book/author/first-name",u);