home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / jload18.zip / configloader.java < prev    next >
Text File  |  2000-04-12  |  14KB  |  324 lines

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class configloader
  6. {
  7.  private Vector loc;
  8.  
  9.  /* global setup */ 
  10.  // proxy server
  11.  
  12.  private InetAddress proxyserver;
  13.  private int          proxyport;
  14.  
  15.  private boolean     proxydefined;
  16.  private short       threads;
  17.  private byte         maxretry;
  18.  private float         retryprio;
  19.  
  20.  private localstore store;
  21.  
  22.  public static final byte MAX_RETRY=3;
  23.  public static final float RETRY_PRIORITY=-1f;
  24.   
  25.  public static boolean case_sensitive;
  26.  
  27.  /* default */
  28.  location def;
  29.  
  30.  private int debuglevel=5;
  31.  
  32.  public final static String[] OPTIONS_SORTMAP={ "systemdefault","none","default","nodefaults"};
  33.  public final static String[] ACT_SORTMAP={ "noparse","reject"};
  34.  
  35.  configloader(String cfgfile) throws IOException
  36.  {
  37.    loc=new Vector();
  38.    def=new location("http://add.system.defaults/");
  39.    location lastloc;
  40.    lastloc=null;
  41.  
  42.    proxyserver=null; 
  43.    proxyport=0;
  44.    proxydefined=false;
  45.    threads=5;
  46.    maxretry=MAX_RETRY;
  47.    retryprio=RETRY_PRIORITY;
  48.    store=new localstore(null);
  49.    
  50.    case_sensitive=false;
  51.    
  52.    DataInputStream dis=new DataInputStream(new BufferedInputStream(
  53.                         new FileInputStream(cfgfile) ) );
  54.                         
  55.   System.err.println("[CONFIG] Processing "+loader.NAME+" config file "+cfgfile);
  56.   int lineno=0;                        
  57.   String line,token;
  58.   StringTokenizer st;                        
  59.   while ( (line = dis.readLine()) != null)
  60.   {
  61.           lineno++;
  62.           if(line.startsWith("#")) continue;
  63.       st=new StringTokenizer(line);
  64.           if(st.hasMoreTokens()==false) continue;
  65.           token=st.nextToken();
  66.           token=token.toLowerCase();
  67.           try
  68.           {
  69.           if(token.equals("localstore")) 
  70.           {
  71.             String nt=st.nextToken();
  72.             if(nt.equalsIgnoreCase("null"))
  73.              {
  74.               store=new nullstore(null);
  75.              }
  76.             else if(nt.equalsIgnoreCase("smartcache"))
  77.              {
  78.           String arg[]=new String[1];
  79.           arg[0]=st.nextToken();
  80.               store=new scachestore(arg);
  81.              }
  82.             else if(nt.equalsIgnoreCase("directory"))
  83.          {
  84.            String arg[]=new String[1];
  85.            arg[0]=st.nextToken();
  86.            store=new localstore(arg);
  87.          }
  88.             else
  89.              System.err.println("[CONFIG_ERROR] Unknown localstore type: "+nt);
  90.             continue;
  91.           }
  92.           if(token.equals("threads")) {        int c=Integer.valueOf(st.nextToken()).intValue();
  93.                                                threads=(short)c;
  94.                                    continue;
  95.                                       }
  96.  
  97.           if(token.equals("http_proxy")) 
  98.                                       {  
  99.                                         proxyserver=InetAddress.getByName(st.nextToken());
  100.                                         proxyport=Integer.valueOf(st.nextToken()).intValue();
  101.                                         proxydefined=true;
  102.                                         continue;
  103.                                       }
  104.                                                                             
  105.  
  106.           if(token.equals("retry")) {        int c=Integer.valueOf(st.nextToken()).intValue();
  107.                                                maxretry=(byte )c;
  108.                                    continue;
  109.                                       }
  110.  
  111.           if(token.equals("retrypriority")) {
  112.                                              float c=Float.valueOf(st.nextToken()).floatValue();
  113.                                                            retryprio=c;
  114.                                                            continue;
  115.                                                       }
  116.                                                                             
  117.                                                                                       
  118.           if(token.equals("case_sensitive_matching")) {
  119.                                              char c=(char)Integer.valueOf(st.nextToken()).intValue();
  120.                                                            if(c==1) case_sensitive=true; else case_sensitive=false;
  121.                                                            continue;
  122.                                                       }
  123.  
  124.           if(token.equals("defaultserverpriority")) {
  125.                                              float c=Float.valueOf(st.nextToken()).floatValue();
  126.                                                            def.setPriority(c);
  127.                                                            continue;
  128.                                                       }
  129.  
  130.           if(token.equals("defaultscandepth")) {           // Java 1.0.X doesn't have Short class, using Integer
  131.                                              short c=(short)Integer.valueOf(st.nextToken()).intValue();
  132.                                                            def.setDepth(c);
  133.                                                            continue;
  134.                                                       }
  135.           if(token.equals("defaultserveroptions")) {       
  136.                                              String c=st.nextToken("\n");
  137.                                                            options o;
  138.                                                            o=new options("options="+c);
  139.                                                            // o.parse();
  140.                                                            //System.out.println("[DEBUG] "+o);
  141.                                                            o.prioritySort("options",OPTIONS_SORTMAP,false);
  142.                                                            //System.out.println("[DEBUG] Sorted as "+o);
  143.                                                            def.serveroptions(o);
  144.                                                            continue;
  145.                                                       }
  146.  
  147.           if(token.equals("defaultactions")) {       
  148.                                              String c=st.nextToken("\n");
  149.                                                            options o;
  150.                                                            o=new options(c);
  151.                                                            def.setActions(o,null);
  152.                                                            continue;
  153.                                                       }
  154.           if(token.equals("defaultmask"))             
  155.                                                      {
  156.                                              String c=st.nextToken("\n");
  157.                                                            options o;
  158.                                                            o=new options(c);
  159.                                                            o.addDefault(def.actions);
  160.                                                            o.prioritySort("act",ACT_SORTMAP,false);
  161.                                                            def.addMask(o);
  162.                                                            // System.out.println(o);
  163.                                                            continue;
  164.                                                      }
  165.  
  166.           if(token.equals("location"))             
  167.                                                      {
  168.                                              String c=st.nextToken();
  169.                                                            lastloc=new location(c,def);
  170.                                                            loc.addElement(lastloc);
  171.                                                            //System.out.println("Location: "+c);
  172.                                                            continue;
  173.                                                      }
  174.  
  175.           if(token.equals("starturl"))             
  176.                                                      {
  177.                                                            while(st.hasMoreTokens())
  178.                                                              lastloc.addStartURL(st.nextToken());
  179.                                                            continue;
  180.                                                      }
  181.  
  182.           if(token.equals("name"))             
  183.                                                      {
  184.                                              String c=st.nextToken();
  185.                                                            lastloc.setName(c);
  186.                                                            continue;
  187.                                                      }
  188.  
  189.           if(token.equals("alias"))             
  190.                                                      {
  191.                                                            while(st.hasMoreTokens())
  192.                                                            {
  193.                                                              lastloc.addAlias(st.nextToken());
  194.                                                            }
  195.                                                            continue;
  196.                                                      }
  197.  
  198.           if(token.equals("priority")) {
  199.                                              float c=Float.valueOf(st.nextToken()).floatValue();
  200.                                                            lastloc.setPriority(c);
  201.                                                            continue;
  202.                                                       }
  203.  
  204.           if(token.equals("scandepth")
  205.              ||token.equals("depth")
  206.                                     ) {           // Java 1.0.X doesn't have Short class, using Integer
  207.                                              short c=(short)Integer.valueOf(st.nextToken()).intValue();
  208.                                                            lastloc.setDepth(c);
  209.                                                            continue;
  210.                                                       }
  211.           if(token.equals("options")) {       
  212.                                              String c=st.nextToken("\n");
  213.                                                            options o;
  214.                                                            o=new options("options="+c);
  215.                                                            // o.parse();
  216.                                                            //System.out.println("[DEBUG] "+o);
  217.                                                            o.prioritySort("options",OPTIONS_SORTMAP,false);
  218.                                                            //System.out.println("[DEBUG] Sorted as "+o);
  219.                                                            lastloc.serveroptions(o);
  220.                                                            continue;
  221.                                                       }
  222.                                                       
  223.           if(token.equals("actions")) {       
  224.                                              String c=st.nextToken("\n");
  225.                                                            options o;
  226.                                                            o=new options(c);
  227.                                                            lastloc.setActions(o,def.actions);
  228.                                                            continue;
  229.                                                       }
  230.  
  231.           if(token.equals("addactions")) {       
  232.                                              String c=st.nextToken("\n");
  233.                                                            options o;
  234.                                                            o=new options(c);
  235.                                                            lastloc.addActions(o,false);
  236.                                                            continue;
  237.                                                       }
  238.                                                                                                             
  239.                                                       
  240.           if(token.equals("mask"))             
  241.                                                      {
  242.                                              String c=st.nextToken("\n");
  243.                                                            options o;
  244.                                                            o=new options(c);
  245.                                                            o.addDefault(lastloc.actions);
  246.                                                            o.prioritySort("act",ACT_SORTMAP,false);
  247.                                                            lastloc.addMask(o);
  248.                                                            // System.out.println(o);
  249.                                                            continue;
  250.                                                      }
  251.                                                       
  252.                                                       
  253.           System.err.println("[CONFIG_ERROR] unknown keyword "+token+" at line "+lineno);
  254.  
  255.                                                 
  256.           }
  257.           catch (NoSuchElementException nse)
  258.            { System.err.println("[CONFIG_ERROR] "+cfgfile+":"+lineno+" Missing arguent(s).");
  259.              continue;}                                     
  260.    }                      
  261.    dis.close();          
  262.    System.err.println("[CONFIG] Proccessing finished.\n");
  263.  }
  264.  
  265.  
  266.  
  267.  
  268.  /* proxy set-up */
  269.  public final boolean isProxyDefined()
  270.  {
  271.   return proxydefined;
  272.  }
  273.  
  274.  public final InetAddress getProxyServer()
  275.  {
  276.   return proxyserver;
  277.  }
  278.  
  279.  public final int getProxyPort()
  280.  {
  281.   return proxyport;
  282.  }
  283.  
  284.  public final location getDefaultLocation()
  285.  {
  286.   return def;
  287.  }
  288.  
  289.  public final short getThreads()
  290.  {
  291.   return threads;
  292.  }
  293.  
  294.  public final byte getMaxretry()
  295.  {
  296.   return maxretry;
  297.  }
  298.  
  299.  public final float getRetryPriority()
  300.  {
  301.   return retryprio;
  302.  }
  303.  
  304.  public final location[] getLocations()
  305.  {
  306.   location tmp[];
  307.   tmp=new location[loc.size()];
  308.   int i=0;
  309.   Enumeration en=loc.elements();
  310.   while(en.hasMoreElements())
  311.   {
  312.    tmp[i++]=(location)en.nextElement();
  313.   
  314.   }
  315.   return tmp;
  316.  }
  317.  
  318.  public final localstore getLocalStore()
  319.  {
  320.   return store;
  321.  }
  322.  
  323. }
  324.