KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > TxInterceptor


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.core;
17
18 import scriptella.configuration.Location;
19 import scriptella.configuration.ScriptEl;
20 import scriptella.configuration.ScriptingElement;
21 import scriptella.spi.Connection;
22
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26
27 /**
28  * TODO: Add documentation
29  *
30  * @author Fyodor Kupolov
31  * @version 1.0
32  */

33 public class TxInterceptor extends ElementInterceptor {
34     private static final Logger JavaDoc LOG = Logger.getLogger(TxInterceptor.class.getName());
35     private Location location;
36
37     public TxInterceptor(ExecutableElement next, ScriptEl scriptEl) {
38         super(next, new TxDecorator(scriptEl));
39         location=scriptEl.getLocation();
40     }
41
42     public void execute(final DynamicContext ctx) {
43         final TxDecorator ctxDecorator = (TxDecorator) getCtxDecorator();
44         ctxDecorator.setContext(ctx);
45
46         try {
47             executeNext(ctxDecorator);
48         } catch (Throwable JavaDoc e) {
49             try {
50                 ctxDecorator.c.rollback();
51             } catch (Exception JavaDoc e1) {
52                 LOG.log(Level.WARNING, "Unable to rollback transaction", e1);
53             }
54             LOG.log(Level.INFO,
55                     "Script " + location + " failed during invocation in a separate transaction", e);
56         }
57     }
58
59     public static ExecutableElement prepare(
60             final ExecutableElement next, final ScriptEl s) {
61         if (s.isNewTx()) {
62             return new TxInterceptor(next, s);
63
64         } else {
65             return next;
66         }
67     }
68
69     private static class TxDecorator extends DynamicContextDecorator {
70         private Connection c;
71         private String JavaDoc connectionId;
72
73         public TxDecorator(ScriptEl script) {
74             String JavaDoc cid = script.getConnectionId();
75             //if connection id is null, iterate parents to get connection
76
if (cid == null) {
77                 for (ScriptingElement s = script; (s = s.getParent()) != null;) {
78                     cid = s.getConnectionId();
79                     if (cid != null) {
80                         break;
81                     }
82                 }
83             }
84             connectionId=cid; //If single connection script
85
}
86
87         @Override JavaDoc
88         public Connection getConnection() {
89             if (c==null) {
90                 c=getGlobalContext().getSession().getConnection(connectionId).newConnection();
91             }
92             return c;
93         }
94     }
95 }
96
Popular Tags