001 /** 002 * www.jcoverage.com 003 * Copyright (C)2003 jcoverage ltd. 004 * 005 * This file is part of jcoverage. 006 * 007 * jcoverage is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published 009 * by the Free Software Foundation; either version 2 of the License, 010 * or (at your option) any later version. 011 * 012 * jcoverage is distributed in the hope that it will be useful, but 013 * WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with jcoverage; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 020 * USA 021 * 022 */ 023 package com.jcoverage.coverage; 024 025 import java.io.File; 026 import java.io.FileInputStream; 027 import java.io.FileNotFoundException; 028 import java.io.FileOutputStream; 029 import java.io.IOException; 030 import java.io.InputStream; 031 import java.io.ObjectInputStream; 032 import java.io.ObjectOutputStream; 033 034 import java.util.Collections; 035 import java.util.HashMap; 036 import java.util.Iterator; 037 import java.util.Map; 038 import java.util.Set; 039 040 import org.apache.log4j.Logger; 041 042 043 public class InstrumentationPersistence implements HasBeenInstrumented { 044 static final Logger logger=Logger.getLogger(InstrumentationPersistence.class); 045 046 final Map instrumentation=new HashMap(); 047 048 protected InstrumentationPersistence() { 049 } 050 051 protected Map loadInstrumentation() { 052 File directory=getDirectory(); 053 054 if(logger.isInfoEnabled()) { 055 logger.info("loading: "+directory+'/'+Instrumentation.FILE_NAME); 056 } 057 058 try { 059 return loadInstrumentation(new FileInputStream(new File(directory,Instrumentation.FILE_NAME))); 060 } catch(FileNotFoundException ex) { 061 logger.info(ex); 062 return Collections.EMPTY_MAP; 063 } 064 } 065 066 protected Map loadInstrumentation(InputStream is) { 067 ObjectInputStream objects=null; 068 try { 069 objects=new ObjectInputStream(is); 070 Map m=(Map)objects.readObject(); 071 if(logger.isInfoEnabled()) { 072 logger.info("loaded "+m.size()+" entries."); 073 } 074 return m; 075 } catch(ClassNotFoundException ex) { 076 logger.error(ex); 077 return Collections.EMPTY_MAP; 078 } catch(IOException ex) { 079 logger.error(ex); 080 return Collections.EMPTY_MAP; 081 } finally { 082 if(objects!=null) { 083 try { 084 objects.close(); 085 } catch(IOException ex) { 086 if(logger.isDebugEnabled()) { 087 logger.debug(ex); 088 } 089 } 090 } 091 092 if(is!=null) { 093 try { 094 is.close(); 095 } catch(IOException ex) { 096 if(logger.isDebugEnabled()) { 097 logger.debug(ex); 098 } 099 } 100 } 101 } 102 } 103 104 protected void merge(Map m) { 105 Iterator i=m.entrySet().iterator(); 106 while(i.hasNext()) { 107 Map.Entry entry=(Map.Entry)i.next(); 108 if(instrumentation.containsKey(entry.getKey())) { 109 getInstrumentation(entry.getKey()).merge((Instrumentation)entry.getValue()); 110 } else { 111 instrumentation.put(entry.getKey(),entry.getValue()); 112 } 113 } 114 } 115 116 private File getDirectory() { 117 if (System.getProperty("com.jcoverage.rawcoverage.dir")!=null) { 118 return new File(System.getProperty("com.jcoverage.rawcoverage.dir")); 119 } else { 120 return new File(System.getProperty("user.dir")); 121 } 122 } 123 124 protected void saveInstrumentation() { 125 File directory=getDirectory(); 126 127 if(logger.isInfoEnabled()) { 128 logger.info("saving: "+directory+'/'+Instrumentation.FILE_NAME); 129 } 130 131 saveInstrumentation(directory); 132 } 133 134 protected void saveInstrumentation(File destDir) { 135 FileOutputStream os=null; 136 ObjectOutputStream objects=null; 137 138 try { 139 os=new FileOutputStream(new File(destDir,Instrumentation.FILE_NAME)); 140 objects=new ObjectOutputStream(os); 141 objects.writeObject(instrumentation); 142 if(logger.isInfoEnabled()) { 143 logger.info("saved "+instrumentation.size()+" entries."); 144 } 145 } catch(IOException ex) { 146 logger.error(ex); 147 } finally { 148 if(objects!=null) { 149 try { 150 objects.close(); 151 } catch(IOException ex) { 152 if(logger.isDebugEnabled()) { 153 logger.debug(ex); 154 } 155 } 156 } 157 158 if(os!=null) { 159 try { 160 os.close(); 161 } catch(IOException ex) { 162 if(logger.isDebugEnabled()) { 163 logger.debug(ex); 164 } 165 } 166 } 167 } 168 } 169 170 protected Instrumentation getInstrumentation(Object key) { 171 return (Instrumentation)instrumentation.get(key); 172 } 173 174 protected Set keySet() { 175 return Collections.unmodifiableSet(instrumentation.keySet()); 176 } 177 178 }