package edu.vt.marian.WebGate; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; /** Class name: rate Class description: this class represent he rate under which a user will be charged to use the system, we design it as a class since there maybe many kinds of rates at the same time. Author: Jianxin Zhao Finished time: ????, 1998 Known bugs: none Platform: jdk1.1.5 under UNIX */ public class rate { /** this is just for debugging */ Debug debug; /** this vector contains all the rate types of this object */ private Vector rates; /** this constructor will create a rate object, the object is empty upon creation */ public rate(Debug debug) { // first initialize data members init(); this.debug = debug; } /** this constructor will create a rate object from the specified directory. */ public rate(String dir, Debug debug) { // first initialize data members init(); this.debug = debug; // we only save the address of a user to a file at this time File rate_config = new File(dir); // read preferences from the file BufferedReader br; try { FileReader fr = new FileReader(rate_config); br = new BufferedReader(fr); // first read out number of preferences int number_types = Integer.parseInt(br.readLine()); // then read out each preference int i; name_value_pair nvp; for (i = 0; i < number_types; i++) { nvp = new name_value_pair(br, debug); rates.addElement(nvp); } br.close(); } catch (IOException e3) { debug.dumpTrace("class rate: error reading data from " + rate_config.getAbsolutePath()); } } /** this constructor will create a rate object from a stream, this might be helpful for remote management in the future. (not implemented yet) */ public rate(DataInputStream dis, Debug debug) { } /** this method will return all the types currently contained in this rate object */ public Vector get_types() { int i; name_value_pair nvp; Vector types = new Vector(); for (i = 0; i < rates.size(); i++) { nvp = (name_value_pair) rates.elementAt(i); types.addElement(nvp.get_name()); } return types; } /** this method will add the specified rate type with the specified value to the rate object */ public String add(String type, String value) { int i; name_value_pair nvp; // first search to see if this type exists in our current // rate types for (i = 0; i < rates.size(); i++) { nvp = (name_value_pair) rates.elementAt(i); if (nvp.get_name().equals(type)) { // already exist, cann't add it return "this type already exists"; } } // this is a new type, so add it nvp = new name_value_pair(type, value, debug); rates.addElement(nvp); return "OK"; } /** this method will set the specified rate type to the specified value */ public String set(String type, String value) { int i; name_value_pair nvp; // first search to see if this type exists in our current // rate types for (i = 0; i < rates.size(); i++) { nvp = (name_value_pair) rates.elementAt(i); if (nvp.get_name().equals(type)) { // exist, set it's value nvp.set_value(value); return "OK"; } } // this is a new type, cann't set it's value directly return "no such rate type"; } /** this method will delete the specified rate */ public String delete(String type) { int i; name_value_pair nvp; // first search to see if this rate type exists in our current // rate types for (i = 0; i < rates.size(); i++) { nvp = (name_value_pair) rates.elementAt(i); if (nvp.get_name().equals(type)) { // exist, delete it rates.removeElementAt(i); return "OK"; } } // we couldn't found such rate type return "no such address type"; } /** this method will return the value of the specified type of this rate object */ public String get_value(String type) { int i; name_value_pair nvp = null; for (i = 0; i < rates.size(); i++) { nvp = (name_value_pair) rates.elementAt(i); if (nvp.get_name().equals(type)) { // a match is found, return it's value return nvp.get_value(); } } // no match is found return null; } /** this method will print the content of this object to a stream, this might be helpful for remote management in the future. (not implemented yet) */ public String to_stream(DataOutputStream dos) { return null; // not implemented yet } /** this method will save this object to the specified file */ public String save(String filename) { FileOutputStream fos = null; try { fos = new FileOutputStream(filename, false); } catch (IOException e3) { debug.dumpTrace("class rate: error opening rate config file to write"); } PrintWriter pw = new PrintWriter(fos); // first write number of address types pw.println(rates.size()); // then write out each address type int i; name_value_pair nvp; for (i = 0; i < rates.size(); i++) { nvp = (name_value_pair) rates.elementAt(i); nvp.to_stream(pw); } pw.flush(); pw.close(); return "OK"; } /** this method will initialize the data members of this object */ private void init() { debug = null; rates = new Vector(); } }