001: package com.ice.jcvsweb.manager;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.text.DecimalFormat;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.Properties;
009:
010: import javax.servlet.ServletContext;
011:
012: import com.ice.jcvsweb.bean.JCVSConfiguration;
013:
014: /**
015: * This class Manages our temporary files used to send images and binaries.
016: *
017: * @author Tim Endres, <a href="mailto:time@jcvs.org">time@jcvs.org</a>
018: * @see com.ice.cvsc
019: */
020:
021: public class JCVSFileManager {
022: public static final String ATTR_NAME = "jcvsFileManager";
023:
024: private ServletContext context = null;
025: private JCVSConfiguration config = null;
026: private HashMap fileMap = null;
027: private DecimalFormat hashFmt = null;
028:
029: public JCVSFileManager(ServletContext ctx, JCVSConfiguration config) {
030: this .context = ctx;
031: this .config = config;
032: this .fileMap = new HashMap(32);
033: this .hashFmt = new DecimalFormat("-00000000000;-00000000000");
034: }
035:
036: private String computeFileHash(String prefix, File f) {
037: int hashcode = f.hashCode();
038: StringBuffer hexBuf = new StringBuffer();
039: char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
040: '9', 'A', 'B', 'C', 'D', 'E', 'F', };
041:
042: // NOTE
043: // This algorithm reverses the hash. Do you care?
044: //
045: for (int i = 0; i < 8; ++i) {
046: int nibble = (hashcode & 15);
047: hashcode = (hashcode >> 4);
048: hexBuf.append(hex[nibble]);
049: }
050:
051: return (prefix + hexBuf.toString());
052: }
053:
054: public String manageFile(String prefix, File f,
055: boolean deleteOnRemove) {
056: String mimeType = this .context.getMimeType(f.getName());
057: return this .manageFile(prefix, f, mimeType, deleteOnRemove);
058: }
059:
060: public String manageFile(String prefix, File f, String mimeType,
061: boolean deleteOnRemove) {
062: String hash = this .computeFileHash(prefix, f);
063:
064: this .fileMap.put(hash, this .new ManagedFile(f, mimeType,
065: deleteOnRemove));
066:
067: return hash;
068: }
069:
070: public File getFile(String fileHash) {
071: File result = null;
072: ManagedFile mf = (ManagedFile) this .fileMap.get(fileHash);
073: if (mf != null) {
074: result = mf.getFile();
075: }
076:
077: return result;
078: }
079:
080: public String getMimeType(String fileHash) {
081: String result = null;
082: ManagedFile mf = (ManagedFile) this .fileMap.get(fileHash);
083: if (mf != null) {
084: result = mf.getMimeType();
085: }
086:
087: return result;
088: }
089:
090: public boolean remove(String fileHash) {
091: boolean result = true;
092: ManagedFile mf = (ManagedFile) this .fileMap.get(fileHash);
093:
094: if (mf != null && mf.getDelete()) {
095: File f = mf.getFile();
096: try {
097: result = f.delete();
098: } catch (Exception ex) {
099: this .context.log("JCVSFileManager: '" + f.getPath()
100: + "'", ex);
101: }
102: } else {
103: this .context.log("JCVSFileManager: '" + fileHash
104: + "' not found");
105: }
106:
107: if (result) {
108: this .fileMap.remove(fileHash);
109: }
110:
111: return result;
112: }
113:
114: public void removeAll() {
115: }
116:
117: public class ManagedFile {
118: private File file = null;
119: private String mimeType = null;
120: private boolean deleteOnRemove = false;
121:
122: public ManagedFile(File f, String mimeType, boolean delete) {
123: this .file = f;
124: this .mimeType = mimeType;
125: this .deleteOnRemove = delete;
126: }
127:
128: public File getFile() {
129: return this .file;
130: }
131:
132: public String getMimeType() {
133: return this .mimeType;
134: }
135:
136: public boolean getDelete() {
137: return this.deleteOnRemove;
138: }
139:
140: }
141:
142: }
|