KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > StateFiles


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: StateFiles.java 88 2006-09-18 06:37:36Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core;
9
10 import java.io.File JavaDoc;
11 import java.io.UnsupportedEncodingException JavaDoc;
12 import java.lang.ref.PhantomReference JavaDoc;
13 import java.lang.ref.ReferenceQueue JavaDoc;
14 import java.security.MessageDigest JavaDoc;
15 import java.security.NoSuchAlgorithmException JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
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 /**
25  * Internal utility to manage files in the plugin's state locations.
26  *
27  * @author Marc R. Hoffmann
28  * @version $Revision: 88 $
29  */

30 public class StateFiles {
31
32   private static final String JavaDoc LAUNCHDATA_FOLDER = ".launch/"; //$NON-NLS-1$
33

34   private static final String JavaDoc INSTRDATA_FOLDER = ".instr/"; //$NON-NLS-1$
35

36   private static final String JavaDoc IMPORTDATA_FOLDER = ".import/"; //$NON-NLS-1$
37

38   private static final String JavaDoc SOURCE_FOLDER = ".src/"; //$NON-NLS-1$
39

40   private static final ReferenceQueue JavaDoc CLEANUPQUEUE = new ReferenceQueue JavaDoc();
41   
42   private static final Set JavaDoc CLEANUPFILES = new HashSet JavaDoc();
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 JavaDoc file, boolean deleteparent) {
64     if (file.isDirectory()) {
65       File JavaDoc[] 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 JavaDoc getInternalId(IPath location, boolean withtimestamp) throws CoreException {
104     long timestamp = 0;
105     if (withtimestamp) {
106       File JavaDoc f = location.toFile();
107       if (f.exists()) {
108         timestamp = f.lastModified();
109       }
110     }
111     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
112     try {
113       MessageDigest JavaDoc md = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
114
md.update(location.toString().getBytes("UTF8")); //$NON-NLS-1$
115
md.update(Long.toHexString(timestamp).getBytes("UTF8")); //$NON-NLS-1$
116
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 JavaDoc e) {
122       throw new CoreException(EclEmmaStatus.ID_CREATION_ERROR.getStatus(e));
123     } catch (UnsupportedEncodingException JavaDoc e) {
124       throw new CoreException(EclEmmaStatus.ID_CREATION_ERROR.getStatus(e));
125     }
126     return sb.toString();
127   }
128   
129   /**
130    * Registers the file the given path points to for deletion as soon as the
131    * reference to the path objects gets garbage collected. The caller must
132    * ensure to hold a reference to the given path object as long as the file
133    * is required. The file is not required to (jet) actually exist.
134    *
135    * @param file path object that points to the file
136    */

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 JavaDoc {
154
155     private final File JavaDoc 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