Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To develop an Enterprise Java Bean for Library operations
#1

Aim : To develop an Enterprise Java Bean for Library operations
Algorthm :
1. Create five programs Book, Bookbome, Bookbean, Bookexception, and Bookclient
2. In the Bookhome class just invoke the create() method with appropriate arguments
3. In the Book class, just define the prototytpes of addbook(), removebook() and getcontents() methods
4. In the Bookbean class provide the implementation for the various methods
5. In the Bookexception class provide the implementation for BookException
6. In the Bookclient class get the customer name and the various books he wants, also perform the other operations accordingly
Program
Book- Remote interface

Code:
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public interface Book extends EJBObject
{
public void addBook(String title) throws RemoteException;
public void removeBook(String title) throws RemoteException,BookException;
public Vector getContents() throws RemoteException;
}

Book-Home interface

import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface BookHome extends EJBHome
{
public Book create(String person) throws RemoteException,CreateException;
public Book create(String person,String id) throws RemoteException,CreateException;
}

Book Bean class
import java.util.*;
import javax.ejb.*;
public class BookEJB implements SessionBean
{
String CustomerName;
String customerId;
Vector contents;
public void ejbCreate(String person) throws CreateException
{
if(person==null)
throw new CreateException("\nNull person not allowed");
else
CustomerName= person;
System.out.println("\nCustomer name: "+person);
customerId = "0";
contents = new Vector();
}
public void ejbCreate(String person,String id) throws CreateException
{
if(person==null)
throw new CreateException("\nNull person not allowed");
else
CustomerName= person;
System.out.println("\nCustomer name: "+person);
customerId = "0";
contents = new Vector();
}
public void addBook(String title)
{
contents.addElement(title);
}
public void removeBook(String title) throws BookException
{
boolean result = contents.removeElement(title);
if(result==false)
throw new BookException(title+"  Book is not in cart");
}
public Vector getContents()
{
return contents;
}
public BookEJB() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void setSessionContext(SessionContext ctx) {}
}

Book-Exception

public class BookException extends Exception
{
public BookException() {}
public BookException(String msg)
{
super(msg);
}
}

Book-Client
Code:
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import java.io.*;
import java.util.*;
public class BookClient
{
public static void main(String arg[])
{
Enumeration enum;
Vector bookList;
Book shoppingCart;
try
{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://localhost:7001");
p.put(Context.SECURITY_PRINCIPAL,"mca");
p.put(Context.SECURITY_CREDENTIALS,"mcastudent");
InitialContext ict = new InitialContext(p);
System.out.println("Context sucessful");
Object t = ict.lookup("BookEJB2");
System.out.println("Lookup successful");
BookHome home = (BookHome)PortableRemoteObject.narrow(t,BookHome.class);
shoppingCart = home.create("Siva","123");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter customer name ");
String cust = br.readLine();
System.out.println("\nEnter the bookname U want: ");
String book1 = br.readLine();
shoppingCart.addBook(book1);
System.out.println("\nEnter the bookname U want: ");
String book2 = br.readLine();
shoppingCart.addBook(book2);
System.out.println("\nEnter the bookname U want: ");
String book3 = br.readLine();
shoppingCart.addBook(book3);
System.out.println("\nCustomer name is :"+cust);
System.out.println("Selected books are listed below");
bookList  = new Vector();
bookList = shoppingCart.getContents();
enum = bookList.elements();
while(enum.hasMoreElements())
{
String title = (String)enum.nextElement();
System.out.println(title);
}
System.out.println("\nDo U want to remove any book from cart");
String ch = br.readLine();
if(ch.equals("y"))
{
try
{
System.out.println("Enter the book name");
String remBook = br.readLine();
shoppingCart.removeBook(remBook);
System.out.println("\nBook has been removed from the cart");
System.out.println("Customer nmae: "+cust);
System.out.println("Slected books are listed below");
bookList = shoppingCart.getContents();
enum = bookList.elements();
while(enum.hasMoreElements())
{
String title1 = (String)enum.nextElement();
System.out.println(title1);
}
System.out.println("Thanks for dealing with us");
}
catch(Exception ex)
{
System.out.println("caught a BookException " +ex.getMessage());
}
}
else
System.out.println("thanks for dealing with us");
shoppingCart.remove();
}
catch(Exception ex)
{
System.out.println("Caught a Book Exception");
ex.printStackTrace();
}
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.