summaryrefslogtreecommitdiffstats
path: root/sandbox/kgoodson/SourceExplore/src/main/java/spiversioning/SPIVersioning.java
blob: 536a55f13e8ffe7ffcd7f30ddc78e1bb6030d8b0 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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("<spifiles>");
		for (File file : contents) {
			hs.analyseProjectDir(file);
		}
		System.out.println("</spifiles>");
			
				
	}

	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("    <file path=\""+cpath+"\" md5=\"" +digest + "\"/>");
				}


			}
			
		}
		
	}

	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;
	}




}