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 public final static String MD5S = "hibernate-generatedschema.md5s";
31 private Map<String,String> properties;
32 private Map<String,String> classes;
34 private final Set<String> propertyNames;
35 private final Set<String> classNames;
37 private boolean modified = false;
39 private final File saved;
40 private final MessageDigest digest;
41 private final Log log;
44 ModificationTracker(String buildDirectory, Log log)
46 NoSuchAlgorithmException
48 propertyNames = new HashSet<String>();
49 classNames = new HashSet<String>();
50 saved = new File(buildDirectory + File.separator + MD5S);
51 digest = java.security.MessageDigest.getInstance("MD5");
56 private String calculate(InputStream is)
60 byte[] buffer = new byte[1024*4]; // copy data in 4MB-chunks
62 while((i = is.read(buffer)) > -1)
63 digest.update(buffer, 0, i);
65 byte[] bytes = digest.digest();
66 BigInteger bi = new BigInteger(1, bytes);
67 return String.format("%0" + (bytes.length << 1) + "x", bi);
70 private boolean check(Map<String,String> values, String name, String value)
72 if (!values.containsKey(name) || !values.get(name).equals(value))
74 values.put(name, value);
82 boolean check(String name, InputStream is) throws IOException
84 boolean result = check(classes, name, calculate(is));
90 boolean check(String name, String property)
92 boolean result = check(properties, name, property);
93 propertyNames.add(name);
98 boolean check(Properties properties)
100 boolean result = false;
101 for (String name : properties.stringPropertyNames())
102 result |= check(name, properties.getProperty(name));
109 modified |= !propertyNames.containsAll(properties.keySet());
110 modified |= !properties.keySet().containsAll(propertyNames);
111 modified |= !classNames.containsAll(classes.keySet());
112 modified |= !classes.keySet().containsAll(classNames);
119 if (saved.isFile() && saved.length() > 0)
123 FileInputStream fis = new FileInputStream(saved);
124 ObjectInputStream ois = new ObjectInputStream(fis);
125 properties = (HashMap<String,String>)ois.readObject();
126 classes = (HashMap<String,String>)ois.readObject();
131 properties = new HashMap<String,String>();
132 classes = new HashMap<String,String>();
133 log.warn("Cannot read md5s from saved: " + e);
138 properties = new HashMap<String,String>();
139 classes = new HashMap<String,String>();
142 saved.createNewFile();
144 catch (IOException e)
146 log.debug("Cannot create file \"" + saved.getPath() + "\" for md5s: " + e);
156 /** Write md5-sums for annotated classes to file */
159 FileOutputStream fos = new FileOutputStream(saved);
160 ObjectOutputStream oos = new ObjectOutputStream(fos);
161 oos.writeObject(properties);
162 oos.writeObject(classes);
168 log.error("Cannot write md5-sums to file: " + e);