⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 airlineshoppingcartbean.java

📁 BEA WebLogic Server 8.1大全 = BEA webLogic server 8.1 unleashed (美) Mark Artiges等著 袁毅 ... [等] 译 eng
💻 JAVA
字号:
/* 
 * WebLogic Server Unleashed
 * 
 */


package com.wlsunleashed.ejb.session.stateful;

import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.wlsunleashed.ejb.session.stateless.AirlineReservationLocalHome;
import com.wlsunleashed.ejb.session.stateless.AirlineReservationLocalObject;


/**
 * This class Represents the Bean class  for the AirlineShoppingCart EJB.
 * 
 * @version 1.0
 */
public class AirlineShoppingCartBean implements SessionBean {
    /** Represents the home interface of the reservation bean */
    private AirlineReservationLocalHome reservationHome = null;

    /** Stores the SessionContext object */
    private SessionContext context = null;

    /** The destination city */
    private String destination = null;

    /** The origin city */
    private String origin = null;

    /** The total cost */
    private double totalCost = 0;

    /** The flight number */
    private int flightNumber = 0;

    /** The total number of seats */
    private int numSeats = 0;

    /**
     * Constructor for AirlineReservationBean.
     */
    public AirlineShoppingCartBean() {
        super();
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#setDestination(String)
     */
    public void setDestination(String destination) {
        this.destination = destination;
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#getFlightInfo()
     */
    public Properties getFlightInfo()
                             throws AirlineShoppingCartException {
        // make sure flight num has been set
        if (flightNumber == 0) {
            throw new AirlineShoppingCartException(
                    "Flight number needs to be set");
        }

        try {
            AirlineReservationLocalObject reservationObject = 
                    reservationHome.create();

            Properties output = reservationObject.getFlightInfo(flightNumber);

            return output;
        } catch (Exception ex) {
            throw new AirlineShoppingCartException(ex.getMessage());
        }
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#setFlightNumber(int)
     */
    public void setFlightNumber(int flightNum) {
        this.flightNumber = flightNum;
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#setNumSeats(int)
     */
    public void setNumSeats(int numSeats) {
        this.numSeats = numSeats;
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#setOrigin(String)
     */
    public void setOrigin(String origin) {
        this.origin = origin;
    }

    /**
     * @see javax.ejb.SessionBean#setSessionContext (SessionContext)
     */
    public void setSessionContext(SessionContext aContext)
                           throws RemoteException {
        System.out.println(" setSessionContext called ");
        context = aContext;
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#getTotalCost()
     */
    public double getTotalCost() {
        return totalCost;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        System.out.println("ejbActivate called");

        try {
            initialize();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @see javax.ejb.SessionBean#ejbCreate
     */
    public void ejbCreate()
                   throws CreateException {
        System.out.println("ejbCreate called");
        initialize();
    }

    /**
     * @see javax.ejb.SessionBean#ejbCreate
     */
    public void ejbCreate(String origin, String destination)
                   throws CreateException {
        System.out.println("ejbCreate called");
        initialize();
        setOrigin(origin);
        setDestination(destination);

        // Create the prepared statements
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        System.out.println("ejbPassivate called");
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        System.out.println("ejbRemove called");
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#reserveSeats()
     */
    public void reserveSeats()
                      throws AirlineShoppingCartException {
        // make sure flight num and numseats have been set
        if ((flightNumber == 0) || (numSeats == 0)) {
            throw new AirlineShoppingCartException(
                    "Flight number and numSeats need to be set");
        }

        try {
            AirlineReservationLocalObject reservationObject = 
                    reservationHome.create();

            reservationObject.reserveSeats(flightNumber, numSeats);

            // if reservation was successful, 
            // we will query the cost and add it up with 
            // the local instance of total cost 
            // (for the customer)
            Properties props = getFlightInfo();

            double unitPrice = Double.parseDouble(
                                       (String) props.get("TOTAL_COST_PER_SEAT"));

            totalCost += (unitPrice * numSeats);
        } catch (Exception ex) {
            context.setRollbackOnly();
            throw new AirlineShoppingCartException(ex.getMessage());
        }
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateful.
     *      AirlineShoppingCartRemoteObject#searchFlights()
     */
    public int[] searchFlights()
                        throws AirlineShoppingCartException {
        // make sure origin and destination have been set
        if ((this.destination == null) || (this.origin == null)) {
            throw new AirlineShoppingCartException(
                    "Origin and destination need to be set");
        }

        try {
            AirlineReservationLocalObject reservationObject = 
                    reservationHome.create();

            return reservationObject.getFlightNumbers(origin, destination);
        } catch (Exception ex) {
            throw new AirlineShoppingCartException(ex.getMessage());
        }
    }

    /**
     * Initialization code
     * 
     * @throws CreateException Thrown when any error occurs while performing
     *         initialization routine
     */
    private void initialize()
                     throws CreateException {
        System.out.println("initialize routine");

        try {
            Context ctx = new InitialContext();
            reservationHome = (AirlineReservationLocalHome) ctx.lookup(
                                      "java:comp/env/ejb/ReservationStatelessEJB");
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new CreateException(ex.getMessage());
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -