1 package de.juplo.plugins.hibernate;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.ObjectInputStream;
10 import java.io.ObjectOutputStream;
11 import java.math.BigInteger;
12 import java.security.MessageDigest;
13 import java.security.NoSuchAlgorithmException;
14 import java.util.HashMap;
15 import java.util.HashSet;
17 import java.util.Properties;
19 import org.apache.maven.plugin.logging.Log;
27 public class ModificationTracker
29 private Map<String,String> properties;
30 private Map<String,String> classes;
32 private final Set<String> propertyNames;
33 private final Set<String> classNames;
35 private boolean modified = false;
37 private final File saved;
38 private final MessageDigest digest;
39 private final Log log;
42 ModificationTracker(String buildDirectory, String filename, Log log)
44 NoSuchAlgorithmException
46 propertyNames = new HashSet<String>();
47 classNames = new HashSet<String>();
48 File output = new File(filename + ".md5s");
49 if (output.isAbsolute())
55 // Interpret relative file path relative to build directory
56 saved = new File(buildDirectory, output.getPath());
57 log.debug("Adjusted relative path, resulting path is " + saved.getPath());
59 digest = java.security.MessageDigest.getInstance("MD5");
64 private String calculate(InputStream is)
68 byte[] buffer = new byte[1024*4]; // copy data in 4MB-chunks
70 while((i = is.read(buffer)) > -1)
71 digest.update(buffer, 0, i);
73 byte[] bytes = digest.digest();
74 BigInteger bi = new BigInteger(1, bytes);
75 return String.format("%0" + (bytes.length << 1) + "x", bi);
78 private boolean check(Map<String,String> values, String name, String value)
80 if (!values.containsKey(name) || !values.get(name).equals(value))
82 values.put(name, value);
90 boolean track(String name, InputStream is) throws IOException
92 boolean result = check(classes, name, calculate(is));
99 boolean check(String name, String property)
101 propertyNames.add(name);
102 return check(properties, name, property);
105 boolean track(String name, String property)
107 boolean result = check(name, property);
112 boolean track(Properties properties)
114 boolean result = false;
115 for (String name : properties.stringPropertyNames())
116 result |= track(name, properties.getProperty(name));
128 modified |= !propertyNames.containsAll(properties.keySet());
129 modified |= !properties.keySet().containsAll(propertyNames);
130 modified |= !classNames.containsAll(classes.keySet());
131 modified |= !classes.keySet().containsAll(classNames);
138 if (saved.isFile() && saved.length() > 0)
142 FileInputStream fis = new FileInputStream(saved);
143 ObjectInputStream ois = new ObjectInputStream(fis);
144 properties = (HashMap<String,String>)ois.readObject();
145 classes = (HashMap<String,String>)ois.readObject();
150 properties = new HashMap<String,String>();
151 classes = new HashMap<String,String>();
152 log.warn("Cannot read md5s from saved: " + e);
157 properties = new HashMap<String,String>();
158 classes = new HashMap<String,String>();
161 saved.createNewFile();
163 catch (IOException e)
165 log.debug("Cannot create file \"" + saved.getPath() + "\" for md5s: " + e);
175 /** Write md5-sums for annotated classes to file */
178 FileOutputStream fos = new FileOutputStream(saved);
179 ObjectOutputStream oos = new ObjectOutputStream(fos);
180 oos.writeObject(properties);
181 oos.writeObject(classes);
187 log.error("Cannot write md5-sums to file: " + e);