1 16 package org.apache.cocoon.components; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 27 import org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon; 28 import org.apache.cocoon.environment.Request; 29 import org.apache.cocoon.servlet.multipart.Part; 30 31 34 public class Repository { 35 36 public static final String FILE_NAME = "document"; 37 38 private static Repository instance; 39 40 private Repository() { 41 } 43 44 public static Repository getInstance() { 45 if (instance == null) { 46 instance = new Repository(); 47 } 48 return instance; 49 } 50 51 private static File getDir(String dirName) { 52 File dir = new File (dirName); 53 if (!dir.isDirectory()) throw new RuntimeException ("'"+ dirName + "' is not a directory!"); 54 return dir; 55 } 56 57 public static void save(Request request, String dirName) throws Exception { 58 File dir = getDir(dirName); 59 60 Enumeration params = request.getParameterNames(); 61 while (params.hasMoreElements()) { 62 String name = (String ) params.nextElement(); 63 if (name.indexOf("..") > -1) throw new Exception ("We are under attack!!"); 64 if (name.startsWith("save:")) { 66 Part part = (Part) request.get(name); 67 String code = name.substring(5); 68 File file = new File (dir, code); 69 save(part,file); 70 } else if (name.startsWith("delete:")) { 71 String value = request.getParameter(name); 72 if (value.length() > 0) { 73 String code = name.substring(7); 74 File file = new File (dir, code); 75 remove(file); 77 } 78 } 79 } 80 } 81 82 public static void fomSave(FOM_Cocoon cocoon, String dirName) throws Exception { 83 save(cocoon.getRequest(), dirName); 84 } 85 86 public static void save(Request request, String param, String file) throws Exception { 87 Part part = (Part) request.get(param); 88 save(part,new File (file)); 89 } 90 91 public static void save(Part part, File file) throws Exception { 92 InputStream in = null; 94 FileOutputStream out = null; 95 try { 96 in = part.getInputStream(); 97 out = new FileOutputStream (file); 98 copy(in, out); 99 } finally { 100 if (out != null) { 101 out.close(); 102 } 103 if (in != null) { 104 in.close(); 105 } 106 } 107 } 108 109 public static OutputStream getOutputStream(String dir) throws IOException { 110 String mainFile = dir + "/" + FILE_NAME + ".xml"; 111 String versionedFile = dir + "/" + FILE_NAME + "." + getVersionID(dir) + ".xml"; 112 copy(mainFile, versionedFile); 113 return new FileOutputStream (mainFile); 114 } 115 116 public static void revertFrom(String dir, int version) throws IOException { 117 String mainFile = dir + "/" + FILE_NAME + ".xml"; 118 String versionedFile = dir + "/" + FILE_NAME + "." + version + ".xml"; 119 copy(versionedFile,mainFile); 120 } 121 122 126 public static int getVersionID(String dirName) { 127 File dir = getDir(dirName); 128 129 File [] content = dir.listFiles(); 130 int id = 0; 131 for (int i = 0; i < content.length; i++) { 132 if (content[i].isFile()) { 133 try { 134 int localid = getVersion(content[i].getName()); 135 if (localid > id) id = localid; 136 } catch (Exception e) {} 137 } 138 } 139 140 return ++id; 141 } 142 143 public static Object [] getVersions(String dirName) { 144 File dir = getDir(dirName); 145 ArrayList versions = new ArrayList (); 146 147 File [] content = dir.listFiles(); 148 for (int i = 0; i < content.length; i++) { 149 if (content[i].isFile()) { 150 try { 151 int version = getVersion(content[i].getName()); 152 if (version > 0) { 153 versions.add(new Integer (version)); 154 } 155 } catch (Exception e) {} 156 } 157 } 158 159 return versions.toArray(); 160 } 161 162 173 private static int getVersion(String name) { 174 int extIndex = name.lastIndexOf(".xml"); 175 if (extIndex > 0) { 176 String nameWithoutExtension = name.substring(0,extIndex); 177 int dotIndex = nameWithoutExtension.lastIndexOf('.'); 178 if (dotIndex > 0) { 179 String localidString = nameWithoutExtension.substring(dotIndex + 1); 180 return Integer.parseInt(localidString); 181 } 182 } 183 return -1; 184 } 185 186 public static int getID(String dirName) { 187 File dir = getDir(dirName); 188 189 File [] content = dir.listFiles(); 190 int id = 0; 191 for (int i = 0; i < content.length; i++) { 192 if (content[i].isDirectory()) { 193 try { 194 String name = content[i].getName(); 195 int localid = Integer.parseInt(name); 196 if (localid > id) id = localid; 197 } catch (Exception e) {} 198 } 199 } 200 201 return ++id; 202 } 203 204 public static boolean remove(String fileName) { 205 return remove(new File (fileName)); 206 } 207 208 public static boolean remove(File file) { 209 boolean success = true; 210 211 if (file.isDirectory()) { 212 File [] content = file.listFiles(); 213 for (int i = 0; i < content.length; i++) { 214 success = remove(content[i]); 215 } 216 217 } 218 219 success = file.delete(); 221 222 return success; 223 } 224 225 public static void copy(String from, String to) throws IOException { 226 copy(new File (from),new File (to)); 227 } 228 229 public static void copy(File from, File to) throws IOException { 230 231 233 if (!from.exists()) { 234 throw new IOException ("Cannot find source file/folder"); 235 } 236 237 if (from.isDirectory()) { 238 to.mkdirs(); 239 File [] content = from.listFiles(); 240 for (int i = 0; i < content.length; i++) { 241 File src = content[i]; 242 copy(src,new File (to, src.getName())); 243 } 244 } else { 245 to.createNewFile(); 246 FileInputStream in = null; 247 FileOutputStream out = null; 248 try { 249 in = new FileInputStream (from); 250 out = new FileOutputStream (to); 251 copy(in,out); 252 } finally { 253 if (out != null) out.close(); 254 if (in != null) in.close(); 255 } 256 } 257 } 258 259 public static void copy(InputStream from, OutputStream to) throws IOException { 260 byte[] buffer = new byte[64 * 1024]; 261 int count = 0; 262 do { 263 to.write(buffer, 0, count); 264 count = from.read(buffer, 0, buffer.length); 265 } while (count != -1); 266 } 267 } 268 | Popular Tags |