1
2
3
4
5
6
7 package org.astrogrid.webapp;
8
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.Date;
12 import javax.servlet.jsp.JspWriter;
13
14 /***
15 * Methods used by JSPs to show details of the sites configuration
16 * <p>
17 * @author M Hill, from N Winstanley's fingerprint.jsp page from Brian Ewis
18 */
19
20 public class Fingerprint
21 {
22
23 /***
24 * Returns an HTML details on theIdentify the version of a jar file. This uses a properties file
25 * containing known names and sizes in the format
26 * 'name(size)=version'. Version strings should be like 'xerces-1.4'
27 * ie they should include the name of the library.
28 */
29 public String getFileVersion(File file) throws IOException {
30 String key="<td>"+file.getName()+"</td>";
31 key+= "<td>"+file.length()+"</td>";
32 Date timestamp=new Date(file.lastModified());
33 key+= "<td>"+timestamp.toString()+"</td>";
34 return key;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 }
51
52 /***
53 * Split up a classpath-like variable. Returns a list of files.
54 * TODO: this can't cope with relative paths. I think theres code in BCEL that
55 * can be used for this?
56 */
57 private File[] splitClasspath(String path) throws IOException {
58 java.util.StringTokenizer st=
59 new java.util.StringTokenizer(path,
60 System.getProperty("path.separator"));
61 int toks=st.countTokens();
62 File[] files=new File[toks];
63 for(int i=0;i<toks;i++) {
64 files[i]=new File(st.nextToken());
65 }
66 return files;
67 }
68
69 /*** given a list of files, return a list of jars which actually exist */
70 File[] scanFiles(File[] files) throws IOException {
71 File[] jars=new File[files.length];
72 int found=0;
73 for (int i=0; i<files.length; i++) {
74 if (files[i].getName().toLowerCase().endsWith(".jar")
75 && files[i].exists()) {
76 jars[found]=files[i];
77 found++;
78 }
79 }
80 if (found<files.length) {
81 File[] temp=new File[found];
82 System.arraycopy(jars,0,temp,0,found);
83 jars=temp;
84 }
85 return jars;
86 }
87
88 private static final File[] NO_FILES=new File[0];
89
90 /*** scan a directory for jars */
91 public File[] scanDir(String dir) throws IOException
92 {
93 return scanDir(new File(dir));
94 }
95
96 public File[] scanDir(File dir) throws IOException {
97 if (!dir.exists() || !dir.isDirectory()) {
98 return NO_FILES;
99 }
100 return scanFiles(dir.listFiles());
101 }
102
103 /*** scan a classpath for jars */
104 public File[] scanClasspath(String path) throws IOException {
105 if (path==null) {
106 return NO_FILES;
107 }
108 return scanFiles(splitClasspath(path));
109 }
110
111 /***
112 * scan a 'dirpath' (like the java.ext.dirs system property) for jars
113 */
114 public File[] scanDirpath(String path) throws IOException {
115 if (path==null) {
116 return NO_FILES;
117 }
118 File[] current=new File[0];
119 File[] dirs=splitClasspath(path);
120 for(int i=0; i<dirs.length; i++) {
121 File[] jars=scanDir(dirs[i]);
122 File[] temp=new File[current.length+jars.length];
123 System.arraycopy(current,0,temp,0,current.length);
124 System.arraycopy(jars,0,temp,current.length,jars.length);
125 current=temp;
126 }
127 return scanFiles(current);
128 }
129
130 /*** print out the jar versions for a directory */
131 public void listDirectory(String title, JspWriter out,String dir, String comment) throws IOException {
132 listVersions(title, out,scanDir(dir), comment);
133 }
134
135 /*** print out the jar versions for a directory-like system property */
136 public void listDirProperty(String title, JspWriter out,String key, String comment) throws IOException {
137 listVersions(title, out,scanDir(System.getProperty(key)), comment);
138 }
139
140 /*** print out the jar versions for a classpath-like system property */
141 public void listClasspathProperty(String title, JspWriter out,String key, String comment) throws IOException {
142 listVersions(title, out,scanClasspath(System.getProperty(key)), comment);
143 }
144
145 /*** print out the jar versions for a 'java.ext.dirs'-like system property */
146 public void listDirpathProperty(String title, JspWriter out,String key, String comment) throws IOException {
147 listVersions(title, out,scanDirpath(System.getProperty(key)), comment);
148 }
149
150 /*** print out the jar versions for a given list of files */
151 public void listVersions(String title, JspWriter out,File[] jars, String comment) throws IOException {
152 out.print("<h2>");
153 out.print(title);
154 out.println("</h2>");
155 out.println("<table>");
156 for (int i=0; i<jars.length; i++) {
157 out.println("<tr>"+getFileVersion(jars[i])+"</tr>");
158 }
159 out.println("</table>");
160 if(comment!=null && comment.length()>0) {
161 out.println("<p>");
162 out.println(comment);
163 out.println("<p>");
164 }
165 }
166
167 }