View Javadoc

1   package org.apache.jcs.engine;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  import java.io.Serializable;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  
30  import org.apache.jcs.engine.behavior.IElementAttributes;
31  import org.apache.jcs.engine.control.event.behavior.IElementEventHandler;
32  
33  /***
34   * This it the element attribute descriptor class. Each element in the cache has
35   * an ElementAttribute object associated with it. An ElementAttributes object
36   * can be associated with an element in 3 ways:
37   * <ol>
38   * <li>When the item is put into the cache, you can associate an element
39   * attributes object.</li>
40   * <li>If not attributes object is include when the element is put into the
41   * cache, then the default attributes for the region will be used.</li>
42   * <li>The element attributes can be reset. This effectively results in a
43   * retrieval followed by a put. Hence, this is the same as 1.</li>
44   * </ol>
45   * @version $Id: ILateralCacheTCPListener.java,v 1.2 2002/01/18 22:08:26
46   */
47  public class ElementAttributes
48      implements IElementAttributes, Serializable, Cloneable
49  {
50      private static final long serialVersionUID = 7814990748035017441L;
51  
52      /***
53       * Can this item be flushed to disk
54       */
55      public boolean IS_SPOOL = true;
56  
57      /***
58       * Is this item laterally distributable
59       */
60      public boolean IS_LATERAL = true;
61  
62      /***
63       * Can this item be sent to the remote cache
64       */
65      public boolean IS_REMOTE = true;
66  
67      /***
68       * You can turn off expiration by setting this to true. This causes the
69       * cache to bypass both max life and idle time expiration.
70       */
71      public boolean IS_ETERNAL = true;
72  
73      /***
74       * The object version. This is currently not used.
75       */
76      public long version = 0;
77  
78      /***
79       * Max life seconds
80       */
81      public long maxLifeSeconds = -1;
82  
83      /***
84       * The maximum time an entry can be idle. Setting this to -1 causes the idle
85       * time check to be ignored.
86       */
87      public long maxIdleTimeSeconds = -1;
88  
89      /***
90       * The byte size of the field. Must be manually set.
91       */
92      public int size = 0;
93  
94      /***
95       * The creation time. This is used to enforce the max life.
96       */
97      public long createTime = 0;
98  
99      /***
100      * The last access time. This is used to enforce the max idel time.
101      */
102     public long lastAccessTime = 0;
103 
104     /***
105      * The list of Event handlers to use. This is transient, since the event
106      * handlers cannot usually be serialized. This means that you cannot attach
107      * a post serialization event to an item.
108      * <p>
109      * TODO we need to check that when an item is passed to a non-local cache
110      * that if the local cache had a copy with event handlers, that those
111      * handlers are used.
112      */
113     public transient ArrayList eventHandlers;
114 
115     /***
116      * Constructor for the IElementAttributes object
117      */
118     public ElementAttributes()
119     {
120         this.createTime = System.currentTimeMillis();
121         this.lastAccessTime = this.createTime;
122     }
123 
124     /***
125      * Constructor for the IElementAttributes object
126      * <p>
127      * @param attr
128      */
129     protected ElementAttributes( ElementAttributes attr )
130     {
131         IS_ETERNAL = attr.IS_ETERNAL;
132 
133         // waterfal onto disk, for pure disk set memory to 0
134         IS_SPOOL = attr.IS_SPOOL;
135 
136         // lateral
137         IS_LATERAL = attr.IS_LATERAL;
138 
139         // central rmi store
140         IS_REMOTE = attr.IS_REMOTE;
141 
142         maxLifeSeconds = attr.maxLifeSeconds;
143         // timetolive
144         maxIdleTimeSeconds = attr.maxIdleTimeSeconds;
145         size = attr.size;
146     }
147 
148     /***
149      * Copies the attributes, including references to event handlers.
150      * <p>
151      * @return a copy of the Attributes
152      */
153     public IElementAttributes copy()
154     {
155         try
156         {
157             // need to make this more efficient. Just want to insure
158             // a proper copy
159             ElementAttributes attr = new ElementAttributes();
160             attr.setIdleTime( this.getIdleTime() );
161             attr.setIsEternal( this.getIsEternal() );
162             attr.setIsLateral( this.getIsLateral() );
163             attr.setIsRemote( this.getIsRemote() );
164             attr.setIsSpool( this.getIsSpool() );
165             attr.setMaxLifeSeconds( this.getMaxLifeSeconds() );
166             attr.addElementEventHandlers( this.eventHandlers );
167             return attr;
168         }
169         catch ( Exception e )
170         {
171             return new ElementAttributes();
172         }
173     }
174 
175     /***
176      * Deep clone the attributes.
177      * <p>
178      * @return a clone of these attributes
179      */
180     public Object clone2()
181     {
182         try
183         {
184             ByteArrayOutputStream baos = new ByteArrayOutputStream( 100 );
185             ObjectOutputStream oos = new ObjectOutputStream( baos );
186             oos.writeObject( this );
187             byte buf[] = baos.toByteArray();
188             oos.close();
189 
190             // deserialize byte array into ArrayList
191 
192             ByteArrayInputStream bais = new ByteArrayInputStream( buf );
193             ObjectInputStream ois = new ObjectInputStream( bais );
194             ElementAttributes attr = (ElementAttributes) ois.readObject();
195             ois.close();
196 
197             attr.createTime = System.currentTimeMillis();
198             return attr;
199         }
200         catch ( Exception e )
201         {
202             // swallow
203         }
204         return null;
205     }
206 
207     /*
208      * (non-Javadoc)
209      * @see org.apache.jcs.engine.behavior.IElementAttributes#setVersion(long)
210      */
211     public void setVersion( long version )
212     {
213         this.version = version;
214     }
215 
216     /*
217      * (non-Javadoc)
218      * @see org.apache.jcs.engine.behavior.IElementAttributes#setMaxLifeSeconds(long)
219      */
220     public void setMaxLifeSeconds( long mls )
221     {
222         this.maxLifeSeconds = mls;
223     }
224 
225     /*
226      * (non-Javadoc)
227      * @see org.apache.jcs.engine.behavior.IElementAttributes#getMaxLifeSeconds()
228      */
229     public long getMaxLifeSeconds()
230     {
231         return this.maxLifeSeconds;
232     }
233 
234     /*
235      * (non-Javadoc)
236      * @see org.apache.jcs.engine.behavior.IElementAttributes#setIdleTime(long)
237      */
238     public void setIdleTime( long idle )
239     {
240         this.maxIdleTimeSeconds = idle;
241     }
242 
243     /*
244      * (non-Javadoc)
245      * @see org.apache.jcs.engine.behavior.IElementAttributes#setSize(int)
246      */
247     public void setSize( int size )
248     {
249         this.size = size;
250     }
251 
252     /*
253      * (non-Javadoc)
254      * @see org.apache.jcs.engine.behavior.IElementAttributes#getSize()
255      */
256     public int getSize()
257     {
258         return size;
259     }
260 
261     /*
262      * (non-Javadoc)
263      * @see org.apache.jcs.engine.behavior.IElementAttributes#getCreateTime()
264      */
265     public long getCreateTime()
266     {
267         return createTime;
268     }
269 
270     /***
271      * Sets the createTime attribute of the IElementAttributes object
272      */
273     public void setCreateTime()
274     {
275         createTime = System.currentTimeMillis();
276     }
277 
278     /*
279      * (non-Javadoc)
280      * @see org.apache.jcs.engine.behavior.IElementAttributes#getVersion()
281      */
282     public long getVersion()
283     {
284         return version;
285     }
286 
287     /*
288      * (non-Javadoc)
289      * @see org.apache.jcs.engine.behavior.IElementAttributes#getIdleTime()
290      */
291     public long getIdleTime()
292     {
293         return this.maxIdleTimeSeconds;
294     }
295 
296     /*
297      * (non-Javadoc)
298      * @see org.apache.jcs.engine.behavior.IElementAttributes#getTimeToLiveSeconds()
299      */
300     public long getTimeToLiveSeconds()
301     {
302         long now = System.currentTimeMillis();
303         return ( ( this.getCreateTime() + ( this.getMaxLifeSeconds() * 1000 ) ) - now ) / 1000;
304     }
305 
306     /*
307      * (non-Javadoc)
308      * @see org.apache.jcs.engine.behavior.IElementAttributes#getLastAccessTime()
309      */
310     public long getLastAccessTime()
311     {
312         return this.lastAccessTime;
313     }
314 
315     /*
316      * (non-Javadoc)
317      * @see org.apache.jcs.engine.behavior.IElementAttributes#setLastAccessTimeNow()
318      */
319     public void setLastAccessTimeNow()
320     {
321         this.lastAccessTime = System.currentTimeMillis();
322     }
323 
324     /*
325      * (non-Javadoc)
326      * @see org.apache.jcs.engine.behavior.IElementAttributes#getIsSpool()
327      */
328     public boolean getIsSpool()
329     {
330         return this.IS_SPOOL;
331     }
332 
333     /*
334      * (non-Javadoc)
335      * @see org.apache.jcs.engine.behavior.IElementAttributes#setIsSpool(boolean)
336      */
337     public void setIsSpool( boolean val )
338     {
339         this.IS_SPOOL = val;
340     }
341 
342     /*
343      * (non-Javadoc)
344      * @see org.apache.jcs.engine.behavior.IElementAttributes#getIsLateral()
345      */
346     public boolean getIsLateral()
347     {
348         return this.IS_LATERAL;
349     }
350 
351     /*
352      * (non-Javadoc)
353      * @see org.apache.jcs.engine.behavior.IElementAttributes#setIsLateral(boolean)
354      */
355     public void setIsLateral( boolean val )
356     {
357         this.IS_LATERAL = val;
358     }
359 
360     /***
361      * Can this item be sent to the remote cache
362      * @return true if the item can be sent to a remote auxiliary
363      */
364     public boolean getIsRemote()
365     {
366         return this.IS_REMOTE;
367     }
368 
369     /***
370      * Sets the isRemote attribute of the ElementAttributes object
371      * @param val
372      *            The new isRemote value
373      */
374     public void setIsRemote( boolean val )
375     {
376         this.IS_REMOTE = val;
377     }
378 
379     /***
380      * You can turn off expiration by setting this to true. The max life value
381      * will be ignored.
382      * <p>
383      * @return true if the item cannot expire.
384      */
385     public boolean getIsEternal()
386     {
387         return this.IS_ETERNAL;
388     }
389 
390     /***
391      * Sets the isEternal attribute of the ElementAttributes object. True means
392      * that the item should never expire. If can still be removed if it is the
393      * least recently used, and you are using the LRUMemory cache. it just will
394      * not be filtered for expiration by the cache hub.
395      * <p>
396      * @param val
397      *            The new isEternal value
398      */
399     public void setIsEternal( boolean val )
400     {
401         this.IS_ETERNAL = val;
402     }
403 
404     /***
405      * Adds a ElementEventHandler. Handler's can be registered for multiple
406      * events. A registered handler will be called at every recognized event.
407      * <p>
408      * The alternative would be to register handlers for each event. Or maybe
409      * The handler interface should have a method to return whether it cares
410      * about certain events.
411      * <p>
412      * @param eventHandler
413      *            The ElementEventHandler to be added to the list.
414      */
415     public void addElementEventHandler( IElementEventHandler eventHandler )
416     {
417         // lazy here, no concurrency problems expected
418         if ( this.eventHandlers == null )
419         {
420             this.eventHandlers = new ArrayList();
421         }
422         this.eventHandlers.add( eventHandler );
423     }
424 
425     /***
426      * Sets the eventHandlers of the IElementAttributes object.
427      * <p>
428      * This add the references to the local list. Subsequent changes in the
429      * caller's list will not be reflected.
430      * <p>
431      * @param eventHandlers
432      *            List of IElementEventHandler objects
433      */
434     public void addElementEventHandlers( ArrayList eventHandlers )
435     {
436         if ( eventHandlers == null )
437         {
438             return;
439         }
440 
441         for ( Iterator iter = eventHandlers.iterator(); iter.hasNext(); )
442         {
443             addElementEventHandler( (IElementEventHandler) iter.next() );
444         }
445     }
446 
447     /***
448      * Gets the elementEventHandlers. Returns null if none exist. Makes checking
449      * easy.
450      * <p>
451      * @return The elementEventHandlers List of IElementEventHandler objects
452      */
453     public ArrayList getElementEventHandlers()
454     {
455         return this.eventHandlers;
456     }
457 
458     /***
459      * For logging and debugging the element IElementAttributes.
460      * <p>
461      * @return String info about the values.
462      */
463     public String toString()
464     {
465         StringBuffer dump = new StringBuffer();
466 
467         dump.append( "[ IS_LATERAL = " ).append( IS_LATERAL );
468         dump.append( ", IS_SPOOL = " ).append( IS_SPOOL );
469         dump.append( ", IS_REMOTE = " ).append( IS_REMOTE );
470         dump.append( ", IS_ETERNAL = " ).append( IS_ETERNAL );
471         dump.append( ", MaxLifeSeconds = " ).append( this.getMaxLifeSeconds() );
472         dump.append( ", IdleTime = " ).append( this.getIdleTime() );
473         dump.append( ", CreateTime = " ).append( this.getCreateTime() );
474         dump.append( ", LastAccessTime = " ).append( this.getLastAccessTime() );
475         dump.append( ", getTimeToLiveSeconds() = " ).append( String.valueOf( getTimeToLiveSeconds() ) );
476         dump.append( ", createTime = " ).append( String.valueOf( createTime ) ).append( " ]" );
477 
478         return dump.toString();
479     }
480 }