Certification
SCBCDPlease find the Study Notes and resources which covers 2nd Part of Chapter 13 : Enterprise Bean Environment, as part of the Sun Certified Business Component Developer exam CX-310-090.
Identify correct and incorrect statements about the purpose and use of the deployment descriptor elements for environment entries, EJB references, and resource manager connection factory references; including whether a given code listing is appropriate and correct with respect to a particular deployment descriptor element.
Identify correct and incorrect statements about the purpose and use of the deployment descriptor elements for environment entries, EJB references, and resource manager connection factory references; including whether a given code listing is appropriate and correct with respect to a particular deployment descriptor element.
An enterprise bean instance locates the environment naming context using the JNDI interfaces. An instance creates a javax.naming.InitialContext object by using the constructor with NO arguments, and looks up the environment naming via the InitialContext under the name java:comp/env. The enterprise bean's ENVIRONMENT ENTRIES are stored directly in the environment naming context, or in any of its direct or indirect subcontexts.
........................;........................;........................;;;;
...public void setTaxInfo(int numberOfExemptions, ...) ...................;;;;
...throws InvalidNumberOfExemptionsException { ...................;;;;....;;;;
...... ...................;;;;...................;;;;...................;;;;;;
.../ / Obtain the enterprise bean's environment naming context. ....;;;;;;;;;;
...Context initCtx = new InitialContext(); ;;;;;;;;;;;;;;;;;;;;;;;;.;;;;;;;;;;
...Context myEnv = (Context)initCtx.lookup("java:comp/env"); .;;;;;;;;;;;;;;;;
........................;........................;........................;;;;
.../ / Obtain the maximum number of tax exemptions ;;;;;;;;;;;;;;;;;;.....;;;;
.../ / configured by the Deployer. .....;;;;.....;;;;;;;;;;;;;;;;;;;;.....;;;;
...Integer max = (Integer)myEnv.lookup("maxExemptions"); ;;;;;;;;;;;.....;;;;l
........................;........................;........................;;;;
.../ / Obtain the minimum number of tax exemptions .......................;;;;
.../ / configured by the Deployer. .......................;;;;............;;;;
...Integer min = (Integer)myEnv.lookup("minExemptions"); ............;;;;.;;;;
........................;........................;........................;;;;
.../ / Use the environment entries to customize business logic. .;;;;.....;;;;
...if (numberOfExeptions > Integer.intValue(max) || .....;;;;.....;;;;....;;;;
......numberOfExemptions < Integer.intValue(min)) ....;;;;....;;;;;;;;....;;;;
......throw new InvalidNumberOfExemptionsException(); ;;;;....;;;;;;;;....;;;;
........................;........................;........................;;;;
.../ / Get some more environment entries. These environment ;;;;....;;;;..;;;;
.../ / entries are stored in subcontexts. ........................;..;;;;;;;;;
...String val1 = (String)myEnv.lookup("foo/name1"); .........................;
...Boolean val2 = (Boolean)myEnv.lookup("foo/bar/name2"); ....................
..............................................................................
.../ / The enterprise bean can also lookup using full pathnames. .............
...Integer val3 = (Integer) ..................................................
...initCtx.lookup("java:comp/env/name3"); ....................................
...Integer val4 = (Integer) ..................................................
...initCtx.lookup("java:comp/env/foo/name4"); ................................
...... .......................................................................
} ............................................................................
..............................................................................
..............................................................................
< session > ..................................................................
....... ..................................................................;;;;
....< ejb-name >EmployeeService< /ejb-name > .;;;;;;;;;;.;;;;;;;;;;;;;;;;;;;;;
....< ejb-class >com.wombat.empl.EmployeeServiceBean< /ejb-class > .;;;;;;;;;;
....... ............................................................;;;;;;;;;;
....< env-entry > ............................................................
........< description > ......................................................
............The maximum number of tax exemptions .............................
............allowed to be set. ...............................................
........< /description > .....................................................
........< env-entry-name >maxExemptions< /env-entry-name > ...............;;;;
........< env-entry-type >java.lang.Integer< /env-entry-type > ...............
........< env-entry-value >15< /env-entry-value > ............................
....< /env-entry > ...........................................................
..............................................................................
....< env-entry > ...........................................................;
........< description > .....................................................;
............The minimum number of tax exemptions ............................;
............allowed to be set. ............................;.................;
........< /description > .................;................;.................;
........< env-entry-name >minExemptions< /env-entry-name > ..............;;;;;
........< env-entry-type >java.lang.Integer< /env-entry-type > ..............;
........< env-entry-value >1< /env-entry-value > ............................;
....< /env-entry > ..........................................................;
..............................................................................
....< env-entry > ............................................................
........< env-entry-name >foo/name1< /env-entry-name > ;;;;;;;;;;;;;;;;;;;;;;;
........< env-entry-type >java.lang.String< /env-entry-type > ;;;;;;;;;;;;;;;;
........< env-entry-value >value1< /env-entry-value > ;;;;;;;;;;;;;;;;;;;;;;;;
....< /env-entry > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
....;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
....< env-entry > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
........< env-entry-name >foo/bar/name2< /env-entry-name > ;;;;;;;;;;;;;;;;;;;
........< env-entry-type >java.lang.Boolean< /env-entry-type > ;;;;;;;;;;;;;;;
........< env-entry-value >true< /env-entry-value > ........;;;;;;;;;;;;;;;;;;
....< /env-entry > ...........;;;;...........;;;;...........;;;;;;;;;;;;;;;;;;
..............................................................................
....< env-entry > ...........;;;;...........;;;;...........;;;;lllllllllllllll
........< description >Some description.< /description > ...........;;;;llllll
........< env-entry-name >name3< /env-entry-name > ...........;;;;llllllllllll
........< env-entry-type >java.lang.Integer< /env-entry-type > ...........;;;;
....< /env-entry > .......................................................;;;;
..............................................................................
....< env-entry > .........;;;;;;;;;;;;;......................................
........< env-entry-name >foo/name4< /env-entry-name > ..........;;;;;;;;;;;;;
........< env-entry-type >java.lang.Integer< /env-entry-type > ..........;;;;;
........< env-entry-value >10< /env-entry-value > .......................;;;;;
....< /env-entry > ......................................................;;;;;
....... .................................................................;;;;;
< /session > .................................................................
..............................................................................
The following example illustrates how an enterprise bean uses an EJB REFERENCE to locate the remote home interface of another enterprise bean.
..............................................................................
public class EmployeeServiceBean implements SessionBean { ....................
....public void changePhoneNumber(...) { .....................................
........... ..................................................................
..............................................................................
........// Obtain the default initial JNDI context. ..........................
........Context initCtx = new InitialContext(); ..............................
..............................................................................
........// Look up the home interface of the EmployeeRecord .................;
........// enterprise bean in the environment. ...................;;;;;;;;;;;;
........Object result = initCtx.lookup("java:comp/env/ejb/EmplRecord"); ;;;;;;
..............................................................................
........// Convert the result to the proper type. ;;;;;;......................
........EmployeeRecordHome emplRecordHome = (EmployeeRecordHome) .............
............javax.rmi.PortableRemoteObject.narrow(result, ....................
............EmployeeRecordHome.class); .......................................
..............................................................................
........... ..................................................................
....} ........................................................................
}.............................................................................
..............................................................................
................................................................................................................................................
....... ......................................................................
....< ejb-name >EmployeeService< /ejb-name > .................................
....< ejb-class >com.wombat.empl.EmployeeServiceBean< /ejb-class > ...........
....... ......................................................................
....< ejb-ref > ..............................................................
........< description > ......................................................
............This is a reference to the entity bean that ......................
............encapsulates access to employee records. ..................lllllll
........< /description > ....................................lllllllllllllllll
........< ejb-ref-name >ejb/EmplRecord< /ejb-ref-name > ..................;;;;
........< ejb-ref-type >Entity< /ejb-ref-type > ..................;;;;;;;;;;;;
........< home >com.wombat.empl.EmployeeRecordHome< /home > ..................
........< remote >com.wombat.empl.EmployeeRecord< /remote > ..................
....< /ejb-ref > ..........................................lllllllllllllllllll
..............................................................................
....< ejb-ref > ..............................................................
........< ejb-ref-name >ejb/Payroll< /ejb-ref-name > ....................;;;;;
........< ejb-ref-type >Entity< /ejb-ref-type > ....................;;;;;;;;;;
........< home >com.aardvark.payroll.PayrollHome< /home > ....................
........< remote >com.aardvark.payroll.Payroll< /remote > ....................
....< /ejb-ref > .............................................................
..............................................................................
....< ejb-ref > .........;;;;;;;;;;;;.........................................
........< ejb-ref-name >ejb/PensionPlan< /ejb-ref-name > .........;;;;;;;;;;;;
........< ejb-ref-type >Session< /ejb-ref-type > .................;;;;;;;;;;;;
........< home >com.wombat.empl.PensionPlanHome< /home > .................;;;;
........< remote >com.wombat.empl.PensionPlan< /remote > .................;;;;
....< /ejb-ref > .........................................................;;;;
....... ..................................................................;;;;
< /session >..................................................................
..............................................................................
The following illustrates an ejb-link in the deployment descriptor. The Application Assembler uses the ejb-link element to indicate that the EJB reference EmplRecord declared in the EmployeeService enterprise bean has been linked to the EmployeeRecord enterprise bean.
..............................................................................
................................................................;;
....... ...................................................................;;;
....< ejb-name >EmployeeService< /ejb-name > ...................;;;;;;;;;;;;;;
....< ejb-class >com.wombat.empl.EmployeeServiceBean< /ejb-class > ;;;;;;;;;;;
....... ...................................................................;;;
....< ejb-ref > ...........................................................;;;
........< ejb-ref-name >ejb/EmplRecord< /ejb-ref-name > ...................;;;
........< ejb-ref-type >Entity< /ejb-ref-type > ...................;;;;;;;;;;;
........< home >com.wombat.empl.EmployeeRecordHome< /home > .................;
........< remote >com.wombat.empl.EmployeeRecord< /remote > ;;;;;;;;;;;lllllll
........< ejb-link >EmployeeRecord< /ejb-link > ...................lllllllllll
....< /ejb-ref > ...........................................................ll
....... ...................................................................ll;
< /session >..................................................................
..............................................................................
... ...................................................................;;;;;;;
..............................................................................
< entity > ...................................................................
....< ejb-name >EmployeeRecord< /ejb-name > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
....< home >com.wombat.empl.EmployeeRecordHome< /home > ;;;;;;;;;;;;;;;;;;;;;;
....< remote >com.wombat.empl.EmployeeRecord< /remote > ;;;;;;;;;;;;;;;;;;;;;;
....... ...................................................................;;;
< /entity >...................................................................
..............................................................................
The following example illustrates using the ejb-link element to indicate an enterprise bean reference to the ProductEJB enterprise bean that is in the same J2EE application unit but in a different ejb-jar file:
..............................................................................
< entity > ..................................................................;
....... ..................................................................;;;;
....< ejb-name >OrderEJB< /ejb-name > ....................................;;;;
....< ejb-class >com.wombat.orders.OrderBean< /ejb-class > ...............;;;;
....... ..................................................................;;;;
....< ejb-ref > .............................................................;
........< ejb-ref-name >ejb/Product< /ejb-ref-name > .........................
........< ejb-ref-type >Entity< /ejb-ref-type > ..............................
........< home >com.acme.orders.ProductHome< /home > .........................
........< remote >com.acme.orders.Product< /remote > .........................
........< ejb-link >../products/product.jar#ProductEJB< /ejb-link > ..........
....< /ejb-ref > ............................................................;
....... ................................................................;;;;;;
< entity > ..................................................................;
..............................................................................
The following code sample illustrates obtaining a JDBC connection:
.............................................................................;
public class EmployeeServiceBean implements SessionBean { ...................;
.....EJBContext ejbContext; ...................;...................;;;;;;;;;;;
..............................................................................
.....public void changePhoneNumber(...) { ...................;...............;
............. ...................;...................;.......;.............;;;
..............................................................................
...............// obtain the initial JNDI context .......;...............;;;;;
...............Context initCtx = new InitialContext(); .......;...........;;;;
..............................................................................
...............// perform JNDI lookup to obtain resource manager .............
...............// connection factory .......;...............;.............;;;;
...............javax.sql.DataSource ds = (javax.sql.DataSource) .............;
...............initCtx.lookup("java:comp/env/jdbc/EmployeeAppDB"); ...........
..............................................................................
...............// Invoke factory to obtain a connection. The security ;;;;;;;;
...............// principal is not given, and therefore ............;;;;;;;;;;
...............// it will be configured by the Deployer. ............;;;;;;;;;
...............java.sql.Connection con = ds.getConnection(); ............;;;;;
............. ...................;...................;...................;;;;;
.....} ...................;...................;...................;;;;;;;;;;;;
}...................;...................;...................;................;
...................;...................;...................;;;;;;;;;;;;;;;;;;;
The following example is the declaration of resource manager connection factory references used by the EmployeeService enterprise bean:
...................;...................;...................;;;;;;;;;;;;;;;;;;;
< enterprise-beans > ..................;...................;;;;;;;;;;;;;;;;;;;
....< session > ..................;...................;;;;;;;;;;;;;;;;;;;;;;;;
........... ..................;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;
........< ejb-name >EmployeeService< /ejb-name > .;;;;;;;;;;;;;;;;;;;;;;;;;;;;
........< ejb-class >com.wombat.empl.EmployeeServiceBean< /ejb-class > ;;;;;;;
........... ..................;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;
........< resource-ref > ..................;...................;;;;;;;;;;;;;;;
............< description > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;
....................A data source for the database in which ;;;;;;;;;;;;;;;;;;
....................the EmployeeService enterprise bean will ;;;;;;;;;;;;;;;;;
....................record a log of all transactions. ;;;;;;;;;;;;;;;;;;;;;;;;
............< /description > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;
............< res-ref-name >jdbc/EmployeeAppDB< /res-ref-name > ;;;;;;;;;;;;;;
............< res-type >javax.sql.DataSource< /res-type > ;;;;;;;;;;;;;;;;;;;;
............< res-auth >Container< /res-auth > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
............< res-sharing-scope >Shareable< /res-sharing-scope > ;;;;;;;;;;;;;
........< /resource-ref > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;
........... ..................;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;
....< /session > ..................;...................;;;;;;;;;;;;;;;;;;;;;;;
< /enterprise-beans > ..................;...................;;;;;;;;;;;;;;;;;;
...................;...................;...................;;;;;;;;;;;;;;;;;;;
The following example illustrates the declaration of the JMS resource manager connection factory references:
...................;...................;...................;;;;;;;;;;;;;;;;;;;
< enterprise-beans > ..................;...................;;;;;;;;;;;;;;;;;;;
....< session > ..................;...................;;;;;;;;;;;;;;;;;;;'';;;
........... ..................;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;
........< resource-ref > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
............< description > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;
................A queue connection factory used by the ;;;;;;;;;;;;;;;;;;;;;;;
................MySession enterprise bean to send ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
................notifications. ....;...................;;;;;;;;;;;;;;;;;;;;;;;
............< /description > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;
............< res-ref-name >jms/QueueConnFactory< /res-ref-name > ;;;;;;;;;;;;
............< res-type >javax.jms.QueueConnectionFactory< /res-type > ;;;;;;;;
............< res-auth >Container< /res-auth > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
............< res-sharing-scope >Unshareable< /res-sharing-scope > ;;;;;;;;;;;
........< /resource-ref > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;
........... ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
....< /session > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
< /enterprise-beans > ....;...................;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
...................;...................;...................;;;;;;;;;;;;;;;;;;;
The following example illustrates how an enterprise bean uses a RESOURCE ENVIRONMENT REFERENCE to locate a JMS Destination:
...................;...................;...................;;;;;;;;;;;;;;;;;;;
public class StockServiceBean implements SessionBean { ....;;;;;;;;;;;;;;;;;;;
;;;;public void processStockInfo(...) { ....;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;... ....;;;;;;;;;;;;;;;;;;;....;;;;;;;;;;;;;;;;;;;....;;;;;;;;;;;;;;;;
...................;...................;...................;;;;;;;;;;;;;;;;;;;
;;;;;;;;// Obtain the default initial JNDI context ;;;;;;;;;;;;;;;.;;;;;;;;;;;
;;;;;;;;Context initCtx = new InitialContext(); ;;;;;;;;;;;;;;;;;;;;.;;;;;;;;;
...................;...................;...................;;;;;;;;;;;;;;;;;;;
;;;;;;;;// Look up the JMS StockQueue in the environment. .;;;;;;;;;;;;;;;;;;;
;;;;;;;;Object result = initCtx.lookup("java:comp/env/jms/StockQueue"); ;;;;;;
........;;;;;;;;...........;...................;...................;;;;;;;;;;;
;;;;;;;;// Convert the result to the proper type. ;;;;;;;;;;;;.....;;;;;;;;;;;
;;;;;;;;javax.jms.Queue queue = (javax.jms.Queue)result; .....;;;;;;;;;;;;;;;;
;;;;;} ;;;;;;;;;;..;...................;...................;;;;;;;;;;;;;;;;;;;
} .................;...................;...................;;;;;;;;;;;;;;;;;;;
...................;...................;...................;;;;;;;;;;;;;;;;;;;
In the example, the Bean Provider of the StockServiceBean enterprise bean has ASSIGNED the environment entry jms/StockQueue as the RESOURCE ENVIRONMENT REFERENCE name to refer to a JMS queue.
The following example illustrates the declaration of RESOURCE ENVIRONMENT REFERENCES in the deployment descriptor:
.
.
.
.
.
...................;...................;...................;;;;;;;;;;;;;;;;;;;
... ...............;...................;...................;;;;;;;;;;;;;;;;;;;
< resource-env-ref > ..................;...................;;;;;;;;;;;;;;;;;;;
.....< description > ..................;...................;;;;;;;;;;;;;;;;;;;
..........This is a reference to a JMS queue used in the ..;;;;;;;;;;;;;;;;;;;
..........processing of Stock info ..;;;;;;;;;;;;;;;;;;;..;;;;;;;;;;;;;;;;;;;;
.....< /description > ..;;;;;;;;;;;;;;;;;;;..;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.....< resource-env-ref-name >jms/StockQueue< /resource-env-ref-name > ;;;;;;;
.....< resource-env-ref-type >javax.jms.Queue< /resource-env-ref-type > ;;;;;;
< /resource-env-ref > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
... ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
...................;...................;...................;;;;;;;;;;;;;;;;;;;
________________
Author: Mikalai Zaikin. Please Click Here to visit Authors site for any updates and changes to the study notes.