/*
XMLReaderEJB.java
*/
package com.stardeveloper.ejb.session;
import com.stardeveloper.ejb.util.Contact;
import com.stardeveloper.ejb.exception.CouldNotReadException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
public class XMLReaderEJB implements SessionBean {
public Contact[] readXML(String fileName) throws CouldNotReadException {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(fileName));
Element root = doc.getRootElement();
List list = root.getChildren("contact");
Contact[] contacts = new Contact[list.size()];
Iterator iter = list.iterator();
int i = 0;
while(iter.hasNext()) {
Element e = (Element)iter.next();
String name = e.getChild("name").getText();
String email = e.getChild("email").getText();
String phone = e.getChild("phone-no").getText();
String state = e.getChild("address").getAttribute("state").getValue();
String country = e.getChild("address").getAttribute("country").getValue();
String address = e.getChild("address").getText();
contacts[i] = new Contact(name, email, phone, state, country, address);
i++;
}
return contacts;
} catch (Exception e) {
throw new CouldNotReadException(e.getMessage());
}
}
public void ejbCreate() throws CreateException {}
public void setSessionContext(SessionContext context) {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
}
Explanation :
If you have followed the earlier article, the code above except
readXML() method shouldn't be difficult. For a Session bean, it's implementation class has to implement javax.ejb.SessionBean interface. Since we are
not going to use callback methods provided by SessionBean interface, we will provide empty implementation of those
methods. The real method which we have declared in the Remote interface and which we have to implement in the
XMLReaderEJB class is the readXML() method.
There are different Java libraries available to read and write XML files, one of them is JDOM. We are
going to use JDOM in readXML() method to read the XML document. We begin by creating SAXBuilder object.
public Contact[] readXML(String fileName) throws CouldNotReadException {
try {
SAXBuilder builder = new SAXBuilder();
Next we get hold of the Document object by calling SAXBuilder.build() method. A Document object
encapsulates the complete XML document in it. We then use methods of this Document object to get hold of
different Elements in the XML file and their values.
Document doc = builder.build(new File(fileName));
In an XML file every tag is an 'Element'. We get hold of the root element, which in our case is <contacts>
tag, by calling Document.getRootElement() method.
Element root = doc.getRootElement();
Since we have two <contact> elements in our Contacts.xml file. We get hold of both of these elements
by calling Element.getChildren("contact") method. We have to return an array of Contact objects found in the
Contacts.xml file, so we create a new Contact object's array equal to the size of the List we got.
List list = root.getChildren("contact");
Contact[] contacts = new Contact[list.size()];
Then iterate through the List of contact elements we retrieved earlier, and get the actual values of different
sub-elements and attributes out by using different but easy to remember methods. We store these values in temporary
variables. Then we create new Contact object from these retrieved values and store it in the Contact object array
we had created earlier. As simple as that!
Iterator iter = list.iterator();
int i = 0;
while(iter.hasNext()) {
Element e = (Element)iter.next();
String name = e.getChild("name").getText();
String email = e.getChild("email").getText();
String phone = e.getChild("phone-no").getText();
String state = e.getChild("address").getAttribute("state").getValue();
String country = e.getChild("address").getAttribute("country").getValue();
String address = e.getChild("address").getText();
contacts[i] = new Contact(name, email, phone, state, country, address);
i++;
}
return contacts;
In the end, we catch any exceptions and wrap their messages in our own CouldNotReadException.
} catch (Exception e) {
throw new CouldNotReadException(e.getMessage());
}
}
v. Contact.java :
Let's now create the Contact class we used above. Create a new 'Contact.java' source file in the
C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\util\ folder. Copy and paste following code in it :
/*
Contact.java
*/
package com.stardeveloper.ejb.util;
import java.io.Serializable;
public class Contact implements Serializable {
private String name;
private String email;
private String phone;
private String state;
private String country;
private String address;
public Contact(String name, String email, String phone,
String state, String country, String address) {
this.name = name;
this.email = email;
this.phone = phone;
this.state = state;
this.country = country;
this.address = address;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public String getState() {
return state;
}
public String getCountry() {
return country;
}
public String getAddress() {
return address;
}
}
Explanation :
'Contact' is a simple JavaBean style class which contains certain private variables and their public
getter methods. The point for you to remember is that it is Serializable, i.e. it implements java.io.Serializable
interface. Every object which you expect to pass between different virtual machines *should* be Serializable.
vi.CouldNotReadException.java :
Let's create the our own CouldNotReadException which we have used in readXML() method of XMLReaderEJB bean. Create
new CouldNotReadException.java source file in C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\exceptionfolder. Copy and paste following code in it :
/*
CouldNotReadException.java
*/
package com.stardeveloper.ejb.exception;
public class CouldNotReadException extends Exception {
public CouldNotReadException(String msg) {
super(msg);
}
Nothing much to say about the code above, it is quite simple to understand.
vii. ejb-jar.xml :
Now in the end we need to create deployment descriptor for our XMLReaderEJB Session bean. This deployment
descriptor contains important information about XMLReaderEJB like what are it's Remote, Home and EJB-Class interfaces,
who controls the transactions? etc.
Create a new 'META-INF' folder under the 'XMLReaderEJB' folder. In the 'META-INF' folder create a new 'ejb-jar.xml'
file. The complete path to 'ejb-jar.xml' should be :
C:\Projects\EJB\XMLReaderEJB\META-INF\ejb-jar.xml
Copy and paste following text in it :