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 package net.sf.ehcache.transaction.xa.commands;
17
18 import net.sf.ehcache.Element;
19 import net.sf.ehcache.store.ElementValueComparator;
20 import net.sf.ehcache.store.Store;
21 import net.sf.ehcache.transaction.SoftLock;
22 import net.sf.ehcache.transaction.SoftLockFactory;
23 import net.sf.ehcache.transaction.xa.OptimisticLockFailureException;
24 import net.sf.ehcache.transaction.xa.XidTransactionID;
25
26 /***
27 * @author Ludovic Orban
28 */
29 public abstract class AbstractStoreCommand implements Command {
30
31 private final Element oldElement;
32 private final Element newElement;
33
34 private Element softLockedElement;
35
36 /***
37 * Create a Store Command
38 * @param oldElement the element in the underlying store at the time this command is created
39 * @param newElement the new element to put in the underlying store
40 */
41 public AbstractStoreCommand(final Element oldElement, final Element newElement) {
42 this.newElement = newElement;
43 this.oldElement = oldElement;
44 }
45
46 /***
47 * Get the element in the underlying store at the time this command is created
48 * @return the old element
49 */
50 protected Element getOldElement() {
51 return oldElement;
52 }
53
54 /***
55 * Get the new element to put in the underlying store
56 * @return the new element to put in the underlying store
57 */
58 protected Element getNewElement() {
59 return newElement;
60 }
61
62 /***
63 * {@inheritDoc}
64 */
65 public boolean prepare(Store store, SoftLockFactory softLockFactory, XidTransactionID transactionId,
66 ElementValueComparator comparator) {
67 Object objectKey = getObjectKey();
68
69 SoftLock softLock = softLockFactory.createSoftLock(transactionId, objectKey, newElement, oldElement);
70 softLockedElement = createElement(objectKey, softLock);
71 softLock.lock();
72 softLock.freeze();
73
74 if (oldElement == null) {
75 Element previousElement = store.putIfAbsent(softLockedElement);
76 if (previousElement != null) {
77 softLock.unfreeze();
78 softLock.unlock();
79 softLockedElement = null;
80 throw new OptimisticLockFailureException();
81 }
82 } else {
83 boolean replaced = store.replace(oldElement, softLockedElement, comparator);
84 if (!replaced) {
85 softLock.unfreeze();
86 softLock.unlock();
87 softLockedElement = null;
88 throw new OptimisticLockFailureException();
89 }
90 }
91
92 return true;
93 }
94
95 /***
96 * {@inheritDoc}
97 */
98 public void rollback(Store store) {
99 if (oldElement == null) {
100 store.remove(getObjectKey());
101 } else {
102 store.put(oldElement);
103 }
104
105 SoftLock softLock = (SoftLock) softLockedElement.getObjectValue();
106 softLock.unfreeze();
107 softLock.unlock();
108 softLockedElement = null;
109 }
110
111 private Element createElement(Object key, SoftLock softLock) {
112 Element element = new Element(key, softLock);
113 element.setEternal(true);
114 element.setPinned(true);
115 return element;
116 }
117
118 }