KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > DynamicContextDecorator


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.spi.Connection;
19
20
21 /**
22  * Allows to change behaviour of wrapped DynamicContext.
23  * <p>Based on Decorator (from GOF) design pattern.
24  * <p>Mostly intended to be used by {@link ElementInterceptor interceptors}.
25  *
26  * @author Fyodor Kupolov
27  * @version 1.0
28  */

29 public class DynamicContextDecorator extends DynamicContext {
30     private DynamicContext context;
31     private Connection cachedConnection;
32
33     public DynamicContextDecorator(DynamicContext context) {
34         setContext(context);
35     }
36
37     /**
38      * Specific constructor allows to set context later.
39      * <p>Used for performance reasons to allow deferred initialization and dynamic context change.
40      */

41     DynamicContextDecorator() {
42     }
43
44     @Override JavaDoc
45     public Object JavaDoc getParameter(final String JavaDoc name) {
46         return context.getParameter(name);
47     }
48
49     @Override JavaDoc
50     public Connection getConnection() {
51         if (cachedConnection==null) {
52             cachedConnection=context.getConnection();
53         }
54         return cachedConnection;
55     }
56
57
58     /**
59      * Dynamically changes context beign decorated.
60      * <p>Should be used with caution mostly for performance reasosns.
61      *
62      * @param context new context to decorate.
63      */

64     void setContext(final DynamicContext context) {
65         this.context = context;
66         globalContext = context.getGlobalContext();
67         cachedConnection = null;
68     }
69
70 }
71
Popular Tags