【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了CreditManagerClient.java提供大家参考,希望对大家有所帮助!
package ejava.ejbch35.creditmgr;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.naming.NamingEnumeration;
import ejava.ejbch35.creditmgr.CreditManagerSession;
import ejava.ejbch35.creditmgr.CreditManagerSessionHome;
public class CreditManagerClient
{
/** Main method determines if is standalone or J2EE client */
public static void main(String[] args)
{
try{
// Create new client instance
CreditManagerClient myClient = new CreditManagerClient();
// If pass in "-standalone" flag, then run standalone client
if( (args.length > 0) && (args[0].equalsIgnoreCase("-standalone")) ){
myClient.runStandaloneClient();
}
else{ // Else run J2EE application client
myClient.runJ2EEClient();
}
}
catch(Exception exception){
exception.printStackTrace();
}
}
/* Method to run client as a standalone client */
public void runStandaloneClient()
{
try{
// Get initial context for standalone client using
// system environment variables for context parameters
Context context = new InitialContext();
// Look up server reference using raw JNDI name
Object object =
context.lookup("CreditManagerHome");
// Now run common client code
runCommonClientBody(object);
}
catch(Exception exception){
exception.printStackTrace();
}
}
/* Method to run client as a J2EE application client */
public void runJ2EEClient()
{
try{
// Get initial context using container provided context
Context context = new InitialContext();
// Look up server reference using EJB reference
// from the XML deployment descriptor
Object object =
context.lookup("java:comp/env/ejb/CreditManagerHome");
;
// Now run common client code
runCommonClientBody(object);
}
catch(Exception exception){
exception.printStackTrace();
}
}
/** Method to run code that is common for both client types */
private void runCommonClientBody(Object creditManagerHomeRef)
throws Exception
{
System.out.println("Class Type :"+
creditManagerHomeRef.getClass().getName());
// Narrow to credit manager reference
CreditManagerSessionHome creditManagerHome =
(CreditManagerSessionHome)
PortableRemoteObject.narrow(creditManagerHomeRef,
ejava.ejbch35.creditmgr.CreditManagerSessionHome.class);
// Create handle to server object now using home factory
CreditManagerSession creditManagerSession =
creditManagerHome.create();
// Solicit user information from input and send to CreditManager
processCreditRecords(creditManagerSession);
}
/** Solicits user information from input and sends to EJB */
private void processCreditRecords(CreditManagerSession mgr)
throws Exception
{
// Declare credit record variables
int customerID = 0;
String cardType = "";
String cardName = "";
java.util.Date expirationDate = new java.util.Date();
String defaultConfirmationNumber = "";
String cardNumber = "";
// Loop forever to get input and send to manager until
// escape from program with Ctrl-C
while(true){
// Solicit credit record variable information from user input
System.out.println(" Enter a new credit record.");
System.out.println("Enter Customer ID (####):" );
customerID = Integer.parseInt(readStringFromInput());
System.out.println("Enter Card Type (CREDIT/DEBIT):");
cardType = readStringFromInput();
System.out.println("Enter Card Name (VISA/MC/AMEX):");
cardName = readStringFromInput();
System.out.println("Enter Card Number (##############) :");
cardNumber = readStringFromInput();
System.out.println("Enter Expire Date (MM/DD/YYYY):");
String expirationDateString = readStringFromInput();
// Create and format date using date formatter
java.text.DateFormat dateFormat =
java.text.DateFormat.getInstance();
expirationDate =
dateFormat.parse(expirationDateString+ " 0:0 PM EST" );
// Now print info on client side
System.out.println("\nCLIENT SIDE: Credit Information Record");
System.out.println(" Customer ID: " + customerID);
System.out.println( " Card Type: " + cardType
+ " Card Name: " + cardName);
System.out.println(" Card Number :" +cardNumber);
System.out.println( " Expire Date: " + expirationDate.toString()
+" Confirm Number: " + defaultConfirmationNumber);
// Now send info to manager
mgr.createCreditRecord(customerID, cardType, cardName, cardNumber,
expirationDate, defaultConfirmationNumber);
}
}
/* Special helper to read a String from standard user input */
public static String readStringFromInput()
{
String readValue = null;
try{
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
readValue = reader.readLine();
}
catch(IOException ioException){
ioException.printStackTrace();
}
return readValue;
}
}
上一篇:
CreditHome.java
下一篇:
法律专业开题报告范文