KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > CacheMgt


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.util.*;
17
18 /**
19  * Compiere Cache Manangement
20  *
21  * @author Jorg Janke
22  * @version $Id: CacheMgt.java,v 1.4 2003/10/04 03:55:46 jjanke Exp $
23  */

24 public class CacheMgt
25 {
26     /**
27      * Get Cache Management
28      * @return Cache Mgr
29      */

30     public static CacheMgt get()
31     {
32         if (s_cache == null)
33             s_cache = new CacheMgt();
34         return s_cache;
35     } // get
36

37     /** Singleton */
38     private static CacheMgt s_cache = null;
39
40     /**
41      * Private Constructor
42      */

43     private CacheMgt()
44     {
45     } // CacheMgt
46

47     private ArrayList m_instances = new ArrayList();
48     private Logger log = Logger.getCLogger(getClass());
49
50     /*************************************************************************/
51
52     /**
53      * Register Cache Instance
54      * @param instance Cache
55      * @return true if added
56      */

57     public boolean register (CacheInterface instance)
58     {
59         if (instance == null)
60             return false;
61         return m_instances.add (instance);
62     } // register
63

64     /**
65      * Un-Register Cache Instance
66      * @param instance Cache
67      * @return true if removed
68      */

69     public boolean unregister (CacheInterface instance)
70     {
71         if (instance == null)
72             return false;
73         boolean found = false;
74         // Could be included multiple times
75
for (int i = m_instances.size()-1; i >= 0; i--)
76         {
77             CacheInterface stored = (CacheInterface)m_instances.get(i);
78             if (instance.equals(stored))
79             {
80                 m_instances.remove(i);
81                 found = true;
82             }
83         }
84         return found;
85     } // unregister
86

87     /*************************************************************************/
88
89     /**
90      * Reset All registered Cache
91      */

92     public void reset()
93     {
94         int counter = 0;
95         int total = 0;
96         for (int i = 0; i < m_instances.size(); i++)
97         {
98             CacheInterface stored = (CacheInterface)m_instances.get(i);
99             if (stored != null)
100             {
101                 log.debug("reset - " + stored);
102                 total += stored.reset();
103                 counter++;
104             }
105         }
106         log.info("reset #" + counter + " (" + total + ")");
107     } // reset
108

109 } // CCache
110
Popular Tags