1 /* Open Source Java Caching Service
2 * Copyright (C) 2002 Frank Karlstrøm
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * The author can be contacted by email: fjankk@users.sourceforge.net
18 */
19 package org.fjank.jcache.distribution;
20
21 import java.io.Serializable;
22 import java.util.Enumeration;
23 import javax.util.jcache.CacheException;
24 import org.fjank.jcache.CacheImpl;
25
26 /**
27 * Created by IntelliJ IDEA.
28 * User: Philippe
29 * Date: Jun 7, 2004
30 * Time: 11:39:37 PM
31 * To change this template use Options | File Templates.
32 */
33 public abstract class DistributionEngine
34 {
35 public static DistributionEngine getInstance(CacheImpl cache)
36 {
37 // TODO: allows instanciation of any Distribution Engine. For now, just use the JGroups based one
38 // In future, can support JMS as well
39 return JGroupsDistributionEngine.instanceOf(cache);
40 }
41
42 public abstract Enumeration getCacheAddr();
43
44 public void cacheObjectUpdated(String region,
45 String group,
46 Serializable key,
47 Serializable data)
48 {
49 sendNotification(new ClusterNotification(region,
50 group,
51 ClusterNotification.OBJECT_UPDATED,
52 new SerializableCacheObject(key, data)));
53 }
54
55
56 public void cacheObjectAdded(String region,
57 String group,
58 Serializable key,
59 Serializable data) throws CacheException
60 {
61 sendNotification(new ClusterNotification(region,
62 group,
63 ClusterNotification.OBJECT_ADDED,
64 new SerializableCacheObject(key, data)));
65
66 }
67
68
69 public void cacheObjectInvalidated(String region,
70 Serializable key)
71 {
72 sendNotification(new ClusterNotification(region,
73 null,
74 ClusterNotification.OBJECT_INVALIDATED,
75 key));
76 }
77
78 protected void handleClusterNotification(ClusterNotification clusterNotification)
79 {
80 // try
81 // {
82 // CacheLogger logger = CacheAccessFactory.getInstance().getCache().getAttributes().getLogger();
83 // logger.log("handleClusterNotification - message received");
84 //
85 // CacheMap cacheAccess = CacheAccessFactory.getInstance().getMapAccess(clusterNotification.getRegionName());
86 // SerializableCacheObject cacheObject;
87 // switch (clusterNotification.getType())
88 // {
89 // case ClusterNotification.OBJECT_ADDED:
90 // cacheObject = (SerializableCacheObject) clusterNotification.getData();
91 // logger.log("handleClusterNotification - object added: " + cacheObject.getKey());
92 // // Add object to the cache if it does not exist
93 // try
94 // {
95 // Object object = cacheAccess.get(cacheObject.getKey());
96 // }
97 // catch (ObjectNotFoundException e)
98 // {
99 // cacheAccess.putWithoutDistribution(cacheObject.getKey(),
100 // clusterNotification.getGroupName(),
101 // null,
102 // cacheObject.getValue());
103 // }
104 // break;
105 //
106 // case ClusterNotification.OBJECT_UPDATED:
107 // cacheObject = (SerializableCacheObject) clusterNotification.getData();
108 // logger.log("handleClusterNotification - object updated: " + cacheObject.getKey());
109 // if (clusterNotification.getGroupName() == null)
110 // {
111 // Object name = cacheObject.getKey();
112 // cacheAccess.replaceWithoutDistribution(name, cacheObject.getValue());
113 // }
114 // else
115 // {
116 // Object name = cacheObject.getKey();
117 // cacheAccess.replaceWithoutDistribution(name, clusterNotification.getGroupName(), cacheObject.getValue());
118 // }
119 // break;
120 //
121 // case ClusterNotification.OBJECT_INVALIDATED:
122 // Object key = clusterNotification.getData();
123 // logger.log("handleClusterNotification - object invalidated: " + key);
124 // Attributes attributes = cacheAccess.getAttributes(key);
125 // cacheAccess.invalidateWithoutDistribution(key);
126 // if (attributes.getListener() != null)
127 // attributes.getListener().handleEvent(new CacheEventImpl(Attributes.INVALIDATE_EVENT,
128 // key));
129 // break;
130 //
131 // }
132 // }
133 // catch (CacheException e)
134 // {
135 // e.printStackTrace();
136 // }
137 }
138
139 public abstract void sendNotification(ClusterNotification clusterNotification);
140
141
142 protected CacheImpl cache;
143 }