2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.md.sal.dom.xsql.jdbc;
11 import java.io.BufferedInputStream;
12 import java.io.BufferedOutputStream;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.DataInputStream;
16 import java.io.DataOutputStream;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.net.ServerSocket;
20 import java.net.Socket;
21 import java.sql.Array;
23 import java.sql.CallableStatement;
25 import java.sql.Connection;
26 import java.sql.DatabaseMetaData;
27 import java.sql.NClob;
28 import java.sql.PreparedStatement;
29 import java.sql.SQLClientInfoException;
30 import java.sql.SQLException;
31 import java.sql.SQLWarning;
32 import java.sql.SQLXML;
33 import java.sql.Savepoint;
34 import java.sql.Statement;
35 import java.sql.Struct;
36 import java.util.LinkedList;
38 import java.util.Properties;
39 import java.util.concurrent.Executor;
41 import org.opendaylight.controller.md.sal.dom.xsql.XSQLAdapter;
42 import org.opendaylight.controller.md.sal.dom.xsql.XSQLBluePrint;
45 * To be removed in Nitrogen
48 public class JDBCConnection implements Connection, Runnable {
49 private Socket socket = null;
50 private DataInputStream in = null;
51 private DataOutputStream out = null;
52 private LinkedList<byte[]> queue = new LinkedList<>();
53 private XSQLAdapter adapter = null;
54 private XSQLBluePrint metaData = null;
55 private String addr = null;
56 private boolean wasClosed = false;
58 public JDBCConnection(Socket s, XSQLAdapter _a) {
62 in = new DataInputStream(
63 new BufferedInputStream(s.getInputStream()));
64 out = new DataOutputStream(new BufferedOutputStream(
65 s.getOutputStream()));
66 new JDBCObjectReader();
67 new Thread(this).start();
68 } catch (Exception err) {
69 err.printStackTrace();
73 public Connection getProxy() {
76 return (Connection) Proxy.newProxyInstance(this.getClass()
77 .getClassLoader(), new Class[] { Connection.class },
82 public JDBCConnection(String _addr) throws Exception {
87 private void init() throws Exception {
88 if (addr.startsWith("http://")) {
89 addr = addr.substring(7);
91 System.err.print("Address is:" + addr);
92 socket = new Socket(addr, 40004);
94 in = new DataInputStream(new BufferedInputStream(
95 socket.getInputStream()));
96 out = new DataOutputStream(new BufferedOutputStream(
97 socket.getOutputStream()));
98 new JDBCObjectReader();
99 new Thread(this).start();
100 } catch (Exception err) {
101 err.printStackTrace();
105 public JDBCConnection(boolean server) {
107 try (ServerSocket s = new ServerSocket(50003)) {
110 in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
111 out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
112 new JDBCObjectReader();
113 new Thread(this).start();
114 } catch (Exception err) {
115 err.printStackTrace();
118 } catch (Exception err) {
119 err.printStackTrace();
123 private boolean isStopped() {
124 if (adapter != null && adapter.stopped) {
127 if (socket == null || socket.isClosed()) {
135 while (!isStopped()) {
137 int len = in.readInt();
138 data = new byte[len];
142 } catch (Exception err) {
143 System.out.println("Connection Lost or Closed.");
146 } catch (Exception er) {
151 } catch (Exception er) {
156 } catch (Exception err2) {
163 private void addObject(byte[] data) {
164 synchronized (queue) {
170 private class JDBCObjectReader extends Thread {
172 public JDBCObjectReader() {
173 super("JDBCObjectReader");
178 while (!isStopped()) {
180 synchronized (queue) {
181 if (queue.size() == 0) {
184 } catch (Exception err) {
187 if (queue.size() > 0) {
188 data = queue.removeFirst();
192 JDBCCommand command = (JDBCCommand) deSerialize(data);
193 processCommand(command);
198 private Object deSerialize(byte data[]) {
200 ByteArrayInputStream in = new ByteArrayInputStream(data);
201 ObjectInputStream oin = new ObjectInputStream(in);
202 return oin.readObject();
203 } catch (Exception err) {
204 err.printStackTrace();
210 public void processCommand(JDBCCommand cmd) {
211 switch (cmd.getType()) {
212 case JDBCCommand.TYPE_METADATA_REPLY:
213 this.metaData = cmd.getBluePrint();
214 synchronized (this) {
218 case JDBCCommand.TYPE_METADATA:
219 send(new JDBCCommand(this.adapter.getBluePrint()));
221 case JDBCCommand.TYPE_EXECUTE_QUERY:
223 JDBCServer.execute(cmd.getRS(), adapter);
224 send(new JDBCCommand(cmd.getRS(), JDBCCommand.TYPE_QUERY_REPLY));
225 QueryUpdater u = new QueryUpdater(cmd.getRS());
226 new Thread(u).start();
227 } catch (Exception err) {
228 send(new JDBCCommand(err, cmd.getRSID()));
231 case JDBCCommand.TYPE_QUERY_REPLY:
232 JDBCResultSet rs1 = JDBCStatement.getQuery(cmd.getRS().getID());
233 rs1.updateData(cmd.getRS());
235 case JDBCCommand.TYPE_QUERY_RECORD:
236 JDBCResultSet rs2 = JDBCStatement.getQuery(cmd.getRSID());
237 rs2.addRecord(cmd.getRecord());
239 case JDBCCommand.TYPE_QUERY_FINISH:
240 JDBCResultSet rs3 = JDBCStatement.removeQuery(cmd.getRSID());
241 rs3.setFinished(true);
243 case JDBCCommand.TYPE_QUERY_ERROR:
244 System.err.println("ERROR Executing Query\n");
245 cmd.getERROR().printStackTrace();
246 JDBCResultSet rs4 = JDBCStatement.removeQuery(cmd.getRSID());
247 rs4.setError(cmd.getERROR());
248 rs4.setFinished(true);
255 private class QueryUpdater implements Runnable {
257 private JDBCResultSet rs = null;
259 public QueryUpdater(JDBCResultSet _rs) {
265 JDBCCommand rec = new JDBCCommand(rs.getCurrent(), rs.getID());
268 JDBCCommand end = new JDBCCommand(rs.getID());
273 public void send(Object o) {
275 if (this.socket == null) {
278 } catch (Exception err) {
279 err.printStackTrace();
284 ByteArrayOutputStream bout = new ByteArrayOutputStream();
285 ObjectOutputStream oout = new ObjectOutputStream(bout);
287 byte data[] = bout.toByteArray();
288 synchronized (socket) {
289 out.writeInt(data.length);
293 } catch (Exception err) {
294 err.printStackTrace();
299 public boolean isWrapperFor(Class<?> arg0) throws SQLException {
300 // TODO Auto-generated method stub
305 public <T> T unwrap(Class<T> arg0) throws SQLException {
306 // TODO Auto-generated method stub
311 public void clearWarnings() throws SQLException {
312 // TODO Auto-generated method stub
317 public void close() throws SQLException {
321 } catch (Exception err) {
327 public void commit() throws SQLException {
328 // TODO Auto-generated method stub
333 public Array createArrayOf(String typeName, Object[] elements)
334 throws SQLException {
335 // TODO Auto-generated method stub
340 public Blob createBlob() throws SQLException {
341 // TODO Auto-generated method stub
346 public Clob createClob() throws SQLException {
347 // TODO Auto-generated method stub
352 public NClob createNClob() throws SQLException {
353 // TODO Auto-generated method stub
358 public SQLXML createSQLXML() throws SQLException {
359 // TODO Auto-generated method stub
364 public Statement createStatement() throws SQLException {
365 return new JDBCStatement(this).getProxy();
369 public Statement createStatement(int resultSetType,
370 int resultSetConcurrency, int resultSetHoldability)
371 throws SQLException {
372 return new JDBCStatement(this).getProxy();
376 public Statement createStatement(int resultSetType, int resultSetConcurrency)
377 throws SQLException {
378 return new JDBCStatement(this).getProxy();
382 public Struct createStruct(String typeName, Object[] attributes)
383 throws SQLException {
384 // TODO Auto-generated method stub
389 public boolean getAutoCommit() throws SQLException {
390 // TODO Auto-generated method stub
395 public String getCatalog() throws SQLException {
396 // TODO Auto-generated method stub
401 public Properties getClientInfo() throws SQLException {
402 // TODO Auto-generated method stub
407 public String getClientInfo(String name) throws SQLException {
408 // TODO Auto-generated method stub
413 public int getHoldability() throws SQLException {
414 // TODO Auto-generated method stub
419 public DatabaseMetaData getMetaData() throws SQLException {
420 if (this.metaData == null) {
421 JDBCCommand cmd = new JDBCCommand();
422 cmd.setType(JDBCCommand.TYPE_METADATA);
423 synchronized (this) {
427 } catch (Exception err) {
428 err.printStackTrace();
436 public int getTransactionIsolation() throws SQLException {
437 // TODO Auto-generated method stub
442 public Map<String, Class<?>> getTypeMap() throws SQLException {
443 // TODO Auto-generated method stub
448 public SQLWarning getWarnings() throws SQLException {
449 // TODO Auto-generated method stub
454 public boolean isClosed() throws SQLException {
459 public boolean isReadOnly() throws SQLException {
460 // TODO Auto-generated method stub
465 public boolean isValid(int timeout) throws SQLException {
466 // TODO Auto-generated method stub
471 public String nativeSQL(String sql) throws SQLException {
472 // TODO Auto-generated method stub
477 public CallableStatement prepareCall(String sql, int resultSetType,
478 int resultSetConcurrency, int resultSetHoldability)
479 throws SQLException {
480 // TODO Auto-generated method stub
485 public CallableStatement prepareCall(String sql, int resultSetType,
486 int resultSetConcurrency) throws SQLException {
487 // TODO Auto-generated method stub
492 public CallableStatement prepareCall(String sql) throws SQLException {
493 // TODO Auto-generated method stub
498 public PreparedStatement prepareStatement(String sql, int resultSetType,
499 int resultSetConcurrency, int resultSetHoldability)
500 throws SQLException {
501 System.err.println("SQL 1=" + sql);
502 return new JDBCStatement(this, sql).getProxy();
506 public PreparedStatement prepareStatement(String sql, int resultSetType,
507 int resultSetConcurrency) throws SQLException {
508 System.err.println("SQL 2=" + sql);
509 return new JDBCStatement(this, sql).getProxy();
513 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
514 throws SQLException {
515 System.err.println("SQL 3=" + sql);
516 return new JDBCStatement(this, sql).getProxy();
520 public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
521 throws SQLException {
522 System.err.println("SQL 4=" + sql);
523 return new JDBCStatement(this, sql).getProxy();
527 public PreparedStatement prepareStatement(String sql, String[] columnNames)
528 throws SQLException {
529 System.err.println("SQL 5=" + sql);
530 return new JDBCStatement(this, sql).getProxy();
534 public PreparedStatement prepareStatement(String sql) throws SQLException {
535 System.err.println("SQL 6=" + sql);
536 return new JDBCStatement(this, sql).getProxy();
540 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
541 // TODO Auto-generated method stub
546 public void rollback() throws SQLException {
547 // TODO Auto-generated method stub
552 public void rollback(Savepoint savepoint) throws SQLException {
553 // TODO Auto-generated method stub
558 public void setAutoCommit(boolean autoCommit) throws SQLException {
559 // TODO Auto-generated method stub
564 public void setCatalog(String catalog) throws SQLException {
565 // TODO Auto-generated method stub
570 public void setClientInfo(Properties properties)
571 throws SQLClientInfoException {
572 // TODO Auto-generated method stub
577 public void setClientInfo(String name, String value)
578 throws SQLClientInfoException {
579 // TODO Auto-generated method stub
584 public void setHoldability(int holdability) throws SQLException {
585 // TODO Auto-generated method stub
590 public void setReadOnly(boolean readOnly) throws SQLException {
591 // TODO Auto-generated method stub
596 public Savepoint setSavepoint() throws SQLException {
597 // TODO Auto-generated method stub
602 public Savepoint setSavepoint(String name) throws SQLException {
603 // TODO Auto-generated method stub
608 public void setTransactionIsolation(int level) throws SQLException {
609 // TODO Auto-generated method stub
614 public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
615 // TODO Auto-generated method stub
620 public void setSchema(String schema) throws SQLException {
621 // TODO Auto-generated method stub
626 public String getSchema() throws SQLException {
627 // TODO Auto-generated method stub
632 public void abort(Executor executor) throws SQLException {
633 // TODO Auto-generated method stub
638 public void setNetworkTimeout(Executor executor, int milliseconds)
639 throws SQLException {
640 // TODO Auto-generated method stub
645 public int getNetworkTimeout() throws SQLException {
646 // TODO Auto-generated method stub