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.util.Vector; 034 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.kepler.util.DotKeplerManager; 038 039/** 040 * This class will handle compressed data object(for example a zip file) 041 * download from server in Chache system. This class will encapsulate unzip the 042 * zip file into a directory and give a api to user to get a local file path by 043 * given a file entension. For example: a zip file will be download as 044 * 1209.dat(it has xyz.bil, xyz.blw, xyz.hdr, xyz.stx)then the zip file will be 045 * unzziped to /cachedir/uncompress/1029.dat. If you give a file extension - 046 * bil, it will return /cachedir/uncompress/l029.dat/xyz.bil 047 * 048 * @author Jing Tao 049 * 050 */ 051 052public abstract class EcogridCompressedDataCacheItem extends 053 EcogridDataCacheItem { 054 private static final String UNZIPDIR = "unzip"; 055 private static final String FILEEXTENSIONSEP = "."; 056 protected String unCompressedFilePath = null; 057 protected File unCompressedCacheItemDir = null; 058 protected String[] unCompressedFileList = null; 059 protected boolean refresh = false; 060 061 private static Log log; 062 static { 063 log = LogFactory 064 .getLog("org.ecoinformatics.seek.datasource.EcogridCompressedDataCacheItem"); 065 } 066 067 /** 068 * Default constructor 069 */ 070 public EcogridCompressedDataCacheItem() { 071 super(); 072 } 073 074 /** 075 * Constructor 076 * 077 * @param refresh 078 * if the compressed file need be uncompressed again even already 079 * has umpressed before 080 */ 081 public EcogridCompressedDataCacheItem(boolean refresh) { 082 super(); 083 this.refresh = refresh; 084 } 085 086 /** 087 * This method will load data from local file first. If there is no loca 088 * file It will download from server. Then it will unzip the file. 089 */ 090 public int doWork() { 091 log.debug("EcogridCompressedDataCacheItem - doing Work mStatus " 092 + getStatus()); 093 094 int superStatus = super.doWork(); 095 096 // Was there a problem with the download? 097 if (superStatus != CACHE_COMPLETE) 098 return CACHE_ERROR; 099 100 try { 101 String fname = getBaseFileName(); 102 if (fname != null) { 103 log.debug("The local file name is " + fname); 104 // DBPATH already has File.separetor as end 105 unCompressedFilePath = DotKeplerManager.getInstance().getCacheDirString() 106 + UNZIPDIR + File.separator + fname; 107 log.debug("The unzip file path is " + unCompressedFilePath); 108 unCompressedCacheItemDir = new File(unCompressedFilePath); 109 // if this dir alreay exist we don't want uncompressed again 110 if (unCompressedCacheItemDir.exists() && !refresh) { 111 log 112 .debug("The uncompress cache item dir already exist and refresh is " 113 + refresh + "we don't uncomressed it again"); 114 unCompressedFileList = unCompressedCacheItemDir.list(); 115 return CACHE_COMPLETE; 116 } else { 117 log 118 .debug("The uncomress cache item dir doesn't exist or refresh is true" 119 + ", we need to unCompressedCacheItemDir again"); 120 unCompressedCacheItemDir.mkdirs(); 121 unCompressCacheItem(); 122 } 123 124 } 125 126 } catch (Exception e) { 127 log.error("Error unzipping data item " + getName() + " -- Error msg: " + e.getMessage() ); 128 // we need to delete the uncompress dir for this data item 129 unCompressedCacheItemDir.delete(); 130 return CACHE_ERROR; 131 } 132 return CACHE_COMPLETE; 133 } 134 135 /** 136 * This is an abracted method for uncompress cache item. It will be 137 * overwritten by subclass - EcogridZippedDataCacheItem, 138 * EcogridGZippedDataCacheItem 139 */ 140 public abstract void unCompressCacheItem() throws Exception; 141 142 /** 143 * This method will get a file path which matches the given file extension 144 * in unzipped file list. If no match, null will be returned 145 * 146 * @param fileExtension 147 * String 148 * @return String[] 149 */ 150 public String[] getUnzippedFilePath(String fileExtension) { 151 String[] result = null; 152 if (unCompressedFileList == null || fileExtension == null) { 153 return result; 154 } 155 int length = unCompressedFileList.length; 156 Vector tmp = new Vector(); 157 for (int i = 0; i < length; i++) { 158 String fileName = (String) unCompressedFileList[i]; 159 if (fileName != null) { 160 log.debug("file name in file list is " + fileName); 161 int dotPosition = fileName.lastIndexOf(FILEEXTENSIONSEP); 162 String extension = fileName.substring(dotPosition + 1, fileName 163 .length()); 164 log.debug("The file extension for file name " + fileName 165 + " in file list is " + extension); 166 if (extension.equals(fileExtension)) { 167 // store the file path into a tmp array 168 tmp.add(unCompressedFilePath + File.separator + fileName); 169 } 170 } 171 } 172 // transfer vector in array 173 if (!tmp.isEmpty()) { 174 int len = tmp.size(); 175 result = new String[len]; 176 for (int j = 0; j < len; j++) { 177 result[j] = (String) tmp.elementAt(j); 178 log.debug("The file path which math file extension " 179 + fileExtension + " is " + result[j]); 180 } 181 } 182 return result; 183 } 184 185 /** 186 * Method to set up refresh variable 187 * 188 * @param refresh 189 * boolean 190 */ 191 public void setRefresh(boolean refresh) { 192 this.refresh = refresh; 193 } 194 195 /** 196 * Method to get refresh variable 197 * 198 * @return boolean 199 */ 200 public boolean getRefresh() { 201 return this.refresh; 202 } 203}