home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / jload18.zip / scachestore.java < prev    next >
Text File  |  2000-04-13  |  5KB  |  174 lines

  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.zip.CRC32;
  4.  
  5. /* localstore */
  6. public class scachestore extends localstore
  7. {
  8.  
  9.  /* smartcache */
  10.  private String scacheroot;
  11.  private int swap_level1_dirs,swap_level2_dirs;
  12.  private LruHashtable dircache;
  13.  
  14.  public scachestore(String cfg[])
  15.  {
  16.   super(cfg);
  17.  }
  18.  
  19.  protected void localinit()
  20.  {
  21.   swap_level1_dirs=swap_level2_dirs=4;
  22.   scacheroot="store";
  23.   smartcacheconfig(arg[0]);  
  24.   dircache=new LruHashtable(25);
  25.  }
  26.  
  27.  public String toString()
  28.  {
  29.   return "Smart Cache directory storage";
  30.  }
  31.  
  32.  public boolean isReadOnly()
  33.  {
  34.   return true;
  35.  }
  36.  
  37.  public void close()
  38.  {
  39.   if(dircache==null) return;
  40.   dircache=null;
  41. }
  42.  
  43.  public localurl getURL(String URL) throws java.net.MalformedURLException
  44.  {
  45.   // 0. parse URL
  46.   String[] parsed=parseURL(URL);
  47.   
  48.   // 1. get local directory
  49.   String locdir=getLocalDir(parsed[0],parsed[1],parsed[2],parsed[4]);
  50.   
  51.   // 2. get cache directory
  52.   minicachedir dir=null;
  53.    synchronized(this)
  54.    {
  55.     dir=(minicachedir)dircache.get(locdir);
  56.     if(dir==null) 
  57.      {
  58.       dir=new minicachedir(locdir); 
  59.       dircache.put(locdir,dir);
  60.      }
  61.    }
  62.   
  63.   return dir.getObject(parsed[3]);
  64.  }
  65.  
  66.  
  67.  
  68.  private final void smartcacheconfig(String cfgfile)
  69.  {
  70.   try
  71.   {
  72.   String line,token;
  73.   StringTokenizer st;
  74.  
  75.   DataInputStream dis=new DataInputStream(new BufferedInputStream(new FileInputStream(cfgfile))); 
  76.   System.err.println("[CONFIG] Processing Smart_Cache config file "+cfgfile);
  77.   while ( (line = dis.readLine()) != null)
  78.   {
  79.           if(line.startsWith("#")) continue;
  80.       st=new StringTokenizer(line);
  81.           if(st.hasMoreTokens()==false) continue;
  82.           token=st.nextToken().toLowerCase();
  83.           
  84.         if(token.equals("swap_level1_dirs")) {swap_level1_dirs=Integer.valueOf(st.nextToken()).intValue();
  85.                               continue;
  86.                                                     }
  87.  
  88.         if(token.equals("swap_level2_dirs")) {swap_level2_dirs=Integer.valueOf(st.nextToken()).intValue();
  89.                               continue;
  90.                                                     }
  91.                                                                                                                                                              
  92.                                                     
  93.          if(token.equals("cacheroot")) {  scacheroot=st.nextToken();
  94.                                                     if(scacheroot.charAt(1)!=':'
  95.                                                        &&
  96.                                                        scacheroot.charAt(0)!=File.separatorChar
  97.                                                        )
  98.                                                        System.err.println("[CONFIG_WARNING] SmartCache root is not absolute path, may not work");
  99.                                        continue;
  100.                                                     }
  101.   
  102.   }
  103.  
  104.   // musime znat swap dirs levels
  105.   if(scacheroot!=null && ( swap_level1_dirs==0 || swap_level2_dirs==0) ) scacheroot=null; 
  106.   dis.close();  
  107.   }
  108.   catch (IOException fnf)
  109.   {
  110.    throw new IllegalArgumentException("Can not read Smart Cache config file: "+cfgfile);
  111.   }
  112.    
  113.   //System.err.println("[CONFIG_DEBUG] smartcacheconfig Proxy configured: "+proxydefined);
  114.   //System.err.println("[CONFIG_DEBUG] smartcacheconfig CacheRoot: "+scacheroot+" ("+swap_level1_dirs+"/"+swap_level2_dirs+")");
  115.  }
  116.  
  117. /**
  118.   Vrati lokalni adresar k zadanym parametrum
  119. */
  120. final private String getLocalDir(String host,String port,String urldir,String proto)
  121. {
  122.   StringBuffer result=new StringBuffer(80);
  123.   result.append(scacheroot);
  124.   // String result=cache_dir;
  125.   int i;
  126.   
  127.   /* 1. spocitat hash z host stringu */
  128.   CRC32 crc=new CRC32();
  129.   
  130.   crc.update(host.getBytes());
  131.  
  132.   /* mame hash - rozdelime ho na adresar */
  133.   /* zacneme budovat cestu */
  134.       i=(int)(crc.getValue()/(0x100000000L/(swap_level1_dirs*swap_level2_dirs)));
  135.       result.append(File.separator+(i/swap_level2_dirs)+File.separator+(i % swap_level2_dirs)+File.separator+host);
  136.  
  137.   /* pridame port */
  138.   if(port!=null)
  139.    {result.append('_');
  140.     result.append(port);
  141.    }
  142.  
  143.   /* add protocol */
  144.   if(proto!=null) 
  145.              {
  146.               result.append('^');
  147.               result.append(proto);
  148.              }
  149.   
  150.   /* pridame adresar */
  151.   if(File.separatorChar!='/') urldir=urldir.replace('/',File.separatorChar);
  152.   
  153.   /* *************************************************** */
  154.   /* ALERT: Synchronize by hand with garbage.encodechars */
  155.   /* *************************************************** */
  156.   /* nahrazujeme nepratelske znaky */
  157.   // urldir=urldir.replace(':','_');
  158.   // zde to neni nutne, neb muze byt jen v portu
  159.   
  160.   //  urldir=urldir.replace('*','@');
  161.   //  urldir=urldir.replace('?','#');  
  162.   // urldir=urldir.replace('~','-');
  163.   
  164.   // these two are not needed. Browsers sends < >
  165.   // urldir=urldir.replace('>','}');  
  166.   // urldir=urldir.replace('<','{');
  167.   urldir=urldir.replace('|','!');
  168.   
  169.   result.append(urldir);
  170.   return result.toString();
  171. }
  172.  
  173. }
  174.