Skip to main content
summaryrefslogtreecommitdiffstats
blob: 56179f787b800f0c3670811c1d78ad023f49759c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Created on Dec 10, 2009
 *
 * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE
 */
package org.eclipse.osee.framework.ui.workspacebundleloader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;

import org.eclipse.osee.framework.jdk.core.util.ChecksumUtil;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Andrew M. Finkbeiner
 *
 */
public class FileChangeDetector {

	private ConcurrentHashMap<URL, byte[]> bundleNameToMd5Map;

	public FileChangeDetector(){
		bundleNameToMd5Map = new ConcurrentHashMap<URL, byte[]>();
	}
	
	public boolean isChanged(URL url) {
		byte[] digest = getMd5Checksum(url);
		if (bundleNameToMd5Map.containsKey(url)) {
			// check for bundle binary equality
			if (!Arrays.equals(bundleNameToMd5Map.get(url), digest)) {
				bundleNameToMd5Map.put(url, digest);
				return true;
			} else {
				return false;
			}
		} else {
			bundleNameToMd5Map.put(url, digest);
			return true;
		}
	}

	private byte[] getMd5Checksum(URL url) {
		InputStream in = null;
		byte[] digest = new byte[0];
		try {
			in = url.openStream();
			digest = ChecksumUtil.createChecksum(url.openStream(), "MD5");
		} catch (IOException ex) {
			OseeLog.log(FileChangeDetector.class, Level.SEVERE, ex);
		} catch (NoSuchAlgorithmException ex) {
			OseeLog.log(FileChangeDetector.class, Level.SEVERE, ex);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException ex) {
					OseeLog.log(FileChangeDetector.class, Level.SEVERE, ex);
				}
			}
		}
		return digest;
	}

	/**
	 * @param url
	 * @return
	 */
	public boolean remove(URL url) {
		bundleNameToMd5Map.remove(url);
		return true;
	}

	/**
	 * 
	 */
	public void clear() {
		bundleNameToMd5Map.clear();
	}

}

Back to the top