001    package org.apache.fulcrum.cache;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.Serializable;
023    
024    /**
025     * Wrapper for an object you want to store in a cache for a period of time.
026     * 
027     * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
028     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
029     * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
030     * @version $Id: CachedObject.java 670326 2008-06-22 09:31:47Z tv $
031     */
032    
033    public class CachedObject implements Serializable
034    {
035        /*
036         * TODO: The old Turbine version you could set the default age from Turbine.
037         * What we need is a CachedObjectFactory that would generate CachedObject's
038         * that could then have their default age set.
039         */
040    
041        /**
042         * Serialization key
043         */
044        private static final long serialVersionUID = -9107764093769042092L;
045    
046        /** Cache the object with the Default TTL */
047        public static final int DEFAULT = 0;
048    
049        /** Do not expire the object */
050        public static final int FOREVER = -1;
051    
052        /** The object to be cached. */
053        private Object contents = null;
054    
055        /** Default age (30 minutes). */
056        private long defaultage = 1800000;
057    
058        /** When created. * */
059        protected long created = 0;
060    
061        /** When it expires. * */
062        private long expires = 0;
063    
064        /** Is this object stale/expired? */
065        private boolean stale = false;
066    
067        /**
068         * Constructor; sets the object to expire in the default time (30 minutes).
069         * 
070         * @param o
071         *            The object you want to cache.
072         */
073        public CachedObject(Object o)
074        {
075            this.contents = o;
076            this.expires = this.defaultage;
077            this.created = System.currentTimeMillis();
078        }
079    
080        /**
081         * Constructor.
082         * 
083         * @param o
084         *            The object to cache.
085         * @param expires
086         *            How long before the object expires, in ms, e.g. 1000 = 1
087         *            second.
088         */
089        public CachedObject(Object o, long expires)
090        {
091            if (expires == DEFAULT)
092            {
093                this.expires = this.defaultage;
094            }
095    
096            this.contents = o;
097            this.expires = expires;
098            this.created = System.currentTimeMillis();
099        }
100    
101        /**
102         * Returns the cached object.
103         * 
104         * @return The cached object.
105         */
106        public Object getContents()
107        {
108            return this.contents;
109        }
110    
111        /**
112         * Returns the creation time for the object.
113         * 
114         * @return When the object was created.
115         */
116        public long getCreated()
117        {
118            return this.created;
119        }
120    
121        /**
122         * Returns the expiration time for the object.
123         * 
124         * @return When the object expires.
125         */
126        public long getExpires()
127        {
128            return this.expires;
129        }
130    
131        /**
132         * Set the expiration interval for the object.
133         * 
134         * @param expires
135         *            Expiration interval in millis ( 1 second = 1000 millis)
136         */
137        public void setExpires(long expires)
138        {
139            if (expires == DEFAULT)
140            {
141                this.expires = this.defaultage;
142            }
143            else
144            {
145                this.expires = expires;
146            }
147            if (expires == FOREVER)
148            {
149                setStale(false);
150            }
151            else
152            {
153                setStale((System.currentTimeMillis() - this.created) > expires);
154            }
155        }
156    
157        /**
158         * Set the stale status for the object.
159         * 
160         * @param stale
161         *            Whether the object is stale or not.
162         */
163        public synchronized void setStale(boolean stale)
164        {
165            this.stale = stale;
166        }
167    
168        /**
169         * Get the stale status for the object.
170         * 
171         * @return Whether the object is stale or not.
172         */
173        public synchronized boolean getStale()
174        {
175            return this.stale;
176        }
177    
178        /**
179         * Is the object stale?
180         * 
181         * @return True if the object is stale.
182         */
183        public synchronized boolean isStale()
184        {
185            if (this.expires == FOREVER)
186            {
187                return false;
188            }
189    
190            setStale((System.currentTimeMillis() - this.created) > this.expires);
191            return getStale();
192        }
193    }