1 package org.opendaylight.controller.md.sal.dom.xsql.jdbc;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7 import java.io.DataInputStream;
8 import java.io.DataOutputStream;
9 import java.io.ObjectInputStream;
10 import java.io.ObjectOutputStream;
11 import java.net.ServerSocket;
12 import java.net.Socket;
13 import java.sql.Array;
15 import java.sql.CallableStatement;
17 import java.sql.Connection;
18 import java.sql.DatabaseMetaData;
19 import java.sql.NClob;
20 import java.sql.PreparedStatement;
21 import java.sql.SQLClientInfoException;
22 import java.sql.SQLException;
23 import java.sql.SQLWarning;
24 import java.sql.SQLXML;
25 import java.sql.Savepoint;
26 import java.sql.Statement;
27 import java.sql.Struct;
28 import java.util.LinkedList;
30 import java.util.Properties;
31 import java.util.concurrent.Executor;
33 import org.opendaylight.controller.md.sal.dom.xsql.XSQLAdapter;
34 import org.opendaylight.controller.md.sal.dom.xsql.XSQLBluePrint;
36 public class JDBCConnection implements Connection, Runnable {
37 private Socket socket = null;
38 private DataInputStream in = null;
39 private DataOutputStream out = null;
40 private LinkedList<byte[]> queue = new LinkedList<byte[]>();
41 private XSQLAdapter adapter = null;
42 private XSQLBluePrint metaData = null;
43 private String addr = null;
44 private boolean wasClosed = false;
46 public JDBCConnection(Socket s, XSQLAdapter _a) {
50 in = new DataInputStream(
51 new BufferedInputStream(s.getInputStream()));
52 out = new DataOutputStream(new BufferedOutputStream(
53 s.getOutputStream()));
54 new JDBCObjectReader();
55 new Thread(this).start();
56 } catch (Exception err) {
57 err.printStackTrace();
61 public Connection getProxy() {
64 return (Connection) Proxy.newProxyInstance(this.getClass()
65 .getClassLoader(), new Class[] { Connection.class },
70 public JDBCConnection(String _addr) throws Exception {
75 private void init() throws Exception {
76 if (addr.startsWith("http://")) {
77 addr = addr.substring(7);
79 System.err.print("Address is:" + addr);
80 socket = new Socket(addr, 40004);
82 in = new DataInputStream(new BufferedInputStream(
83 socket.getInputStream()));
84 out = new DataOutputStream(new BufferedOutputStream(
85 socket.getOutputStream()));
86 new JDBCObjectReader();
87 new Thread(this).start();
88 } catch (Exception err) {
89 err.printStackTrace();
93 public JDBCConnection(boolean server) {
95 ServerSocket s = new ServerSocket(50003);
98 in = new DataInputStream(new BufferedInputStream(
99 socket.getInputStream()));
100 out = new DataOutputStream(new BufferedOutputStream(
101 socket.getOutputStream()));
102 new JDBCObjectReader();
103 new Thread(this).start();
104 } catch (Exception err) {
105 err.printStackTrace();
107 } catch (Exception err) {
108 err.printStackTrace();
112 private boolean isStopped() {
113 if (adapter != null && adapter.stopped) {
116 if (socket == null || socket.isClosed()) {
124 while (!isStopped()) {
126 int len = in.readInt();
127 data = new byte[len];
131 } catch (Exception err) {
132 System.out.println("Connection Lost or Closed.");
135 } catch (Exception er) {
140 } catch (Exception er) {
145 } catch (Exception err2) {
152 private void addObject(byte[] data) {
153 synchronized (queue) {
159 private class JDBCObjectReader extends Thread {
161 public JDBCObjectReader() {
162 super("JDBCObjectReader");
167 while (!isStopped()) {
169 synchronized (queue) {
170 if (queue.size() == 0) {
173 } catch (Exception err) {
176 if (queue.size() > 0) {
177 data = queue.removeFirst();
181 JDBCCommand command = (JDBCCommand) deSerialize(data);
182 processCommand(command);
187 private Object deSerialize(byte data[]) {
189 ByteArrayInputStream in = new ByteArrayInputStream(data);
190 ObjectInputStream oin = new ObjectInputStream(in);
191 return oin.readObject();
192 } catch (Exception err) {
193 err.printStackTrace();
199 public void processCommand(JDBCCommand cmd) {
200 switch (cmd.getType()) {
201 case JDBCCommand.TYPE_METADATA_REPLY:
202 this.metaData = cmd.getBluePrint();
203 synchronized (this) {
207 case JDBCCommand.TYPE_METADATA:
208 send(new JDBCCommand(this.adapter.getBluePrint()));
210 case JDBCCommand.TYPE_EXECUTE_QUERY:
212 JDBCServer.execute(cmd.getRS(), adapter);
213 send(new JDBCCommand(cmd.getRS(), JDBCCommand.TYPE_QUERY_REPLY));
214 QueryUpdater u = new QueryUpdater(cmd.getRS());
215 new Thread(u).start();
216 } catch (Exception err) {
217 send(new JDBCCommand(err, cmd.getRSID()));
220 case JDBCCommand.TYPE_QUERY_REPLY:
221 JDBCResultSet rs1 = JDBCStatement.getQuery(cmd.getRS().getID());
222 rs1.updateData(cmd.getRS());
224 case JDBCCommand.TYPE_QUERY_RECORD:
225 JDBCResultSet rs2 = JDBCStatement.getQuery(cmd.getRSID());
226 rs2.addRecord(cmd.getRecord());
228 case JDBCCommand.TYPE_QUERY_FINISH:
229 JDBCResultSet rs3 = JDBCStatement.removeQuery(cmd.getRSID());
230 rs3.setFinished(true);
232 case JDBCCommand.TYPE_QUERY_ERROR:
233 System.err.println("ERROR Executing Query\n");
234 cmd.getERROR().printStackTrace();
235 JDBCResultSet rs4 = JDBCStatement.removeQuery(cmd.getRSID());
236 rs4.setError(cmd.getERROR());
237 rs4.setFinished(true);
244 private class QueryUpdater implements Runnable {
246 private JDBCResultSet rs = null;
248 public QueryUpdater(JDBCResultSet _rs) {
254 JDBCCommand rec = new JDBCCommand(rs.getCurrent(), rs.getID());
257 JDBCCommand end = new JDBCCommand(rs.getID());
262 public void send(Object o) {
264 if (this.socket == null) {
267 } catch (Exception err) {
268 err.printStackTrace();
273 ByteArrayOutputStream bout = new ByteArrayOutputStream();
274 ObjectOutputStream oout = new ObjectOutputStream(bout);
276 byte data[] = bout.toByteArray();
277 synchronized (socket) {
278 out.writeInt(data.length);
282 } catch (Exception err) {
283 err.printStackTrace();
288 public boolean isWrapperFor(Class<?> arg0) throws SQLException {
289 // TODO Auto-generated method stub
294 public <T> T unwrap(Class<T> arg0) throws SQLException {
295 // TODO Auto-generated method stub
300 public void clearWarnings() throws SQLException {
301 // TODO Auto-generated method stub
306 public void close() throws SQLException {
310 } catch (Exception err) {
316 public void commit() throws SQLException {
317 // TODO Auto-generated method stub
322 public Array createArrayOf(String typeName, Object[] elements)
323 throws SQLException {
324 // TODO Auto-generated method stub
329 public Blob createBlob() throws SQLException {
330 // TODO Auto-generated method stub
335 public Clob createClob() throws SQLException {
336 // TODO Auto-generated method stub
341 public NClob createNClob() throws SQLException {
342 // TODO Auto-generated method stub
347 public SQLXML createSQLXML() throws SQLException {
348 // TODO Auto-generated method stub
353 public Statement createStatement() throws SQLException {
354 return new JDBCStatement(this).getProxy();
358 public Statement createStatement(int resultSetType,
359 int resultSetConcurrency, int resultSetHoldability)
360 throws SQLException {
361 return new JDBCStatement(this).getProxy();
365 public Statement createStatement(int resultSetType, int resultSetConcurrency)
366 throws SQLException {
367 return new JDBCStatement(this).getProxy();
371 public Struct createStruct(String typeName, Object[] attributes)
372 throws SQLException {
373 // TODO Auto-generated method stub
378 public boolean getAutoCommit() throws SQLException {
379 // TODO Auto-generated method stub
384 public String getCatalog() throws SQLException {
385 // TODO Auto-generated method stub
390 public Properties getClientInfo() throws SQLException {
391 // TODO Auto-generated method stub
396 public String getClientInfo(String name) throws SQLException {
397 // TODO Auto-generated method stub
402 public int getHoldability() throws SQLException {
403 // TODO Auto-generated method stub
408 public DatabaseMetaData getMetaData() throws SQLException {
409 if (this.metaData == null) {
410 JDBCCommand cmd = new JDBCCommand();
411 cmd.setType(JDBCCommand.TYPE_METADATA);
412 synchronized (this) {
416 } catch (Exception err) {
417 err.printStackTrace();
425 public int getTransactionIsolation() throws SQLException {
426 // TODO Auto-generated method stub
431 public Map<String, Class<?>> getTypeMap() throws SQLException {
432 // TODO Auto-generated method stub
437 public SQLWarning getWarnings() throws SQLException {
438 // TODO Auto-generated method stub
443 public boolean isClosed() throws SQLException {
448 public boolean isReadOnly() throws SQLException {
449 // TODO Auto-generated method stub
454 public boolean isValid(int timeout) throws SQLException {
455 // TODO Auto-generated method stub
460 public String nativeSQL(String sql) throws SQLException {
461 // TODO Auto-generated method stub
466 public CallableStatement prepareCall(String sql, int resultSetType,
467 int resultSetConcurrency, int resultSetHoldability)
468 throws SQLException {
469 // TODO Auto-generated method stub
474 public CallableStatement prepareCall(String sql, int resultSetType,
475 int resultSetConcurrency) throws SQLException {
476 // TODO Auto-generated method stub
481 public CallableStatement prepareCall(String sql) throws SQLException {
482 // TODO Auto-generated method stub
487 public PreparedStatement prepareStatement(String sql, int resultSetType,
488 int resultSetConcurrency, int resultSetHoldability)
489 throws SQLException {
490 System.err.println("SQL 1=" + sql);
491 return new JDBCStatement(this, sql).getProxy();
495 public PreparedStatement prepareStatement(String sql, int resultSetType,
496 int resultSetConcurrency) throws SQLException {
497 System.err.println("SQL 2=" + sql);
498 return new JDBCStatement(this, sql).getProxy();
502 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
503 throws SQLException {
504 System.err.println("SQL 3=" + sql);
505 return new JDBCStatement(this, sql).getProxy();
509 public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
510 throws SQLException {
511 System.err.println("SQL 4=" + sql);
512 return new JDBCStatement(this, sql).getProxy();
516 public PreparedStatement prepareStatement(String sql, String[] columnNames)
517 throws SQLException {
518 System.err.println("SQL 5=" + sql);
519 return new JDBCStatement(this, sql).getProxy();
523 public PreparedStatement prepareStatement(String sql) throws SQLException {
524 System.err.println("SQL 6=" + sql);
525 return new JDBCStatement(this, sql).getProxy();
529 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
530 // TODO Auto-generated method stub
535 public void rollback() throws SQLException {
536 // TODO Auto-generated method stub
541 public void rollback(Savepoint savepoint) throws SQLException {
542 // TODO Auto-generated method stub
547 public void setAutoCommit(boolean autoCommit) throws SQLException {
548 // TODO Auto-generated method stub
553 public void setCatalog(String catalog) throws SQLException {
554 // TODO Auto-generated method stub
559 public void setClientInfo(Properties properties)
560 throws SQLClientInfoException {
561 // TODO Auto-generated method stub
566 public void setClientInfo(String name, String value)
567 throws SQLClientInfoException {
568 // TODO Auto-generated method stub
573 public void setHoldability(int holdability) throws SQLException {
574 // TODO Auto-generated method stub
579 public void setReadOnly(boolean readOnly) throws SQLException {
580 // TODO Auto-generated method stub
585 public Savepoint setSavepoint() throws SQLException {
586 // TODO Auto-generated method stub
591 public Savepoint setSavepoint(String name) throws SQLException {
592 // TODO Auto-generated method stub
597 public void setTransactionIsolation(int level) throws SQLException {
598 // TODO Auto-generated method stub
603 public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
604 // TODO Auto-generated method stub
609 public void setSchema(String schema) throws SQLException {
610 // TODO Auto-generated method stub
615 public String getSchema() throws SQLException {
616 // TODO Auto-generated method stub
621 public void abort(Executor executor) throws SQLException {
622 // TODO Auto-generated method stub
627 public void setNetworkTimeout(Executor executor, int milliseconds)
628 throws SQLException {
629 // TODO Auto-generated method stub
634 public int getNetworkTimeout() throws SQLException {
635 // TODO Auto-generated method stub