KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > ConnectionInterceptor


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.ScriptingElement;
19 import scriptella.spi.Connection;
20
21
22 /**
23  * Handles connections for elements being executed.
24  *
25  * @author Fyodor Kupolov
26  * @version 1.0
27  */

28 public class ConnectionInterceptor extends ElementInterceptor {
29     private ConnectionInterceptor(ExecutableElement next, String JavaDoc conId) {
30         super(next, new ConnectionDecorator(conId));
31     }
32
33     public void execute(final DynamicContext ctx) {
34         final DynamicContextDecorator ctxDecorator = getCtxDecorator();
35         ctxDecorator.setContext(ctx);
36         executeNext(ctxDecorator);
37     }
38
39     public static ExecutableElement prepare(
40             final ExecutableElement next, final ScriptingElement se) {
41         final String JavaDoc cid = se.getConnectionId();
42         if (cid == null) {
43             return next;
44         } else {
45             for (ScriptingElement s = se; (s = s.getParent()) != null;) {
46                 if (s.getConnectionId() != null) {
47                     if (cid.equals(s.getConnectionId())) {
48                         return next;
49                     }
50                     break;
51                 }
52             }
53             return new ConnectionInterceptor(next, cid);
54         }
55     }
56
57     private static class ConnectionDecorator extends DynamicContextDecorator {
58         private String JavaDoc connectionId;
59
60         public ConnectionDecorator(String JavaDoc connectionId) {
61             this.connectionId = connectionId;
62         }
63
64         @Override JavaDoc
65         public Connection getConnection() {
66             return getGlobalContext().getSession().getConnection(connectionId)
67                     .getConnection();
68         }
69     }
70 }
71
Popular Tags