KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > cache > Application


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.perseus.cache;
19
20 import org.objectweb.perseus.cache.api.CacheManager;
21 import org.objectweb.perseus.cache.api.CacheEntry;
22 import org.objectweb.perseus.cache.api.CacheAttributeController;
23 import org.objectweb.util.monolog.Monolog;
24 import org.objectweb.util.monolog.api.LoggerFactory;
25 import org.objectweb.util.monolog.api.Logger;
26 import org.objectweb.util.monolog.api.BasicLevel;
27
28 import java.util.HashMap JavaDoc;
29
30 /**
31  *
32  * @author S.Chassande-Barrioz
33  */

34 public class Application {
35     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
36         Logger logger = Monolog.initialize().getLogger(Application.class.getName());
37
38
39         logger.log(BasicLevel.INFO, "CacheManager instance fetched.");
40         CacheManager cm = CacheManagerFactory.getCacheManager();
41
42         logger.log(BasicLevel.INFO, "Change the maximal cache size to 50 000");
43         CacheAttributeController cac = CacheManagerFactory.getCacheAttributeController(cm);
44         cac.setMaxObjects(50000);
45
46         //bind entries to the cache
47
HashMap JavaDoc o1 = new HashMap JavaDoc();
48         o1.put("k1", new long[]{345234});
49         logger.log(BasicLevel.INFO, "bind (\"id1\", " + o1 + ") to the cache");
50         cm.bind("id1", o1);
51
52         logger.log(BasicLevel.INFO, "bind (\"id2\", \"value1\") to the cache");
53         cm.bind("id2", "value1");
54
55         logger.log(BasicLevel.INFO, "Currently there are " + cac.getCurrentSize() + " entries");
56
57         //check if the bound entries are available
58
CacheEntry ce = cm.lookup("id1");
59         if (ce == null) {
60             throw new Exception JavaDoc("Error in the cache: no entry matching id1");
61         }
62         logger.log(BasicLevel.INFO, "lookup(\"id1\"): " + cm.lookup("id1").getCeObject());
63         if (ce.getCeObject() != o1) {
64             throw new Exception JavaDoc(
65                 "Error in the cache: bad entry for the identifier id1: "
66                 + ce.getCeObject());
67         }
68
69         ce = cm.lookup("id2");
70         if (ce == null) {
71             throw new Exception JavaDoc("Error in the cache: no entry matching id2");
72         }
73         logger.log(BasicLevel.INFO, "lookup(\"id2\"): " + cm.lookup("id2").getCeObject());
74         if (!"value1".equals(ce.getCeObject())) {
75             throw new Exception JavaDoc(
76                 "Error in the cache: bad entry for the identifier id2: "
77                 + ce.getCeObject());
78         }
79     }
80 }
81
Popular Tags