KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > cache > CacheImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.cache;
26
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.Pull;
31 import org.objectweb.dream.PullException;
32 import org.objectweb.dream.message.Message;
33 import org.objectweb.dream.message.manager.MessageManager;
34 import org.objectweb.dream.time.GetTimeStamp;
35 import org.objectweb.fractal.api.NoSuchInterfaceException;
36 import org.objectweb.fractal.api.control.IllegalBindingException;
37 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
38
39 /**
40  * This class defines a basic cache. A cached message is considered as
41  * up-to-date as long as it has not been cached more than <code>timeout</code>
42  * milliseconds.
43  * <p>
44  * <code>timeout</code> can be changed using the
45  * <code>CacheAttributeController</code> controller.
46  */

47 public class CacheImpl extends AbstractComponent
48     implements
49       Pull,
50       CacheAttributeController
51 {
52
53   /** The cache time out. */
54   protected long timeOut = 0;
55
56   /** The message manager client interface. */
57   protected MessageManager messageManagerItf;
58
59   /** The GetTimeStamp client interface. */
60   protected GetTimeStamp getTimeStamp;
61
62   /** The Pull client interface. */
63   protected Pull inPull;
64
65   /** The last cached message. */
66   protected Message cachedMessage;
67
68   /** The last time stamp. */
69   protected long lastTimeStamp;
70
71   // -------------------------------------------------------------------------
72
// Implementation of the Pull interface
73
// -------------------------------------------------------------------------
74

75   /**
76    * @see Pull#pull(Map)
77    */

78   public Message pull(Map JavaDoc context) throws PullException
79   {
80     long currentTimeStamp = getTimeStamp.getTimeStamp();
81     if (currentTimeStamp - lastTimeStamp > timeOut)
82     {
83       lastTimeStamp = currentTimeStamp;
84       messageManagerItf.deleteMessage(cachedMessage);
85       cachedMessage = inPull.pull(context);
86     }
87     return messageManagerItf.duplicateMessage(cachedMessage, true);
88   }
89
90   // -------------------------------------------------------------------------
91
// Implementation of the CacheAttributeController interface
92
// -------------------------------------------------------------------------
93

94   /**
95    * @see org.objectweb.dream.cache.CacheAttributeController#setTimeOut(long)
96    */

97   public void setTimeOut(long timeOut)
98   {
99     this.timeOut = timeOut;
100   }
101
102   /**
103    * @see org.objectweb.dream.cache.CacheAttributeController#getTimeOut()
104    */

105   public long getTimeOut()
106   {
107     return timeOut;
108   }
109
110   // -------------------------------------------------------------------------
111
// Implementation of the BindingController interface
112
// -------------------------------------------------------------------------
113

114   /**
115    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
116    * Object)
117    */

118   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
119       throws NoSuchInterfaceException, IllegalBindingException,
120       IllegalLifeCycleException
121   {
122     super.bindFc(clientItfName, serverItf);
123     if (clientItfName.equals(Pull.IN_PULL_ITF_NAME))
124     {
125       inPull = (Pull) serverItf;
126     }
127     else if (clientItfName.equals(GetTimeStamp.ITF_NAME))
128     {
129       getTimeStamp = (GetTimeStamp) serverItf;
130     }
131     else if (clientItfName.equals(MessageManager.ITF_NAME))
132     {
133       messageManagerItf = (MessageManager) serverItf;
134     }
135   }
136
137   /**
138    * @see org.objectweb.fractal.api.control.BindingController#listFc()
139    */

140   public String JavaDoc[] listFc()
141   {
142     return new String JavaDoc[]{Pull.IN_PULL_ITF_NAME, MessageManager.ITF_NAME,
143         GetTimeStamp.ITF_NAME};
144   }
145
146 }
Popular Tags