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.File;
033import java.io.FileInputStream;
034import java.io.FileOutputStream;
035import java.io.IOException;
036import java.io.InputStream;
037import java.io.ObjectInput;
038import java.io.ObjectOutput;
039
040import org.kepler.objectmanager.lsid.KeplerLSID;
041
042/**
043 * Class that represents an object in the CacheManager. This class should be
044 * extended by each type of object that wants to control its own lifecycle
045 * events and serialization events.
046 */
047public class RawDataCacheObject extends CacheObject {
048        private File datafile;
049
050        public RawDataCacheObject() {
051                super();
052                datafile = null;
053        }
054
055        /**
056         * construct a new CacheObject
057         */
058        public RawDataCacheObject(String name, KeplerLSID lsid) {
059                super(name, lsid);
060                datafile = new File(
061                                CacheManager.cachePath + File.separator + "RawData", lsid
062                                                .createFilename());
063        }
064
065        /**
066         * construct a new CacheObject with a given filename.
067         */
068        RawDataCacheObject(String name, KeplerLSID lsid, String filename) {
069                super(name, lsid);
070                datafile = new File(filename);
071        }
072
073        /**
074         * This returns a file object pointing to the data file. You'll need to cast
075         * the object to a File to use it.
076         */
077        public Object getObject() {
078                return datafile;
079        }
080
081        /**
082         * set the data file that is associated with this RawDataCacheObject
083         */
084        public void setData(InputStream is) throws IOException {
085                FileOutputStream fos = new FileOutputStream(datafile);
086                CacheUtil.writeInputStreamToOutputStream(is, fos);
087        }
088
089        /**
090         * return the data as a stream
091         */
092        public InputStream getDataAsStream() throws CacheException {
093                try {
094                        FileInputStream fis = new FileInputStream(datafile);
095                        return fis;
096                } catch (Exception e) {
097                        throw new CacheException("Could not get the data stream for "
098                                        + "RawDataCacheObject " + getLSID().toString() + " : "
099                                        + e.getMessage());
100                }
101        }
102
103        /**
104         * call back for when this object is added to the cache
105         */
106        public void objectAdded() {
107                // System.out.println("object " + lsid.toString() + " added");
108        }
109
110        /**
111         * call back for when this object is removed by the user
112         */
113        public void objectRemoved() {
114                // System.out.println("object " + lsid.toString() + " removed");
115        }
116
117        /**
118         * call back for when this object is purged by CacheManager
119         */
120        public void objectPurged() {
121                // System.out.println("object " + lsid.toString() + " purged");
122        }
123
124        /**
125         * deserialize this object
126         */
127        public void readExternal(ObjectInput in) throws IOException,
128                        ClassNotFoundException {
129                StringBuffer sb = new StringBuffer();
130                byte[] b = new byte[1024];
131                int numread = in.read(b, 0, 1024);
132                while (numread != -1) {
133                        sb.append(new String(b, 0, numread));
134                        numread = in.read(b, 0, 1024);
135                }
136                String serialStr = sb.toString();
137                _name = serialStr.substring(serialStr.indexOf("<name>") + 6,
138                                serialStr.indexOf("</name>"));
139                try {
140                        _lsid = new KeplerLSID(serialStr.substring(serialStr
141                                        .indexOf("<lsid>") + 6, serialStr.indexOf("</lsid>")));
142                } catch (Exception e) {
143                        throw new IOException("Could not create kepler lsid: "
144                                        + e.getMessage());
145                }
146                this.datafile = new File(serialStr.substring(serialStr
147                                .indexOf("<filename>") + 10, serialStr.indexOf("</filename>")));
148        }
149
150        /**
151         * serialize this object
152         */
153        public void writeExternal(ObjectOutput out) throws IOException {
154                String serialStr = "<name>" + getName() + "</name><lsid>" + _lsid.toString()
155                                + "</lsid><filename>" + datafile.getAbsolutePath()
156                                + "</filename>";
157                byte[] b = serialStr.getBytes();
158                out.write(b, 0, b.length);
159                out.flush();
160        }
161}