📄 shoppingfacadeejb.java
字号:
package com.j2eeapp.cdstore.cart;
import com.j2eeapp.cdstore.customer.*;
import com.j2eeapp.cdstore.servicelocator.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import com.j2eeapp.cdstore.util.*;
public class ShoppingFacadeEJB implements SessionBean
{
/**
* Attributes declaration
*/
public SessionContext EJB_Context = null;
private String userId;
private ShoppingCart cart;
private CustomerLocal customer;
/**
* @J2EE_METHOD -- ShoppingFacadeEJB
*/
public ShoppingFacadeEJB ()
{
}
/**
* @J2EE_METHOD -- ejbCreate
* Called by the container to create a session bean instance. Its parameters typically
* contain the information the client uses to customize the bean instance for its use.
* It requires a matching pair in the bean class and its home interface.
*/
public void ejbCreate ()
{
}
/**
* @J2EE_METHOD -- ejbRemove
* A container invokes this method before it ends the life of the session object. This
* happens as a result of a client's invoking a remove operation, or when a container
* decides to terminate the session object after a timeout. This method is called with
* no transaction context.
*/
public void ejbRemove ()
{
}
/**
* @J2EE_METHOD -- ejbActivate
* The activate method is called when the instance is activated from its 'passive' state.
* The instance should acquire any resource that it has released earlier in the ejbPassivate()
* method. This method is called with no transaction context.
*/
public void ejbActivate ()
{
}
/**
* @J2EE_METHOD -- ejbPassivate
* The passivate method is called before the instance enters the 'passive' state. The
* instance should release any resources that it can re-acquire later in the ejbActivate()
* method. After the passivate method completes, the instance must be in a state that
* allows the container to use the Java Serialization protocol to externalize and store
* away the instance's state. This method is called with no transaction context.
*/
public void ejbPassivate ()
{
}
/**
* @J2EE_METHOD -- setSessionContext
* Set the associated session context. The container calls this method after the instance
* creation. The enterprise Bean instance should store the reference to the context
* object in an instance variable. This method is called with no transaction context.
*/
public void setSessionContext (SessionContext sc)
{
this.EJB_Context=sc;
}
/**
* @J2EE_METHOD -- setUserId
*/
public void setUserId (String userId)
{
this.userId = userId;
}
/**
* @J2EE_METHOD -- getUserId
*/
public String getUserId ()
{
return userId;
}
/**
* @J2EE_METHOD -- getShoppingCart
*/
public ShoppingCart getShoppingCart ()
{
if (cart == null) {
try {
EJBServiceLocator sl = new EJBServiceLocator();
ShoppingCartHome home =(ShoppingCartHome)sl.getRemoteHome(JNDINames.SHOPPING_CART,ShoppingCartHome.class);
cart = home.create();
} catch (javax.ejb.CreateException cx) {
System.out.println("ShoppingClientFacade: failed to create cart: caught " + cx);
} catch (ServiceLocatorException slx) {
System.out.println("ShoppingClientFacade: failed to look up name of cart: caught " + slx);
}
catch(RemoteException e)
{
System.out.println(e.getMessage());
}
}
return cart;
}
/**
* @J2EE_METHOD -- getCustomer
*/
public CustomerLocal getCustomer ()
{
if (userId == null) {
System.out.println("ShoppingClientFacade: failed to look up name of customer: userId is not set" );
}
try {
EJBServiceLocator sl = new EJBServiceLocator();
CustomerLocalHome home =(CustomerLocalHome)sl.getLocalHome(JNDINames.CUSTOMER);
customer = home.findByPrimaryKey(userId);
} catch (ServiceLocatorException slx) {
System.out.println("ShoppingClientFacade: failed to look up name of customer: caught " + slx);
}
catch(FinderException e)
{
System.out.println("can't find customer"+userId+e.getMessage());
}
return customer;
}
/**
* @J2EE_METHOD -- createCustomer
*/
public CustomerLocal createCustomer ()
{
try {
EJBServiceLocator sl = new EJBServiceLocator();
CustomerLocalHome home =(CustomerLocalHome)sl.getLocalHome(JNDINames.CUSTOMER);
customer = home.create(userId);
this.userId = userId;
} catch (javax.ejb.CreateException ce) {
System.out.println("ShoppingClientFacade: failed to create customer: caught " + ce);
} catch (ServiceLocatorException slx) {
System.out.println("ShoppingClientFacade: failed to look up name of customer: caught " + slx);
}
return customer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -