1 8 package com.mountainminds.eclemma.internal.core; 9 10 import java.io.File ; 11 import java.io.UnsupportedEncodingException ; 12 import java.lang.ref.PhantomReference ; 13 import java.lang.ref.ReferenceQueue ; 14 import java.security.MessageDigest ; 15 import java.security.NoSuchAlgorithmException ; 16 import java.util.HashSet ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 22 import com.mountainminds.eclemma.core.EclEmmaStatus; 23 24 30 public class StateFiles { 31 32 private static final String LAUNCHDATA_FOLDER = ".launch/"; 34 private static final String INSTRDATA_FOLDER = ".instr/"; 36 private static final String IMPORTDATA_FOLDER = ".import/"; 38 private static final String SOURCE_FOLDER = ".src/"; 40 private static final ReferenceQueue CLEANUPQUEUE = new ReferenceQueue (); 41 42 private static final Set CLEANUPFILES = new HashSet (); 43 44 45 private final IPath stateLocation; 46 47 public StateFiles(IPath stateLocation) { 48 this.stateLocation = stateLocation; 49 this.stateLocation.toFile().mkdirs(); 50 getLaunchDataFolder().toFile().mkdirs(); 51 getInstrDataFolder().toFile().mkdirs(); 52 getSourceDataFolder().toFile().mkdirs(); 53 getImportDataFolder().toFile().mkdirs(); 54 } 55 56 public void deleteTemporaryFiles() { 57 deleteFiles(getLaunchDataFolder().toFile(), false); 58 deleteFiles(getInstrDataFolder().toFile(), false); 59 deleteFiles(getSourceDataFolder().toFile(), false); 60 deleteFiles(getImportDataFolder().toFile(), false); 61 } 62 63 private static void deleteFiles(File file, boolean deleteparent) { 64 if (file.isDirectory()) { 65 File [] files = file.listFiles(); 66 for (int i = 0; files != null && i < files.length; i++) { 67 deleteFiles(files[i], true); 68 } 69 } 70 if (deleteparent) file.delete(); 71 } 72 73 public IPath getLaunchDataFolder() { 74 return stateLocation.append(LAUNCHDATA_FOLDER); 75 } 76 77 private IPath getInstrDataFolder() { 78 return stateLocation.append(INSTRDATA_FOLDER); 79 } 80 81 public IPath getInstrDataFolder(IPath location) throws CoreException { 82 return getInstrDataFolder().append(getInternalId(location, false)); 83 } 84 85 private IPath getSourceDataFolder() { 86 return stateLocation.append(SOURCE_FOLDER); 87 } 88 89 public IPath getSourceFolder(IPath location) throws CoreException { 90 return getSourceDataFolder().append(getInternalId(location, true)); 91 } 92 93 private IPath getImportDataFolder() { 94 return stateLocation.append(IMPORTDATA_FOLDER); 95 } 96 97 public IPath getImportSessionFile(IPath original) throws CoreException { 98 IPath p = getImportDataFolder().append(getInternalId(original, true)); 99 registerForCleanup(p); 100 return p; 101 } 102 103 private static String getInternalId(IPath location, boolean withtimestamp) throws CoreException { 104 long timestamp = 0; 105 if (withtimestamp) { 106 File f = location.toFile(); 107 if (f.exists()) { 108 timestamp = f.lastModified(); 109 } 110 } 111 StringBuffer sb = new StringBuffer (); 112 try { 113 MessageDigest md = MessageDigest.getInstance("MD5"); md.update(location.toString().getBytes("UTF8")); md.update(Long.toHexString(timestamp).getBytes("UTF8")); byte[] sig = md.digest(); 117 for (int i = 0; i < sig.length; i++) { 118 sb.append(Character.forDigit((sig[i] >> 4) & 0xf, 0x10)); 119 sb.append(Character.forDigit(sig[i] & 0xf, 0x10)); 120 } 121 } catch (NoSuchAlgorithmException e) { 122 throw new CoreException(EclEmmaStatus.ID_CREATION_ERROR.getStatus(e)); 123 } catch (UnsupportedEncodingException e) { 124 throw new CoreException(EclEmmaStatus.ID_CREATION_ERROR.getStatus(e)); 125 } 126 return sb.toString(); 127 } 128 129 137 public void registerForCleanup(IPath file) { 138 cleanupObsoleteFiles(); 139 CLEANUPFILES.add(new CleanupFile(file)); 140 } 141 142 private void cleanupObsoleteFiles() { 143 while (true) { 144 CleanupFile f = (CleanupFile) CLEANUPQUEUE.poll(); 145 if (f == null) { 146 break; 147 } 148 CLEANUPFILES.remove(f); 149 f.delete(); 150 } 151 } 152 153 private static class CleanupFile extends PhantomReference { 154 155 private final File file; 156 157 public CleanupFile(IPath path) { 158 super(path, CLEANUPQUEUE); 159 this.file = path.toFile(); 160 } 161 162 public void delete() { 163 deleteFiles(file, true); 164 clear(); 165 } 166 167 } 168 169 } 170 | Popular Tags |