HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"An Online Airline Ticket Store (Part 4) - Leasing and Flying"
Volume: 4 Issue: 11, p. 18

	

Listing 1: StoreServlet.java
 
import java.io.*; 
import java.util.*; 
import java.text.*; 
import java.rmi.*; 
import javax.servlet.http.*; 
import javax.servlet.*; 

public class StoreServlet extends HttpServlet 
{ 

  protected HttpServletRequest request_ = null; 
  protected HttpServletResponse response_ = null; 
  protected StringBuffer result_ = null; 

  XMLProcessor xp_ = null; 
  PrintWriter toClient_ = null; 
  StoreClientManager clientManager_ = null; 
  Confirmation confirmation_ = null; 

  // handle POST for servlet 
  public void doPost (HttpServletRequest request, 
          HttpServletResponse response) 
    throws ServletException, IOException { 

    request_ = request; 
    response_ = response; 

    toClient_ = new PrintWriter(response_.getOutputStream()); 

    // Begin HTML 
    toClient_.println("<HTML>"); 

    if (clientManager_ == null) { 
        clientManager_ = new StoreClientManager(); } 

        result_ = new StringBuffer(); 

        // Extract the arguments into local 
        // variables 
        processParameters(); 
        toClient_.println(result_.toString()); 
        toClient_.println("<HTML>"); 
        toClient_.flush(); } 
  

  private void processParameters () { 
    //XMLProcessor for converting XML 
    //to Java 
    xp_ = new XMLProcessor(); 

    try { 
      InputStream in = request_.get 
      InputStream(); 
      xp_.initParser(request_.getInput 
      Stream(), toClient_); } 

    catch (IOException ioe) { ioe.printStackTrace(); } 

    LeaseOrder leaseOrder = null; 
    PurchaseOrder purchaseOrder = null; 
    String queryType = xp_.getQueryType(); 

    if (queryType.equals("Lease")) { 
       leaseOrder = xp_.processLease(); 
        confirmation_ = doLease(leaseOrder); } 

    else if (queryType.equals("Purchase")) { 
        purchaseOrder = xp_.processPurchase(); 
        confirmation_ = doPurchase(purchaseOrder); } 

    else { 
        result_.append("<HI>ERROR:: Unknown query type"); 
            return; } 

    String xmlConfirmation = 
     xp_.processConfirmation(confirmation_); 
    result_.append(xmlConfirmation); 
  } 

  private Confirmation doLease(LeaseOrder leaseOrder) { 
        try { return clientManager_.doLease(leaseOrder); } 
        catch (Exception e) { return null; } 
  } 

  private Confirmation doPurchase(PurchaseOrder purchaseOrder) { 
        try { return clientManager_.doPurchase(purchaseOrder); } 
        catch (Exception e) { return null; } 
  } 
  
  

Listing 2: .java: XML processor for the Online Store
 
<!-- Confirm.txt --> 

<?xml version="1.0"?> 
<Confirmation Type="Ticket"> 
  <ConfirmationNo>"SA2345678"</ConfirmationNo> 
</Confirmation> 
  

<!-- Lease.txt> 

<?xml version="1.0"?> 
<Lease> 
  <Passenger> 
    <Name> 
      <LastName>"Kent"</LastName> 
      <FirstName>"Clark"</FirstName> 
    </Name> 
  </Passenger> 
  <Flight> 
   <ReferenceNo>"SA123456"</ReferenceNo> 
   </Flight> 
    <ItemList> 
      <Item> 
       <ItemName>"CD Player"</ItemName> 
       <ItemId>cd00321</ItemId> 
       <Quantity>1</Quantity> 
      </Item> 
      <Item> 
       <ItemName>"Book"</ItemName> 
       <ItemId>bookx453</ItemId> 
       <Quantity>1</Quantity> 
     </Item> 
   </ItemList> 
 </Lease> 
  

<!-- Purchase.txt> 

<?xml version="1.0"?> 
<Purchase> 
 <Passenger> 
  <Name> 
    <LastName>"Kent"</LastName> 
    <FirstName>"Clark"</FirstName> 
  </Name> 
  <Address> 
    <Street>"123 Tiny Lane"</Street> 
    <City>"Smallville"</City> 
    <State>"Kansas"</State> 
    <Country>USA</Country> 
    <ZIP>12345</ZIP> 
   </Address> 
  </Passenger> 
    <ItemList> 
      <Item> 
       <ItemName>"CD Player"</ItemName> 
          <ItemId>cd00321</ItemId> 
          <Quantity>1</Quantity> 
      </Item> 
      <Item> 
          <ItemName>"Book"</ItemName> 
          <ItemId>bookx453</ItemId> 
          <Quantity>1</Quantity> 
      </Item> 
    </ItemList> 
</Purchase> 

} 
  
  

Listing 3: Item.java, PurchaseOrder.java, LeaseOrder.java, Confirmation.java 
import java.io.*; 

public class Item implements Serializable { 

 private String name_ = null; 
 private String sku_ = null; 
 private int quantity_ = 0; 

 public Item (String name, String sku, 
 int quantity) { 
     name_ = name; 
     sku_ = sku; 
     quantity_ = quantity; } 

 public String getName () { return name_; } 
 public String getSKU () { return sku_; } 
 public int getQuantity () { return quantity_; } 
} 
  

// PurchaseOrder.java 
import java.io.*; 

public class PurchaseOrder implements Serializable { 
private Item[] items_ = null; 
private String customerName_ = null; 
private String address_ = null; 

public PurchaseOrder (Item[] items, 
                      String customerName, 
                      String address) { 
          items_ = items; 
          customerName_ = customerName; 
          address_ = address; 
        } 
        public Item[] getItems () { return 
 items_; } 
        public Item getItem(int index) { return 
 items_[index]; } 
        public String getCustomerName () { 
 return customerName_; } 
        public String getAddress () { return 
 address_; } 
} 
  

// LeaseOrder.java 
import java.io.*; 

public class LeaseOrder extends PurchaseOrder implements Serializable { 

        private String referenceNo_ = null; 

        public LeaseOrder (Item[] items, 
                                           String customerName, 
                                           String address, 
                                           String referenceNo) { 

                super(items, customerName, address); 
                referenceNo_ = referenceNo; 
        } 
        public String getReferenceNo () { return referenceNo_; } 
} 
  

// Confirmation.java 
public class Confirmation { 
        String type_ = null; 
        String confirmationNo_ = null; 

        public Confirmation (String type, String confirmationNo) { 
                type_ = type; 
                confirmationNo_ = confirmationNo; 
        } 

        public String getType () { return type_; } 
        public String getConfirmationNo () { return confirmationNo_; } 

} 
  
  

Listing 4: StoreClientManager.java, RMIStoreService.java, RMIStoreClient.java, RMIStoreServer.java 

public class StoreClientManager { 

        RMIStoreClient rmiClient_ = null; 

        public StoreClientManager () { 
                if (rmiClient_ == null) 
              rmiClient_ = new RMIStoreClient(); 
        } 

        public Confirmation doLease (LeaseOrder leaseOrder) 
          throws Exception { 
                Boolean result = rmiClient_.getLeaseAvailibility(leaseOrder); 

                if (result.booleanValue()) 
                  return new Confirmation("Lease", "AS1234"); 
                else 
                  return new Confirmation("Lease", "NIL"); 
        } 
  

        public Confirmation doPurchase (PurchaseOrder purchaseOrder) 
          throws Exception { 
                return new Confirmation("Purchase", "AS1234"); 
        } 
} 
  

// RMIStoreService.java 
import java.rmi.*; 

public interface RMIStoreService extends Remote { 
  public static final String SERVER_NAME = "rmi://t8000x321/servlet/rmiStoreServer"; 
  public Boolean getLeaseAvailibility (LeaseOrder leaseOrder) throws 
RemoteException; 
} 
  

// RMIStoreClient.java 
import java.rmi.*; 

public class RMIStoreClient implements RMIStoreService { 

  private RMIStoreService server_ = null; 

  public RMIStoreClient() { connect ToServer(); } 

  public Boolean getLeaseAvailibility (LeaseOrder leaseOrder) throws 
RemoteException { 
        Boolean result = new Boolean(false); 
        try { result = server_.getLeaseAvailibility(leaseOrder); } 
        catch (Exception re) { re.printStackTrace(); } 
    System.out.println("result = " + result); 
        return result; 
  } 

  public void connectToServer () { 
        if (System.getSecurityManager() == null) 
          System.setSecurityManager(new RMISecurityManager()); 

    try { server_ = 
(RMIStoreService)(Naming.lookup(RMIStoreService.SERVER_NAME)); } 
        catch (Exception e) { e.printStack Trace(); } 
  } 

} 
  

// RMIStoreServer.java 
import java.rmi.server.*; 

public class RMIStoreServer 
  extends UnicastRemoteObject implements 
  RMIStoreService { 

  public String RMI_SERVER_NAME = "rmi://t8000x321/servlet/rmiStoreServer"; 

  public RMIStoreServer() throws RemoteException { 
        super(); 
  } 

  public Boolean getLeaseAvailibility (LeaseOrder leaseOrder) throws 
RemoteException { 
        return new Boolean(true); 

  } 

 public static void main (String[] args) { 
        System.setSecurityManager(new RMISecurityManager()); 
        try { 
      RMIStoreServer server = new RMIStoreServer(); 
          Naming.rebind(server.RMI_SERVER_NAME, server); 
          System.out.println("RMIStoreServer ready ..."); } 
        catch (Exception e) { e.printStackTrace(); } 
  } 
} 
  
 
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.