View Javadoc

1   /*
2    * $Id: Fingerprint.java,v 1.1.1.1 2005/02/17 18:37:35 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
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          /* TODO: implement
37          String value=versionProps.getProperty(key);
38          if (value==null) {
39              // make it possible to have jars without version nos
40              value=versionProps.getProperty(file.getName());
41          }
42          if (value==null) {
43              // fall back on something obvious
44              value=key;
45              Date timestamp=new Date(file.lastModified());
46              value+=" / "+timestamp.toString();
47          }
48          return value;
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  }