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 try (ServerSocket s = new ServerSocket(50003)) {
106 in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
107 out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
108 new JDBCObjectReader();
109 new Thread(this).start();
110 } catch (Exception err) {
111 err.printStackTrace();
114 } catch (Exception err) {
115 err.printStackTrace();
119 private boolean isStopped() {
120 if (adapter != null && adapter.stopped) {
123 if (socket == null || socket.isClosed()) {
131 while (!isStopped()) {
133 int len = in.readInt();
134 data = new byte[len];
138 } catch (Exception err) {
139 System.out.println("Connection Lost or Closed.");
142 } catch (Exception er) {
147 } catch (Exception er) {
152 } catch (Exception err2) {
159 private void addObject(byte[] data) {
160 synchronized (queue) {
166 private class JDBCObjectReader extends Thread {
168 public JDBCObjectReader() {
169 super("JDBCObjectReader");
174 while (!isStopped()) {
176 synchronized (queue) {
177 if (queue.size() == 0) {
180 } catch (Exception err) {
183 if (queue.size() > 0) {
184 data = queue.removeFirst();
188 JDBCCommand command = (JDBCCommand) deSerialize(data);
189 processCommand(command);
194 private Object deSerialize(byte data[]) {
196 ByteArrayInputStream in = new ByteArrayInputStream(data);
197 ObjectInputStream oin = new ObjectInputStream(in);
198 return oin.readObject();
199 } catch (Exception err) {
200 err.printStackTrace();
206 public void processCommand(JDBCCommand cmd) {
207 switch (cmd.getType()) {
208 case JDBCCommand.TYPE_METADATA_REPLY:
209 this.metaData = cmd.getBluePrint();
210 synchronized (this) {
214 case JDBCCommand.TYPE_METADATA:
215 send(new JDBCCommand(this.adapter.getBluePrint()));
217 case JDBCCommand.TYPE_EXECUTE_QUERY:
219 JDBCServer.execute(cmd.getRS(), adapter);
220 send(new JDBCCommand(cmd.getRS(), JDBCCommand.TYPE_QUERY_REPLY));
221 QueryUpdater u = new QueryUpdater(cmd.getRS());
222 new Thread(u).start();
223 } catch (Exception err) {
224 send(new JDBCCommand(err, cmd.getRSID()));
227 case JDBCCommand.TYPE_QUERY_REPLY:
228 JDBCResultSet rs1 = JDBCStatement.getQuery(cmd.getRS().getID());
229 rs1.updateData(cmd.getRS());
231 case JDBCCommand.TYPE_QUERY_RECORD:
232 JDBCResultSet rs2 = JDBCStatement.getQuery(cmd.getRSID());
233 rs2.addRecord(cmd.getRecord());
235 case JDBCCommand.TYPE_QUERY_FINISH:
236 JDBCResultSet rs3 = JDBCStatement.removeQuery(cmd.getRSID());
237 rs3.setFinished(true);
239 case JDBCCommand.TYPE_QUERY_ERROR:
240 System.err.println("ERROR Executing Query\n");
241 cmd.getERROR().printStackTrace();
242 JDBCResultSet rs4 = JDBCStatement.removeQuery(cmd.getRSID());
243 rs4.setError(cmd.getERROR());
244 rs4.setFinished(true);
251 private class QueryUpdater implements Runnable {
253 private JDBCResultSet rs = null;
255 public QueryUpdater(JDBCResultSet _rs) {
261 JDBCCommand rec = new JDBCCommand(rs.getCurrent(), rs.getID());
264 JDBCCommand end = new JDBCCommand(rs.getID());
269 public void send(Object o) {
271 if (this.socket == null) {
274 } catch (Exception err) {
275 err.printStackTrace();
280 ByteArrayOutputStream bout = new ByteArrayOutputStream();
281 ObjectOutputStream oout = new ObjectOutputStream(bout);
283 byte data[] = bout.toByteArray();
284 synchronized (socket) {
285 out.writeInt(data.length);
289 } catch (Exception err) {
290 err.printStackTrace();
295 public boolean isWrapperFor(Class<?> arg0) throws SQLException {
296 // TODO Auto-generated method stub
301 public <T> T unwrap(Class<T> arg0) throws SQLException {
302 // TODO Auto-generated method stub
307 public void clearWarnings() throws SQLException {
308 // TODO Auto-generated method stub
313 public void close() throws SQLException {
317 } catch (Exception err) {
323 public void commit() throws SQLException {
324 // TODO Auto-generated method stub
329 public Array createArrayOf(String typeName, Object[] elements)
330 throws SQLException {
331 // TODO Auto-generated method stub
336 public Blob createBlob() throws SQLException {
337 // TODO Auto-generated method stub
342 public Clob createClob() throws SQLException {
343 // TODO Auto-generated method stub
348 public NClob createNClob() throws SQLException {
349 // TODO Auto-generated method stub
354 public SQLXML createSQLXML() throws SQLException {
355 // TODO Auto-generated method stub
360 public Statement createStatement() throws SQLException {
361 return new JDBCStatement(this).getProxy();
365 public Statement createStatement(int resultSetType,
366 int resultSetConcurrency, int resultSetHoldability)
367 throws SQLException {
368 return new JDBCStatement(this).getProxy();
372 public Statement createStatement(int resultSetType, int resultSetConcurrency)
373 throws SQLException {
374 return new JDBCStatement(this).getProxy();
378 public Struct createStruct(String typeName, Object[] attributes)
379 throws SQLException {
380 // TODO Auto-generated method stub
385 public boolean getAutoCommit() throws SQLException {
386 // TODO Auto-generated method stub
391 public String getCatalog() throws SQLException {
392 // TODO Auto-generated method stub
397 public Properties getClientInfo() throws SQLException {
398 // TODO Auto-generated method stub
403 public String getClientInfo(String name) throws SQLException {
404 // TODO Auto-generated method stub
409 public int getHoldability() throws SQLException {
410 // TODO Auto-generated method stub
415 public DatabaseMetaData getMetaData() throws SQLException {
416 if (this.metaData == null) {
417 JDBCCommand cmd = new JDBCCommand();
418 cmd.setType(JDBCCommand.TYPE_METADATA);
419 synchronized (this) {
423 } catch (Exception err) {
424 err.printStackTrace();
432 public int getTransactionIsolation() throws SQLException {
433 // TODO Auto-generated method stub
438 public Map<String, Class<?>> getTypeMap() throws SQLException {
439 // TODO Auto-generated method stub
444 public SQLWarning getWarnings() throws SQLException {
445 // TODO Auto-generated method stub
450 public boolean isClosed() throws SQLException {
455 public boolean isReadOnly() throws SQLException {
456 // TODO Auto-generated method stub
461 public boolean isValid(int timeout) throws SQLException {
462 // TODO Auto-generated method stub
467 public String nativeSQL(String sql) throws SQLException {
468 // TODO Auto-generated method stub
473 public CallableStatement prepareCall(String sql, int resultSetType,
474 int resultSetConcurrency, int resultSetHoldability)
475 throws SQLException {
476 // TODO Auto-generated method stub
481 public CallableStatement prepareCall(String sql, int resultSetType,
482 int resultSetConcurrency) throws SQLException {
483 // TODO Auto-generated method stub
488 public CallableStatement prepareCall(String sql) throws SQLException {
489 // TODO Auto-generated method stub
494 public PreparedStatement prepareStatement(String sql, int resultSetType,
495 int resultSetConcurrency, int resultSetHoldability)
496 throws SQLException {
497 System.err.println("SQL 1=" + sql);
498 return new JDBCStatement(this, sql).getProxy();
502 public PreparedStatement prepareStatement(String sql, int resultSetType,
503 int resultSetConcurrency) throws SQLException {
504 System.err.println("SQL 2=" + sql);
505 return new JDBCStatement(this, sql).getProxy();
509 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
510 throws SQLException {
511 System.err.println("SQL 3=" + sql);
512 return new JDBCStatement(this, sql).getProxy();
516 public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
517 throws SQLException {
518 System.err.println("SQL 4=" + sql);
519 return new JDBCStatement(this, sql).getProxy();
523 public PreparedStatement prepareStatement(String sql, String[] columnNames)
524 throws SQLException {
525 System.err.println("SQL 5=" + sql);
526 return new JDBCStatement(this, sql).getProxy();
530 public PreparedStatement prepareStatement(String sql) throws SQLException {
531 System.err.println("SQL 6=" + sql);
532 return new JDBCStatement(this, sql).getProxy();
536 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
537 // TODO Auto-generated method stub
542 public void rollback() throws SQLException {
543 // TODO Auto-generated method stub
548 public void rollback(Savepoint savepoint) throws SQLException {
549 // TODO Auto-generated method stub
554 public void setAutoCommit(boolean autoCommit) throws SQLException {
555 // TODO Auto-generated method stub
560 public void setCatalog(String catalog) throws SQLException {
561 // TODO Auto-generated method stub
566 public void setClientInfo(Properties properties)
567 throws SQLClientInfoException {
568 // TODO Auto-generated method stub
573 public void setClientInfo(String name, String value)
574 throws SQLClientInfoException {
575 // TODO Auto-generated method stub
580 public void setHoldability(int holdability) throws SQLException {
581 // TODO Auto-generated method stub
586 public void setReadOnly(boolean readOnly) throws SQLException {
587 // TODO Auto-generated method stub
592 public Savepoint setSavepoint() throws SQLException {
593 // TODO Auto-generated method stub
598 public Savepoint setSavepoint(String name) throws SQLException {
599 // TODO Auto-generated method stub
604 public void setTransactionIsolation(int level) throws SQLException {
605 // TODO Auto-generated method stub
610 public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
611 // TODO Auto-generated method stub
616 public void setSchema(String schema) throws SQLException {
617 // TODO Auto-generated method stub
622 public String getSchema() throws SQLException {
623 // TODO Auto-generated method stub
628 public void abort(Executor executor) throws SQLException {
629 // TODO Auto-generated method stub
634 public void setNetworkTimeout(Executor executor, int milliseconds)
635 throws SQLException {
636 // TODO Auto-generated method stub
641 public int getNetworkTimeout() throws SQLException {
642 // TODO Auto-generated method stub