View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.transaction;
18  
19  import net.sf.ehcache.Element;
20  import net.sf.ehcache.search.attribute.AttributeExtractor;
21  import net.sf.ehcache.search.attribute.AttributeExtractorException;
22  import net.sf.ehcache.store.compound.ReadWriteCopyStrategy;
23  
24  /***
25   * Used to extract a search attribute value from an element in a transactional store.
26   * <p>
27   *
28   * @author Chris Dennis
29   */
30  public class TransactionAwareAttributeExtractor implements AttributeExtractor {
31  
32      private final ReadWriteCopyStrategy<Element> copyStrategy;
33      private final AttributeExtractor delegate;
34  
35      /***
36       * Creates an attributed delegating to the supplied extractor, via the given copy strategy.
37       *
38       * @param copyStrategy copy strategy used by the transactional store
39       * @param delegate original configured attribute extractor
40       */
41      public TransactionAwareAttributeExtractor(ReadWriteCopyStrategy<Element> copyStrategy, AttributeExtractor delegate) {
42          this.copyStrategy = copyStrategy;
43          this.delegate = delegate;
44      }
45  
46      /***
47       * {@inheritDoc}
48       */
49      public Object attributeFor(Element element, String attributeName) throws AttributeExtractorException {
50          Object value = element.getObjectValue();
51          if (value instanceof SoftLock) {
52              throw new AssertionError();
53          } else {
54              return delegate.attributeFor(copyStrategy.copyForRead(element), attributeName);
55          }
56      }
57  }