View Javadoc
1 /* 2 Bloof - visualize the evolution of your software project 3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de> 4 http://bloof.sf.net/ 5 6 This program is free software; you can redistribute it and/or modify it 7 under the terms of the GNU General Public License. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License along with 15 this program; if not, write to the Free Software Foundation, Inc., 16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 18 $RCSfile: MetricRegistry.java,v $ 19 Created on $Date: 2003/09/29 17:24:02 $ 20 */ 21 package net.sf.bloof.metrics; 22 23 import java.io.BufferedReader; 24 import java.io.File; 25 import java.io.FileOutputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.InputStreamReader; 29 import java.net.MalformedURLException; 30 import java.net.URL; 31 import java.net.URLClassLoader; 32 import java.util.Enumeration; 33 import java.util.HashMap; 34 import java.util.Iterator; 35 import java.util.StringTokenizer; 36 import java.util.jar.JarFile; 37 import java.util.zip.ZipEntry; 38 39 import net.sf.bloof.Bloof; 40 import net.sf.bloof.BloofException; 41 42 /*** 43 * Registry for available Metrics and Filters 44 * @author Lukasz Pekacki <pekacki@users.sourceforge.net> 45 * @version $Id: MetricRegistry.java,v 1.8 2003/09/29 17:24:02 pekacki Exp $ 46 */ 47 public class MetricRegistry { 48 private static final String METRICS_DIR = Bloof.LOCAL_BLOOF_DIR + "/metrics"; 49 /*** 50 * Creates a metric 51 * @param aName type of metric 52 * @return metric 53 */ 54 public static Metric createMetric(String aName) { 55 Metric m = (Metric) sRegisteredMetrics.get(aName); 56 Class metricClass = m.getClass(); 57 try { 58 return (Metric) metricClass.newInstance(); 59 } catch (InstantiationException e) { 60 } catch (IllegalAccessException e) { 61 } 62 return null; 63 } 64 /*** 65 * Returns the availabe Metrics 66 * @return array of metrics that are currently available 67 */ 68 public static Iterator getAvailableMetricNames() { 69 return sRegisteredMetrics.keySet().iterator(); 70 } 71 /*** 72 * Registered metrics 73 * */ 74 75 private static HashMap sRegisteredMetrics = new HashMap(); 76 77 private static void extractMetricsJar() throws IOException { 78 File metricsDir = new File(METRICS_DIR); 79 metricsDir.mkdir(); 80 81 boolean jarFound = false; 82 String classPath = (System.getProperty("java.class.path")); 83 StringTokenizer st = new StringTokenizer(classPath, System.getProperty("path.separator")); 84 while (st.hasMoreTokens()) { 85 String token = st.nextToken(); 86 if (token.indexOf("bloofmetrics") > 0) { 87 jarFound = true; 88 JarFile metricJar = new JarFile(token); 89 for (Enumeration entries = metricJar.entries(); entries.hasMoreElements();) { 90 ZipEntry jarEntry = (ZipEntry) entries.nextElement(); 91 if (jarEntry.getName().indexOf(".jar") > 0) { 92 String singleDirName = extractDirName(jarEntry.getName()); 93 File mDir = new File(metricsDir.getPath() + "/" + singleDirName); 94 if (!mDir.exists()) { 95 mDir.mkdir(); 96 } 97 String fileName = mDir + "/" + extractFileName(jarEntry.getName()); 98 writeFile(fileName, metricJar.getInputStream(jarEntry)); 99 } 100 } 101 102 } 103 } 104 if (!jarFound) { 105 //check metrics file 106 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 107 InputStream stream = cl.getResourceAsStream("filelist.txt"); 108 if (stream != null) { 109 BufferedReader lineReader = new BufferedReader(new InputStreamReader(stream)); 110 boolean hasMore = true; 111 while (hasMore) { 112 String metricsJarEntry = lineReader.readLine(); 113 if (metricsJarEntry == null || metricsJarEntry.length() == 0) { 114 hasMore = false; 115 break; 116 } else { 117 createDir(metricsJarEntry); 118 writeFile( 119 metricsDir.getPath() + "/" + metricsJarEntry, 120 cl.getResourceAsStream(metricsJarEntry)); 121 } 122 123 } 124 125 } else { 126 System.err.println("Could not find any metrics. Must exit."); 127 System.exit(2); 128 } 129 130 } 131 132 } 133 134 private static void createDir(String aDirFile) { 135 aDirFile.replace('//', '/'); 136 int sepIndex = aDirFile.indexOf('/'); 137 String dir = aDirFile.substring(0, sepIndex); 138 File dirF = new File(METRICS_DIR + "/" + dir); 139 if (!dirF.exists()) { 140 dirF.mkdir(); 141 } 142 } 143 144 private static void writeFile(String aFile, InputStream aStream) throws IOException { 145 File jFile = new File(aFile); 146 if (!jFile.exists()) { 147 jFile.createNewFile(); 148 } 149 FileOutputStream fos = new FileOutputStream(jFile); 150 InputStream is = aStream; 151 byte[] buffer = new byte[10 * 1024]; 152 int length; 153 while ((length = is.read(buffer)) >= 0) { 154 fos.write(buffer, 0, length); 155 } 156 if (fos != null) { 157 fos.close(); 158 } 159 } 160 161 private static String extractFileName(String aJarEntry) { 162 aJarEntry.replace('//', '/'); 163 int lastSep = aJarEntry.lastIndexOf('/'); 164 return aJarEntry.substring(lastSep + 1); 165 } 166 167 private static String extractDirName(String aJarEntryFile) { 168 aJarEntryFile.replace('//', '/'); 169 int sep = aJarEntryFile.indexOf('/'); 170 return aJarEntryFile.substring(0, sep); 171 } 172 173 private static void loadMetrics() 174 throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { 175 File metricsDir = new File(METRICS_DIR); 176 if (!metricsDir.exists()) { 177 extractMetricsJar(); 178 } 179 File jarArchiveDir = new File(metricsDir + "/lib"); 180 File[] jarArchiveFiles = jarArchiveDir.listFiles(); 181 URL[] jarArchivesUrls = new URL[jarArchiveFiles.length]; 182 for (int i = 0; i < jarArchiveFiles.length; i++) { 183 jarArchivesUrls[i] = jarArchiveFiles[i].toURL(); 184 } 185 URLClassLoader jarLoader = 186 new URLClassLoader(jarArchivesUrls, Thread.currentThread().getContextClassLoader()); 187 String[] metricDirs = metricsDir.list(); 188 for (int i = 0; i < metricDirs.length; i++) { 189 if (metricDirs[i].equals("lib")) { 190 continue; 191 } 192 File singeMetricDir = new File(metricsDir.getPath() + "/" + metricDirs[i]); 193 String[] dirEntries = singeMetricDir.list(); 194 for (int j = 0; j < dirEntries.length; j++) { 195 String fileName = dirEntries[j]; 196 if (fileName.indexOf(".jar") > 0) { 197 String jarFileName = singeMetricDir.getPath() + "/" + fileName; 198 URL jarUrl = new File(jarFileName).toURL(); 199 URL[] urlList = new URL[1]; 200 urlList[0] = jarUrl; 201 URLClassLoader uCl = new URLClassLoader(urlList, jarLoader); 202 Class metricClass = uCl.loadClass("net.sf.bloof.metric.MyMetric"); 203 Metric m = (Metric) metricClass.newInstance(); 204 sRegisteredMetrics.put(m.getName(), m); 205 break; 206 } 207 } 208 209 } 210 211 } 212 213 static { 214 try { 215 loadMetrics(); 216 } catch (MalformedURLException e) { 217 Bloof.propagateBloofException( 218 new BloofException("Could not load metric." + " Bad Url." + e.toString())); 219 } catch (ClassNotFoundException e) { 220 Bloof.propagateBloofException( 221 new BloofException("Could not load metric." + " Class not found." + e.toString())); 222 } catch (InstantiationException e) { 223 Bloof.propagateBloofException( 224 new BloofException( 225 "Could not load metric." + " Could not instantiate." + e.toString())); 226 } catch (IllegalAccessException e) { 227 Bloof.propagateBloofException( 228 new BloofException("Could not load metric." + " Illegal Access." + e.toString())); 229 } catch (IOException e) { 230 Bloof.propagateBloofException( 231 new BloofException( 232 "Could not load metric." + " Could not load jar file." + e.toString())); 233 } 234 } 235 236 }

This page was automatically generated by Maven