KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > cms > ha > InvalidatableCMS


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.cms.ha;
12
13 import org.jboss.portal.cms.CMS;
14 import org.jboss.cache.invalidation.Invalidatable;
15 import org.jboss.cache.invalidation.InvalidationGroup;
16 import org.jboss.cache.invalidation.InvalidationManagerMBean;
17 import org.jboss.mx.util.MBeanProxy;
18 import org.apache.slide.common.NamespaceAccessToken;
19 import org.apache.slide.common.Domain;
20 import org.apache.slide.common.Uri;
21 import org.apache.slide.common.SlideTokenImpl;
22 import org.apache.slide.authenticate.SecurityToken;
23 import org.apache.slide.authenticate.CredentialsToken;
24 import org.apache.slide.store.Store;
25 import org.apache.slide.store.ExtendedStore;
26 import org.apache.slide.event.EventCollectionListener;
27 import org.apache.slide.event.EventCollection;
28 import org.apache.slide.event.VetoException;
29 import org.apache.slide.event.ContentEvent;
30 import org.apache.slide.event.EventCollectionFilter;
31 import org.apache.log4j.Logger;
32
33 import javax.management.ObjectName JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.HashSet JavaDoc;
37
38 /**
39  * @jmx.mbean
40  * @jboss.xmbean
41  *
42  * Add cache invalidation on top of the repository. It is based on JBoss invalidation manager.
43  * This add the capability to use cms on a cluster. Obviously all the cms instances must
44  * share the same underlying back end (i.e an rdbms or shared file system).
45  *
46  * todo : add a time based policy refresher to avoid rare stale entries to stay issues
47  *
48  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
49  * @version $Revision: 1.1 $
50  */

51 public class InvalidatableCMS
52       extends CMS
53       implements Invalidatable,
54       EventCollectionListener
55
56 {
57    /** Our logger. */
58    private static final Logger log = Logger.getLogger(InvalidatableCMS.class);
59
60    /** The invalidation manager name. */
61    private ObjectName JavaDoc invalidationManagerName;
62
63    /** The invalidation manager. */
64    private InvalidationManagerMBean invalidationManager;
65
66    /** The invalidation group we use as communication backbone. */
67    private InvalidationGroup group;
68
69    /** The invalidation group name. */
70    private String JavaDoc groupName;
71
72    /**
73     * @jmx.managed-attribute
74     */

75    public ObjectName JavaDoc getInvalidationManagerName()
76    {
77       return invalidationManagerName;
78    }
79
80    /**
81     * @jmx.managed-attribute
82     */

83    public void setInvalidationManagerName(ObjectName JavaDoc invalidationManagerName)
84    {
85       this.invalidationManagerName = invalidationManagerName;
86    }
87
88    /**
89     * @jmx.managed-attribute
90     */

91    public String JavaDoc getGroupName()
92    {
93       return groupName;
94    }
95
96    /**
97     * @jmx.managed-attribute
98     */

99    public void setGroupName(String JavaDoc groupName)
100    {
101       this.groupName = groupName;
102    }
103
104    public void createService() throws Exception JavaDoc
105    {
106       super.createService();
107       invalidationManager = (InvalidationManagerMBean)MBeanProxy.get(InvalidationManagerMBean.class, invalidationManagerName, server);
108       group = invalidationManager.getInvalidationGroup(groupName);
109       group.register(this);
110    }
111
112    public void destroyService() throws Exception JavaDoc
113    {
114       group.unregister(this);
115       super.destroyService();
116    }
117
118    public void vetoableCollected(EventCollection collection) throws VetoException
119    {
120       // Do nothing
121
}
122
123    public void collected(EventCollection collection)
124    {
125       Set JavaDoc uris = new HashSet JavaDoc();
126
127       // Keep update events
128
ContentEvent[] update = EventCollectionFilter.getChangedContents(collection);
129       for (int i = 0; i < update.length; i++)
130       {
131          ContentEvent event = update[i];
132          uris.add(event.getUri());
133       }
134
135       // Keep delete events
136
ContentEvent[] delete = EventCollectionFilter.getRemovedContents(collection);
137       for (int i = 0; i < delete.length; i++)
138       {
139          ContentEvent event = delete[i];
140          uris.add(event.getUri());
141       }
142
143       //
144
if (uris.size() > 0)
145       {
146          if (log.isDebugEnabled())
147          {
148             log.debug("Received slide event collection, batch invalidation with " + uris.size() + " elements");
149          }
150          group.invalidate((Serializable JavaDoc[])uris.toArray(new Serializable JavaDoc[uris.size()]), true);
151       }
152    }
153
154    public void areInvalid(Serializable JavaDoc[] uris)
155    {
156       if (log.isDebugEnabled())
157       {
158          log.debug("Received batch invalidation with " + uris.length + " elements");
159       }
160       if (uris.length == 1 && uris[0] instanceof Serializable JavaDoc[])
161       {
162          uris = (Serializable JavaDoc[])uris[0];
163       }
164       NamespaceAccessToken nat = Domain.accessNamespace(new SecurityToken(this), Domain.getDefaultNamespace());
165       try
166       {
167          nat.begin();
168          for (int i = 0; i < uris.length; i++)
169          {
170             String JavaDoc uri = (String JavaDoc)uris[i];
171             if (log.isDebugEnabled())
172             {
173                log.debug("Invalidate URI " + uri);
174             }
175             Uri slideURI = nat.getUri(new SlideTokenImpl(new CredentialsToken("")), uri);
176             Store store = slideURI.getParentUri().getStore();
177             ((ExtendedStore)store).removeObjectFromCache(slideURI.getParentUri());
178          }
179       }
180       catch (Exception JavaDoc e)
181       {
182          log.error("Error during invalidation process", e);
183       }
184       finally
185       {
186          try
187          {
188             nat.commit();
189          }
190          catch (Exception JavaDoc e)
191          {
192             log.error("Error on commit slide transaction during invalidation process", e);
193          }
194       }
195    }
196
197    public void isInvalid(Serializable JavaDoc serializable)
198    {
199       areInvalid(new Serializable JavaDoc[]{serializable});
200    }
201
202    public void invalidateAll()
203    {
204       throw new Error JavaDoc("Not yet implemented but should not be called");
205    }
206 }
207
Popular Tags