001    /*
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * Copyright (C) 2009 John Lewis
005     *
006     * Note: This file is dual licensed under the GPL and the Apache
007     * Source License (so that it can be used from both the main
008     * Cobertura classes and the ant tasks).
009     *
010     * Cobertura is free software; you can redistribute it and/or modify
011     * it under the terms of the GNU General Public License as published
012     * by the Free Software Foundation; either version 2 of the License,
013     * or (at your option) any later version.
014     *
015     * Cobertura is distributed in the hope that it will be useful, but
016     * WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018     * General Public License for more details.
019     *
020     * You should have received a copy of the GNU General Public License
021     * along with Cobertura; if not, write to the Free Software
022     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023     * USA
024     */
025    package net.sourceforge.cobertura.util;
026    
027    import java.io.File;
028    import java.io.InputStream;
029    import java.util.zip.ZipFile;
030    
031    import org.apache.log4j.Logger;
032    
033    public class Source {
034            private InputStream is;
035            
036            //streamOrigin is either a File or a ZipFile
037            private Object streamOrigin;
038            
039            private static Logger LOGGER = Logger.getLogger(Source.class);
040    
041            public Source(InputStream is, Object streamOrigin)
042            {
043                    this.is = is;
044                    this.streamOrigin = streamOrigin;
045            }
046            
047            public InputStream getInputStream()
048            {
049                    return is;
050            }
051            
052            /**
053             * Close the source input stream and the archive if it came from one.
054             * 
055             * This will not throw anything.   Any throwable is caught and a warning is logged.
056             */
057            public void close()
058            {
059                    try
060                    {
061                            is.close();
062                    }
063                    catch (Throwable t)
064                    {
065                            LOGGER.warn("Failure closing input stream for " + getOriginDesc(), t);
066                    }
067                    
068                    if (streamOrigin instanceof ZipFile)
069                    {
070                            try
071                            {
072                                    ((ZipFile) streamOrigin).close();
073                            }
074                            catch (Throwable t)
075                            {
076                                    LOGGER.warn("Failure closing " + getOriginDesc(), t);
077                            }
078                    }
079            }
080            
081            public String getOriginDesc()
082            {
083                    String ret = "";
084                    
085                    if (streamOrigin instanceof File)
086                    {
087                            ret = "file " + ((File) streamOrigin).getAbsolutePath();
088                    }
089                    else
090                    {
091                            ret = "archive " + ((ZipFile) streamOrigin).getName();
092                    }
093                    return ret;
094            }
095    }