org.appops.cache.impl.SubscriberRegistryImpl Maven / Gradle / Ivy
/*
* AppOps is a Java framework to develop, deploy microservices with ease and is available for free
* and common use developed by AinoSoft ( www.ainosoft.com )
*
* AppOps and AinoSoft are registered trademarks of Aino Softwares private limited, India.
*
* Copyright (C) <2016>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version along with applicable additional terms as
* provisioned by GPL 3.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License and applicable additional terms
* along with this program.
*
* If not, see and
*/
package org.appops.cache.impl;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.appops.cache.event.Condition;
import org.appops.cache.event.EventSubscriber;
import org.appops.cache.event.SubscriberRegistry;
/**
* Storage class which stores all event listeners.
*
* @author deba
* @version $Id: $Id
*/
public class SubscriberRegistryImpl implements SubscriberRegistry {
private Table> subscriberListenerTable = HashBasedTable.create();
private static final Condition DEFAULT = new Condition();
private Logger logger = Logger.getLogger(getClass().getCanonicalName());
/**
* Constructor for SubscriberRegistryImpl.
*/
public SubscriberRegistryImpl() {
}
/**
* {@inheritDoc}
*
* Locates and registers listener method(s) from given event subscriber.
*/
@Override
public Boolean register(EventSubscriber eventSubscriber) {
String serviceUrl = eventSubscriber.getServiceUrl();
String serviceName = eventSubscriber.getServiceName();
String serviceSubscriber = eventSubscriber.getSubscriber().getMethodName();
String eventType = eventSubscriber.getSubscriber().getParameterType();
Condition condition = eventSubscriber.getCondition();
if (condition == null) {
condition = DEFAULT;
}
String subscriberUrl = generateSubscriberUrl(serviceUrl, serviceName, serviceSubscriber);
try {
return addListener(eventType, condition, subscriberUrl);
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception occured in " + getClass() + "::register()" + e);
}
return false;
}
/**
* It adds the listener and subscriber into registry.
*
* @param eventType event type
* @param condition condition if any for subscriber
* @param subscriber event subscriber url
* @return true, after adding subscriber otherwise false
*/
private boolean addListener(String eventType, Condition condition, String subscriber) {
return getSubscribers(eventType, condition).add(subscriber);
}
/**
* It creates url using given parameters and return it.
*
* @param serviceUrl service url
* @param serviceName service name which have listener
* @param serviceSubscriber subscriber method name
* @return url string using given paramaters
*/
private String generateSubscriberUrl(String serviceUrl, String serviceName,
String serviceSubscriber) {
if (!serviceUrl.endsWith("/")) {
serviceUrl = serviceUrl.concat("/");
}
return serviceUrl + "Op/" + serviceName + "/" + serviceSubscriber;
}
/**
* {@inheritDoc}
*
* Fetches listeners which are registered against event type and condition provided.
*/
@Override
public Set getSubscribers(String eventType, Condition condition) {
condition = condition == null ? DEFAULT : condition;
Set listeners = subscriberListenerTable.get(eventType, condition);
if (listeners == null) {
listeners = new HashSet<>();
subscriberListenerTable.put(eventType, condition, listeners);
}
return subscriberListenerTable.get(eventType, condition);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy