001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.datasource;
031
032import java.io.File;
033import java.io.FileInputStream;
034import java.io.IOException;
035
036import org.apache.commons.logging.Log;
037import org.apache.commons.logging.LogFactory;
038
039import com.ice.tar.TarArchive;
040
041/**
042 * This class reprents a tar archive object. Thought tar file is not a realy a
043 * comression file, we still consider it is because want to untar it like unzip
044 * file.
045 * 
046 * @author Jing Tao
047 * 
048 */
049
050public class EcogridTarArchivedDataCacheItem extends
051                EcogridCompressedDataCacheItem {
052        private static Log log;
053        static {
054                log = LogFactory
055                                .getLog("org.ecoinformatics.seek.datasource.EcogridTarArchivedDataCacheItem");
056        }
057
058        /**
059         * Default constructor
060         */
061        public EcogridTarArchivedDataCacheItem() {
062                super();
063        }
064
065        /**
066         * Constructor if we need refresh it even the cache item already untared
067         * 
068         * @param refresh
069         *            boolean
070         */
071        public EcogridTarArchivedDataCacheItem(boolean refresh) {
072                super(refresh);
073        }
074
075        /**
076         * This method will specifically to untar a cache item. The un tared file
077         * location will be cachedata/unzip/cacheitemlocationfilename/
078         * 
079         * @throws Exception
080         */
081        public void unCompressCacheItem() throws Exception {
082                if (unCompressedCacheItemDir != null) {
083                        log
084                                        .debug("At unCompressCacheItem method in EcogridTarArchive ojbect");
085                        extractTarFile(getAbsoluteFileName(), unCompressedCacheItemDir);
086                        unCompressedFileList = unCompressedCacheItemDir.list();
087                }
088
089        }
090
091        /**
092         * This method will untar a given file to destination dir
093         * 
094         * @param tarFileName
095         *            String
096         * @param unTarDesDir
097         *            File
098         */
099        public static void extractTarFile(String tarFileName, File unTarDesDir)
100                        throws Exception {
101                TarArchive tar = null;
102                if (tarFileName != null && unTarDesDir != null) {
103                        log.debug("In extractTarFile method ");
104                        log.debug("The source tar file is " + tarFileName);
105                        log.debug("The un tar destionation dir is " + unTarDesDir);
106                        try {
107                                unTarDesDir.mkdirs();
108                                tar = new TarArchive(new FileInputStream(tarFileName));
109                                tar.extractContents(unTarDesDir);
110                        } catch (Exception ex) {
111                                log.debug("The exception in extractTarFile 1", ex);
112                                throw ex;
113                        } finally {
114                                if (tar != null) {
115                                        try {
116                                                tar.closeArchive();
117                                        } catch (IOException ioe) {
118                                                // don't know what to do now
119                                                log.debug("The exception in extractTarFile 2", ioe);
120                                        }
121                                }
122                        }
123                }
124        }
125
126}