001/* 002 * Cobertura - http://cobertura.sourceforge.net/ 003 * 004 * Copyright (C) 2003 jcoverage ltd. 005 * Copyright (C) 2005 Mark Doliner 006 * Copyright (C) 2007 Joakim Erdfelt 007 * Copyright (C) 2007 Ignat Zapolsky 008 * 009 * Cobertura is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published 011 * by the Free Software Foundation; either version 2 of the License, 012 * or (at your option) any later version. 013 * 014 * Cobertura is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with Cobertura; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 022 * USA 023 */ 024 025package net.sourceforge.cobertura.coveragedata; 026 027import net.sourceforge.cobertura.CoverageIgnore; 028import net.sourceforge.cobertura.util.ConfigurationUtil; 029 030import java.io.BufferedInputStream; 031import java.io.File; 032import java.io.FileInputStream; 033import java.io.FileOutputStream; 034import java.io.IOException; 035import java.io.InputStream; 036import java.io.ObjectInputStream; 037import java.io.ObjectOutputStream; 038import java.io.OutputStream; 039import java.util.logging.Level; 040import java.util.logging.Logger; 041 042/** 043 * This contains methods used for reading and writing the 044 * "cobertura.ser" file. 045 */ 046@CoverageIgnore 047public abstract class CoverageDataFileHandler { 048 private static Logger logger = Logger.getLogger(CoverageDataFileHandler.class.getCanonicalName()); 049 private static File defaultFile = null; 050 051 public static File getDefaultDataFile() 052 { 053 // return cached defaultFile 054 if (defaultFile != null) 055 { 056 return defaultFile; 057 } 058 059 // load and cache datafile configuration 060 ConfigurationUtil config = new ConfigurationUtil(); 061 defaultFile = new File(config.getDatafile()); 062 063 return defaultFile; 064 } 065 066 public static ProjectData loadCoverageData(File dataFile) 067 { 068 InputStream is = null; 069 try 070 { 071 is = new BufferedInputStream(new FileInputStream(dataFile), 16384); 072 return loadCoverageData(is); 073 } 074 catch (IOException e) 075 { 076 logger.log(Level.SEVERE, "Cobertura: Error reading file " 077 + dataFile.getAbsolutePath() + ": " 078 + e.getLocalizedMessage(), e); 079 return null; 080 } 081 finally 082 { 083 if (is != null) 084 try 085 { 086 is.close(); 087 } 088 catch (IOException e) 089 { 090 logger.log(Level.SEVERE, "Cobertura: Error closing file " 091 + dataFile.getAbsolutePath() + ": " 092 + e.getLocalizedMessage(), e); 093 } 094 } 095 } 096 097 private static ProjectData loadCoverageData(InputStream dataFile) throws IOException 098 { 099 ObjectInputStream objects = null; 100 101 try 102 { 103 objects = new ObjectInputStream(dataFile); 104 ProjectData projectData = (ProjectData)objects.readObject(); 105 logger.log(Level.INFO, "Cobertura: Loaded information on " 106 + projectData.getNumberOfClasses() + " classes."); 107 return projectData; 108 } 109 catch (IOException e) { 110 throw e; 111 } 112 catch (Exception e) { 113 logger.log(Level.SEVERE, "Cobertura: Error reading from object stream.", e); 114 return null; 115 } 116 finally 117 { 118 if (objects != null) 119 { 120 try 121 { 122 objects.close(); 123 } 124 catch (IOException e) 125 { 126 logger.log(Level.SEVERE, "Cobertura: Error closing object stream."); 127 } 128 } 129 } 130 } 131 132 public static void saveCoverageData(ProjectData projectData, 133 File dataFile) 134 { 135 FileOutputStream os = null; 136 137 try 138 { 139 File dataDir = dataFile.getParentFile(); 140 if( (dataDir != null) && !dataDir.exists() ) { 141 dataDir.mkdirs(); 142 } 143 os = new FileOutputStream(dataFile); 144 saveCoverageData(projectData, os); 145 } 146 catch (IOException e) 147 { 148 logger.log(Level.SEVERE, "Cobertura: Error writing file " + dataFile.getAbsolutePath(), e); 149 } 150 finally 151 { 152 if (os != null) 153 { 154 try 155 { 156 os.close(); 157 } 158 catch (IOException e) 159 { 160 logger.log(Level.SEVERE, "Cobertura: Error closing file " 161 + dataFile.getAbsolutePath(), e); 162 } 163 } 164 } 165 } 166 167 private static void saveCoverageData(ProjectData projectData, 168 OutputStream dataFile) { 169 ObjectOutputStream objects = null; 170 171 try { 172 objects = new ObjectOutputStream(dataFile); 173 objects.writeObject(projectData); 174 logger.info("Cobertura: Saved information on " + projectData.getNumberOfClasses() + " classes."); 175 } catch (IOException e) { 176 logger.log(Level.SEVERE, "Cobertura: Error writing to object stream.", e); 177 } finally { 178 if (objects != null) { 179 try { 180 objects.close(); 181 } catch (IOException e) { 182 logger.log(Level.SEVERE, "Cobertura: Error closing object stream.", e); 183 } 184 } 185 } 186 } 187}