X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-xsql%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fxsql%2Fjdbc%2FJDBCConnection.java;h=a6b5ca60638a803432839425706337e1c4726c56;hp=3e72dc95ee83a572d9b45f240afcf5b34468df0c;hb=2fb20e0091de8945147ca984721b23d28161aa8d;hpb=ff2f98614e20366d532439b73d9a51470210ae61 diff --git a/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/jdbc/JDBCConnection.java b/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/jdbc/JDBCConnection.java index 3e72dc95ee..a6b5ca6063 100644 --- a/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/jdbc/JDBCConnection.java +++ b/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/jdbc/JDBCConnection.java @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + package org.opendaylight.controller.md.sal.dom.xsql.jdbc; import java.io.BufferedInputStream; @@ -31,38 +39,60 @@ import java.util.Properties; import java.util.concurrent.Executor; import org.opendaylight.controller.md.sal.dom.xsql.XSQLAdapter; +import org.opendaylight.controller.md.sal.dom.xsql.XSQLBluePrint; -public class JDBCConnection extends Thread implements Connection { +public class JDBCConnection implements Connection, Runnable { private Socket socket = null; private DataInputStream in = null; private DataOutputStream out = null; private LinkedList queue = new LinkedList(); private XSQLAdapter adapter = null; + private XSQLBluePrint metaData = null; + private String addr = null; + private boolean wasClosed = false; public JDBCConnection(Socket s, XSQLAdapter _a) { this.socket = s; this.adapter = _a; try { in = new DataInputStream( - new BufferedInputStream(s.getInputStream())); - out = new DataOutputStream( - new BufferedOutputStream(s.getOutputStream())); + new BufferedInputStream(s.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream( + s.getOutputStream())); new JDBCObjectReader(); - this.start(); + new Thread(this).start(); } catch (Exception err) { err.printStackTrace(); } } - public JDBCConnection(String addr) throws Exception { + public Connection getProxy() { + return this; + /* + return (Connection) Proxy.newProxyInstance(this.getClass() + .getClassLoader(), new Class[] { Connection.class }, + new JDBCProxy(this)); + */ + } + + public JDBCConnection(String _addr) throws Exception { + this.addr = _addr; + init(); + } + + private void init() throws Exception { + if (addr.startsWith("http://")) { + addr = addr.substring(7); + } + System.err.print("Address is:" + addr); socket = new Socket(addr, 40004); try { - in = new DataInputStream( - new BufferedInputStream(socket.getInputStream())); - out = new DataOutputStream( - new BufferedOutputStream(socket.getOutputStream())); + in = new DataInputStream(new BufferedInputStream( + socket.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream( + socket.getOutputStream())); new JDBCObjectReader(); - this.start(); + new Thread(this).start(); } catch (Exception err) { err.printStackTrace(); } @@ -70,24 +100,22 @@ public class JDBCConnection extends Thread implements Connection { public JDBCConnection(boolean server) { try { - ServerSocket s = new ServerSocket(50003); - socket = s.accept(); - try { - in = new DataInputStream( - new BufferedInputStream(socket.getInputStream())); - out = new DataOutputStream( - new BufferedOutputStream(socket.getOutputStream())); - new JDBCObjectReader(); - this.start(); - } catch (Exception err) { - err.printStackTrace(); + try (ServerSocket s = new ServerSocket(50003)) { + socket = s.accept(); + try { + in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); + new JDBCObjectReader(); + new Thread(this).start(); + } catch (Exception err) { + err.printStackTrace(); + } } } catch (Exception err) { err.printStackTrace(); } } - private boolean isStopped() { if (adapter != null && adapter.stopped) { return true; @@ -109,11 +137,21 @@ public class JDBCConnection extends Thread implements Connection { } catch (Exception err) { System.out.println("Connection Lost or Closed."); + try { + out.close(); + } catch (Exception er) { + } + out = null; + try { + in.close(); + } catch (Exception er) { + } + in = null; try { socket.close(); } catch (Exception err2) { } - //err.printStackTrace(); + socket = null; } } } @@ -167,38 +205,46 @@ public class JDBCConnection extends Thread implements Connection { public void processCommand(JDBCCommand cmd) { switch (cmd.getType()) { - case JDBCCommand.TYPE_EXECUTE_QUERY: - try { - JDBCServer.execute(cmd.getRS(), adapter); - send(new JDBCCommand(cmd.getRS(), - JDBCCommand.TYPE_QUERY_REPLY)); - QueryUpdater u = new QueryUpdater(cmd.getRS()); - new Thread(u).start(); - } catch (Exception err) { - send(new JDBCCommand(err, cmd.getRSID())); - } - break; - case JDBCCommand.TYPE_QUERY_REPLY: - JDBCResultSet rs1 = JDBCStatement.getQuery(cmd.getRS().getID()); - rs1.updateData(cmd.getRS()); - break; - case JDBCCommand.TYPE_QUERY_RECORD: - JDBCResultSet rs2 = JDBCStatement.getQuery(cmd.getRSID()); - rs2.addRecord(cmd.getRecord()); - break; - case JDBCCommand.TYPE_QUERY_FINISH: - JDBCResultSet rs3 = JDBCStatement.removeQuery(cmd.getRSID()); - rs3.setFinished(true); - break; - case JDBCCommand.TYPE_QUERY_ERROR: - System.err.println("ERROR Executing Query\n"); - cmd.getERROR().printStackTrace(); - JDBCResultSet rs4 = JDBCStatement.removeQuery(cmd.getRSID()); - rs4.setError(cmd.getERROR()); - rs4.setFinished(true); - synchronized (rs4) { - rs4.notifyAll(); - } + case JDBCCommand.TYPE_METADATA_REPLY: + this.metaData = cmd.getBluePrint(); + synchronized (this) { + this.notifyAll(); + } + break; + case JDBCCommand.TYPE_METADATA: + send(new JDBCCommand(this.adapter.getBluePrint())); + break; + case JDBCCommand.TYPE_EXECUTE_QUERY: + try { + JDBCServer.execute(cmd.getRS(), adapter); + send(new JDBCCommand(cmd.getRS(), JDBCCommand.TYPE_QUERY_REPLY)); + QueryUpdater u = new QueryUpdater(cmd.getRS()); + new Thread(u).start(); + } catch (Exception err) { + send(new JDBCCommand(err, cmd.getRSID())); + } + break; + case JDBCCommand.TYPE_QUERY_REPLY: + JDBCResultSet rs1 = JDBCStatement.getQuery(cmd.getRS().getID()); + rs1.updateData(cmd.getRS()); + break; + case JDBCCommand.TYPE_QUERY_RECORD: + JDBCResultSet rs2 = JDBCStatement.getQuery(cmd.getRSID()); + rs2.addRecord(cmd.getRecord()); + break; + case JDBCCommand.TYPE_QUERY_FINISH: + JDBCResultSet rs3 = JDBCStatement.removeQuery(cmd.getRSID()); + rs3.setFinished(true); + break; + case JDBCCommand.TYPE_QUERY_ERROR: + System.err.println("ERROR Executing Query\n"); + cmd.getERROR().printStackTrace(); + JDBCResultSet rs4 = JDBCStatement.removeQuery(cmd.getRSID()); + rs4.setError(cmd.getERROR()); + rs4.setFinished(true); + synchronized (rs4) { + rs4.notifyAll(); + } } } @@ -221,6 +267,15 @@ public class JDBCConnection extends Thread implements Connection { } public void send(Object o) { + + if (this.socket == null) { + try { + init(); + } catch (Exception err) { + err.printStackTrace(); + } + } + try { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(bout); @@ -256,6 +311,7 @@ public class JDBCConnection extends Thread implements Connection { @Override public void close() throws SQLException { + wasClosed = true; try { socket.close(); } catch (Exception err) { @@ -271,7 +327,7 @@ public class JDBCConnection extends Thread implements Connection { @Override public Array createArrayOf(String typeName, Object[] elements) - throws SQLException { + throws SQLException { // TODO Auto-generated method stub return null; } @@ -302,28 +358,25 @@ public class JDBCConnection extends Thread implements Connection { @Override public Statement createStatement() throws SQLException { - return new JDBCStatement(this); + return new JDBCStatement(this).getProxy(); } @Override public Statement createStatement(int resultSetType, - int resultSetConcurrency, int resultSetHoldability) - throws SQLException { - // TODO Auto-generated method stub - return null; + int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + return new JDBCStatement(this).getProxy(); } @Override - public Statement createStatement(int resultSetType, - int resultSetConcurrency) - throws SQLException { - // TODO Auto-generated method stub - return null; + public Statement createStatement(int resultSetType, int resultSetConcurrency) + throws SQLException { + return new JDBCStatement(this).getProxy(); } @Override public Struct createStruct(String typeName, Object[] attributes) - throws SQLException { + throws SQLException { // TODO Auto-generated method stub return null; } @@ -360,8 +413,19 @@ public class JDBCConnection extends Thread implements Connection { @Override public DatabaseMetaData getMetaData() throws SQLException { - // TODO Auto-generated method stub - return null; + if (this.metaData == null) { + JDBCCommand cmd = new JDBCCommand(); + cmd.setType(JDBCCommand.TYPE_METADATA); + synchronized (this) { + send(cmd); + try { + this.wait(); + } catch (Exception err) { + err.printStackTrace(); + } + } + } + return metaData; } @Override @@ -384,7 +448,6 @@ public class JDBCConnection extends Thread implements Connection { @Override public boolean isClosed() throws SQLException { - // TODO Auto-generated method stub return false; } @@ -408,15 +471,15 @@ public class JDBCConnection extends Thread implements Connection { @Override public CallableStatement prepareCall(String sql, int resultSetType, - int resultSetConcurrency, int resultSetHoldability) - throws SQLException { + int resultSetConcurrency, int resultSetHoldability) + throws SQLException { // TODO Auto-generated method stub return null; } @Override public CallableStatement prepareCall(String sql, int resultSetType, - int resultSetConcurrency) throws SQLException { + int resultSetConcurrency) throws SQLException { // TODO Auto-generated method stub return null; } @@ -429,44 +492,44 @@ public class JDBCConnection extends Thread implements Connection { @Override public PreparedStatement prepareStatement(String sql, int resultSetType, - int resultSetConcurrency, int resultSetHoldability) - throws SQLException { - // TODO Auto-generated method stub - return null; + int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + System.err.println("SQL 1=" + sql); + return new JDBCStatement(this, sql).getProxy(); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, - int resultSetConcurrency) throws SQLException { - // TODO Auto-generated method stub - return null; + int resultSetConcurrency) throws SQLException { + System.err.println("SQL 2=" + sql); + return new JDBCStatement(this, sql).getProxy(); } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) - throws SQLException { - // TODO Auto-generated method stub - return null; + throws SQLException { + System.err.println("SQL 3=" + sql); + return new JDBCStatement(this, sql).getProxy(); } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) - throws SQLException { - // TODO Auto-generated method stub - return null; + throws SQLException { + System.err.println("SQL 4=" + sql); + return new JDBCStatement(this, sql).getProxy(); } @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) - throws SQLException { - // TODO Auto-generated method stub - return null; + throws SQLException { + System.err.println("SQL 5=" + sql); + return new JDBCStatement(this, sql).getProxy(); } @Override public PreparedStatement prepareStatement(String sql) throws SQLException { - // TODO Auto-generated method stub - return null; + System.err.println("SQL 6=" + sql); + return new JDBCStatement(this, sql).getProxy(); } @Override @@ -501,14 +564,14 @@ public class JDBCConnection extends Thread implements Connection { @Override public void setClientInfo(Properties properties) - throws SQLClientInfoException { + throws SQLClientInfoException { // TODO Auto-generated method stub } @Override public void setClientInfo(String name, String value) - throws SQLClientInfoException { + throws SQLClientInfoException { // TODO Auto-generated method stub } @@ -569,7 +632,7 @@ public class JDBCConnection extends Thread implements Connection { @Override public void setNetworkTimeout(Executor executor, int milliseconds) - throws SQLException { + throws SQLException { // TODO Auto-generated method stub } @@ -579,5 +642,5 @@ public class JDBCConnection extends Thread implements Connection { // TODO Auto-generated method stub return 0; } -} +}