Bug 6795 - Deprecated sal-dom-xsql
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / jdbc / JDBCConnection.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.md.sal.dom.xsql.jdbc;
10
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;
22 import java.sql.Blob;
23 import java.sql.CallableStatement;
24 import java.sql.Clob;
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;
37 import java.util.Map;
38 import java.util.Properties;
39 import java.util.concurrent.Executor;
40
41 import org.opendaylight.controller.md.sal.dom.xsql.XSQLAdapter;
42 import org.opendaylight.controller.md.sal.dom.xsql.XSQLBluePrint;
43
44 /**
45  * To be removed in Nitrogen
46  */
47 @Deprecated
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;
57
58     public JDBCConnection(Socket s, XSQLAdapter _a) {
59         this.socket = s;
60         this.adapter = _a;
61         try {
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();
70         }
71     }
72
73     public Connection getProxy() {
74         return this;
75         /*
76         return (Connection) Proxy.newProxyInstance(this.getClass()
77                 .getClassLoader(), new Class[] { Connection.class },
78                 new JDBCProxy(this));
79                 */
80     }
81
82     public JDBCConnection(String _addr) throws Exception {
83         this.addr = _addr;
84         init();
85     }
86
87     private void init() throws Exception {
88         if (addr.startsWith("http://")) {
89             addr = addr.substring(7);
90         }
91         System.err.print("Address is:" + addr);
92         socket = new Socket(addr, 40004);
93         try {
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();
102         }
103     }
104
105     public JDBCConnection(boolean server) {
106         try {
107             try (ServerSocket s = new ServerSocket(50003)) {
108                 socket = s.accept();
109                 try {
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();
116                 }
117             }
118         } catch (Exception err) {
119             err.printStackTrace();
120         }
121     }
122
123     private boolean isStopped() {
124         if (adapter != null && adapter.stopped) {
125             return true;
126         }
127         if (socket == null || socket.isClosed()) {
128             return true;
129         }
130         return false;
131     }
132
133     public void run() {
134         byte data[] = null;
135         while (!isStopped()) {
136             try {
137                 int len = in.readInt();
138                 data = new byte[len];
139                 in.readFully(data);
140                 addObject(data);
141
142             } catch (Exception err) {
143                 System.out.println("Connection Lost or Closed.");
144                 try {
145                     out.close();
146                 } catch (Exception er) {
147                 }
148                 out = null;
149                 try {
150                     in.close();
151                 } catch (Exception er) {
152                 }
153                 in = null;
154                 try {
155                     socket.close();
156                 } catch (Exception err2) {
157                 }
158                 socket = null;
159             }
160         }
161     }
162
163     private void addObject(byte[] data) {
164         synchronized (queue) {
165             queue.add(data);
166             queue.notifyAll();
167         }
168     }
169
170     private class JDBCObjectReader extends Thread {
171
172         public JDBCObjectReader() {
173             super("JDBCObjectReader");
174             start();
175         }
176
177         public void run() {
178             while (!isStopped()) {
179                 byte data[] = null;
180                 synchronized (queue) {
181                     if (queue.size() == 0) {
182                         try {
183                             queue.wait(1000);
184                         } catch (Exception err) {
185                         }
186                     }
187                     if (queue.size() > 0) {
188                         data = queue.removeFirst();
189                     }
190                 }
191                 if (data != null) {
192                     JDBCCommand command = (JDBCCommand) deSerialize(data);
193                     processCommand(command);
194                 }
195             }
196         }
197
198         private Object deSerialize(byte data[]) {
199             try {
200                 ByteArrayInputStream in = new ByteArrayInputStream(data);
201                 ObjectInputStream oin = new ObjectInputStream(in);
202                 return oin.readObject();
203             } catch (Exception err) {
204                 err.printStackTrace();
205             }
206             return null;
207         }
208     }
209
210     public void processCommand(JDBCCommand cmd) {
211         switch (cmd.getType()) {
212         case JDBCCommand.TYPE_METADATA_REPLY:
213             this.metaData = cmd.getBluePrint();
214             synchronized (this) {
215                 this.notifyAll();
216             }
217             break;
218         case JDBCCommand.TYPE_METADATA:
219             send(new JDBCCommand(this.adapter.getBluePrint()));
220             break;
221         case JDBCCommand.TYPE_EXECUTE_QUERY:
222             try {
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()));
229             }
230             break;
231         case JDBCCommand.TYPE_QUERY_REPLY:
232             JDBCResultSet rs1 = JDBCStatement.getQuery(cmd.getRS().getID());
233             rs1.updateData(cmd.getRS());
234             break;
235         case JDBCCommand.TYPE_QUERY_RECORD:
236             JDBCResultSet rs2 = JDBCStatement.getQuery(cmd.getRSID());
237             rs2.addRecord(cmd.getRecord());
238             break;
239         case JDBCCommand.TYPE_QUERY_FINISH:
240             JDBCResultSet rs3 = JDBCStatement.removeQuery(cmd.getRSID());
241             rs3.setFinished(true);
242             break;
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);
249             synchronized (rs4) {
250                 rs4.notifyAll();
251             }
252         }
253     }
254
255     private class QueryUpdater implements Runnable {
256
257         private JDBCResultSet rs = null;
258
259         public QueryUpdater(JDBCResultSet _rs) {
260             this.rs = _rs;
261         }
262
263         public void run() {
264             while (rs.next()) {
265                 JDBCCommand rec = new JDBCCommand(rs.getCurrent(), rs.getID());
266                 send(rec);
267             }
268             JDBCCommand end = new JDBCCommand(rs.getID());
269             send(end);
270         }
271     }
272
273     public void send(Object o) {
274
275         if (this.socket == null) {
276             try {
277                 init();
278             } catch (Exception err) {
279                 err.printStackTrace();
280             }
281         }
282
283         try {
284             ByteArrayOutputStream bout = new ByteArrayOutputStream();
285             ObjectOutputStream oout = new ObjectOutputStream(bout);
286             oout.writeObject(o);
287             byte data[] = bout.toByteArray();
288             synchronized (socket) {
289                 out.writeInt(data.length);
290                 out.write(data);
291                 out.flush();
292             }
293         } catch (Exception err) {
294             err.printStackTrace();
295         }
296     }
297
298     @Override
299     public boolean isWrapperFor(Class<?> arg0) throws SQLException {
300         // TODO Auto-generated method stub
301         return false;
302     }
303
304     @Override
305     public <T> T unwrap(Class<T> arg0) throws SQLException {
306         // TODO Auto-generated method stub
307         return null;
308     }
309
310     @Override
311     public void clearWarnings() throws SQLException {
312         // TODO Auto-generated method stub
313
314     }
315
316     @Override
317     public void close() throws SQLException {
318         wasClosed = true;
319         try {
320             socket.close();
321         } catch (Exception err) {
322         }
323         socket = null;
324     }
325
326     @Override
327     public void commit() throws SQLException {
328         // TODO Auto-generated method stub
329
330     }
331
332     @Override
333     public Array createArrayOf(String typeName, Object[] elements)
334             throws SQLException {
335         // TODO Auto-generated method stub
336         return null;
337     }
338
339     @Override
340     public Blob createBlob() throws SQLException {
341         // TODO Auto-generated method stub
342         return null;
343     }
344
345     @Override
346     public Clob createClob() throws SQLException {
347         // TODO Auto-generated method stub
348         return null;
349     }
350
351     @Override
352     public NClob createNClob() throws SQLException {
353         // TODO Auto-generated method stub
354         return null;
355     }
356
357     @Override
358     public SQLXML createSQLXML() throws SQLException {
359         // TODO Auto-generated method stub
360         return null;
361     }
362
363     @Override
364     public Statement createStatement() throws SQLException {
365         return new JDBCStatement(this).getProxy();
366     }
367
368     @Override
369     public Statement createStatement(int resultSetType,
370             int resultSetConcurrency, int resultSetHoldability)
371             throws SQLException {
372         return new JDBCStatement(this).getProxy();
373     }
374
375     @Override
376     public Statement createStatement(int resultSetType, int resultSetConcurrency)
377             throws SQLException {
378         return new JDBCStatement(this).getProxy();
379     }
380
381     @Override
382     public Struct createStruct(String typeName, Object[] attributes)
383             throws SQLException {
384         // TODO Auto-generated method stub
385         return null;
386     }
387
388     @Override
389     public boolean getAutoCommit() throws SQLException {
390         // TODO Auto-generated method stub
391         return false;
392     }
393
394     @Override
395     public String getCatalog() throws SQLException {
396         // TODO Auto-generated method stub
397         return null;
398     }
399
400     @Override
401     public Properties getClientInfo() throws SQLException {
402         // TODO Auto-generated method stub
403         return null;
404     }
405
406     @Override
407     public String getClientInfo(String name) throws SQLException {
408         // TODO Auto-generated method stub
409         return null;
410     }
411
412     @Override
413     public int getHoldability() throws SQLException {
414         // TODO Auto-generated method stub
415         return 0;
416     }
417
418     @Override
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) {
424                 send(cmd);
425                 try {
426                     this.wait();
427                 } catch (Exception err) {
428                     err.printStackTrace();
429                 }
430             }
431         }
432         return metaData;
433     }
434
435     @Override
436     public int getTransactionIsolation() throws SQLException {
437         // TODO Auto-generated method stub
438         return 0;
439     }
440
441     @Override
442     public Map<String, Class<?>> getTypeMap() throws SQLException {
443         // TODO Auto-generated method stub
444         return null;
445     }
446
447     @Override
448     public SQLWarning getWarnings() throws SQLException {
449         // TODO Auto-generated method stub
450         return null;
451     }
452
453     @Override
454     public boolean isClosed() throws SQLException {
455         return false;
456     }
457
458     @Override
459     public boolean isReadOnly() throws SQLException {
460         // TODO Auto-generated method stub
461         return false;
462     }
463
464     @Override
465     public boolean isValid(int timeout) throws SQLException {
466         // TODO Auto-generated method stub
467         return false;
468     }
469
470     @Override
471     public String nativeSQL(String sql) throws SQLException {
472         // TODO Auto-generated method stub
473         return null;
474     }
475
476     @Override
477     public CallableStatement prepareCall(String sql, int resultSetType,
478             int resultSetConcurrency, int resultSetHoldability)
479             throws SQLException {
480         // TODO Auto-generated method stub
481         return null;
482     }
483
484     @Override
485     public CallableStatement prepareCall(String sql, int resultSetType,
486             int resultSetConcurrency) throws SQLException {
487         // TODO Auto-generated method stub
488         return null;
489     }
490
491     @Override
492     public CallableStatement prepareCall(String sql) throws SQLException {
493         // TODO Auto-generated method stub
494         return null;
495     }
496
497     @Override
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();
503     }
504
505     @Override
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();
510     }
511
512     @Override
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();
517     }
518
519     @Override
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();
524     }
525
526     @Override
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();
531     }
532
533     @Override
534     public PreparedStatement prepareStatement(String sql) throws SQLException {
535         System.err.println("SQL 6=" + sql);
536         return new JDBCStatement(this, sql).getProxy();
537     }
538
539     @Override
540     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
541         // TODO Auto-generated method stub
542
543     }
544
545     @Override
546     public void rollback() throws SQLException {
547         // TODO Auto-generated method stub
548
549     }
550
551     @Override
552     public void rollback(Savepoint savepoint) throws SQLException {
553         // TODO Auto-generated method stub
554
555     }
556
557     @Override
558     public void setAutoCommit(boolean autoCommit) throws SQLException {
559         // TODO Auto-generated method stub
560
561     }
562
563     @Override
564     public void setCatalog(String catalog) throws SQLException {
565         // TODO Auto-generated method stub
566
567     }
568
569     @Override
570     public void setClientInfo(Properties properties)
571             throws SQLClientInfoException {
572         // TODO Auto-generated method stub
573
574     }
575
576     @Override
577     public void setClientInfo(String name, String value)
578             throws SQLClientInfoException {
579         // TODO Auto-generated method stub
580
581     }
582
583     @Override
584     public void setHoldability(int holdability) throws SQLException {
585         // TODO Auto-generated method stub
586
587     }
588
589     @Override
590     public void setReadOnly(boolean readOnly) throws SQLException {
591         // TODO Auto-generated method stub
592
593     }
594
595     @Override
596     public Savepoint setSavepoint() throws SQLException {
597         // TODO Auto-generated method stub
598         return null;
599     }
600
601     @Override
602     public Savepoint setSavepoint(String name) throws SQLException {
603         // TODO Auto-generated method stub
604         return null;
605     }
606
607     @Override
608     public void setTransactionIsolation(int level) throws SQLException {
609         // TODO Auto-generated method stub
610
611     }
612
613     @Override
614     public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
615         // TODO Auto-generated method stub
616
617     }
618
619     @Override
620     public void setSchema(String schema) throws SQLException {
621         // TODO Auto-generated method stub
622
623     }
624
625     @Override
626     public String getSchema() throws SQLException {
627         // TODO Auto-generated method stub
628         return null;
629     }
630
631     @Override
632     public void abort(Executor executor) throws SQLException {
633         // TODO Auto-generated method stub
634
635     }
636
637     @Override
638     public void setNetworkTimeout(Executor executor, int milliseconds)
639             throws SQLException {
640         // TODO Auto-generated method stub
641
642     }
643
644     @Override
645     public int getNetworkTimeout() throws SQLException {
646         // TODO Auto-generated method stub
647         return 0;
648     }
649
650 }