KickJava   Java API By Example, From Geeks To Geeks.

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


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.CacheAttributeController;
22 import org.objectweb.fractal.util.Fractal;
23 import org.objectweb.fractal.api.Component;
24 import org.objectweb.fractal.api.Interface;
25 import org.objectweb.fractal.api.control.NameController;
26 import org.objectweb.util.monolog.api.BasicLevel;
27
28 import java.util.StringTokenizer JavaDoc;
29
30 /**
31  * Manages an unique instance of a CacheManager.
32  * (singleton pattern)
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class CacheManagerFactory {
37
38
39     private static CacheManager singleton = null;
40
41     private final static String JavaDoc CACHE_MANAGER_TM =
42         "org.objectweb.perseus.cache.lib.SimpleCacheManager";
43
44     public static CacheManager getCacheManager() throws Exception JavaDoc {
45         if (singleton == null) {
46             synchronized(CacheManagerFactory.class) {
47                 if (singleton == null) {
48                     singleton = newCacheManager();
49                 }
50             }
51         }
52         return singleton;
53     }
54
55     public static CacheManager newCacheManager() throws Exception JavaDoc {
56         Component cm = FractalHelper.instanciate(CACHE_MANAGER_TM);
57         Fractal.getLifeCycleController(cm).startFc();
58         return (CacheManager) cm.getFcInterface("cache-manager");
59     }
60
61     public static CacheAttributeController getCacheAttributeController(CacheManager cm) throws Exception JavaDoc {
62         Component primitivecm = getSubComponent(
63             ((Interface) cm).getFcItfOwner(), "cache-manager");
64         if (primitivecm == null) {
65             throw new Exception JavaDoc("Impossible to find the CacheAttributeController for the given CacheManager: " + cm);
66         }
67         return (CacheAttributeController) Fractal.getAttributeController(primitivecm);
68     }
69
70     private static Component getSubComponent(Component parent,
71                                       String JavaDoc path) throws Exception JavaDoc {
72         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".", false);
73         Component res = parent;
74         while(st.hasMoreTokens()) {
75             String JavaDoc commponenentname = st.nextToken();
76             Component[] children = Fractal.getContentController(res)
77                     .getFcSubComponents();
78             int i = 0;
79             while(i<children.length &&
80                     !Fractal.getNameController(children[i])
81                         .getFcName().equals(commponenentname)) {
82                 i++;
83             }
84             if (i<children.length) {
85                 res = children[i];
86             } else {
87                 return null;
88             }
89         }
90         return res;
91     }
92
93 }
94
Popular Tags