Java tutorial
/** * Copyright (C) 2012 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * 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 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package nl.minbzk.dwr.zoeken.enricher.notifier; import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.locks.ReentrantReadWriteLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import com.seajas.search.attender.bridge.model.notifier.NotifierProfile; import com.seajas.search.attender.wsdl.IProfile; /** * Read-most modifier cache implementation. * * @author Jasper van Veghel <jasper@seajas.com> */ @Lazy @Component public class DirectResultCache { /** * The logger. */ private static final Logger logger = LoggerFactory.getLogger(DirectResultCache.class); /** * Notifier profiles by their UUIDs. */ private volatile HashMap<String, NotifierProfile> notifierProfilesByUUID; /** * Reentrant read-write lock for notifier profiles. */ private volatile ReentrantReadWriteLock notifierProfilesLock = new ReentrantReadWriteLock(); /** * Bootstrap the cache. * * XXX: This is not set by Spring, but must instead be initialized by the DirectResultNotifier. * * @param profileService */ // @Autowired public void setProfileService(final IProfile profileService) { if (!validateNotifierProfileModelIntegrity()) throw new IllegalStateException( "Object copying does not account for all fields - model definition too new"); notifierProfilesByUUID = new HashMap<String, NotifierProfile>(); for (com.seajas.search.attender.bridge.wsdl.profile.NotifierProfile notifierProfile : profileService .retrieveConfirmedDirectNotifierProfiles()) { NotifierProfile convertedNotifierProfile = convertNotifierProfileWsToInternal(notifierProfile); if (logger.isInfoEnabled()) { logger.info("Adding notifier profile " + convertedNotifierProfile.getUUID() + " with e-mail '" + convertedNotifierProfile.getEmail() + "' and query '" + convertedNotifierProfile.getQuery() + "'"); for (String searchParameter : convertedNotifierProfile.getSearchParameters()) logger.info("\tNotifier profile includes parameter '" + searchParameter + "'"); } notifierProfilesByUUID.put(convertedNotifierProfile.getUUID(), convertedNotifierProfile); } } /** * Process an incoming update. * * @param notifierProfile */ public void update(final NotifierProfile notifierProfile) { if (logger.isInfoEnabled()) { logger.info("Updating notifier profile " + notifierProfile.getUUID() + " with e-mail '" + notifierProfile.getEmail() + "' and query '" + notifierProfile.getQuery() + "'"); for (String searchParameter : notifierProfile.getSearchParameters()) logger.info("\tNotifier profile includes parameter '" + searchParameter + "'"); } notifierProfilesLock.writeLock().lock(); try { notifierProfilesByUUID.put(notifierProfile.getUUID(), notifierProfile); } finally { notifierProfilesLock.writeLock().unlock(); } } /** * Process an incoming delete. * * @param uuid */ public void delete(final String uuid) { notifierProfilesLock.writeLock().lock(); logger.info("Deleting notifier profile with UUID " + uuid); try { notifierProfilesByUUID.remove(uuid); } finally { notifierProfilesLock.writeLock().unlock(); } } /** * Retrieve a given notifier profile by its UUID. * * @param uuid * @return NotifierProfile */ public NotifierProfile getNotifierProfileByUUID(final String uuid) { notifierProfilesLock.readLock().lock(); try { return notifierProfilesByUUID.get(uuid); } finally { notifierProfilesLock.readLock().unlock(); } } /** * Retrieve all notifier profiles. * * @return Map<String, NotifierProfile> */ @SuppressWarnings("unchecked") public Map<String, NotifierProfile> getNotifierProfiles() { notifierProfilesLock.readLock().lock(); try { return (Map<String, NotifierProfile>) notifierProfilesByUUID.clone(); } finally { notifierProfilesLock.readLock().unlock(); } } /** * Create an internal notifier profile from the given web-service notifier profile. * * XXX: This is dangerous business, as we count on the fact that no additional fields are added (hence validateModelIntegrity()!) * * @param node * @return TaxonomyNode */ public static NotifierProfile convertNotifierProfileWsToInternal( final com.seajas.search.attender.bridge.wsdl.profile.NotifierProfile notifierProfile) { NotifierProfile result = new NotifierProfile(); result.setUUID(notifierProfile.getUuid()); result.setEmail(notifierProfile.getEmail()); result.setEmailLanguage(notifierProfile.getEmailLanguage()); result.setQuery(notifierProfile.getQuery()); result.setSearchParameters(notifierProfile.getSearchParameters()); result.setNotificationInterval(notifierProfile.getNotificationInterval()); result.setNotificationMaximum(notifierProfile.getNotificationMaximum()); return result; } /** * Since we don't really have a better way to quickly verify integrity, we resort to doing it this way. * * @return boolean */ public static boolean validateNotifierProfileModelIntegrity() { final List<String> notifierProfileInternalFields = Arrays.asList(new String[] { "uuid", "email", "emailLanguage", "query", "searchParameters", "notificationInterval", "notificationMaximum" }); for (Field nodeField : com.seajas.search.attender.bridge.wsdl.profile.NotifierProfile.class .getDeclaredFields()) if (!notifierProfileInternalFields.contains(nodeField.getName())) { logger.error("Unable to account for field '" + nodeField.getName() + "' in NotifierProfile"); return false; } return true; } }