KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > spi > AbstractConnection


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.spi;
17
18 /**
19  * A base class for connections.
20  * <p>Subclassing is more safe than directly implementing {@link Connection} interface.
21  *
22  * @author Fyodor Kupolov
23  * @version 1.0
24  */

25 public abstract class AbstractConnection implements Connection {
26     private DialectIdentifier dialectIdentifier;
27     private boolean readonly;
28     //Counter for statements use counter.statements++ to update statistics
29
protected final StatementCounter counter = new StatementCounter();
30
31     /**
32      * May be used by sublasses to allow full customization
33      */

34     protected AbstractConnection() {
35     }
36
37     /**
38      * Instantiates a connection with dialectIdentifier and connection parameters.
39      * @param dialectIdentifier dialect identifier.
40      * @param parameters connection parameters to use for general properties.
41      */

42     protected AbstractConnection(DialectIdentifier dialectIdentifier, ConnectionParameters parameters) {
43         this(parameters);
44         if (dialectIdentifier==null) {
45             throw new IllegalArgumentException JavaDoc("Dialect identifier cannot be null");
46         }
47         this.dialectIdentifier = dialectIdentifier;
48     }
49
50     /**
51      * Instantiates a connection with dialectIdentifier and connection parameters.
52      * @param parameters connection parameters to use for general properties.
53      */

54     protected AbstractConnection(ConnectionParameters parameters) {
55         if (parameters==null) {
56             throw new IllegalArgumentException JavaDoc("Connection parameters cannot be null");
57         }
58         //General Scriptella property for debugging non transactional providers
59
readonly=parameters.getBooleanProperty("readonly");
60     }
61
62
63     public DialectIdentifier getDialectIdentifier() {
64         return dialectIdentifier;
65     }
66
67     protected void setDialectIdentifier(DialectIdentifier dialectIdentifier) {
68         this.dialectIdentifier = dialectIdentifier;
69     }
70
71     public long getExecutedStatementsCount() {
72         return counter.statements; //The default implementation
73
}
74
75     /**
76      * Returns readonly mode.
77      * <p>readonly=true means updates must be skipped by the driver.
78      * This property is configurable by readonly property of connection declaration element.
79      * Drivers are not required to support this feauture.
80      * @return true if connection is in readonly mode.
81      */

82     public boolean isReadonly() {
83         return readonly;
84     }
85
86     public void commit() throws ProviderException {
87     }
88
89     public void rollback() throws ProviderException, UnsupportedOperationException JavaDoc {
90         throw new UnsupportedOperationException JavaDoc("Transactions are not supported by "+toString());
91     }
92
93     public String JavaDoc toString() {
94         String JavaDoc simpleName = getClass().getSimpleName();
95         return simpleName.length() == 0 ? "connection" : simpleName;
96     }
97
98     /**
99      * Helper class to use for executed statements counting.
100      */

101     public static class StatementCounter {
102         /**
103          * Stores number of executed statements.
104          */

105         public volatile long statements;
106     }
107 }
108
Popular Tags