View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.jdbc.impl;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.client.HConnection;
26  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
27  import org.apache.hadoop.hbase.hbql.util.AtomicReferences;
28  import org.apache.hadoop.hbase.hbql.util.Lists;
29  
30  import javax.sql.ConnectionEvent;
31  import javax.sql.ConnectionEventListener;
32  import javax.sql.PooledConnection;
33  import javax.sql.StatementEvent;
34  import javax.sql.StatementEventListener;
35  import java.sql.Array;
36  import java.sql.Blob;
37  import java.sql.CallableStatement;
38  import java.sql.Clob;
39  import java.sql.Connection;
40  import java.sql.DatabaseMetaData;
41  import java.sql.NClob;
42  import java.sql.PreparedStatement;
43  import java.sql.SQLClientInfoException;
44  import java.sql.SQLException;
45  import java.sql.SQLFeatureNotSupportedException;
46  import java.sql.SQLWarning;
47  import java.sql.SQLXML;
48  import java.sql.Savepoint;
49  import java.sql.Statement;
50  import java.sql.Struct;
51  import java.util.List;
52  import java.util.Map;
53  import java.util.Properties;
54  import java.util.concurrent.atomic.AtomicReference;
55  
56  public class ConnectionImpl implements Connection, PooledConnection {
57  
58      private final HConnectionImpl hconnectionImpl;
59  
60      private AtomicReference<List<ConnectionEventListener>> connectionEventListenerList = AtomicReferences
61              .newAtomicReference();
62      private AtomicReference<List<StatementEventListener>>  statementEventListenerList  = AtomicReferences
63              .newAtomicReference();
64  
65      public ConnectionImpl(final Configuration config, final int maxPoolReferencesPerTable) throws HBqlException {
66          this.hconnectionImpl = new HConnectionImpl(config, null, maxPoolReferencesPerTable);
67      }
68  
69      public ConnectionImpl(final HConnection hconnectionImpl) {
70          this.hconnectionImpl = (HConnectionImpl)hconnectionImpl;
71      }
72  
73      public HConnectionImpl getHConnectionImpl() {
74          return this.hconnectionImpl;
75      }
76  
77      public Connection getConnection() throws SQLException {
78          return this;
79      }
80  
81      public HConnection getHConnection() {
82          return this.getHConnectionImpl();
83      }
84  
85      public Statement createStatement() throws SQLException {
86          return new StatementImpl(this);
87      }
88  
89      public PreparedStatement prepareStatement(final String sql) throws SQLException {
90          return new PreparedStatementImpl(this, sql);
91      }
92  
93      public CallableStatement prepareCall(final String s) throws SQLException {
94          throw new SQLFeatureNotSupportedException();
95      }
96  
97      public String nativeSQL(final String s) throws SQLException {
98          throw new SQLFeatureNotSupportedException();
99      }
100 
101     public void setAutoCommit(final boolean b) throws SQLException {
102         throw new SQLFeatureNotSupportedException();
103     }
104 
105     public boolean getAutoCommit() throws SQLException {
106         return true;
107     }
108 
109     public void commit() throws SQLException {
110         throw new SQLFeatureNotSupportedException();
111     }
112 
113     public void rollback() throws SQLException {
114         throw new SQLFeatureNotSupportedException();
115     }
116 
117     public void close() throws SQLException {
118         try {
119             this.getHConnectionImpl().close();
120         }
121         finally {
122             this.fireConnectionClosed();
123         }
124     }
125 
126     public boolean isClosed() throws SQLException {
127         return this.getHConnectionImpl().isClosed();
128     }
129 
130     public DatabaseMetaData getMetaData() throws SQLException {
131         return null;
132     }
133 
134     public void setReadOnly(final boolean b) throws SQLException {
135 
136     }
137 
138     public boolean isReadOnly() throws SQLException {
139         return false;
140     }
141 
142     public void setCatalog(final String s) throws SQLException {
143 
144     }
145 
146     public String getCatalog() throws SQLException {
147         return null;
148     }
149 
150     public void setTransactionIsolation(final int i) throws SQLException {
151         throw new SQLFeatureNotSupportedException();
152     }
153 
154     public int getTransactionIsolation() throws SQLException {
155         throw new SQLFeatureNotSupportedException();
156     }
157 
158     public SQLWarning getWarnings() throws SQLException {
159         return null;
160     }
161 
162     public void clearWarnings() throws SQLException {
163 
164     }
165 
166     public Statement createStatement(final int i, final int i1) throws SQLException {
167         throw new SQLFeatureNotSupportedException();
168     }
169 
170     public PreparedStatement prepareStatement(final String s, final int i, final int i1) throws SQLException {
171         throw new SQLFeatureNotSupportedException();
172     }
173 
174     public CallableStatement prepareCall(final String s, final int i, final int i1) throws SQLException {
175         throw new SQLFeatureNotSupportedException();
176     }
177 
178     public Map<String, Class<?>> getTypeMap() throws SQLException {
179         return null;
180     }
181 
182     public void setTypeMap(final Map<String, Class<?>> stringClassMap) throws SQLException {
183 
184     }
185 
186     public void setHoldability(final int i) throws SQLException {
187         throw new SQLFeatureNotSupportedException();
188     }
189 
190     public int getHoldability() throws SQLException {
191         throw new SQLFeatureNotSupportedException();
192     }
193 
194     public Savepoint setSavepoint() throws SQLException {
195         throw new SQLFeatureNotSupportedException();
196     }
197 
198     public Savepoint setSavepoint(final String s) throws SQLException {
199         throw new SQLFeatureNotSupportedException();
200     }
201 
202     public void rollback(final Savepoint savepoint) throws SQLException {
203         throw new SQLFeatureNotSupportedException();
204     }
205 
206     public void releaseSavepoint(final Savepoint savepoint) throws SQLException {
207         throw new SQLFeatureNotSupportedException();
208     }
209 
210     public Statement createStatement(final int i, final int i1, final int i2) throws SQLException {
211         throw new SQLFeatureNotSupportedException();
212     }
213 
214     public PreparedStatement prepareStatement(final String s, final int i, final int i1, final int i2) throws SQLException {
215         throw new SQLFeatureNotSupportedException();
216     }
217 
218     public CallableStatement prepareCall(final String s, final int i, final int i1, final int i2) throws SQLException {
219         throw new SQLFeatureNotSupportedException();
220     }
221 
222     public PreparedStatement prepareStatement(final String s, final int i) throws SQLException {
223         throw new SQLFeatureNotSupportedException();
224     }
225 
226     public PreparedStatement prepareStatement(final String s, final int[] ints) throws SQLException {
227         throw new SQLFeatureNotSupportedException();
228     }
229 
230     public PreparedStatement prepareStatement(final String s, final String[] strings) throws SQLException {
231         throw new SQLFeatureNotSupportedException();
232     }
233 
234     public Clob createClob() throws SQLException {
235         throw new SQLFeatureNotSupportedException();
236     }
237 
238     public Blob createBlob() throws SQLException {
239         throw new SQLFeatureNotSupportedException();
240     }
241 
242     public NClob createNClob() throws SQLException {
243         throw new SQLFeatureNotSupportedException();
244     }
245 
246     public SQLXML createSQLXML() throws SQLException {
247         throw new SQLFeatureNotSupportedException();
248     }
249 
250     public boolean isValid(final int i) throws SQLException {
251         return false;
252     }
253 
254     public void setClientInfo(final String s, final String s1) throws SQLClientInfoException {
255 
256     }
257 
258     public void setClientInfo(final Properties properties) throws SQLClientInfoException {
259 
260     }
261 
262     public String getClientInfo(final String s) throws SQLException {
263         return null;
264     }
265 
266     public Properties getClientInfo() throws SQLException {
267         return null;
268     }
269 
270     public Array createArrayOf(final String s, final Object[] objects) throws SQLException {
271         return null;
272     }
273 
274     public Struct createStruct(final String s, final Object[] objects) throws SQLException {
275         return null;
276     }
277 
278     public <T> T unwrap(final Class<T> tClass) throws SQLException {
279         return null;
280     }
281 
282     public boolean isWrapperFor(final Class<?> aClass) throws SQLException {
283         return false;
284     }
285 
286     private AtomicReference<List<ConnectionEventListener>> getAtomicConnectionEventListenerList() {
287         return this.connectionEventListenerList;
288     }
289 
290     private List<ConnectionEventListener> getConnectionEventListenerList() {
291         if (this.getAtomicConnectionEventListenerList().get() == null)
292             synchronized (this) {
293                 if (this.getAtomicConnectionEventListenerList().get() == null) {
294                     final List<ConnectionEventListener> val = Lists.newArrayList();
295                     this.getAtomicConnectionEventListenerList().set(val);
296                 }
297             }
298         return this.getAtomicConnectionEventListenerList().get();
299     }
300 
301     private void fireConnectionClosed() {
302         if (this.getAtomicConnectionEventListenerList().get() != null) {
303             for (final ConnectionEventListener listener : this.getConnectionEventListenerList())
304                 listener.connectionClosed(new ConnectionEvent(this));
305         }
306     }
307 
308     private AtomicReference<List<StatementEventListener>> getAtomicStatementEventListenerList() {
309         return this.statementEventListenerList;
310     }
311 
312     private List<StatementEventListener> getStatementEventListenerList() {
313         if (this.getAtomicStatementEventListenerList().get() == null)
314             synchronized (this) {
315                 if (this.getAtomicStatementEventListenerList().get() == null) {
316                     final List<StatementEventListener> val = Lists.newArrayList();
317                     this.getAtomicStatementEventListenerList().set(val);
318                 }
319             }
320         return this.getAtomicStatementEventListenerList().get();
321     }
322 
323     void fireStatementClosed(final PreparedStatement pstmt) {
324         if (this.getAtomicStatementEventListenerList().get() != null) {
325             for (final StatementEventListener listener : this.getStatementEventListenerList())
326                 listener.statementClosed(new StatementEvent(this, pstmt));
327         }
328     }
329 
330     public void addConnectionEventListener(final ConnectionEventListener connectionEventListener) {
331         this.getConnectionEventListenerList().add(connectionEventListener);
332     }
333 
334     public void removeConnectionEventListener(final ConnectionEventListener connectionEventListener) {
335         this.getConnectionEventListenerList().remove(connectionEventListener);
336     }
337 
338     public void addStatementEventListener(final StatementEventListener statementEventListener) {
339         this.getStatementEventListenerList().add(statementEventListener);
340     }
341 
342     public void removeStatementEventListener(final StatementEventListener statementEventListener) {
343         this.getStatementEventListenerList().remove(statementEventListener);
344     }
345 }