View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs.jcache;
20  
21  import javax.cache.configuration.CacheEntryListenerConfiguration;
22  import javax.cache.configuration.Factory;
23  import javax.cache.event.CacheEntryCreatedListener;
24  import javax.cache.event.CacheEntryEvent;
25  import javax.cache.event.CacheEntryEventFilter;
26  import javax.cache.event.CacheEntryExpiredListener;
27  import javax.cache.event.CacheEntryListener;
28  import javax.cache.event.CacheEntryListenerException;
29  import javax.cache.event.CacheEntryRemovedListener;
30  import javax.cache.event.CacheEntryUpdatedListener;
31  import java.io.Closeable;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  public class JCSListener<K, V> implements Closeable
36  {
37      private final boolean oldValue;
38      private final boolean synchronous;
39      private final CacheEntryEventFilter<? super K, ? super V> filter;
40      private final CacheEntryListener<? super K, ? super V> delegate;
41      private final boolean remove;
42      private final boolean expire;
43      private final boolean update;
44      private final boolean create;
45  
46      public JCSListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
47      {
48          oldValue = cacheEntryListenerConfiguration.isOldValueRequired();
49          synchronous = cacheEntryListenerConfiguration.isSynchronous();
50  
51          final Factory<CacheEntryEventFilter<? super K, ? super V>> filterFactory = cacheEntryListenerConfiguration
52                  .getCacheEntryEventFilterFactory();
53          if (filterFactory == null)
54          {
55              filter = NoFilter.INSTANCE;
56          }
57          else
58          {
59              filter = filterFactory.create();
60          }
61  
62          delegate = cacheEntryListenerConfiguration.getCacheEntryListenerFactory().create();
63          remove = CacheEntryRemovedListener.class.isInstance(delegate);
64          expire = CacheEntryExpiredListener.class.isInstance(delegate);
65          update = CacheEntryUpdatedListener.class.isInstance(delegate);
66          create = CacheEntryCreatedListener.class.isInstance(delegate);
67      }
68  
69      public void onRemoved(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
70      {
71          if (remove)
72          {
73              CacheEntryRemovedListener.class.cast(delegate).onRemoved(filter(events));
74          }
75      }
76  
77      public void onExpired(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
78      {
79          if (expire)
80          {
81              CacheEntryExpiredListener.class.cast(delegate).onExpired(filter(events));
82          }
83      }
84  
85      public void onUpdated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
86      {
87          if (update)
88          {
89              CacheEntryUpdatedListener.class.cast(delegate).onUpdated(filter(events));
90          }
91      }
92  
93      public void onCreated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
94      {
95          if (create)
96          {
97              CacheEntryCreatedListener.class.cast(delegate).onCreated(filter(events));
98          }
99      }
100 
101     private Iterable<CacheEntryEvent<? extends K, ? extends V>> filter(final List<CacheEntryEvent<? extends K, ? extends V>> events)
102     {
103         if (filter == NoFilter.INSTANCE)
104         {
105             return events;
106         }
107 
108         final List<CacheEntryEvent<? extends K, ? extends V>> filtered = new ArrayList<CacheEntryEvent<? extends K, ? extends V>>(
109                 events.size());
110         for (final CacheEntryEvent<? extends K, ? extends V> event : events)
111         {
112             if (filter.evaluate(event))
113             {
114                 filtered.add(event);
115             }
116         }
117         return filtered;
118     }
119 
120     @Override
121     public void close()
122     {
123         if (Closeable.class.isInstance(delegate)) {
124             Closeable.class.cast(delegate);
125         }
126     }
127 }