package spiversioning; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SPIVersioning { /** * alter this to your own environment. The format must match the systems canonical filename form. */ static final String sourceRoot = "C:\\Dev3\\SCA\\modules\\"; public static void main(String[] args) throws Throwable { File root = new File(sourceRoot); File rootf =root.getAbsoluteFile(); File[] contents = rootf.listFiles(); SPIVersioning hs = new SPIVersioning(); System.out.println(""); for (File file : contents) { hs.analyseProjectDir(file); } System.out.println(""); } private void analyseProjectDir(File pdir) { try { File javaSrc = new File(pdir.getAbsolutePath() + "/src/main/java"); File jsrcdir = javaSrc.getAbsoluteFile(); File[] pkgRoots = jsrcdir.listFiles(); for (File proot : pkgRoots) { analyzeJavaFiles(proot); } } catch (Exception e) { // TODO: handle exception } } String fileToString (File f) throws java.io.IOException{ StringBuffer sb = new StringBuffer(1000); BufferedReader reader = new BufferedReader(new FileReader(f)); char[] buf = new char[1024]; int count = 0; while((count=reader.read(buf)) != -1){ String readData = String.valueOf(buf, 0, count); sb.append(readData); buf = new char[1024]; } reader.close(); return sb.toString(); } static class PkgDirFilter implements FileFilter { public boolean accept(File pathname) { boolean rv = false; if (pathname.isDirectory()) { rv = true; } else if(pathname.getName().endsWith(".java")) { rv = true; } else if (pathname.getName().equals("package.html")) { rv = true; } return rv; } } private void analyzeJavaFiles(File proot) throws Exception { File[] filesOfInterest = proot.listFiles(new PkgDirFilter()); for (File file : filesOfInterest) { if(file.isDirectory()) { analyzeJavaFiles(file); } else if (file.getName().equals("package.html")) { // TODO decide if this means every java file in this package is implicitly spi } else { String fcontents = fileToString(file); if(fcontents.contains("@tuscany.spi.extension")) { String digest = md5(file); String cpath = file.getCanonicalPath(); cpath = cpath.replace(sourceRoot, ""); System.out.println(" "); } } } } private static String md5(File f) { MessageDigest digest = null; try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } InputStream is = null; String output = null; try { is = new FileInputStream(f); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } byte[] buffer = new byte[8192]; int read = 0; try { while( (read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); output = bigInt.toString(16); } catch(IOException e) { throw new RuntimeException("Unable to process file for MD5", e); } finally { try { is.close(); } catch(IOException e) { throw new RuntimeException("Unable to close input stream for MD5 calculation", e); } } return output; } }