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.hibernate.strategy;
18
19 import net.sf.ehcache.hibernate.regions.EhcacheCollectionRegion;
20 import net.sf.ehcache.hibernate.regions.EhcacheEntityRegion;
21
22 import org.hibernate.cache.access.AccessType;
23 import org.hibernate.cache.access.CollectionRegionAccessStrategy;
24 import org.hibernate.cache.access.EntityRegionAccessStrategy;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /***
29 * Class implementing {@link EhcacheAccessStrategyFactory}
30 *
31 * @author Abhishek Sanoujam
32 *
33 */
34 public class EhcacheAccessStrategyFactoryImpl implements EhcacheAccessStrategyFactory {
35
36 private static final Logger LOG = LoggerFactory.getLogger(EhcacheAccessStrategyFactoryImpl.class);
37
38 /***
39 * {@inheritDoc}
40 */
41 public EntityRegionAccessStrategy createEntityRegionAccessStrategy(EhcacheEntityRegion entityRegion, AccessType accessType) {
42 if (AccessType.READ_ONLY.equals(accessType)) {
43 if (entityRegion.getCacheDataDescription().isMutable()) {
44 LOG.warn("read-only cache configured for mutable entity [" + entityRegion.getName() + "]");
45 }
46 return new ReadOnlyEhcacheEntityRegionAccessStrategy(entityRegion, entityRegion.getSettings());
47 } else if (AccessType.READ_WRITE.equals(accessType)) {
48 return new ReadWriteEhcacheEntityRegionAccessStrategy(entityRegion, entityRegion.getSettings());
49 } else if (AccessType.NONSTRICT_READ_WRITE.equals(accessType)) {
50 return new NonStrictReadWriteEhcacheEntityRegionAccessStrategy(entityRegion, entityRegion.getSettings());
51 } else if (AccessType.TRANSACTIONAL.equals(accessType)) {
52 return new TransactionalEhcacheEntityRegionAccessStrategy(entityRegion, entityRegion.getEhcache(), entityRegion.getSettings());
53 } else {
54 throw new IllegalArgumentException("unrecognized access strategy type [" + accessType + "]");
55 }
56 }
57
58 /***
59 * {@inheritDoc}
60 */
61 public CollectionRegionAccessStrategy createCollectionRegionAccessStrategy(EhcacheCollectionRegion collectionRegion,
62 AccessType accessType) {
63 if (AccessType.READ_ONLY.equals(accessType)) {
64 if (collectionRegion.getCacheDataDescription().isMutable()) {
65 LOG.warn("read-only cache configured for mutable entity [" + collectionRegion.getName() + "]");
66 }
67 return new ReadOnlyEhcacheCollectionRegionAccessStrategy(collectionRegion, collectionRegion.getSettings());
68 } else if (AccessType.READ_WRITE.equals(accessType)) {
69 return new ReadWriteEhcacheCollectionRegionAccessStrategy(collectionRegion, collectionRegion.getSettings());
70 } else if (AccessType.NONSTRICT_READ_WRITE.equals(accessType)) {
71 return new NonStrictReadWriteEhcacheCollectionRegionAccessStrategy(collectionRegion, collectionRegion.getSettings());
72 } else if (AccessType.TRANSACTIONAL.equals(accessType)) {
73 return new TransactionalEhcacheCollectionRegionAccessStrategy(collectionRegion, collectionRegion.getEhcache(), collectionRegion
74 .getSettings());
75 } else {
76 throw new IllegalArgumentException("unrecognized access strategy type [" + accessType + "]");
77 }
78 }
79
80 }