001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.jcs3.jcache; 020 021import java.io.Closeable; 022import java.util.ArrayList; 023import java.util.List; 024 025import javax.cache.configuration.CacheEntryListenerConfiguration; 026import javax.cache.configuration.Factory; 027import javax.cache.event.CacheEntryCreatedListener; 028import javax.cache.event.CacheEntryEvent; 029import javax.cache.event.CacheEntryEventFilter; 030import javax.cache.event.CacheEntryExpiredListener; 031import javax.cache.event.CacheEntryListener; 032import javax.cache.event.CacheEntryListenerException; 033import javax.cache.event.CacheEntryRemovedListener; 034import javax.cache.event.CacheEntryUpdatedListener; 035 036public class JCSListener<K, V> implements Closeable 037{ 038// private final boolean oldValue; 039// private final boolean synchronous; 040 private final CacheEntryEventFilter<? super K, ? super V> filter; 041 private final CacheEntryListener<? super K, ? super V> delegate; 042 private final boolean remove; 043 private final boolean expire; 044 private final boolean update; 045 private final boolean create; 046 047 public JCSListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) 048 { 049// oldValue = cacheEntryListenerConfiguration.isOldValueRequired(); 050// synchronous = cacheEntryListenerConfiguration.isSynchronous(); 051 052 final Factory<CacheEntryEventFilter<? super K, ? super V>> filterFactory = cacheEntryListenerConfiguration 053 .getCacheEntryEventFilterFactory(); 054 if (filterFactory == null) 055 { 056 filter = NoFilter.INSTANCE; 057 } 058 else 059 { 060 filter = filterFactory.create(); 061 } 062 063 delegate = cacheEntryListenerConfiguration.getCacheEntryListenerFactory().create(); 064 remove = CacheEntryRemovedListener.class.isInstance(delegate); 065 expire = CacheEntryExpiredListener.class.isInstance(delegate); 066 update = CacheEntryUpdatedListener.class.isInstance(delegate); 067 create = CacheEntryCreatedListener.class.isInstance(delegate); 068 } 069 070 public void onRemoved(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException 071 { 072 if (remove) 073 { 074 CacheEntryRemovedListener.class.cast(delegate).onRemoved(filter(events)); 075 } 076 } 077 078 public void onExpired(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException 079 { 080 if (expire) 081 { 082 CacheEntryExpiredListener.class.cast(delegate).onExpired(filter(events)); 083 } 084 } 085 086 public void onUpdated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException 087 { 088 if (update) 089 { 090 CacheEntryUpdatedListener.class.cast(delegate).onUpdated(filter(events)); 091 } 092 } 093 094 public void onCreated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException 095 { 096 if (create) 097 { 098 CacheEntryCreatedListener.class.cast(delegate).onCreated(filter(events)); 099 } 100 } 101 102 private Iterable<CacheEntryEvent<? extends K, ? extends V>> filter(final List<CacheEntryEvent<? extends K, ? extends V>> events) 103 { 104 if (filter == NoFilter.INSTANCE) 105 { 106 return events; 107 } 108 109 final List<CacheEntryEvent<? extends K, ? extends V>> filtered = new ArrayList<>( 110 events.size()); 111 for (final CacheEntryEvent<? extends K, ? extends V> event : events) 112 { 113 if (filter.evaluate(event)) 114 { 115 filtered.add(event); 116 } 117 } 118 return filtered; 119 } 120 121 @Override 122 public void close() 123 { 124 if (Closeable.class.isInstance(delegate)) { 125 Closeable.class.cast(delegate); 126 } 127 } 128}