// get drivername, user, pw & server, and return connection id public void serve(InputStream i, OutputStream o) throws IOException { // TODOServiceList.getSingleInstance(); initialize(); NameListMessage nameListMessage = null; try { // read input to know which target panel is required ObjectInputStream input = new ObjectInputStream(i); nameListMessage = (NameListMessage) input.readObject(); // parse the required panel parseSqlFile(nameListMessage); } catch (ClassNotFoundException ex) { if (Debug.isDebug()) ex.printStackTrace(); nameListMessage.errorMessage = ex.toString(); } // send object to the caller ObjectOutputStream output = new ObjectOutputStream(o); output.writeObject(nameListMessage); // end socket connection o.close(); i.close(); }
private void parseSqlFile(NameListMessage nameListMessage) { BufferedWriter buffer = null; ArrayList cmds = new ArrayList(); try { URL url = new URL(nameListMessage.filename.toExternalForm()); InputStreamReader inputStreamReader = new InputStreamReader(url.openStream()); // InputStream inputStream = new // FileInputStream(nameListMessage.filename); //reporté à 2.6 // InputStreamReader inputStreamReader = new // InputStreamReader(inputStream); //reporté à 2.6 BufferedReader bugReader = new BufferedReader(inputStreamReader); SQLLexerStream stream = new SQLLexerStream(bugReader); // execute each SQL statement boolean done = false; int i = 0; while (!done) { int tokenID = stream.nextToken(); // only print statement tokens switch (tokenID) { case SQLLexerStream.TT_STATEMENT: i++; String text = stream.sval; if (text != null) { cmds.add(text); } break; case SQLLexerStream.TT_EOF: done = true; break; default: break; } // end switch } // end while } catch (Exception ex) { if (Debug.isDebug()) ex.printStackTrace(); nameListMessage.errorMessage = ex.toString(); } executeParsedFile(nameListMessage, cmds); } // end parseSqlFile()
private void executeParsedFile(NameListMessage nameListMessage, ArrayList cmds) { BufferedWriter buffer = null; int connectionId = nameListMessage.connectionId; boolean commaSeparated = nameListMessage.commaSeparated; // get java.sql.Connection from connectionId Connection connection = ConnectionService.getConnection(connectionId); if (connection == null) { nameListMessage.errorMessage = kConnectionClosed; } else { try { Thread th = null; buffer = new BufferedWriter(writer); // if reader exists, create reading thread if (reader != null) { final ObjectNameReader objectReader = new ObjectNameReader(nameListMessage.nameList); // This thread will read stream until meets EOL (ie. when // writer is closed) th = new Thread() { public void run() { objectReader.readInputFile(reader); } }; } // end if // if no reader (offline reverse), never start the reading // thread // (because output is written down directly to the disk without // processing in counterpart). if (reader != null) th.start(); // execute each SQL statement // we skip objects that don't need to be reversed Iterator iter = cmds.iterator(); int index = -1; while (iter.hasNext()) { String cmd = (String) iter.next(); index++; // check if this statement id is marked as <to skip> if (nameListMessage.ignoredStatementIds != null && nameListMessage.ignoredStatementIds.contains(new Integer(index))) { if (commaSeparated) { buffer.flush(); buffer.newLine(); } continue; } if (writer != null) { if (cmd != null) { try { execute(connection, cmd, buffer, commaSeparated); } catch (SQLException e) { if (Debug.isDebug()) e.printStackTrace(); nameListMessage.errorMessage = nameListMessage.errorMessage == null ? e.toString() : nameListMessage.errorMessage.concat("\n" + e.toString()); // NOT // LOCALIZABLE, // escape // code } } if (commaSeparated) { buffer.flush(); buffer.newLine(); } } } } /* * catch (SQLException ex) { if (Debug.isDebug()) ex.printStackTrace(); * nameListMessage.errorMessage = nameListMessage.errorMessage == null ? ex.toString() : * nameListMessage.errorMessage.concat("\n" + ex.toString()); //NOT LOCALIZABLE, escape * code } */ catch (IOException ex) { if (Debug.isDebug()) ex.printStackTrace(); nameListMessage.errorMessage = nameListMessage.errorMessage == null ? ex.toString() : nameListMessage.errorMessage.concat( "\n" + ex.toString()); // NOT LOCALIZABLE, escape // code } // close writer: the reading thread terminates. try { // WARNING: we must make a pause after closing the buffer // otherwise // the nameList variable won't be well initialized...strange! // [FG] buffer.close(); Thread.sleep(400); } catch (IOException ex) { } catch (InterruptedException ex) { } } // end if } // end parseSqlFile()