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.util.zip.CRC32;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.kepler.objectmanager.ObjectManager;
037import org.kepler.objectmanager.lsid.KeplerLSID;
038import org.kepler.objectmanager.lsid.LSIDGenerator;
039
040public class DataCacheManager {
041
042        private static final Log log = LogFactory.getLog(ObjectManager.class
043                        .getName());
044        private static final boolean isDebugging = log.isDebugEnabled();
045
046        private static DataCacheManager mDataCacheManager = new DataCacheManager();
047
048        
049        private CacheManager cm;
050
051        private DataCacheManager() {
052                try {
053                        cm = CacheManager.getInstance();
054                } catch (CacheException e) {
055                        log.error("Cannot create CacheManager", e);
056                }
057        }
058
059        /**
060         * Get reference to the singleton DataCacheManager object.
061         * 
062         *       */
063        public static DataCacheManager getInstance() {
064                return mDataCacheManager;
065        }
066
067        /**
068         * 
069         * @param dobj
070         */
071        public synchronized static void removeItem(DataCacheObject dobj) {
072                try {
073                        getInstance().cm.removeObject(dobj.getLSID());
074                } catch (CacheException e) {
075                        log.error("Cannot remove DataCacheObject " + dobj.toString());
076                }
077        }
078
079        /**
080         * 
081         * @param aPhysicalFileName
082         * @param aLogicalName
083         * @param aType
084         * @param aFileLocation
085         *       */
086        public synchronized static DataCacheFileObject putFile(
087                        String aPhysicalFileName, String aLogicalName, String aType,
088                        int aFileLocation) {
089                try {
090                        return mDataCacheManager.putFileInternal(aPhysicalFileName,
091                                        aLogicalName, aType, aFileLocation);
092                } catch (Exception e) {
093                        log.error("Cannot complete putFile", e);
094                }
095                return null;
096        }
097
098        private DataCacheFileObject putFileInternal(String aPhysicalFileName,
099                        String aLogicalName, String aType, int aFileLocation)
100                        throws Exception {
101
102                String magicString = aLogicalName + "|" + aType;
103                KeplerLSID lsid = getDataLSID(magicString);
104                if (isDebugging) log.debug(lsid.toString());
105                DataCacheFileObject dfo = new DataCacheFileObject();
106                dfo.initializeWithFileName(aPhysicalFileName, aLogicalName, aType,
107                                aFileLocation);
108                dfo.setLSID(lsid);
109                cm.insertObject(dfo);
110
111                return dfo;
112        }
113
114        /**
115         * 
116         * Returns NULL if the object is not in the cache.
117         * 
118         * @param aName
119         *            Name of file to retrieve from cache.
120         * @return The DataCacheFileObject or null if not found in cache.
121         */
122        public synchronized static DataCacheFileObject getFile(String aLogicalName,
123                        String aType) {
124                try {
125                        return mDataCacheManager.getFileInternal(aLogicalName, aType);
126                } catch (Exception e) {
127                        log.error("Cannot complete getFile", e);
128                }
129                return null;
130        }
131
132        private DataCacheFileObject getFileInternal(String aName, String aResource)
133                        throws Exception {
134                if (isDebugging) log.debug("getFileInternal("+aName+", "+aResource+")");
135
136                //String magicString = aName + "|" + aResource;
137
138                KeplerLSID lsid = LSIDGenerator.getInstance().getNewLSID();
139                if (isDebugging) log.debug( lsid.toString() );
140                DataCacheFileObject item = (DataCacheFileObject) cm.getObject(lsid);
141                return item;
142        }
143
144        /**
145         * 
146         * @param aListener
147         * @param aName
148         * @param aResourceName
149         * @param aClassName
150         *       */
151        public synchronized static DataCacheObject getCacheItem(
152                        DataCacheListener aListener, String aName, String aResourceName,
153                        String aClassName) {
154                return getCacheItem(aListener, aName, null, aResourceName, aClassName);
155        }
156        
157        /**
158         * 
159         * @param aListener
160         * @param aName
161         * @param aIdentifier
162         * @param aResourceName
163         * @param aClassName
164         *       */
165        public synchronized static DataCacheObject getCacheItem(
166                        DataCacheListener aListener, String aName, String aIdentifier, String aResourceName,
167                        String aClassName) {
168
169                try {
170                        return mDataCacheManager.getCacheItemInternal(aListener, aName, aIdentifier,
171                                        aResourceName, aClassName);
172                } catch (Exception e) {
173                        log.error("Error with Cache");
174                }
175                return null;
176        }
177        
178        /**
179         * Return a temporary local LSID based on a string.
180         * @param magicstring
181         *       * @throws Exception
182         */
183        private KeplerLSID getDataLSID(String magicstring) throws Exception {
184                CRC32 c = new CRC32();
185                c.update(magicstring.getBytes());
186                String hexValue = Long.toHexString(c.getValue());
187                KeplerLSID lsid = new KeplerLSID("localdata",hexValue,0L,0L);
188                return lsid;
189        }
190
191        private DataCacheObject getCacheItemInternal(DataCacheListener aListener,
192                        String aName, String aIdentifier, String aResourceName, String aClassName)
193                        throws Exception {
194                if (isDebugging) log.debug("getCacheItemInternal("+aListener+", "+aName+", "+aResourceName+", "+aClassName+")");
195
196                String magicString = aName + "|" + aResourceName;
197                if (aIdentifier != null) {
198                        magicString = aName + "|" + aIdentifier + "|" + aResourceName;
199                }
200                KeplerLSID lsid = getDataLSID(magicString);
201                if (isDebugging) log.debug( lsid.toString() );
202
203                DataCacheObject dobj = (DataCacheObject) cm.getObject(lsid);
204                if (dobj != null) {
205                        dobj.addListener(aListener);
206                        return dobj;
207                }
208
209                // dboj == null means it was not in the cache. We create a new one and
210                // register it.
211
212                try {
213                        Class clazz = Class.forName(aClassName);
214                        dobj = (DataCacheObject) clazz.newInstance();
215                        dobj.setLSID(lsid);
216                        dobj.setName(aName);
217                        dobj.addListener(aListener);
218                        dobj.setName(aName);
219                        dobj.setResourceName(aResourceName);
220                        dobj.setBaseFileName(lsid.createFilename());
221                        cm.insertObject(dobj);
222
223                        return dobj;
224                } catch (Exception e1) {
225                        log.error("Unable to create new object", e1);
226                }
227
228                return null;
229        }
230
231}