240 likes | 318 Views
Service Computing. Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009. EJB. EJB CartBean Example (1). import java.util .*; import javax.ejb.*; public class CartBean implements SessionBean { String customerName ; String customerId ; Vector contents;
E N D
Service Computing Prof. Dr. Ramin YahyapourIT & Medien Centrum12. November 2009
EJB CartBean Example (1) import java.util.*; import javax.ejb.*; public class CartBean implements SessionBean { String customerName; String customerId; Vector contents; public void ejbCreate(String person) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } customerId = "0"; contents = new Vector(); } public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} …
EJB CartBean Example (2) … 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 + "not in cart."); } } public Vector getContents() { return contents; } public CartBean() {} }
CartBean –Home Interface import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface CartHome extends EJBHome { Cart create(String person) throws RemoteException, CreateException; }
CartBean –Remote Interface import java.util.*; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Cart extends EJBObject { public void addBook(String title) throws RemoteException; public void removeBook(String title) throws BookException, RemoteException; public Vector getContents() throws RemoteException; }
Savings Account -Database Definition CREATE TABLE savingsaccount (id VARCHAR(3) CONSTRAINT pk_savingsaccount PRIMARY KEY, firstname VARCHAR(24), lastname VARCHAR(24), balance NUMERIC(10,2));
Savings Account –ejbCreate public String ejbCreate(String id, String firstName, String lastName, BigDecimal balance) throws CreateException { if (balance.signum() == -1) { throw new CreateException ("A negative initial balance is not allowed."); } try { insertRow(id, firstName, lastName, balance); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.id = id; this.firstName = firstName; this.lastName = lastName; this.balance = balance; return id; }
Savings Account –ejbRemove public void ejbRemove() { try { deleteRow(id); catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } }
Savings Account –ejbLoad/ejbStore public void ejbLoad() { try { loadRow(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } } public void ejbStore() { try { storeRow(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } }
Savings Account –Finder Methods SavingsAccountjones = home.findByPrimaryKey("836"); ... Collection c = home.findByLastName("Smith"); ... Collection c = home.findInRange(20.00, 99.00); • For every finder method available to a client, the entity bean class must implement a corresponding method that begins with the prefix ejbFind. The SavingsAccountBean class, for example, implements the ejbFindByLastName method as follows: public Collection ejbFindByLastName(String lastName) throws FinderException { Collection result; try { result = selectByLastName(lastName); } catch (Exception ex) { throw new EJBException("ejbFindByLastName " + ex.getMessage()); } return result; }
Savings Account –Finder Methods (2) • the ejbFindByPrimaryKey method is required • other Finder methods are optional public String ejbFindByPrimaryKey(String primaryKey) throws FinderException { boolean result; try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if (result) { return primaryKey; } else { throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found."); } }
Deployment Descriptor <?xml version="1.0" encoding="UTF-8"?> <ejb-jar> <description>JBoss Hello World Application</description> <display-name>Hello World EJB</display-name> <enterprise-beans> <session> <ejb-name>HelloWorld</ejb-name> <home>com.mastertech.sample.HelloWorldHome</home> <remote>com.mastertech.sample.HelloWorld</remote> <ejb-class>com.mastertech.sample.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar>
Deployment Descriptorwith Query Definition <query> <query-method> <method-name>findByName</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </query-method> <ejb-ql> select object(p) From CEntityBean2 as p where p.prof = ?1 </ejb-ql> </query>
JNDI Name Service Example try { // Create the initial context Context ctx = new InitialContext(env); // Look up an object Object obj = ctx.lookup(name); // Print it System.out.println(name + " is bound to: " + obj); } catch (NamingException e) { System.err.println("Problem looking up " + name + ": " + e); }
JNDI Directory Service Example try { // Create the initial directory context DirContextctx = new InitialDirContext(env); // Ask for all attributes of the object Attributes attrs = ctx.getAttributes("cn=Ted Geisel, ou=People"); // Find the surname attribute ("sn") and print it System.out.println("sn: " + attrs.get("sn").get()); } catch (NamingException e) { System.err.println("Problem getting attribute:" + e); }
JNDI Create Context { Hashtableenv = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); Context ctx = new InitialContext(env); … } { Hashtableenv = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); DirContextctx = new InitialDirContext(env); … }