Coverage Report - org.kuali.rice.ksb.cache.RiceDistributedCacheListener
 
Classes in this File Line Coverage Branch Coverage Complexity
RiceDistributedCacheListener
0%
0/36
0%
0/6
3
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.ksb.cache;
 18  
 
 19  
 import java.io.Serializable;
 20  
 
 21  
 import javax.xml.namespace.QName;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 26  
 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
 27  
 import org.kuali.rice.ksb.api.bus.ServiceBus;
 28  
 import org.kuali.rice.ksb.api.bus.support.JavaServiceDefinition;
 29  
 import org.kuali.rice.ksb.messaging.service.KSBJavaService;
 30  
 
 31  
 import com.opensymphony.oscache.base.Cache;
 32  
 import com.opensymphony.oscache.base.Config;
 33  
 import com.opensymphony.oscache.base.FinalizationException;
 34  
 import com.opensymphony.oscache.base.InitializationException;
 35  
 import com.opensymphony.oscache.plugins.clustersupport.AbstractBroadcastingListener;
 36  
 import com.opensymphony.oscache.plugins.clustersupport.ClusterNotification;
 37  
 
 38  
 
 39  
 /**
 40  
  * An OSCache listener which listens for events from the local cache and sends
 41  
  * them to a Topic on the bus so that all other entities on the bus can flush
 42  
  * their cache if neccessary.
 43  
  * 
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  */
 46  0
 public class RiceDistributedCacheListener extends AbstractBroadcastingListener implements KSBJavaService {
 47  
 
 48  0
         private static final Logger LOG = Logger.getLogger(RiceDistributedCacheListener.class);
 49  
 
 50  
         private String serviceName;
 51  
         
 52  
         protected ServiceBus serviceBus;
 53  
         
 54  
         @Override
 55  
         public void initialize(Cache cache, Config config) throws InitializationException {
 56  
 
 57  0
                 LOG.info("Initializing cache listener");
 58  0
                 super.initialize(cache, config);
 59  
                 // the following property was put on the OSCache properties used for
 60  
                 // cache configuration
 61  0
                 this.serviceName = config.getProperty(CacheProperties.SERVICE_NAME_KEY);
 62  0
                 if (StringUtils.isBlank(this.serviceName)) {
 63  0
                         throw new RiceRuntimeException("Cannot create DistributedCacheListener with empty serviceName");
 64  
                 }
 65  0
                 boolean forceRegistryRefresh = new Boolean((Boolean)config.getProperties().get(CacheProperties.FORCE_REGISTRY_REFRESH_KEY));
 66  0
                 serviceBus = (ServiceBus)config.getProperties().get(CacheProperties.SERVICE_BUS);
 67  0
                 if (serviceBus == null) {
 68  0
                         serviceBus = KsbApiServiceLocator.getServiceBus();
 69  
                 }
 70  0
                 LOG.info("Publishing Cache Service on bus under service name " + this.serviceName);
 71  0
                 JavaServiceDefinition serviceDef = new JavaServiceDefinition();
 72  0
                 serviceDef.setPriority(3);
 73  0
                 serviceDef.setRetryAttempts(1);
 74  0
                 serviceDef.setService(this);
 75  0
                 serviceDef.setLocalServiceName(this.serviceName);
 76  0
                 serviceDef.setServiceNameSpaceURI("");
 77  0
                 serviceDef.setQueue(false);
 78  
                 try {
 79  0
                         serviceDef.validate();
 80  0
                 } catch (Exception e) {
 81  0
                         throw new RiceRuntimeException(e);
 82  0
                 }
 83  
 
 84  0
                 serviceBus.publishService(serviceDef, forceRegistryRefresh);
 85  0
         }
 86  
 
 87  
         @Override
 88  
         protected void sendNotification(ClusterNotification notification) {
 89  0
                 if (LOG.isDebugEnabled()) {
 90  0
                         LOG.debug("Sending cache notification " + notification);
 91  
                 }
 92  
                 try {
 93  0
                         KSBJavaService oscacheNotificationService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(new QName(this.serviceName));
 94  0
                         oscacheNotificationService.invoke(notification);
 95  0
                 } catch (Exception e) {
 96  0
                         throw new RiceRuntimeException(e);
 97  0
                 }
 98  0
         }
 99  
 
 100  
         @Override
 101  
         public void finialize() throws FinalizationException {
 102  
             //no processing needed
 103  0
         }
 104  
 
 105  
         @Override
 106  
         public void invoke(Serializable payLoad) {
 107  0
                 super.handleClusterNotification((ClusterNotification) payLoad);
 108  0
         }
 109  
 
 110  
 }