A.8 PayrollBean Implementation Class Using ConnectorsHere is the source code for the PayrollBean class, which illustrates using the J2EE Connector architecture to access the mainframe-based payroll system, as described in Chapter 6. Code Example A.8 PayrollBean Classpackage com.aardvark.payroll.impl; import javax.ejb.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.resource.cci.ConnectionFactory; import javax.resource.cci.Connection; import javax.resource.cci.MappedRecord; import javax.resource.cci.RecordFactory; import javax.resource.cci.InteractionSpec; import javax.resource.cci.Interaction; import javax.resource.ResourceException; import com.aardvark.payroll.PayrollException; public class PayrollBean implements SessionBean { // Mainframe connection factory private ConnectionFactory connectionFactory; public void setBenefitsDeduction(int emplNumber, double deduction) throws PayrollException { Connection cx = null; try { // Obtain connection to mainframe. cx = getConnection(); // Create an interaction object. Interaction ix = cx.createInteraction(); InteractionSpecImpl ixSpec = new InteractionSpecImpl(); // Set the name of the TP program to be invoked. ixSpec.setFunctionName("SETPAYROLL_DEDUCTION"); // Specify that we will be sending input parameters // to the TP program but are expecting to receive // no output parameters. ixSpec.setInteractionVerb(InteractionSpec.SYNC_SEND); RecordFactory rf = ix.getRecordFactory(); // Create an object that knows how to // format the input parameters. MappedRecord input = rf.createMappedRecord("PAYROLLINFO_DEDUCTION"); input.put("EMPLOYEENUMBER", new Integer(emplNumber)); input.put("DEDUCTION", new Double(deduction)); // Execute invokes the TP program, passing it // the input parameters. ix.execute(ixSpec, input); } catch (ResourceException ex) { throw new EJBException(ex); } finally { try { if (cx != null) cx.close(); } catch (ResourceException ex) { } } } public double getBenefitsDeduction(int emplNumber) throws PayrollException { Connection cx = null; try { cx = getConnection(); Interaction ix = cx.createInteraction(); InteractionSpecImpl ixSpec = new InteractionSpecImpl(); ixSpec.setFunctionName("GETPAYROLLDATA"); ixSpec.setInteractionVerb( InteractionSpec.SYNC_SEND_RECEIVE); RecordFactory rf = ix.getRecordFactory(); MappedRecord input = rf.createMappedRecord("EMPLOYEEINFO"); input.put("EMPLOYEENUMBER", new Integer(emplNumber)); EmployeeRecord employee = new EmployeeRecordImpl(); if (ix.execute(ixSpec, input, employee)) return employee.getBenefitsDeduction(); else throw new PayrollException( PayrollException.INVAL_EMPL_NUMBER); } catch (ResourceException ex) { throw new EJBException(ex); } finally { try { if (cx != null) cx.close(); } catch (ResourceException ex) { } } } public void setSalary(int emplNumber, double salary) throws PayrollException { Connection cx = null; try { cx = getConnection(); Interaction ix = cx.createInteraction(); InteractionSpecImpl ixSpec = new InteractionSpecImpl(); ixSpec.setFunctionName("SETPAYROLL_SALARY"); ixSpec.setInteractionVerb(InteractionSpec.SYNC_SEND); RecordFactory rf = ix.getRecordFactory(); MappedRecord input = rf.createMappedRecord("PAYROLLINFO_SALARY"); input.put("EMPLOYEENUMBER", new Integer(emplNumber)); input.put("SALARY", new Double(salary)); ix.execute(ixSpec, input); } catch (ResourceException ex) { throw new EJBException(ex); } finally { try { if (cx != null) cx.close(); } catch (ResourceException ex) { } } } public double getSalary(int emplNumber) throws PayrollException { Connection cx = null; try { cx = getConnection(); Interaction ix = cx.createInteraction(); InteractionSpecImpl ixSpec = new InteractionSpecImpl(); ixSpec.setFunctionName("GETPAYROLLDATA"); ixSpec.setInteractionVerb( InteractionSpec.SYNC_SEND_RECEIVE); RecordFactory rf = ix.getRecordFactory(); MappedRecord input = rf.createMappedRecord("EMPLOYEEINFO"); input.put("EMPLOYEENUMBER", new Integer(emplNumber)); EmployeeRecord employee = new EmployeeRecordImpl(); if (ix.execute(ixSpec, input, employee)) return employee.getSalary(); else throw new PayrollException( PayrollException.INVAL_EMPL_NUMBER); } catch (ResourceException ex) { throw new EJBException(ex); } finally { try { if (cx != null) cx.close(); } catch (ResourceException ex) { } } } public void ejbCreate() {} public void ejbRemove() {} public void ejbPassivate() {} public void ejbActivate() {} public void setSessionContext(SessionContext sc) {} private Connection getConnection() { try { Connection cx = connectionFactory.getConnection(); return cx; } catch (ResourceException ex) { throw new EJBException(ex); } } private void readEnvironment() { try { Context nc = new InitialContext(); connectionFactory = (ConnectionFactory)nc.lookup( "java:comp/env/eis/ConnectionFactory"); } catch (NamingException ex) { throw new EJBException(ex); } } } |