001/*
002 * Copyright (c) 2003-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.kepler.objectmanager.cache;
031
032import java.io.BufferedInputStream;
033import java.io.BufferedOutputStream;
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileOutputStream;
037
038/**
039 * This class is not as generic as the name may indicate. It is designed to get
040 * the metadata to determine where the data is stored and then does a Ecogrid
041 * "get" to the data.
042 */
043public class DataCacheFileObject extends DataCacheObject {
044        // File Type
045        public static final int UNKNOWN = 0; // file stored internally
046        public static final int INTERNAL = 1; // file stored internally
047        public static final int EXTERNAL = 2; // file is external to cache
048        public static final int COPY_FILE_TO_CACHE = 3; // file is moved, then Type
049                                                                                                        // is set to INTERNAL
050
051        // Protected
052        protected int mFileLoc = UNKNOWN;
053
054        /**
055         * A DataCacheFileObject object that can point to an itnernal file or
056         * external file object
057         * 
058         */
059        public DataCacheFileObject() {
060                super();
061        }
062
063        /**
064         * @return Returns whether the file is internal or external, if nothing has
065         *         happened then it return UNKNOWN
066         */
067        public int getFileLocation() {
068                return mFileLoc;
069        }
070
071        /**
072         * @return Returns the string describing the files type
073         */
074        public String getType() {
075                return getResourceName();
076        }
077
078        /**
079         * Set the status of the file item
080         * 
081         * @param aFileLoc
082         *            the location type
083         */
084        public void setFileLocation(int aFileLoc) {
085                mFileLoc = aFileLoc;
086        }
087
088        /**
089         * This copies the external file into the cache and saves it
090         * 
091         * @param aPhysicalFileName
092         *            the physical file name to be copied
093         */
094        private void copyFileToCache(String aPhysicalFileName) {
095                File currentFile = new File(aPhysicalFileName);
096                if (currentFile == null || !currentFile.exists()) {
097                        return;
098                }
099                try {
100                        File mFile = getFile();
101                        if (mFile != null) {
102                                FileOutputStream fos = new FileOutputStream(mFile);
103                                if (fos != null) {
104                                        BufferedOutputStream bos = new BufferedOutputStream(fos);
105                                        if (bos != null) {
106                                                FileInputStream fis = new FileInputStream(currentFile);
107                                                if (fis != null) {
108                                                        BufferedInputStream bis = new BufferedInputStream(
109                                                                        fis);
110                                                        if (bis != null) {
111                                                                long filesize = currentFile.length();
112                                                                int size = 20500;
113                                                                byte[] data = new byte[size]; // 20K
114                                                                while (filesize > 0) {
115                                                                        int retSize = bis.read(data, 0, size);
116                                                                        bos.write(data, 0, retSize);
117                                                                        filesize -= retSize;
118                                                                }
119                                                                mFileLoc = INTERNAL;
120                                                                bis.close();
121                                                        }
122                                                        fis.close();
123                                                }
124                                                bos.close();
125                                        }
126                                        fos.close();
127                                }
128                        }
129                } catch (Exception e) {
130                        System.err.println(e);
131                        mFileLoc = UNKNOWN;
132                }
133
134        }
135
136        /**
137         * This is one of the hardest working methods in the class. It determines
138         * what to do the aFileName argument. If the file type is: INTENRAL then it
139         * creates a new "empty" file, and the aFileName is just text EXTERNAL then
140         * it uses aFileName as the mLocalFileName data member in order to point at
141         * the extneral file. NOTE: An extneral file is NEVER deleted by the DCM
142         * COPY_FILE_TO_CACHE then it copies the file from the extneral location
143         * identifed by the aFileName argument to an intneral cache file. Afterward
144         * it sets the mFileLoc to INTERNAL
145         * 
146         * @param aPhysicalFileName
147         *            the "physical" name of a file (this name will not be stored if
148         *            aFileLocation is COPY_FILE_TO_CACHE)
149         * @param aLogicalName
150         *            the "logical" name of a file unrelated to the physical name
151         * @param aType
152         *            a description of the file's type (optional and can be null)
153         * @param aFileLocation
154         *            the file location type (see DataCacheFileObject for values)
155         */
156        public void initializeWithFileName(String aPhysicalName,
157                        String aLogicalName, String aType, int aFileLocation) {
158                if (mFileLoc == INTERNAL) {
159                        File mFile = getFile();
160                        if (mFile != null) {
161                                mFile.delete();
162                        }
163                }
164
165                mFileLoc = aFileLocation;
166                setAbsoluteFileName(null);
167                setName(aLogicalName);
168                setResourceName(aType);
169
170                switch (mFileLoc) {
171                case INTERNAL:
172                        setBaseFileName(aLogicalName);
173                        break;
174
175                case EXTERNAL:
176                        setAbsoluteFileName(aPhysicalName);
177                        break;
178
179                case COPY_FILE_TO_CACHE:
180                        copyFileToCache(aPhysicalName);
181                        break;
182                }
183        }
184
185        // ----------------------------------------------------------------
186        // -- Overrides
187        // ----------------------------------------------------------------
188
189        /*
190         * (non-Javadoc)
191         * 
192         * @see org.ecoinformatics.seek.datasource.DataCacheObject#doWork()
193         */
194        public int doWork() {
195                return CACHE_COMPLETE;
196        }
197
198}