Presentation is loading. Please wait.

Presentation is loading. Please wait.

J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.

Similar presentations


Presentation on theme: "J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento."— Presentation transcript:

1 J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Entity Beans -) cmp -) bmp

2 J0 2 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity Example CMP Entity Example: Currency Converter

3 J0 3 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The Component (Remote) Interface import java.rmi.*; import javax.ejb.*; public interface CurrencyConverter extends EJBObject { public void setRate(double rate) throws RemoteException; public double getRate() throws RemoteException; }

4 J0 4 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The Home Interface import java.rmi.*; import javax.ejb.*; public interface CurrencyConverterHome extends EJBHome { public CurrencyConverter create(String currency,double rate) throws RemoteException, CreateException; public CurrencyConverter findByPrimaryKey(String primaryKey) throws RemoteException, FinderException; /*public Collection findInRange(...) throws RemoteException, FinderException;*/ }

5 J0 5 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The bean import java.rmi.*; import javax.ejb.*; public class CurrencyConverterBean implements EntityBean { public String valuta; public double rate; private EntityContext entityContext; public String ejbCreate(String valuta,double rate) throws CreateException { if (valuta == null) { throw new CreateException("E' obbligatorio specificare la valuta."); } this.valuta=valuta; this.rate=rate; return null; }

6 J0 6 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The bean public void setRate(double rate) throws RemoteException { this.rate=rate; } public double getRate() throws RemoteException{ return rate; }

7 J0 7 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The bean public void ejbPostCreate(String valuta,double rate) throws CreateException {} public void ejbLoad() {//taglia i trailing blank if (valuta !=null) valuta.trim(); } public void ejbStore() {} public void ejbRemove() throws RemoveException {} public void ejbActivate() {} public void ejbPassivate() {} public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; } public void unsetEntityContext() { entityContext = null; }

8 J0 8 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The descriptor currencyConverterCMP CurrencyConverter CurrencyConverterHome CurrencyConverter CurrencyConverterBean Container java.lang.String False nome della valuta valuta

9 J0 9 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The descriptor rapporto di conversione con l'Euro rate valuta CurrencyConverter * NotSupported

10 J0 10 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – activate DB Activate your database (in this case Cloudscape) %J2EEHOME%bin\cloudscape.bat –start

11 J0 11 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento View the properties New tag (Entity)

12 J0 12 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento View the properties Click here

13 J0 13 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento View the properties Click here

14 J0 14 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Click here View the properties

15 J0 15 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The client import javax.ejb.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class ConverterClient { public static void main(String[] args) { CurrencyConverter bean=null; Object objref=null; CurrencyConverterHome home=null; try { Context ctx=new InitialContext(); objref=ctx.lookup("CCcmp"); home =(CurrencyConverterHome)PortableRemoteObject.narrow( objref,CurrencyConverterHome.class); Get naming context and object reference Cast to correct type JNDI name

16 J0 16 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The client try { bean=home.findByPrimaryKey("Pound"); bean.setRate(bean.getRate()*(1+(Math.random()-.5)/100)); } catch (ObjectNotFoundException e) { bean=home.create("Pound",0.666); } try { bean=home.findByPrimaryKey("Dollar"); bean.setRate(bean.getRate()*(1+(Math.random()-.5)/100)); } catch (ObjectNotFoundException e) { bean=home.create("Dollar",0.978); } Update or create record

17 J0 17 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – The client bean=home.findByPrimaryKey("Dollar"); double euro=1000; System.out.println(euro+" Euro = "+euro*bean.getRate()+ " Dollar"); double dollar=1000; System.out.println(dollar+" Dollar = "+euro/bean.getRate()+ " Euro"); bean.remove(); bean=home.findByPrimaryKey("Pound"); System.out.println(euro+" Euro = "+euro*bean.getRate()+ " Pound"); double pound=1000; System.out.println(pound+" Pound = "+euro/bean.getRate()+ " Euro"); } catch (Exception e) { e.printStackTrace(); } Read record

18 J0 18 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento CMP Entity example – execution java -classpath.;%J2EEHOME%\lib\j2ee.jar;…\CurrencyConverterCMPClient.jar -Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient Euro = Dollar Dollar = Euro Euro = Pound Pound = Euro java -classpath.;%J2EEHOME%\lib\j2ee.jar;…\CurrencyConverterCMPClient.jar -Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient Euro = Dollar Dollar = Euro Euro = Pound Pound = Euro

19 J0 19 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity Example BMP Entity Example: Currency Converter

20 J0 20 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The Component (Remote) Interface import java.rmi.*; import javax.ejb.*; public interface CurrencyConverter extends EJBObject { public void setRate(double rate) throws RemoteException; public double getRate() throws RemoteException; } UNCHANGED

21 J0 21 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The Home Interface import java.rmi.*; import javax.ejb.*; public interface CurrencyConverterHome extends EJBHome { public CurrencyConverter create(String currency,double rate) throws RemoteException, CreateException; public CurrencyConverter findByPrimaryKey(String primaryKey) throws RemoteException, FinderException; /*public Collection findInRange(...) throws RemoteException, FinderException;*/ } UNCHANGED

22 J0 22 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean import java.sql.*; import javax.sql.*; import javax.naming.*; import java.rmi.*; import javax.ejb.*; public class CurrencyConverterBean implements EntityBean { private String valuta; private double rate; private EntityContext entityContext; private DataSource ds; private String dbName="java:comp/env/jdbc/Cloudscape"; private Connection con;

23 J0 23 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean public void setRate(double rate) throws RemoteException { this.rate=rate; } public double getRate() throws RemoteException{ return rate; } public void ejbPostCreate(String valuta,double rate) throws CreateException { } UNCHANGED

24 J0 24 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean public String ejbCreate(String valuta,double rate) throws CreateException { if (valuta == null) { throw new CreateException( "E' obbligatorio specificare la valuta." ); } try { String sqlStmt="INSERT INTO ValuteTable VALUES (?,?)"; con=ds.getConnection(); PreparedStatement stmt=con.prepareStatement(sqlStmt); stmt.setString(1,valuta); stmt.setDouble(2,rate); stmt.executeUpdate(); stmt.close(); } catch (SQLException e) {throw new EJBException(e); } finally { try {if (con!=null) con.close();} catch (SQLException e) {} } this.valuta=valuta; this.rate=rate; return valuta; }

25 J0 25 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; try { InitialContext initial= new InitialContext(); ds=(DataSource)initial.lookup(dbName); } catch(NamingException e) {throw new EJBException(e);} } public void unsetEntityContext() { entityContext = null; } public void ejbActivate() { valuta=(String)entityContext.getPrimaryKey(); } public void ejbPassivate() { valuta=null; }

26 J0 26 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean public void ejbLoad() { try { String sqlStmt=" SELECT valuta, rate FROM ValuteTable WHERE valuta = ? "; con=ds.getConnection(); PreparedStatement stmt=con.prepareStatement(sqlStmt); stmt.setString(1,valuta); ResultSet rs= stmt.executeQuery(); if (rs.next()) { this.rate=rs.getDouble("rate"); stmt.close(); } else { stmt.close(); throw new NoSuchEntityException("Valuta: "+valuta); } } catch (SQLException e) {throw new EJBException(e); } finally { try {if (con!=null) con.close();} catch (SQLException e) {} }

27 J0 27 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean public void ejbStore() { try { String sqlStmt="UPDATE ValuteTable SET rate =? WHERE valuta = ? "; con=ds.getConnection(); PreparedStatement stmt=con.prepareStatement(sqlStmt); stmt.setDouble(1,rate); stmt.setString(2,valuta); if (stmt.executeUpdate() != 1) { throw new EJBException("Object state could not be saved"); } stmt.close(); } catch (SQLException e) {throw new EJBException(e); } finally { try {if (con!=null) con.close();} catch (SQLException e) {} }

28 J0 28 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean public void ejbRemove() throws RemoveException { try { String sqlStmt="DELETE FROM ValuteTable WHERE valuta = ? "; con=ds.getConnection(); PreparedStatement stmt=con.prepareStatement(sqlStmt); stmt.setString(1,valuta); if (stmt.executeUpdate() != 1) { throw new EJBException("Object state could not be saved"); } stmt.close(); } catch (SQLException e) {throw new EJBException(e); } finally { try {if (con!=null) con.close();} catch (SQLException e) {} }

29 J0 29 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The bean public String ejbFindByPrimaryKey(String primaryKey) throws FinderException { try { String sqlStmt="SELECT valuta FROM ValuteTable WHERE valuta = ? "; con=ds.getConnection(); PreparedStatement stmt=con.prepareStatement(sqlStmt); stmt.setString(1,primaryKey); ResultSet rs= stmt.executeQuery(); if (!rs.next()) throw new ObjectNotFoundException(); rs.close(); stmt.close(); return primaryKey; } catch (SQLException e) {throw new EJBException(e); } finally { try {if (con!=null) con.close();} catch (SQLException e) {} }

30 J0 30 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The descriptor currencyConverterBMP CurrencyConverter CurrencyConverterHome CurrencyConverter CurrencyConverterBean Bean java.lang.String False

31 J0 31 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The descriptor jdbc javax.sql.DataSource Container CurrencyConverter * NotSupported

32 J0 32 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – activate DB Activate your database (in this case Cloudscape) %J2EEHOME%bin\cloudscape.bat –start UNCHANGED

33 J0 33 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento View the properties jndi name for the bean jndi name for the db (jdbc/Cloudscape)

34 J0 34 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento View the properties

35 J0 35 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The client import javax.ejb.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class ConverterClient { public static void main(String[] args) { CurrencyConverter bean=null; Object objref=null; CurrencyConverterHome home=null; try { Context ctx=new InitialContext(); objref=ctx.lookup("CCcmp"); home =(CurrencyConverterHome)PortableRemoteObject.narrow( objref,CurrencyConverterHome.class); Get naming context and object reference Cast to correct type JNDI name UNCHANGED

36 J0 36 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The client try { bean=home.findByPrimaryKey("Pound"); bean.setRate(bean.getRate()*(1+(Math.random()-.5)/100)); } catch (ObjectNotFoundException e) { bean=home.create("Pound",0.666); } try { bean=home.findByPrimaryKey("Dollar"); bean.setRate(bean.getRate()*(1+(Math.random()-.5)/100)); } catch (ObjectNotFoundException e) { bean=home.create("Dollar",0.978); } Update or create record UNCHANGED

37 J0 37 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – The client bean=home.findByPrimaryKey("Dollar"); double euro=1000; System.out.println(euro+" Euro = "+euro*bean.getRate()+ " Dollar"); double dollar=1000; System.out.println(dollar+" Dollar = "+euro/bean.getRate()+ " Euro"); bean.remove(); bean=home.findByPrimaryKey("Pound"); System.out.println(euro+" Euro = "+euro*bean.getRate()+ " Pound"); double pound=1000; System.out.println(pound+" Pound = "+euro/bean.getRate()+ " Euro"); } catch (Exception e) { e.printStackTrace(); } Read record UNCHANGED

38 J0 38 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento BMP Entity example – execution java -classpath.;%J2EEHOME%\lib\j2ee.jar;..\CurrencyConverterBMPClient.jar -Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient Euro = Dollar Dollar = Euro Euro = Pound Pound = Euro java -classpath.;%J2EEHOME%\lib\j2ee.jar;..\CurrencyConverterBMPClient.jar -Dorg.omg.CORBA.ORBInitialHost=localhost ConverterClient Euro = Dollar Dollar = Euro Euro = Pound Pound = Euro

39 J0 39 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Entity Beans Lifecycle: clients view Does not exist Is not referenced Exists Is not referenced Exists Is referenced Does not exist Is referenced Start home.create() Client releases reference Direct Insert Client releases reference home.find(…) object.remove(), home.remove(), Or direct delete Client invokes method Client invokes method (NoSuchObject Exception) Direct Delete Or home.remove()

40 J0 40 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento Entity Beans Lifecycle Exists, in the pool Does not exist GarbageCollector unsetEntityContext() home.create() newInstance() setEntityContext(sc) ejbFindXXX() Exists, method ready Client invokes method create() ejbCreate(args) ejbPostCreate(args) ejbActivate() remove() ejbRemove() ejbPassivate()


Download ppt "J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento."

Similar presentations


Ads by Google