01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 org.compass.core.transaction.context;
18:
19: import java.util.concurrent.Callable;
20:
21: import org.compass.core.CompassException;
22: import org.compass.core.transaction.InternalCompassTransaction;
23: import org.compass.core.transaction.TransactionException;
24:
25: /**
26: * A wrapper around a delegate callable that will execute it within a transactional context.
27: *
28: * @author kimchy
29: */
30: public class TransactionalCallable<T> implements Callable<T> {
31:
32: private TransactionContext transactionContext;
33:
34: private Callable<T> delegate;
35:
36: public TransactionalCallable(TransactionContext transactionContext,
37: Callable<T> delegate) {
38: this .transactionContext = transactionContext;
39: this .delegate = delegate;
40: }
41:
42: public T call() throws Exception {
43: return transactionContext
44: .execute(new TransactionContextCallback<T>() {
45: public T doInTransaction(
46: InternalCompassTransaction tr)
47: throws CompassException {
48: try {
49: return delegate.call();
50: } catch (Exception e) {
51: throw new TransactionException(
52: "Failed to execute callable", e);
53: }
54: }
55: });
56: }
57: }
|