KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > cache > DistributedCachePeer


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/O�a, 107 1�2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.cache;
34
35 import java.rmi.RemoteException JavaDoc;
36
37 import java.io.ByteArrayOutputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 import java.util.Date JavaDoc;
41 import java.util.Properties JavaDoc;
42 import java.util.Iterator JavaDoc;
43
44
45 import java.net.URL JavaDoc;
46 import java.net.MalformedURLException JavaDoc;
47
48 import com.knowgate.debug.DebugFile;
49 import com.knowgate.misc.Environment;
50 import com.knowgate.dataobjs.DBSubset;
51
52 /**
53  * <p>Distributed Cache Local Peer</p>
54  * <p>Each distributed cache peer holds its own local copy of cached data.</p>
55  * <p>On the simplest scenenario there is only one client cache peer witch
56  * stores data localy for faster access and reduce network bandwitch consumption.</p>
57  * <p>As data is kept localy at each peer, when more than one client peer concurrently
58  * access the same data, a cache coordinator becomes necessary.</p>
59  * <p>The cache coordinator is an EJB that must be installed at an application server
60  * such as JBoss or BEA Weblogic. See cache.DistributedCacheCoordinatorBean for more
61  * information about the cache coordinator.</p>
62  * <br>
63  * <p><b>Distributed Cache Tokens and Policies</b></p>
64  * <p>A cache peer is essentially a named set of objects. Each object name is called
65  * a "cache token". Cache Token associate a String (the object name) with the actual cached object.</p>
66  * <p>Token have an usage count and a last usage date, each time a token is requested its usage count
67  * and last usage dates are updated at the cache peer.</p>
68  * <p>The cache peer the applies a customizable Policy for discarding objects as cache becomes full.</p>
69  * <p>Currently only a Least Recently Used Cache Policy is provided.</p>
70  * <p>By default the cache has a maximum of 400 objects slots.</p>
71  * <p>There is no checking of memory consumption for the cache peer, it is the programmer's responsability
72  * not to cache objects that are too large.</p>
73  * <p>It is also the programmer's task to remove tokens from the cache peer when the cached data has been changed.</p>
74  * <p><b>Comunnication between client cache peers and the cache coordinator</b></p>
75  * <p>The cache coordinator is a single object instance that coordinates data cached by multiple cache peers,
76  * at a given time a cache peer may change data that is already cache at another peer. When doing so the last usage date
77  * for the token of cached data will be updated and the cache coordinator will be notified of this last usage change.</p>
78  * <p>Each cache peer holds its own copy of data, and the cache coordinator keeps a record of all
79  * last usage timestamp for every object at every cache peer. In this way, cached data is not be shared among peers,
80  * but it is kept synchronized by discarding all tokens witch timestamp at the peer is older than the one at the cache coordinator.</p>
81  * <p><b>UML</b></p>
82  * <img SRC="doc-files/DistributedCache-1.gif">
83  * @author Sergio Montoro Ten
84  * @version 1.1
85  * <br>
86  */

87
88 public final class DistributedCachePeer {
89
90   private Object JavaDoc oCtx; // javax.naming.Context
91
private Object JavaDoc oDCC; // cache.DistributedCacheCoordinator
92
private Object JavaDoc oHome; // cache.DistributedCacheCoordinatorHome
93

94   private LRUCachePolicy oCacheStore;
95   private Properties JavaDoc oEnvProps;
96   private int iProviderProtocol;
97
98   final private int PROTOCOL_NONE = -1;
99   final private int PROTOCOL_UNKNOWN = 0;
100   final private int PROTOCOL_HTTP = 1;
101   final private int PROTOCOL_HTTPS = 2;
102   final private int PROTOCOL_JNP = 3;
103
104   /**
105    * <p>Create a local cache peer.</p>
106    * <p>The cache peer may be initialized to work in single-peer mode or in
107    * multi-peer mode with a cache coordinator.</p>
108    * <p>Initializacion properties for connecting with the cache coordinator
109    * when working in multi-peer mode are passed in appserver.cnf properties file.
110    * The appserver.cnf file is read using the singleton Environment object at
111    * com.knowgate.misc package.</p>
112    * <p>An example of a configuration file for JBoss may be as follows:</p>
113    * #DistributedCachePeer JBoss configuration file<br><br>
114    * #set this entry to "disabled" is working in single-peer mode<br>
115    * threetiers=enabled<br><br>
116    * java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory<br>
117    * java.naming.provider.url=jnp://127.1.0.0:1099/<br>
118    * java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces<br>
119    * jnp.socketFactory=org.jnp.interfaces.TimedSocketFactory<br>
120    * <p>An example of a configuration file for Tomcat may be as follows:</p>
121    * #DistributedCachePeer Tomcat configuration file<br><br>
122    * #set this entry to "disabled" is working in single-peer mode<br>
123    * threetiers=enabled<br><br>
124    * java.naming.factory.initial=<br>
125    * java.naming.provider.url=http://www.remotehost.com:1099/cache/server.jsp<br>
126    * java.naming.factory.url.pkgs=<br>
127    * jnp.socketFactory=<br>
128    * @throws InstantiationException
129    * @throws RemoteException
130    * @see Environment
131    */

132
133   public DistributedCachePeer()
134     throws InstantiationException JavaDoc,RemoteException JavaDoc {
135
136     long lServerTime, lClientTime;
137     String JavaDoc s3Tiers;
138
139     if (DebugFile.trace) {
140       DebugFile.writeln("Begin DistributedCachePeer()");
141       DebugFile.incIdent();
142     }
143
144     oCacheStore = new LRUCachePolicy(200, 400);
145
146     oEnvProps = Environment.getProfile("appserver");
147     s3Tiers = oEnvProps.getProperty("threetiers", "disabled");
148
149     /* Only test and debugging purposes ****************************************
150
151     oEnvProps = new Properties();
152     oEnvProps.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
153     oEnvProps.put("java.naming.provider.url", "jnp://server:1099/");
154     oEnvProps.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
155     oEnvProps.put("jnp.socketFactory", "org.jnp.interfaces.TimedSocketFactory");
156
157     ***************************************************************************/

158
159     if (DebugFile.trace) {
160       DebugFile.writeln ("java.naming.factory.initial=" + oEnvProps.getProperty("java.naming.factory.initial",""));
161       DebugFile.writeln ("java.naming.provider.url=" + oEnvProps.getProperty("java.naming.provider.url",""));
162       DebugFile.writeln ("java.naming.factory.url.pkgs=" + oEnvProps.getProperty("java.naming.factory.url.pkgs",""));
163       DebugFile.writeln ("jnp.socketFactory=" + oEnvProps.getProperty("jnp.socketFactory",""));
164     }
165
166     if (s3Tiers.equalsIgnoreCase("enabled") || s3Tiers.equalsIgnoreCase("yes") || s3Tiers.equalsIgnoreCase("true") || s3Tiers.equalsIgnoreCase("on") || s3Tiers.equals("1")) {
167
168       String JavaDoc sProviderURL = oEnvProps.getProperty("java.naming.provider.url","").toLowerCase();
169
170       if (sProviderURL.startsWith("http://"))
171         iProviderProtocol = PROTOCOL_HTTP;
172       else if (sProviderURL.startsWith("https://"))
173         iProviderProtocol = PROTOCOL_HTTPS;
174       else if (sProviderURL.startsWith("jnp://"))
175         iProviderProtocol = PROTOCOL_JNP;
176       else
177         iProviderProtocol = PROTOCOL_UNKNOWN;
178
179       if (PROTOCOL_HTTP!=iProviderProtocol && PROTOCOL_HTTPS!=iProviderProtocol) {
180
181         try {
182
183           if (DebugFile.trace) DebugFile.writeln ("Context oCtx = new InitialContext(Properties)");
184
185           oCtx = new javax.naming.InitialContext JavaDoc(oEnvProps);
186
187           if (DebugFile.trace)
188             DebugFile.writeln("oHome = (DistributedCacheCoordinatorHome) oCtx.lookup(\"DistributedCacheCoordinator\")");
189
190           oHome = ( (javax.naming.Context JavaDoc) oCtx).lookup("DistributedCacheCoordinator");
191
192           if (DebugFile.trace)
193             DebugFile.writeln("DistributedCacheCoordinator = DistributedCacheCoordinatorHome.create()");
194
195           oDCC = ( (com.knowgate.cache.server.DistributedCacheCoordinatorHome) oHome).create();
196
197           // Sincronizar la fecha del cliente con la del servidor
198
lServerTime = ( (com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).now();
199
200           lClientTime = new Date JavaDoc().getTime();
201
202           if (lClientTime < lServerTime) {
203             lServerTime = ( (com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).now() + 1000;
204             Environment.updateSystemTime(lServerTime);
205           } // fi (lClientTime < lServerTime)
206
}
207         catch (javax.naming.NamingException JavaDoc ne) {
208           throw new java.lang.InstantiationException JavaDoc("javax.naming.NamingException " + ne.getMessage() + " " + ne.getExplanation());
209         }
210         catch (javax.ejb.CreateException JavaDoc ce) {
211           throw new java.lang.InstantiationException JavaDoc("javax.ejb.CreateException " + ce.getMessage());
212         }
213       } // fi (iProviderProtocol)
214
else
215         oDCC = null;
216     } // fi (threetiers)
217
else {
218       iProviderProtocol = PROTOCOL_NONE;
219     }
220
221     if (DebugFile.trace) {
222       DebugFile.decIdent();
223       DebugFile.writeln("End new DistributedCachePeer()");
224     }
225
226   } // DistributedCachePeer
227

228   // ----------------------------------------------------------
229

230   private static String JavaDoc trim(String JavaDoc s) {
231     if (null==s)
232       return null;
233     else
234       return (s.replace('\n',' ').replace('\r',' ').replace('\t',' ')).trim();
235   }
236
237   // ----------------------------------------------------------
238

239   /**
240    * Get an object from the cache peer.
241    * @param sTokenKey Token of object to be retrieved
242    * @return Reference to the requested object or <b>null</b> if object is not present at the local cache or it was modified by another cache peer.
243    * @throws RemoteException
244    */

245
246   public Object JavaDoc get(String JavaDoc sTokenKey)
247     throws RemoteException JavaDoc,NullPointerException JavaDoc {
248
249     long lLastMod;
250     long lLastLocal;
251     Object JavaDoc oRetSet = null;
252
253     if (DebugFile.trace) {
254       DebugFile.writeln("Begin DistributedCachePeer.get(" + sTokenKey + ")");
255       DebugFile.incIdent();
256     }
257
258     if (PROTOCOL_NONE==iProviderProtocol)
259       // Si no existe ning�n coordinador de cache distribuido configurado,
260
// tener en cuenta s�lo el cache local.
261
oRetSet = oCacheStore.get(sTokenKey);
262     else
263     {
264       // Recuperar la entrada del cache local
265
oRetSet = oCacheStore.get(sTokenKey);
266
267       if (null!=oRetSet ) {
268         // Obtener la fecha de �ltima modificaci�n del coordinador de caches
269
if (PROTOCOL_HTTP==iProviderProtocol || PROTOCOL_HTTPS==iProviderProtocol) {
270
271           String JavaDoc sProviderURL = oEnvProps.getProperty("java.naming.provider.url");
272
273           if (null==sProviderURL)
274             throw new NullPointerException JavaDoc("Property java.naming.provider.url not set at appserver.cnf");
275
276           else {
277             String JavaDoc sLastMod = "";
278
279             try {
280               if (DebugFile.trace) DebugFile.writeln("new javax.activation.DataHandler(new URL(" + sProviderURL + "?method=get&key=" + sTokenKey + "))");
281
282               javax.activation.DataHandler JavaDoc oDataHndlr = new javax.activation.DataHandler JavaDoc(new URL JavaDoc(sProviderURL+"?method=get&key=" + sTokenKey));
283
284                ByteArrayOutputStream JavaDoc oURLBytes = new ByteArrayOutputStream JavaDoc(128);
285                oDataHndlr.writeTo(oURLBytes);
286                sLastMod = trim(oURLBytes.toString());
287                oURLBytes.close();
288                oURLBytes = null;
289
290                if (DebugFile.trace) DebugFile.writeln("lLastMod=" + sLastMod);
291
292                lLastMod = Long.parseLong(sLastMod);
293             }
294             catch (MalformedURLException JavaDoc badurl) {
295               if (DebugFile.trace) DebugFile.writeln("MalformedURLException " + sProviderURL + "?method=get&key=" + sTokenKey);
296
297               throw new RemoteException JavaDoc("MalformedURLException " + sProviderURL + "?method=get&key=" + sTokenKey);
298             }
299             catch (IOException JavaDoc badurl) {
300               if (DebugFile.trace) DebugFile.writeln("IOException " + sProviderURL + "?method=get&key=" + sTokenKey);
301
302               throw new RemoteException JavaDoc("IOException " + sProviderURL + "?method=get&key=" + sTokenKey);
303             }
304             catch (NumberFormatException JavaDoc nume) {
305               if (DebugFile.trace) DebugFile.writeln("NumberFormatException " + sLastMod);
306
307               throw new RemoteException JavaDoc("NumberFormatException " + sLastMod);
308             }
309           }
310         }
311         else {
312           if (DebugFile.trace) DebugFile.writeln("DistributedCacheCoordinator.lastModified(" + sTokenKey + ")");
313
314           lLastMod = ((com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).lastModified(sTokenKey);
315
316           if (DebugFile.trace) DebugFile.writeln("lLastMod=" + String.valueOf(lLastMod));
317         }
318
319         lLastLocal = oCacheStore.last(sTokenKey);
320
321         if (DebugFile.trace) {
322           if (lLastMod==0)
323             DebugFile.writeln(sTokenKey + " not found at distributed cache");
324           else
325             DebugFile.writeln(sTokenKey + " has timestamp " + new Date JavaDoc(lLastMod).toString() + " at distributed cache");
326
327           DebugFile.writeln(sTokenKey + " has timestamp " + new Date JavaDoc(lLastLocal).toString() + " at local cache");
328         }
329
330         if (lLastLocal>=lLastMod) {
331           // Si la fecha local es mayor o igual que la del coordinador, devolver la entrada local
332
if (DebugFile.trace) DebugFile.writeln("cache hit for " + sTokenKey);
333         }
334         else {
335           // Si la fecha local es anterior a la del coordinador, la entrada local ya no es v�lida
336
if (DebugFile.trace) DebugFile.writeln("cache outdated for " + sTokenKey);
337           oCacheStore.remove(sTokenKey);
338           oRetSet = null;
339         }
340       } // fi(oEntry)
341
else {
342         if (DebugFile.trace) DebugFile.writeln("cache miss for " + sTokenKey);
343       }
344     } // fi(oDCC)
345

346     if (DebugFile.trace) {
347       DebugFile.decIdent();
348       DebugFile.writeln("End DistributedCachePeer.get()");
349     }
350
351     return oRetSet;
352   } // getDBSubset
353

354   // ----------------------------------------------------------
355

356   /**
357    * @return Same as DistributedCachePeer.get() but cast returned object to a {@link DBSubset}.
358    * @throws ClassCastException
359    */

360   public DBSubset getDBSubset(String JavaDoc sTokenKey) throws RemoteException JavaDoc,ClassCastException JavaDoc {
361     Object JavaDoc oObj = get (sTokenKey);
362
363     if (null==oObj)
364       return null;
365     else
366       return (DBSubset) get(sTokenKey);
367   } // getDBSubset
368

369   // ----------------------------------------------------------
370

371   /**
372    * @param sTokenKey Token of object to be retrieved
373    * @return Same as DistributedCachePeer.get() but cast returned object to a String.
374    * @throws ClassCastException
375    */

376   public String JavaDoc getString(String JavaDoc sTokenKey) throws RemoteException JavaDoc,ClassCastException JavaDoc {
377     Object JavaDoc oObj = get (sTokenKey);
378     if (null==oObj)
379       return null;
380     else
381       return (String JavaDoc) oObj;
382   } // getString
383

384   // ----------------------------------------------------------
385

386   /**
387    * @param sTokenKey Token of object to be retrieved
388    * @throws ClassCastException
389    */

390   public Boolean JavaDoc getBoolean(String JavaDoc sTokenKey) throws RemoteException JavaDoc, ClassCastException JavaDoc {
391     Object JavaDoc oObj = get (sTokenKey);
392     if (null==oObj)
393       return null;
394     else
395       return (Boolean JavaDoc) oObj;
396   } // getBoolean
397

398   // ----------------------------------------------------------
399

400   /**
401    * Return keys for entries in cache
402    * @return Set of keys (Strings)
403    */

404
405   public java.util.Set JavaDoc keySet() {
406
407     return oCacheStore.keySet();
408   }
409
410   // ----------------------------------------------------------
411

412   /**
413    * <p>Puts an Object into local cache.</p>
414    * @param sTokenKey Token for object
415    * @param oObj Object to be stored.
416    * @throws RemoteException
417    * @throws IllegalArgumentException If either sTokenKey or oObj is <b>null</b>.
418    * @throws IllegalStateException If object with given token is already present at local cache.
419    */

420   public void put(String JavaDoc sTokenKey, Object JavaDoc oObj)
421     throws IllegalStateException JavaDoc, IllegalArgumentException JavaDoc, RemoteException JavaDoc {
422     long lDtServerModified;
423
424     if (DebugFile.trace) {
425       DebugFile.writeln("Begin DistributedCachePeer.put(" + sTokenKey + ", ...)");
426       DebugFile.incIdent();
427     }
428
429     if (PROTOCOL_NONE==iProviderProtocol)
430
431       lDtServerModified = System.currentTimeMillis();
432
433     else if (PROTOCOL_HTTP==iProviderProtocol || PROTOCOL_HTTPS==iProviderProtocol) {
434
435       String JavaDoc sProviderURL = oEnvProps.getProperty("java.naming.provider.url");
436
437       if (null == sProviderURL)
438         throw new NullPointerException JavaDoc("Property java.naming.provider.url not set at appserver.cnf");
439
440       else {
441         String JavaDoc sServerMod = "";
442
443         try {
444           if (DebugFile.trace) DebugFile.writeln("new javax.activation.DataHandler(new URL(" + sProviderURL + "?method=put&key=" + sTokenKey + "))");
445
446           javax.activation.DataHandler JavaDoc oDataHndlr = new javax.activation.DataHandler JavaDoc(new URL JavaDoc(sProviderURL + "?method=put&key=" + sTokenKey));
447
448           ByteArrayOutputStream JavaDoc oURLBytes = new ByteArrayOutputStream JavaDoc(128);
449           oDataHndlr.writeTo(oURLBytes);
450           sServerMod = trim(oURLBytes.toString());
451           oURLBytes.close();
452           oURLBytes = null;
453
454           if (DebugFile.trace) DebugFile.writeln("lDtServerModified=" + sServerMod);
455
456           lDtServerModified = Long.parseLong(sServerMod);
457         }
458         catch (MalformedURLException JavaDoc badurl) {
459           if (DebugFile.trace) DebugFile.writeln("MalformedURLException " + sProviderURL + "?method=get&put=" + sTokenKey);
460
461           throw new RemoteException JavaDoc("MalformedURLException " + sProviderURL + "?method=put&key=" + sTokenKey);
462         }
463         catch (IOException JavaDoc badurl) {
464           if (DebugFile.trace) DebugFile.writeln("IOException " + sProviderURL + "?method=get&put=" + sTokenKey);
465
466           throw new RemoteException JavaDoc("IOException " + sProviderURL + "?method=get&put=" + sTokenKey);
467         }
468         catch (NumberFormatException JavaDoc nume) {
469           if (DebugFile.trace) DebugFile.writeln("NumberFormatException " + sServerMod);
470
471           throw new RemoteException JavaDoc("NumberFormatException " + sServerMod);
472         }
473       }
474     }
475     else {
476
477       if (DebugFile.trace) DebugFile.writeln("DistributedCacheCoordinator.modify(" + sTokenKey + ")");
478
479       lDtServerModified = ((com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).modify(sTokenKey);
480
481       if (DebugFile.trace) DebugFile.writeln("lDtServerModified=" + String.valueOf(lDtServerModified));
482
483     }
484
485     if (DebugFile.trace) DebugFile.writeln("LRUCachePolicy.insert(" + sTokenKey + ", [Object], " + String.valueOf(lDtServerModified));
486
487     oCacheStore.insert (sTokenKey, oObj, lDtServerModified);
488
489     if (DebugFile.trace) {
490       DebugFile.decIdent();
491       DebugFile.writeln("End DistributedCachePeer.put()");
492     }
493   } // put
494

495   // ----------------------------------------------------------
496

497   /**
498    * <p>Puts a DBSubset into local cache.</p>
499    * @param sTokenKey Token for object
500    * @param oObj Object to be stored.
501    * @throws RemoteException
502    * @throws IllegalArgumentException If either sTokenKey or oObj is <b>null</b>.
503    * @throws IllegalStateException If object with given token is already present at local cache.
504    */

505   public void putDBSubset(String JavaDoc sTableName, String JavaDoc sTokenKey, DBSubset oDBSS) throws RemoteException JavaDoc {
506     if (DebugFile.trace) {
507       DebugFile.writeln("Begin DistributedCachePeer.putDBSubset(" + sTokenKey + ", ...)");
508       DebugFile.incIdent();
509     }
510
511     put (sTokenKey, oDBSS);
512
513     if (DebugFile.trace) {
514       DebugFile.decIdent();
515       DebugFile.writeln("End DistributedCachePeer.putDBSubset()");
516     }
517   } // putDBSubset
518

519   // ----------------------------------------------------------
520

521   /**
522    * <b>Removes an Object from the cache and notify other cache peers that
523    * the objects with the given token should no longer be considered valid.</p>
524    * If Object with given token was not present at cache no error is raised.
525    * @param sTokenKey Token of object to be removed from local cache.
526    * @throws RemoteException
527    * @throws IllegalArgumentException If sTkeney is <b>null</b>.
528    * @throws IllegalStateException If local cache is empty.
529    */

530   public void expire(String JavaDoc sTokenKey) throws IllegalArgumentException JavaDoc, RemoteException JavaDoc {
531     if (DebugFile.trace) {
532       DebugFile.writeln("Begin DistributedCachePeer.expire(" + sTokenKey + ")");
533       DebugFile.incIdent();
534     }
535     if (PROTOCOL_NONE!=iProviderProtocol) {
536
537       if (PROTOCOL_HTTP==iProviderProtocol || PROTOCOL_HTTPS==iProviderProtocol) {
538
539         String JavaDoc sProviderURL = oEnvProps.getProperty("java.naming.provider.url");
540
541         if (null==sProviderURL)
542           throw new NullPointerException JavaDoc("Property java.naming.provider.url not set at appserver.cnf");
543
544         else {
545
546           try {
547
548             if (DebugFile.trace) DebugFile.writeln("new javax.activation.DataHandler(new URL(" + sProviderURL + "?method=expire&key=" + sTokenKey + "))");
549
550             javax.activation.DataHandler JavaDoc oDataHndlr = new javax.activation.DataHandler JavaDoc(new URL JavaDoc(sProviderURL+"?method=expire&key=" + sTokenKey));
551
552              ByteArrayOutputStream JavaDoc oURLBytes = new ByteArrayOutputStream JavaDoc(128);
553              oDataHndlr.writeTo(oURLBytes);
554              oURLBytes.close();
555              oURLBytes = null;
556
557           }
558           catch (MalformedURLException JavaDoc badurl) {
559             throw new RemoteException JavaDoc("MalformedURLException " + sProviderURL + "?method=expire&key=" + sTokenKey);
560           }
561           catch (IOException JavaDoc badurl) {
562             throw new RemoteException JavaDoc("IOException " + sProviderURL + "?method=get&key=" + sTokenKey);
563           }
564         } // fi (null!=sProviderURL)
565
}
566       else {
567         if (DebugFile.trace) DebugFile.writeln("DistributedCacheCoordinator.expire(" + sTokenKey + ")");
568
569         ((com.knowgate.cache.server.DistributedCacheCoordinator) oDCC).expire(sTokenKey);
570       }
571     }
572     oCacheStore.remove(sTokenKey);
573
574     if (DebugFile.trace) {
575       DebugFile.decIdent();
576       DebugFile.writeln("End DistributedCachePeer.expire()");
577     }
578   } // expire
579

580   // ----------------------------------------------------------
581

582   /**
583    * <p>Remove all objects from local cache and expire then and cache coordinator.</p>
584    * @throws RemoteException
585    */

586   public void expireAll() throws RemoteException JavaDoc {
587     if (DebugFile.trace) {
588       DebugFile.writeln("Begin DistributedCachePeer.expireAll()");
589       DebugFile.incIdent();
590     }
591     Iterator JavaDoc oKeys;
592
593     if (PROTOCOL_NONE!=iProviderProtocol) {
594
595       oKeys = oCacheStore.m_map.keySet().iterator();
596
597       while(oKeys.hasNext()) expire((String JavaDoc) oKeys.next());
598
599     } // fi (oDCC)
600

601     oCacheStore.flush();
602
603     if (DebugFile.trace) {
604       DebugFile.decIdent();
605       DebugFile.writeln("End DistributedCachePeer.expireAll()");
606     }
607   } // expireAll
608

609   // ----------------------------------------------------------
610

611   /**
612    * <p>Number of entries in cache</p>
613    */

614   public int size() {
615     return oCacheStore.size();
616   }
617 } // DistributedCachePeer
618
Free Books   Free Magazines  
Popular Tags