📄 carttestclient.java~2~
字号:
package ejb;
import java.lang.Object;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import java.util.Date;
import javax.ejb.Handle;
import java.rmi.RemoteException;
import javax.ejb.EJBMetaData;
import javax.ejb.HomeHandle;
import java.util.List;
public class CartTestClient extends Object {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private Cart cart = null;
private CartHome cartHome = null;
//Construct the EJB test client
public CartTestClient1() {
initialize();
}
public void initialize() {
try {
//get naming context
Context context = new InitialContext();
//look up jndi name
Object ref = context.lookup("Cart");
//look up jndi name and cast to Home interface
cartHome = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class);
} catch (Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public Cart create(String cardHolderName, String creditCardNumber,
Date expirationDate) {
try {
cart = cartHome.create(cardHolderName, creditCardNumber,
expirationDate);
} catch (Exception e) {
e.printStackTrace();
}
return cart;
}
public EJBMetaData getEJBMetaData() {
EJBMetaData returnValue = null;
try {
returnValue = cartHome.getEJBMetaData();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public HomeHandle getHomeHandle() {
HomeHandle returnValue = null;
try {
returnValue = cartHome.getHomeHandle();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public void remove(Object object) {
try {
cartHome.remove(object);
} catch (Exception e) {
e.printStackTrace();
}
}
public void remove(Handle handle) {
try {
cartHome.remove(handle);
} catch (Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public void addItem() throws RemoteException {
/**@todo Update these variables with appropriate values for calling addItem().*/
Item item = null;
if (cart == null) {
System.out.println("Error in addItem(): " + ERROR_NULL_REMOTE);
return;
}
cart.addItem(item);
}
public List getContents() throws RemoteException {
List returnValue = null;
if (cart == null) {
System.out.println("Error in getContents(): " + ERROR_NULL_REMOTE);
return returnValue;
}
returnValue = cart.getContents();
return returnValue;
}
public float getTotalPrice() throws RemoteException {
float returnValue = 0f;
if (cart == null) {
System.out.println("Error in getTotalPrice(): " + ERROR_NULL_REMOTE);
return returnValue;
}
returnValue = cart.getTotalPrice();
return returnValue;
}
public void purchase() throws RemoteException {
if (cart == null) {
System.out.println("Error in purchase(): " + ERROR_NULL_REMOTE);
return;
}
cart.purchase();
}
public void removeItem() throws RemoteException {
/**@todo Update these variables with appropriate values for calling removeItem().*/
Item item = null;
if (cart == null) {
System.out.println("Error in removeItem(): " + ERROR_NULL_REMOTE);
return;
}
cart.removeItem(item);
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public CartHome getHome() {
return cartHome;
}
//Main method
public static void main(String[] args) {
CartTestClient1 client = new CartTestClient1();
// Use the getHome() method of the client object to call Home interface
// methods that will return a Remote interface reference. Then
// use that Remote interface reference to access the EJB.
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -