1 package com.sslexplorer.boot; 2 3 import java.io.File ; 4 import java.io.FileFilter ; 5 import java.io.FileInputStream ; 6 import java.io.FileNotFoundException ; 7 import java.io.FileOutputStream ; 8 import java.io.FilenameFilter ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.util.HashMap ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 24 public class LocalRepository implements Repository { 25 26 static Log log = LogFactory.getLog(LocalRepository.class); 27 28 30 private File basedir; 31 private HashMap stores = new HashMap (); 32 33 38 public LocalRepository() throws IOException { 39 40 43 if(System.getProperty("sslexplorer.localRepositoryPath")!=null) { 44 basedir = new File (System.getProperty("sslexplorer.localRepositoryPath")); 45 basedir.mkdirs(); 46 } 47 48 if(basedir!=null && !basedir.exists()) { 49 log.error("Local repository could not be created [" + basedir.getAbsolutePath() + "]"); 50 basedir = null; 51 } 52 53 if(basedir==null) { 54 basedir = new File (ContextHolder.getContext().getConfDirectory(), "repository"); 55 basedir.mkdirs(); 56 } 57 58 if(!basedir.exists()) { 59 throw new IOException ("Default local repository could not be created [" + basedir.getAbsolutePath() + "]"); 60 } 61 62 65 File [] files = basedir.listFiles(new FileFilter () { 66 public boolean accept(File file) { 67 return file.isDirectory(); 68 } 69 }); 70 71 for(int i=0;i<files.length;i++) { 72 stores.put(files[i].getName(), new LocalRepositoryStore(files[i])); 73 } 74 } 75 76 77 80 public RepositoryStore getStore(String storeName) { 81 82 if(!stores.containsKey(storeName)) { 83 File f = new File (basedir, storeName); 84 f.mkdirs(); 85 stores.put(storeName, new LocalRepositoryStore(f)); 86 } 87 88 return (RepositoryStore) stores.get(storeName); 89 } 90 91 93 class LocalRepositoryStore implements RepositoryStore { 94 95 File basedir; 96 97 LocalRepositoryStore(File basedir) { 98 this.basedir = basedir; 99 } 100 101 public InputStream getEntryInputStream(String name) throws IOException { 102 103 File f = new File (basedir, name); 104 105 if(!f.exists()) 106 throw new FileNotFoundException (name + " is not a valid " + basedir.getName() + " entry"); 107 108 return new FileInputStream (f); 109 } 110 111 public OutputStream getEntryOutputStream(String name) throws IOException { 112 113 File f = new File (basedir, name); 114 115 return new FileOutputStream (f); 116 } 117 118 public void removeEntry(String name) throws IOException { 119 120 File f = new File (basedir, name); 121 122 if(f.exists()) { 123 if(!f.delete()) 124 throw new IOException (name + " could not be deleted from the " + basedir.getName() + " store"); 125 } 126 127 } 128 129 public boolean hasEntry(String name) { 130 return new File (basedir, name).exists(); 131 } 132 133 public String [] listEntries() { 134 return basedir.list(new FilenameFilter () { 135 public boolean accept(File f, String name) { 136 return !new File (f, name).isDirectory(); 137 } 138 }); 139 } 140 141 } 142 143 144 } 145 | Popular Tags |