home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / Transaction.java < prev    next >
Text File  |  1998-10-17  |  7KB  |  246 lines

  1. package symantec.sourcebook.servlet;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.servlet.*;
  6.  
  7.  
  8. class Transaction extends Object implements Serializable
  9. {
  10.     
  11.     // static transaction table shared with other
  12.     // threads
  13.     
  14.     static Hashtable transactions;
  15.     
  16.     
  17.     String orderID;         // ID of the order
  18.     long   recordCreated;   // time this record was created
  19.     boolean verified;       // order ID has been verified with Web Order
  20.     boolean accepted;       // if verified is true, accepted = true if order OK, false if order Declined
  21.     long   downloadCount;   // number of times this order has attempted download
  22.  
  23.  
  24.     // pricing information
  25.  
  26.     int price;
  27.     int tax;
  28.     int total;
  29.     boolean chargeTax;
  30.  
  31.     // license information:
  32.  
  33.     String name;
  34.     String company;
  35.     String email;
  36.     String address;
  37.     String city;
  38.     String state;
  39.     String zip;
  40.     String country;
  41.     String phone;
  42.  
  43.     // authorization information
  44.  
  45.     String AuthCode;
  46.     String ccOID;
  47.  
  48.     public Transaction(String name,
  49.                        String email,
  50.                        String address,
  51.                        String city,
  52.                        String company,
  53.                        String state,
  54.                        String country,
  55.                        String zip,
  56.                        String phone)
  57.     {
  58.         orderID = null;
  59.         recordCreated = System.currentTimeMillis();
  60.         verified = false;
  61.         accepted = false;
  62.         downloadCount = 0;
  63.  
  64.         // pricing information (in cents)
  65.  
  66.          price = 9900;
  67.          tax = 817;
  68.          total = 10717;
  69.          chargeTax = false;
  70.  
  71.         // license information:
  72.  
  73.          this.name = name;
  74.          this.email = email;
  75.          this.address = address;
  76.          this.city = city;
  77.          this.state = state;
  78.          this.country = country;
  79.          this.zip = zip;
  80.          this.phone = phone;
  81.          this.company = company;
  82.  
  83.         // authroization information
  84.  
  85.         AuthCode = "";
  86.         ccOID = "";
  87.     }
  88.  
  89.  
  90.   public static synchronized Transaction findTransaction(String orderId) 
  91.   {
  92.     Transaction t = (Transaction) transactions.get(orderId);
  93.     return t;
  94.     
  95.   }
  96.   
  97.   
  98.   public synchronized void record() throws IOException
  99.   {
  100.     
  101.     transactions.put(orderID,this);
  102.     WriteTransactions();
  103.   }
  104.   
  105.   
  106.   
  107.     
  108.     
  109.     
  110.     /**
  111.      *  Choose a unique, unguessable transaction id
  112.      *
  113.      *  This sample uses a simple unique ID that is non-secure.
  114.      *  
  115.      *  ENHANCE: Add your own algorithm for owner IDs.  ID numbers
  116.      *  need to be unique.  It should also be impossible for someone
  117.      *  who has not received an order page to guess a valid order id.
  118.      *
  119.      */
  120.      
  121.     public synchronized void selectId() throws IOException
  122.     {
  123.  
  124.  
  125.         File fOrderId = new File("order.id");
  126.  
  127.  
  128.         RandomAccessFile inOrderId = new RandomAccessFile(fOrderId,"rw");
  129.  
  130.         // Order IDs are assured uniqueness using a
  131.         // counter on disk. Read and update the counter
  132.         // creating it if needed.
  133.  
  134.         int id = 1;
  135.         if(inOrderId.length() > 0)
  136.         {
  137.             id = inOrderId.readInt();
  138.             inOrderId.seek(0);
  139.         }
  140.         
  141.         inOrderId.writeInt(id+1);
  142.         inOrderId.close();
  143.         
  144.         // Now add some data to the order ID to make it more
  145.         // difficult to guess..
  146.         
  147.         long now = System.currentTimeMillis();
  148.  
  149.         long orderid = ((((now & 0x0000000000000FFF) ^ 0x0000000000000b2E) )  ) |
  150.                         ( ((long)id) << 12) ;
  151.         
  152.         // ENCHNCE: to make the order ID unguessable you can enctrpt it
  153.         // with a fixed key at this stage. 
  154.         
  155.         orderID =  Long.toString(orderid,46);
  156.         
  157.             
  158.            
  159.     }
  160.  
  161.     
  162.  
  163.     // Read the transaction hash table 
  164.     // If servlet was shut down with out proper termination,
  165.     // attempt to do recovery if possible
  166.     
  167.     public synchronized static void InitializeTransactions() throws ServletException, IOException, OptionalDataException, ClassNotFoundException
  168.     {
  169.         File currentTransactions = new File("transactions.current");
  170.         File newTransactions = new File("transactions.new");
  171.         
  172.         // If transactions.new exists, we've got a problem.
  173.         // The servlet probably terminated abnormally while updating the
  174.         // transaction file.  
  175.         
  176.         if(newTransactions.exists())
  177.         {
  178.             System.out.println("Transaction files in an inconsistant state");
  179.             throw new ServletException("Transaction files in an inconsistant state");
  180.         }
  181.         
  182.         // if the current transaction file exists, read it in
  183.         
  184.         if(currentTransactions.exists() )
  185.         {
  186.             FileInputStream fIn = new FileInputStream(currentTransactions);
  187.             ObjectInputStream in = new ObjectInputStream(fIn);
  188.             transactions = (Hashtable) in.readObject();
  189.             in.close();
  190.             fIn.close();
  191.             
  192.         }       
  193.         else
  194.         {   // if there is no transaction file, start a new one
  195.             transactions = new Hashtable();    
  196.         }
  197.             
  198.     }
  199.     
  200.     public synchronized static void WriteTransactions() throws IOException
  201.     {
  202.         
  203.         // Remove transactions more than 48 hours old
  204.         
  205.         Enumeration key = transactions.keys();
  206.         while(key.hasMoreElements())
  207.         {
  208.             String keyName = (String) key.nextElement();
  209.             
  210.             Transaction t = (Transaction)transactions.get(keyName);
  211.             if(t.recordCreated + (48 * 3600 * 1000) < System.currentTimeMillis())
  212.             {
  213.                 transactions.remove(keyName);
  214.             }
  215.             
  216.         }
  217.         
  218.         // write updated transactions to file
  219.         
  220.         File newTrans = new File("transactions.new");
  221.         FileOutputStream fOut = new FileOutputStream(newTrans);
  222.         ObjectOutputStream out = new ObjectOutputStream(fOut);
  223.         out.writeObject(transactions);
  224.         out.close();
  225.         fOut.close();
  226.         
  227.         
  228.         File currentTrans = new File("transactions.current");
  229.         File oldTrans = new File("transactions.old");
  230.         
  231.         // rename files, and delete old ones
  232.         // Note: failure during the execution of this section of code
  233.         //       may leave files in an inconsistant state
  234.         
  235.         if(oldTrans.exists()) oldTrans.delete();
  236.         if(currentTrans.exists()) currentTrans.renameTo(oldTrans);
  237.         newTrans.renameTo(currentTrans);
  238.         
  239.         
  240.         
  241.         
  242.    }
  243.     
  244.     
  245.     
  246. }