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;
44 public class JDBCConnection implements Connection, Runnable {
45 private Socket socket = null;
46 private DataInputStream in = null;
47 private DataOutputStream out = null;
48 private LinkedList<byte[]> queue = new LinkedList<byte[]>();
49 private XSQLAdapter adapter = null;
50 private XSQLBluePrint metaData = null;
51 private String addr = null;
52 private boolean wasClosed = false;
54 public JDBCConnection(Socket s, XSQLAdapter _a) {
58 in = new DataInputStream(
59 new BufferedInputStream(s.getInputStream()));
60 out = new DataOutputStream(new BufferedOutputStream(
61 s.getOutputStream()));
62 new JDBCObjectReader();
63 new Thread(this).start();
64 } catch (Exception err) {
65 err.printStackTrace();
69 public Connection getProxy() {
72 return (Connection) Proxy.newProxyInstance(this.getClass()
73 .getClassLoader(), new Class[] { Connection.class },
78 public JDBCConnection(String _addr) throws Exception {
83 private void init() throws Exception {
84 if (addr.startsWith("http://")) {
85 addr = addr.substring(7);
87 System.err.print("Address is:" + addr);
88 socket = new Socket(addr, 40004);
90 in = new DataInputStream(new BufferedInputStream(
91 socket.getInputStream()));
92 out = new DataOutputStream(new BufferedOutputStream(
93 socket.getOutputStream()));
94 new JDBCObjectReader();
95 new Thread(this).start();
96 } catch (Exception err) {
97 err.printStackTrace();
101 public JDBCConnection(boolean server) {
103 ServerSocket s = new ServerSocket(50003);
106 in = new DataInputStream(new BufferedInputStream(
107 socket.getInputStream()));
108 out = new DataOutputStream(new BufferedOutputStream(
109 socket.getOutputStream()));
110 new JDBCObjectReader();
111 new Thread(this).start();
112 } catch (Exception err) {
113 err.printStackTrace();
115 } catch (Exception err) {
116 err.printStackTrace();
120 private boolean isStopped() {
121 if (adapter != null && adapter.stopped) {
124 if (socket == null || socket.isClosed()) {
132 while (!isStopped()) {
134 int len = in.readInt();
135 data = new byte[len];
139 } catch (Exception err) {
140 System.out.println("Connection Lost or Closed.");
143 } catch (Exception er) {
148 } catch (Exception er) {
153 } catch (Exception err2) {
160 private void addObject(byte[] data) {
161 synchronized (queue) {
167 private class JDBCObjectReader extends Thread {
169 public JDBCObjectReader() {
170 super("JDBCObjectReader");
175 while (!isStopped()) {
177 synchronized (queue) {
178 if (queue.size() == 0) {
181 } catch (Exception err) {
184 if (queue.size() > 0) {
185 data = queue.removeFirst();
189 JDBCCommand command = (JDBCCommand) deSerialize(data);
190 processCommand(command);
195 private Object deSerialize(byte data[]) {
197 ByteArrayInputStream in = new ByteArrayInputStream(data);
198 ObjectInputStream oin = new ObjectInputStream(in);
199 return oin.readObject();
200 } catch (Exception err) {
201 err.printStackTrace();
207 public void processCommand(JDBCCommand cmd) {
208 switch (cmd.getType()) {
209 case JDBCCommand.TYPE_METADATA_REPLY:
210 this.metaData = cmd.getBluePrint();
211 synchronized (this) {
215 case JDBCCommand.TYPE_METADATA:
216 send(new JDBCCommand(this.adapter.getBluePrint()));
218 case JDBCCommand.TYPE_EXECUTE_QUERY:
220 JDBCServer.execute(cmd.getRS(), adapter);
221 send(new JDBCCommand(cmd.getRS(), JDBCCommand.TYPE_QUERY_REPLY));
222 QueryUpdater u = new QueryUpdater(cmd.getRS());
223 new Thread(u).start();
224 } catch (Exception err) {
225 send(new JDBCCommand(err, cmd.getRSID()));
228 case JDBCCommand.TYPE_QUERY_REPLY:
229 JDBCResultSet rs1 = JDBCStatement.getQuery(cmd.getRS().getID());
230 rs1.updateData(cmd.getRS());
232 case JDBCCommand.TYPE_QUERY_RECORD:
233 JDBCResultSet rs2 = JDBCStatement.getQuery(cmd.getRSID());
234 rs2.addRecord(cmd.getRecord());
236 case JDBCCommand.TYPE_QUERY_FINISH:
237 JDBCResultSet rs3 = JDBCStatement.removeQuery(cmd.getRSID());
238 rs3.setFinished(true);
240 case JDBCCommand.TYPE_QUERY_ERROR:
241 System.err.println("ERROR Executing Query\n");
242 cmd.getERROR().printStackTrace();
243 JDBCResultSet rs4 = JDBCStatement.removeQuery(cmd.getRSID());
244 rs4.setError(cmd.getERROR());
245 rs4.setFinished(true);
252 private class QueryUpdater implements Runnable {
254 private JDBCResultSet rs = null;
256 public QueryUpdater(JDBCResultSet _rs) {
262 JDBCCommand rec = new JDBCCommand(rs.getCurrent(), rs.getID());
265 JDBCCommand end = new JDBCCommand(rs.getID());
270 public void send(Object o) {
272 if (this.socket == null) {
275 } catch (Exception err) {
276 err.printStackTrace();
281 ByteArrayOutputStream bout = new ByteArrayOutputStream();
282 ObjectOutputStream oout = new ObjectOutputStream(bout);
284 byte data[] = bout.toByteArray();
285 synchronized (socket) {
286 out.writeInt(data.length);
290 } catch (Exception err) {
291 err.printStackTrace();
296 public boolean isWrapperFor(Class<?> arg0) throws SQLException {
297 // TODO Auto-generated method stub
302 public <T> T unwrap(Class<T> arg0) throws SQLException {
303 // TODO Auto-generated method stub
308 public void clearWarnings() throws SQLException {
309 // TODO Auto-generated method stub
314 public void close() throws SQLException {
318 } catch (Exception err) {
324 public void commit() throws SQLException {
325 // TODO Auto-generated method stub
330 public Array createArrayOf(String typeName, Object[] elements)
331 throws SQLException {
332 // TODO Auto-generated method stub
337 public Blob createBlob() throws SQLException {
338 // TODO Auto-generated method stub
343 public Clob createClob() throws SQLException {
344 // TODO Auto-generated method stub
349 public NClob createNClob() throws SQLException {
350 // TODO Auto-generated method stub
355 public SQLXML createSQLXML() throws SQLException {
356 // TODO Auto-generated method stub
361 public Statement createStatement() throws SQLException {
362 return new JDBCStatement(this).getProxy();
366 public Statement createStatement(int resultSetType,
367 int resultSetConcurrency, int resultSetHoldability)
368 throws SQLException {
369 return new JDBCStatement(this).getProxy();
373 public Statement createStatement(int resultSetType, int resultSetConcurrency)
374 throws SQLException {
375 return new JDBCStatement(this).getProxy();
379 public Struct createStruct(String typeName, Object[] attributes)
380 throws SQLException {
381 // TODO Auto-generated method stub
386 public boolean getAutoCommit() throws SQLException {
387 // TODO Auto-generated method stub
392 public String getCatalog() throws SQLException {
393 // TODO Auto-generated method stub
398 public Properties getClientInfo() throws SQLException {
399 // TODO Auto-generated method stub
404 public String getClientInfo(String name) throws SQLException {
405 // TODO Auto-generated method stub
410 public int getHoldability() throws SQLException {
411 // TODO Auto-generated method stub
416 public DatabaseMetaData getMetaData() throws SQLException {
417 if (this.metaData == null) {
418 JDBCCommand cmd = new JDBCCommand();
419 cmd.setType(JDBCCommand.TYPE_METADATA);
420 synchronized (this) {
424 } catch (Exception err) {
425 err.printStackTrace();
433 public int getTransactionIsolation() throws SQLException {
434 // TODO Auto-generated method stub
439 public Map<String, Class<?>> getTypeMap() throws SQLException {
440 // TODO Auto-generated method stub
445 public SQLWarning getWarnings() throws SQLException {
446 // TODO Auto-generated method stub
451 public boolean isClosed() throws SQLException {
456 public boolean isReadOnly() throws SQLException {
457 // TODO Auto-generated method stub
462 public boolean isValid(int timeout) throws SQLException {
463 // TODO Auto-generated method stub
468 public String nativeSQL(String sql) throws SQLException {
469 // TODO Auto-generated method stub
474 public CallableStatement prepareCall(String sql, int resultSetType,
475 int resultSetConcurrency, int resultSetHoldability)
476 throws SQLException {
477 // TODO Auto-generated method stub
482 public CallableStatement prepareCall(String sql, int resultSetType,
483 int resultSetConcurrency) throws SQLException {
484 // TODO Auto-generated method stub
489 public CallableStatement prepareCall(String sql) throws SQLException {
490 // TODO Auto-generated method stub
495 public PreparedStatement prepareStatement(String sql, int resultSetType,
496 int resultSetConcurrency, int resultSetHoldability)
497 throws SQLException {
498 System.err.println("SQL 1=" + sql);
499 return new JDBCStatement(this, sql).getProxy();
503 public PreparedStatement prepareStatement(String sql, int resultSetType,
504 int resultSetConcurrency) throws SQLException {
505 System.err.println("SQL 2=" + sql);
506 return new JDBCStatement(this, sql).getProxy();
510 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
511 throws SQLException {
512 System.err.println("SQL 3=" + sql);
513 return new JDBCStatement(this, sql).getProxy();
517 public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
518 throws SQLException {
519 System.err.println("SQL 4=" + sql);
520 return new JDBCStatement(this, sql).getProxy();
524 public PreparedStatement prepareStatement(String sql, String[] columnNames)
525 throws SQLException {
526 System.err.println("SQL 5=" + sql);
527 return new JDBCStatement(this, sql).getProxy();
531 public PreparedStatement prepareStatement(String sql) throws SQLException {
532 System.err.println("SQL 6=" + sql);
533 return new JDBCStatement(this, sql).getProxy();
537 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
538 // TODO Auto-generated method stub
543 public void rollback() throws SQLException {
544 // TODO Auto-generated method stub
549 public void rollback(Savepoint savepoint) throws SQLException {
550 // TODO Auto-generated method stub
555 public void setAutoCommit(boolean autoCommit) throws SQLException {
556 // TODO Auto-generated method stub
561 public void setCatalog(String catalog) throws SQLException {
562 // TODO Auto-generated method stub
567 public void setClientInfo(Properties properties)
568 throws SQLClientInfoException {
569 // TODO Auto-generated method stub
574 public void setClientInfo(String name, String value)
575 throws SQLClientInfoException {
576 // TODO Auto-generated method stub
581 public void setHoldability(int holdability) throws SQLException {
582 // TODO Auto-generated method stub
587 public void setReadOnly(boolean readOnly) throws SQLException {
588 // TODO Auto-generated method stub
593 public Savepoint setSavepoint() throws SQLException {
594 // TODO Auto-generated method stub
599 public Savepoint setSavepoint(String name) throws SQLException {
600 // TODO Auto-generated method stub
605 public void setTransactionIsolation(int level) throws SQLException {
606 // TODO Auto-generated method stub
611 public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
612 // TODO Auto-generated method stub
617 public void setSchema(String schema) throws SQLException {
618 // TODO Auto-generated method stub
623 public String getSchema() throws SQLException {
624 // TODO Auto-generated method stub
629 public void abort(Executor executor) throws SQLException {
630 // TODO Auto-generated method stub
635 public void setNetworkTimeout(Executor executor, int milliseconds)
636 throws SQLException {
637 // TODO Auto-generated method stub
642 public int getNetworkTimeout() throws SQLException {
643 // TODO Auto-generated method stub