001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: riddle $'
006 * '$Date: 2010-06-28 19:16:46 +0000 (Mon, 28 Jun 2010) $' 
007 * '$Revision: 25036 $'
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.Serializable;
033import java.util.Hashtable;
034import java.util.Set;
035import java.util.Vector;
036
037import org.apache.commons.logging.Log;
038import org.apache.commons.logging.LogFactory;
039import org.kepler.objectmanager.lsid.KeplerLSID;
040
041/**
042 * Class that represents an object in the ObjectCache. This class should be
043 * extended by each type of object that wants to control its own lifecycle
044 * events and serialization events.
045 */
046public abstract class CacheObject implements CacheObjectInterface, Serializable {
047
048        private static final long serialVersionUID = -3774217914823785296L;
049        private static final Log log = LogFactory.getLog(CacheObject.class.getName());
050        private static final boolean isDebugging = log.isDebugEnabled();
051
052        protected transient String _name;
053        protected transient KeplerLSID _lsid;
054        protected Vector<String> _semanticTypes =  new Vector<String>();
055        protected Hashtable<String,String> _attributes = new Hashtable<String,String>();
056
057        /**
058         * default constructor. if you use this constrctor, you'll need to set the
059         * name and lsid through the setLSID and setName methods.
060         */
061        protected CacheObject() {
062                // default constructor;
063                _lsid = null;
064                _name = null;
065        }
066
067        /**
068         * construct a new CacheObject
069         */
070        public CacheObject(String name, KeplerLSID lsid) {
071                _name = name;
072                _lsid = lsid;
073        }
074
075        /**
076         * get the name of this object
077         */
078        public String getName() {
079                return _name;
080        }
081
082        /**
083         * get the lsid for this object
084         */
085        public KeplerLSID getLSID() {
086                return _lsid;
087        }
088
089        /**
090         * set the lsid
091         */
092        public void setLSID(KeplerLSID lsid) {
093                _lsid = lsid;
094        }
095
096        /**
097         * set the name
098         */
099        public void setName(String name) {
100                _name = name;
101        }
102
103        /**
104         * this returns the semantic types vector
105         * 
106         *@return The semanticTypes value
107         */
108        public void setSemanticTypes(Vector<String> semTypes) {
109                _semanticTypes = semTypes;
110        }
111
112        /**
113         * this returns the semantic types vector
114         * 
115         *@return The semanticTypes value
116         */
117        public Vector<String> getSemanticTypes() {
118                return _semanticTypes;
119        }
120
121        /**
122         * set a user configured attribute on the CacheObject
123         */
124        public void addAttribute(String name, String value) {
125                if (isDebugging) log.debug("addAttribute("+name+","+value+")");
126                _attributes.put(name, value);
127        }
128
129        /**
130         * get the attribute with the specified name
131         */
132        public String getAttribute(String name) {
133                if (isDebugging) log.debug("getAttribute("+name+")");
134                return (String) _attributes.get(name);
135        }
136        
137        public Set<String> getAttributeNames() {
138                return _attributes.keySet();
139        }
140
141        /**
142         * remove the attribute with the given name and return it.
143         */
144        public void removeAttribute(String name) {
145                if (isDebugging) log.debug("removeAttribute("+name+")");
146                _attributes.remove(name);
147        }
148
149        /**
150         * return the java object associated with this CacheObject
151         */
152        public abstract Object getObject();
153
154        /**
155         * call back for when this object is added to the cache. This method should
156         * be overridden by derviced classes if needed. Default action is to do
157         * nothing.
158         */
159        public void objectAdded() {
160        }
161
162        /**
163         * call back for when this object is removed by the user. This method should
164         * be overridden by derived classes if needed. Default action is to do
165         * nothing.
166         */
167        public void objectRemoved() {
168        }
169
170        /**
171         * call back for when this object is purged by ObjectCache. This method
172         * should be overridden by derived classes if needed. Default action is to
173         * do nothing.
174         */
175        public void objectPurged() {
176        }
177
178}