home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
Transaction.java
< prev
next >
Wrap
Text File
|
1998-10-17
|
7KB
|
246 lines
package symantec.sourcebook.servlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
class Transaction extends Object implements Serializable
{
// static transaction table shared with other
// threads
static Hashtable transactions;
String orderID; // ID of the order
long recordCreated; // time this record was created
boolean verified; // order ID has been verified with Web Order
boolean accepted; // if verified is true, accepted = true if order OK, false if order Declined
long downloadCount; // number of times this order has attempted download
// pricing information
int price;
int tax;
int total;
boolean chargeTax;
// license information:
String name;
String company;
String email;
String address;
String city;
String state;
String zip;
String country;
String phone;
// authorization information
String AuthCode;
String ccOID;
public Transaction(String name,
String email,
String address,
String city,
String company,
String state,
String country,
String zip,
String phone)
{
orderID = null;
recordCreated = System.currentTimeMillis();
verified = false;
accepted = false;
downloadCount = 0;
// pricing information (in cents)
price = 9900;
tax = 817;
total = 10717;
chargeTax = false;
// license information:
this.name = name;
this.email = email;
this.address = address;
this.city = city;
this.state = state;
this.country = country;
this.zip = zip;
this.phone = phone;
this.company = company;
// authroization information
AuthCode = "";
ccOID = "";
}
public static synchronized Transaction findTransaction(String orderId)
{
Transaction t = (Transaction) transactions.get(orderId);
return t;
}
public synchronized void record() throws IOException
{
transactions.put(orderID,this);
WriteTransactions();
}
/**
* Choose a unique, unguessable transaction id
*
* This sample uses a simple unique ID that is non-secure.
*
* ENHANCE: Add your own algorithm for owner IDs. ID numbers
* need to be unique. It should also be impossible for someone
* who has not received an order page to guess a valid order id.
*
*/
public synchronized void selectId() throws IOException
{
File fOrderId = new File("order.id");
RandomAccessFile inOrderId = new RandomAccessFile(fOrderId,"rw");
// Order IDs are assured uniqueness using a
// counter on disk. Read and update the counter
// creating it if needed.
int id = 1;
if(inOrderId.length() > 0)
{
id = inOrderId.readInt();
inOrderId.seek(0);
}
inOrderId.writeInt(id+1);
inOrderId.close();
// Now add some data to the order ID to make it more
// difficult to guess..
long now = System.currentTimeMillis();
long orderid = ((((now & 0x0000000000000FFF) ^ 0x0000000000000b2E) ) ) |
( ((long)id) << 12) ;
// ENCHNCE: to make the order ID unguessable you can enctrpt it
// with a fixed key at this stage.
orderID = Long.toString(orderid,46);
}
// Read the transaction hash table
// If servlet was shut down with out proper termination,
// attempt to do recovery if possible
public synchronized static void InitializeTransactions() throws ServletException, IOException, OptionalDataException, ClassNotFoundException
{
File currentTransactions = new File("transactions.current");
File newTransactions = new File("transactions.new");
// If transactions.new exists, we've got a problem.
// The servlet probably terminated abnormally while updating the
// transaction file.
if(newTransactions.exists())
{
System.out.println("Transaction files in an inconsistant state");
throw new ServletException("Transaction files in an inconsistant state");
}
// if the current transaction file exists, read it in
if(currentTransactions.exists() )
{
FileInputStream fIn = new FileInputStream(currentTransactions);
ObjectInputStream in = new ObjectInputStream(fIn);
transactions = (Hashtable) in.readObject();
in.close();
fIn.close();
}
else
{ // if there is no transaction file, start a new one
transactions = new Hashtable();
}
}
public synchronized static void WriteTransactions() throws IOException
{
// Remove transactions more than 48 hours old
Enumeration key = transactions.keys();
while(key.hasMoreElements())
{
String keyName = (String) key.nextElement();
Transaction t = (Transaction)transactions.get(keyName);
if(t.recordCreated + (48 * 3600 * 1000) < System.currentTimeMillis())
{
transactions.remove(keyName);
}
}
// write updated transactions to file
File newTrans = new File("transactions.new");
FileOutputStream fOut = new FileOutputStream(newTrans);
ObjectOutputStream out = new ObjectOutputStream(fOut);
out.writeObject(transactions);
out.close();
fOut.close();
File currentTrans = new File("transactions.current");
File oldTrans = new File("transactions.old");
// rename files, and delete old ones
// Note: failure during the execution of this section of code
// may leave files in an inconsistant state
if(oldTrans.exists()) oldTrans.delete();
if(currentTrans.exists()) currentTrans.renameTo(oldTrans);
newTrans.renameTo(currentTrans);
}
}