Package org.jayasoft.woj.client.services.cache

Source Code of org.jayasoft.woj.client.services.cache.ZippedFileCache

package org.jayasoft.woj.client.services.cache;

import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;

import de.schlichtherle.io.File;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.io.ZipControllerException;
import fr.jayasoft.commons.logging.LoggingManager;



public class ZippedFileCache extends AbstractCache {
  private final static Logger LOGGER = LoggingManager.getLogger(ZippedFileCache.class.getName());
  
    public ZippedFileCache(String cachePath) {
        super(cachePath);
    }
   
    public String getSource(String md5, String className) {
        java.io.File fileIO = getSourceFile(md5,  className);
        if(fileIO != null && (fileIO.length() != 0)){
          try{
            File file = new File(fileIO.getCanonicalPath());
            if(file.exists()) {
                InputStream is = null;
                try {
                    StringBuffer buffer = new StringBuffer();
                    is =  new FileInputStream(file);
                    byte[] bytes = new byte[1024*5];
                    int readed = 0;
                    while((readed = is.read(bytes))>0) {
                        buffer.append(new String(bytes, 0, readed));
                    }
                    return buffer.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if(is != null) {
                        try {
                            is.close();
                        } catch (IOException silenterr) {}
                    }
                }
            }
           
          }catch(IOException e){  }
        }
        return null;
    }
   
    //  TO DO : test !!
  public java.io.File getSourceFile(String md5, String className) {
    java.io.File file = new java.io.File(getZipName(md5)+"/"+getSourcePath(className)); //$NON-NLS-1$
    if(file.exists() && (file.length() != 0)){
      return file;
    }
    return null;
  }
 
  // TO DO : test !!
  public URL getSourceURL(String md5, String className) {
    try{
      java.io.File file = getSourceFile(md5, className);
      if(file != null){
        String localCachePath = _cachePath.replaceAll("\\s", "%20");
        return new URL("jar:file:///"+localCachePath+"/"+getZipName(md5)+"!/"+getSourcePath(className));
      }
    }catch(MalformedURLException e){
      e.printStackTrace();     
    }
    return null;
    /*
    try{
      return getSourceFile(md5, className).toURL(); //$NON-NLS-1$
    }catch(MalformedURLException e){
      e.printStackTrace();     
    }
    return null;*/
  }
   
    public void putSource(String md5, String className, String source) {
        OutputStream os = null;
        try {
            String zipName = getZipName(md5);
      String fullPath = zipName+"/"+getSourcePath(className); //$NON-NLS-1$
            File file = new File(fullPath);
            if(!file.exists() || (file.length() == 0)) {
                os = new FileOutputStream(fullPath);
                os.write(source.getBytes());
            }
        } catch (Exception e) {
          LOGGER.error("Unable to store class in cache.",e); //$NON-NLS-1$
        finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException silenterr) {}
            }
            try {
                File.update();
                File.umount();
            } catch (ZipControllerException e) {}
        }
    }
   
    public void doCleanup() {
        File file = new File(_cachePath);
        java.io.File[] zips = file.listFiles(new FilenameFilter() {
            public boolean accept(java.io.File dir, String name) {
                return name.endsWith(".zip"); //$NON-NLS-1$
            }
        });
        if(zips != null) {
          for (int i = 0; i < zips.length; i++) {
            zips[i].delete();
          }
        }
        file.delete();
    }

    /**
     * @param md5
     * @return
     */
    private String getZipName(String md5) {
        return _cachePath+"/"+md5+".zip"; //$NON-NLS-1$ //$NON-NLS-2$
    }
   
    // TO DO : test !!
    public String getSourceDir(String jarFile){      
        return _cachePath+"/"+getMD5(jarFile)+".zip";
    }
}
TOP

Related Classes of org.jayasoft.woj.client.services.cache.ZippedFileCache

TOP
Copyright © 2015 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.