View Javadoc

1   /*
2    * Copyright (c) 2010.  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.hbase.HBaseConfiguration;
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.newAtomicReference();
61      private AtomicReference<List<StatementEventListener>> statementEventListenerList = AtomicReferences.newAtomicReference();
62  
63      public ConnectionImpl(final HBaseConfiguration config, final int maxPoolReferencesPerTable) throws HBqlException {
64          this.hconnectionImpl = new HConnectionImpl(config, null, maxPoolReferencesPerTable);
65      }
66  
67      public ConnectionImpl(final HConnection hconnectionImpl) {
68          this.hconnectionImpl = (HConnectionImpl)hconnectionImpl;
69      }
70  
71      public HConnectionImpl getHConnectionImpl() {
72          return this.hconnectionImpl;
73      }
74  
75      public Connection getConnection() throws SQLException {
76          return this;
77      }
78  
79      public HConnection getHConnection() {
80          return this.getHConnectionImpl();
81      }
82  
83      public Statement createStatement() throws SQLException {
84          return new StatementImpl(this);
85      }
86  
87      public PreparedStatement prepareStatement(final String sql) throws SQLException {
88          return new PreparedStatementImpl(this, sql);
89      }
90  
91      public CallableStatement prepareCall(final String s) throws SQLException {
92          throw new SQLFeatureNotSupportedException();
93      }
94  
95      public String nativeSQL(final String s) throws SQLException {
96          throw new SQLFeatureNotSupportedException();
97      }
98  
99      public void setAutoCommit(final boolean b) throws SQLException {
100         throw new SQLFeatureNotSupportedException();
101     }
102 
103     public boolean getAutoCommit() throws SQLException {
104         return true;
105     }
106 
107     public void commit() throws SQLException {
108         throw new SQLFeatureNotSupportedException();
109     }
110 
111     public void rollback() throws SQLException {
112         throw new SQLFeatureNotSupportedException();
113     }
114 
115     public void close() throws SQLException {
116         try {
117             this.getHConnectionImpl().close();
118         }
119         finally {
120             this.fireConnectionClosed();
121         }
122     }
123 
124     public boolean isClosed() throws SQLException {
125         return this.getHConnectionImpl().isClosed();
126     }
127 
128     public DatabaseMetaData getMetaData() throws SQLException {
129         return null;
130     }
131 
132     public void setReadOnly(final boolean b) throws SQLException {
133 
134     }
135 
136     public boolean isReadOnly() throws SQLException {
137         return false;
138     }
139 
140     public void setCatalog(final String s) throws SQLException {
141 
142     }
143 
144     public String getCatalog() throws SQLException {
145         return null;
146     }
147 
148     public void setTransactionIsolation(final int i) throws SQLException {
149         throw new SQLFeatureNotSupportedException();
150     }
151 
152     public int getTransactionIsolation() throws SQLException {
153         throw new SQLFeatureNotSupportedException();
154     }
155 
156     public SQLWarning getWarnings() throws SQLException {
157         return null;
158     }
159 
160     public void clearWarnings() throws SQLException {
161 
162     }
163 
164     public Statement createStatement(final int i, final int i1) throws SQLException {
165         throw new SQLFeatureNotSupportedException();
166     }
167 
168     public PreparedStatement prepareStatement(final String s, final int i, final int i1) throws SQLException {
169         throw new SQLFeatureNotSupportedException();
170     }
171 
172     public CallableStatement prepareCall(final String s, final int i, final int i1) throws SQLException {
173         throw new SQLFeatureNotSupportedException();
174     }
175 
176     public Map<String, Class<?>> getTypeMap() throws SQLException {
177         return null;
178     }
179 
180     public void setTypeMap(final Map<String, Class<?>> stringClassMap) throws SQLException {
181 
182     }
183 
184     public void setHoldability(final int i) throws SQLException {
185         throw new SQLFeatureNotSupportedException();
186     }
187 
188     public int getHoldability() throws SQLException {
189         throw new SQLFeatureNotSupportedException();
190     }
191 
192     public Savepoint setSavepoint() throws SQLException {
193         throw new SQLFeatureNotSupportedException();
194     }
195 
196     public Savepoint setSavepoint(final String s) throws SQLException {
197         throw new SQLFeatureNotSupportedException();
198     }
199 
200     public void rollback(final Savepoint savepoint) throws SQLException {
201         throw new SQLFeatureNotSupportedException();
202     }
203 
204     public void releaseSavepoint(final Savepoint savepoint) throws SQLException {
205         throw new SQLFeatureNotSupportedException();
206     }
207 
208     public Statement createStatement(final int i, final int i1, final int i2) throws SQLException {
209         throw new SQLFeatureNotSupportedException();
210     }
211 
212     public PreparedStatement prepareStatement(final String s, final int i, final int i1, final int i2) throws SQLException {
213         throw new SQLFeatureNotSupportedException();
214     }
215 
216     public CallableStatement prepareCall(final String s, final int i, final int i1, final int i2) throws SQLException {
217         throw new SQLFeatureNotSupportedException();
218     }
219 
220     public PreparedStatement prepareStatement(final String s, final int i) throws SQLException {
221         throw new SQLFeatureNotSupportedException();
222     }
223 
224     public PreparedStatement prepareStatement(final String s, final int[] ints) throws SQLException {
225         throw new SQLFeatureNotSupportedException();
226     }
227 
228     public PreparedStatement prepareStatement(final String s, final String[] strings) throws SQLException {
229         throw new SQLFeatureNotSupportedException();
230     }
231 
232     public Clob createClob() throws SQLException {
233         throw new SQLFeatureNotSupportedException();
234     }
235 
236     public Blob createBlob() throws SQLException {
237         throw new SQLFeatureNotSupportedException();
238     }
239 
240     public NClob createNClob() throws SQLException {
241         throw new SQLFeatureNotSupportedException();
242     }
243 
244     public SQLXML createSQLXML() throws SQLException {
245         throw new SQLFeatureNotSupportedException();
246     }
247 
248     public boolean isValid(final int i) throws SQLException {
249         return false;
250     }
251 
252     public void setClientInfo(final String s, final String s1) throws SQLClientInfoException {
253 
254     }
255 
256     public void setClientInfo(final Properties properties) throws SQLClientInfoException {
257 
258     }
259 
260     public String getClientInfo(final String s) throws SQLException {
261         return null;
262     }
263 
264     public Properties getClientInfo() throws SQLException {
265         return null;
266     }
267 
268     public Array createArrayOf(final String s, final Object[] objects) throws SQLException {
269         return null;
270     }
271 
272     public Struct createStruct(final String s, final Object[] objects) throws SQLException {
273         return null;
274     }
275 
276     public <T> T unwrap(final Class<T> tClass) throws SQLException {
277         return null;
278     }
279 
280     public boolean isWrapperFor(final Class<?> aClass) throws SQLException {
281         return false;
282     }
283 
284     private AtomicReference<List<ConnectionEventListener>> getAtomicConnectionEventListenerList() {
285         return this.connectionEventListenerList;
286     }
287 
288     private List<ConnectionEventListener> getConnectionEventListenerList() {
289         if (this.getAtomicConnectionEventListenerList().get() == null)
290             synchronized (this) {
291                 if (this.getAtomicConnectionEventListenerList().get() == null) {
292                     final List<ConnectionEventListener> val = Lists.newArrayList();
293                     this.getAtomicConnectionEventListenerList().set(val);
294                 }
295             }
296         return this.getAtomicConnectionEventListenerList().get();
297     }
298 
299     private void fireConnectionClosed() {
300         if (this.getAtomicConnectionEventListenerList().get() != null) {
301             for (final ConnectionEventListener listener : this.getConnectionEventListenerList())
302                 listener.connectionClosed(new ConnectionEvent(this));
303         }
304     }
305 
306     private AtomicReference<List<StatementEventListener>> getAtomicStatementEventListenerList() {
307         return this.statementEventListenerList;
308     }
309 
310     private List<StatementEventListener> getStatementEventListenerList() {
311         if (this.getAtomicStatementEventListenerList().get() == null)
312             synchronized (this) {
313                 if (this.getAtomicStatementEventListenerList().get() == null) {
314                     final List<StatementEventListener> val = Lists.newArrayList();
315                     this.getAtomicStatementEventListenerList().set(val);
316                 }
317             }
318         return this.getAtomicStatementEventListenerList().get();
319     }
320 
321     void fireStatementClosed(final PreparedStatement pstmt) {
322         if (this.getAtomicStatementEventListenerList().get() != null) {
323             for (final StatementEventListener listener : this.getStatementEventListenerList())
324                 listener.statementClosed(new StatementEvent(this, pstmt));
325         }
326     }
327 
328     public void addConnectionEventListener(final ConnectionEventListener connectionEventListener) {
329         this.getConnectionEventListenerList().add(connectionEventListener);
330     }
331 
332     public void removeConnectionEventListener(final ConnectionEventListener connectionEventListener) {
333         this.getConnectionEventListenerList().remove(connectionEventListener);
334     }
335 
336     public void addStatementEventListener(final StatementEventListener statementEventListener) {
337         this.getStatementEventListenerList().add(statementEventListener);
338     }
339 
340     public void removeStatementEventListener(final StatementEventListener statementEventListener) {
341         this.getStatementEventListenerList().remove(statementEventListener);
342     }
343 }