/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc. and individual contributors
* by the @authors tag. See the copyright.txt 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.mobicents.slee.runtime.sbbentity;
import org.apache.log4j.Logger;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
import org.infinispan.tree.Fqn;
import org.infinispan.tree.impl.NodeKey;
import org.mobicents.slee.container.SleeContainer;
import org.mobicents.slee.container.sbbentity.SbbEntityID;
import org.restcomm.cache.MobicentsCache;
import javax.slee.ServiceID;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author martins
*
*/
@Listener
public class SbbEntityLockFacility {
private static final Logger logger = Logger.getLogger(SbbEntityLockFacility.class);
private boolean doTraceLogs = logger.isTraceEnabled();
private boolean doInfoLogs = logger.isInfoEnabled();
/**
*
*/
private final ConcurrentHashMap<SbbEntityID,ReentrantLock> locks = new ConcurrentHashMap<SbbEntityID, ReentrantLock>();
/**
*
*/
public SbbEntityLockFacility(SleeContainer container) {
//container.getCluster().addDataRemovalListener(new DataRemovaClusterListener());
MobicentsCache cache = container.getCluster().getMobicentsCache();
if (!cache.isLocalMode()) {
// SergeyLee: test
cache.getJBossCache().getCache().addListener(this);
}
}
/**
*
* @param sbbEntityId
* @return
*/
public ReentrantLock get(SbbEntityID sbbEntityId) {
ReentrantLock lock = locks.get(sbbEntityId);
if (lock == null) {
final ReentrantLock newLock = new ReentrantLock();
lock = locks.putIfAbsent(sbbEntityId, newLock);
if (lock == null) {
if(doInfoLogs) {
logger.info(Thread.currentThread()+" put of lock "+newLock+" for "+sbbEntityId);
}
lock = newLock;
}
}
return lock;
}
/**
*
* @param sbbEntityId
* @return
*/
public ReentrantLock remove(SbbEntityID sbbEntityId) {
if(doInfoLogs) {
logger.info(Thread.currentThread()+" removed lock for "+sbbEntityId);
}
return locks.remove(sbbEntityId);
}
/**
*
* @return
*/
public Set<SbbEntityID> getSbbEntitiesWithLocks() {
return locks.keySet();
}
// FIXME this does not work in the new sbbe tree model, ensure cluster framework gets such feature
/*
private class DataRemovaClusterListener implements DataRemovalListener {
private final Fqn<?> baseFqn = null;
@SuppressWarnings("unchecked")
public void dataRemoved(Fqn arg0) {
final SbbEntityID sbbEntityId = (SbbEntityID) arg0.getLastElement();
if(locks.remove(sbbEntityId) != null) {
if(doTraceLogs) {
logger.trace("Remotely removed lock for "+sbbEntityId);
}
}
}
@SuppressWarnings("unchecked")
public Fqn getBaseFqn() {
return baseFqn;
}
}*/
@CacheEntryRemoved
public void onNodeRemovedEvent(CacheEntryRemovedEvent event) {
if(!event.isOriginLocal() && !event.isPre()) {
// remote node removal
Fqn fqn = ((NodeKey)event.getKey()).getFqn();
if (fqn != null) {
if(doInfoLogs) {
logger.info("onNodeRemovedEvent( fqn = "+fqn+", size = "+fqn.size()+" )");
}
if (fqn.get(0).equals(SbbEntityFactoryCacheData.SBB_ENTITY_FACTORY_FQN_NAME)) {
// is child of sbb entity factory cache data, i.e., /sbbe
int fqnSize = fqn.size();
if (fqnSize < 3) {
return;
}
SbbEntityID sbbEntityID = null;
if (fqnSize == 3) {
// /sbbe/serviceid/convergenceName root sbb entity
ServiceID serviceID = (ServiceID) fqn.get(1);
String convergenceName = (String) fqn.get(2);
sbbEntityID = new RootSbbEntityID(serviceID, convergenceName);
if (doInfoLogs) {
logger.info("Root sbb entity " + sbbEntityID + " was remotely removed, ensuring there is no local lock");
}
} else {
// must end as /chd/chdRelationName/childId
if (!fqn.get(fqnSize - 3).equals(SbbEntityCacheData.CHILD_RELATIONs_CHILD_NODE_NAME)) {
return;
}
// let get the party started and rebuild the sbb entity id!
ServiceID serviceID = (ServiceID) fqn.get(1);
String convergenceName = (String) fqn.get(2);
sbbEntityID = new RootSbbEntityID(serviceID, convergenceName);
int i = 3;
while (fqnSize >= i + 3) {
// fqn get(i) is chd, skip
String childRelationName = (String) fqn.get(i + 1);
String childId = (String) fqn.get(i + 2);
sbbEntityID = new NonRootSbbEntityID(sbbEntityID, childRelationName, childId);
i += 3;
}
if (doInfoLogs) {
logger.info("Non root sbb entity " + sbbEntityID + " was remotely removed, ensuring there is no local lock");
}
}
if (locks.remove(sbbEntityID) != null) {
if (doInfoLogs) {
logger.info("Remotely removed lock for " + sbbEntityID);
}
}
}
}
}
}
}