01: /*
02: * Copyright 2006 Davide Deidda
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: /*
18: * FrameworkCache.java
19: *
20: * Created on 5 marzo 2005, 16.12
21: */
22:
23: package it.biobytes.ammentos.cache;
24:
25: import java.util.*;
26: import java.util.concurrent.*;
27: import java.util.logging.*;
28:
29: import it.biobytes.ammentos.*;
30:
31: /**
32: *
33: * @author davide
34: */
35: public class FrameworkCache {
36: private Map<Class, LookupCache> m_lookupCaches;
37: private Logger m_logger = Logger.getLogger("ammentos");
38:
39: public <T> T lookup(Class<T> c, Object primaryKey) {
40: LookupCache<T> cache = getLookupCache(c);
41: return cache.lookup(primaryKey);
42: }
43:
44: public <T> void store(T obj) throws PersistenceException {
45: LookupCache<T> cache = getLookupCache((Class<T>) obj.getClass());
46: cache.store(obj);
47: }
48:
49: private synchronized <T> LookupCache<T> getLookupCache(Class<T> c) {
50: LookupCache<T> res = m_lookupCaches.get(c);
51: if (res == null) {
52: res = new LookupCache<T>();
53: m_lookupCaches.put(c, res);
54: }
55: return res;
56: }
57:
58: /** Creates a new instance of FrameworkCache */
59: public FrameworkCache() {
60: m_lookupCaches = new ConcurrentHashMap<Class, LookupCache>();
61: }
62:
63: // Removes the provided object from the Framework cache
64: public synchronized void remove(Object obj)
65: throws PersistenceException {
66: if (obj != null) {
67: LookupCache cache = m_lookupCaches.get(obj.getClass());
68: m_logger.info("Looking for Cache...");
69: if (cache != null) {
70: Field primaryKey = Ammentos.getPrimaryKeyField(obj
71: .getClass());
72: m_logger
73: .info("Cache found! - removing element whith id: "
74: + primaryKey.get(obj));
75: cache.removeKey(primaryKey.get(obj));
76: }
77: }
78: }
79:
80: public synchronized void removeKey(Class c, Object key)
81: throws PersistenceException {
82: if (key != null) {
83: LookupCache cache = m_lookupCaches.get(c);
84: m_logger.info("Looking for Cache...");
85: if (cache != null) {
86: m_logger
87: .info("Cache found! - removing element whith id: "
88: + key);
89: cache.removeKey(key);
90: }
91: }
92: }
93:
94: }
|