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.distribution;
18
19 import net.sf.ehcache.Ehcache;
20 import net.sf.ehcache.Element;
21 import net.sf.ehcache.util.CacheTransactionHelper;
22
23 import java.io.Serializable;
24 import java.rmi.RemoteException;
25 import java.util.List;
26
27 /***
28 * An RMI based implementation of <code>CachePeer</code> supporting transactions.
29 *
30 * @author Ludovic Orban
31 */
32 public class TransactionalRMICachePeer extends RMICachePeer {
33
34 private final Ehcache cache;
35
36 /***
37 * Construct a new remote peer supporting transactions
38 *
39 * @param cache The cache attached to the peer
40 * @param hostName The host name the peer is running on.
41 * @param rmiRegistryPort The port number on which the RMI Registry listens. Should be an unused port in
42 * the range 1025 - 65536
43 * @param remoteObjectPort the port number on which the remote objects bound in the registry receive calls.
44 * This defaults to a free port if not specified.
45 * Should be an unused port in the range 1025 - 65536
46 * @param socketTimeoutMillis
47 * @throws java.rmi.RemoteException
48 */
49 public TransactionalRMICachePeer(Ehcache cache, String hostName, Integer rmiRegistryPort,
50 Integer remoteObjectPort, Integer socketTimeoutMillis) throws RemoteException {
51 super(cache, hostName, rmiRegistryPort, remoteObjectPort, socketTimeoutMillis);
52 this.cache = cache;
53 }
54
55 @Override
56 public List getKeys() throws RemoteException {
57 boolean started = CacheTransactionHelper.isTransactionStarted(cache);
58 if (!started) {
59 CacheTransactionHelper.beginTransactionIfNeeded(cache);
60 }
61
62 try {
63 return super.getKeys();
64 } finally {
65 if (!started) {
66 CacheTransactionHelper.commitTransactionIfNeeded(cache);
67 }
68 }
69 }
70
71 @Override
72 public Element getQuiet(Serializable key) throws RemoteException {
73 boolean started = CacheTransactionHelper.isTransactionStarted(cache);
74 if (!started) {
75 CacheTransactionHelper.beginTransactionIfNeeded(cache);
76 }
77
78 try {
79 return super.getQuiet(key);
80 } finally {
81 if (!started) {
82 CacheTransactionHelper.commitTransactionIfNeeded(cache);
83 }
84 }
85 }
86
87 @Override
88 public List getElements(List keys) throws RemoteException {
89 boolean started = CacheTransactionHelper.isTransactionStarted(cache);
90 if (!started) {
91 CacheTransactionHelper.beginTransactionIfNeeded(cache);
92 }
93
94 try {
95 return super.getElements(keys);
96 } finally {
97 if (!started) {
98 CacheTransactionHelper.commitTransactionIfNeeded(cache);
99 }
100 }
101 }
102
103 @Override
104 public void put(Element element) throws RemoteException, IllegalArgumentException, IllegalStateException {
105 boolean started = CacheTransactionHelper.isTransactionStarted(cache);
106 if (!started) {
107 CacheTransactionHelper.beginTransactionIfNeeded(cache);
108 }
109
110 try {
111 super.put(element);
112 } finally {
113 if (!started) {
114 CacheTransactionHelper.commitTransactionIfNeeded(cache);
115 }
116 }
117 }
118
119 @Override
120 public boolean remove(Serializable key) throws RemoteException, IllegalStateException {
121 boolean started = CacheTransactionHelper.isTransactionStarted(cache);
122 if (!started) {
123 CacheTransactionHelper.beginTransactionIfNeeded(cache);
124 }
125
126 try {
127 return super.remove(key);
128 } finally {
129 if (!started) {
130 CacheTransactionHelper.commitTransactionIfNeeded(cache);
131 }
132 }
133 }
134
135 @Override
136 public void removeAll() throws RemoteException, IllegalStateException {
137 boolean started = CacheTransactionHelper.isTransactionStarted(cache);
138 if (!started) {
139 CacheTransactionHelper.beginTransactionIfNeeded(cache);
140 }
141
142 try {
143 super.removeAll();
144 } finally {
145 if (!started) {
146 CacheTransactionHelper.commitTransactionIfNeeded(cache);
147 }
148 }
149 }
150
151 @Override
152 public void send(List eventMessages) throws RemoteException {
153 boolean started = CacheTransactionHelper.isTransactionStarted(cache);
154 if (!started) {
155 CacheTransactionHelper.beginTransactionIfNeeded(cache);
156 }
157
158 try {
159 super.send(eventMessages);
160 } finally {
161 if (!started) {
162 CacheTransactionHelper.commitTransactionIfNeeded(cache);
163 }
164 }
165 }
166 }