/* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.cache.invalidation; import java.io.Serializable; import java.util.HashSet; import javax.management.MBeanParameterInfo; import javax.management.MBeanOperationInfo; import org.jboss.cache.invalidation.InvalidationManager.BridgeInvalidationSubscriptionImpl; /** * Implementation of InvalidationManagerMBean * * @see org.jboss.cache.invalidation.InvalidationManagerMBean * * @author Sacha Labourey. * @version $Revision: 59578 $ */ public class InvalidationManager extends org.jboss.system.ServiceMBeanSupport implements InvalidationManagerMBean { // Constants ----------------------------------------------------- public static final String DEFAULT_JMX_SERVICE_NAME = "jboss.cache:service=InvalidationManager"; public static final String DEFAULT_INVALIDERS_JMX_NAME = "jboss.cache:service=InvalidationGroup"; // Attributes ---------------------------------------------------- protected java.util.Hashtable groups = new java.util.Hashtable (); protected java.util.Vector bridgeSubscribers = new java.util.Vector (); protected int hashcode = 0; protected boolean DEFAULT_TO_ASYNCHRONOUS_MODE = false; // Static -------------------------------------------------------- // Constructors -------------------------------------------------- public InvalidationManager () { super(); } public void startService () throws Exception { // bind us in system registry // log.debug ("Starting Invalidation Manager " + this.getServiceName ().toString ()); org.jboss.system.Registry.bind (this.getServiceName ().toString (), this); this.hashcode = this.getServiceName ().hashCode (); } public void stopService () { log.debug ("Stoping Invalidation Manager " + this.getServiceName ().toString ()); org.jboss.system.Registry.unbind (this.getServiceName ().toString ()); } public boolean getIsAsynchByDefault() { return DEFAULT_TO_ASYNCHRONOUS_MODE; } public void setIsAsynchByDefault(boolean flag) { this.DEFAULT_TO_ASYNCHRONOUS_MODE = flag; } public java.util.Collection getInvalidationGroups () { return this.groups.values (); } public InvalidationGroup getInvalidationGroup (String groupName) { synchronized (this.groups) { InvalidationGroup group = (InvalidationGroup)this.groups.get (groupName); if (group == null) { group = createGroup (groupName); } group.addReference (); return group; } } public synchronized BridgeInvalidationSubscription registerBridgeListener (InvalidationBridgeListener listener) { log.debug ("Subscribing a new cache-invalidation bridge"); BridgeInvalidationSubscription subs = new BridgeInvalidationSubscriptionImpl(listener); java.util.Vector newVector = new java.util.Vector (this.bridgeSubscribers); newVector.add (subs); this.bridgeSubscribers = newVector; return subs; } public void batchInvalidate (BatchInvalidation[] invalidations) { this.batchInvalidate (invalidations, this.DEFAULT_TO_ASYNCHRONOUS_MODE); } public void batchInvalidate (BatchInvalidation[] invalidations, boolean asynchronous) { if (log.isTraceEnabled ()) log.trace ("Batch cache invalidation. Caches concerned: " + invalidations.length); this.crossDomainBatchInvalidate (null, invalidations, asynchronous); } public void invalidateAll(String groupName) { invalidateAll(groupName, DEFAULT_TO_ASYNCHRONOUS_MODE); } public void invalidateAll(String groupName, boolean async) { if (log.isTraceEnabled ()) log.trace ("Invalidate all for group: " + groupName); crossDomainInvalidateAll(null, groupName, async); } // Public -------------------------------------------------------- // Z implementation ---------------------------------------------- // Object overrides --------------------------------------------------- public int hashCode () { return hashcode; } // Package protected --------------------------------------------- // Protected ----------------------------------------------------- protected InvalidationGroup createGroup (String groupName) { InvalidationGroup group = new org.jboss.cache.invalidation.InvalidationManager.InvalidationGroupImpl (groupName); this.groups.put (groupName, group); // register the group with JMX so it can be easyly remotly // reached (and thus cache can be invalidated) // try { log.debug ("Creating and registering a new InvalidationGroup: " + groupName); javax.management.ObjectName groupObjectName = new javax.management.ObjectName (DEFAULT_INVALIDERS_JMX_NAME + ",GroupName="+groupName); this.getServer ().registerMBean (group, groupObjectName); } catch (Exception e) { log.debug ("Problem while trying to register a new invalidation group in JMX", e); } // warn bridges // log.debug ("Informing bridges about new group creation ..."); for (int i=0; i