Search in sources :

Example 41 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class DatabaseXmlUtil method write.

/*
     * Writes the database model to the given output writer. Note that this
     * method does not flush the writer.
     * 
     * @param model The database model
     * 
     * @param output The output writer
     */
public static void write(Database model, Writer output) {
    try {
        output.write("<?xml version=\"1.0\"?>\n<!DOCTYPE database SYSTEM \"" + DTD_PREFIX + "\">\n");
        output.write("<database name=\"" + model.getName() + "\"");
        if (model.getCatalog() != null) {
            output.write(" catalog=\"" + model.getCatalog() + "\"");
        }
        if (model.getSchema() != null) {
            output.write(" schema=\"" + model.getSchema() + "\"");
        }
        if (model.getIdMethod() != null) {
            output.write(" defaultIdMethod=\"" + model.getIdMethod() + "\"");
        }
        output.write(">\n");
        for (Table table : model.getTables()) {
            write(table, output);
        }
        output.write("</database>\n");
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 42 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class SqlScriptReader method readSqlStatement.

public String readSqlStatement() {
    try {
        String line = readLine();
        StringBuilder sql = null;
        int checkStatementEndsIndex = 0;
        CheckEndStatus endStatus = new CheckEndStatus();
        if (line != null) {
            do {
                if (sql == null) {
                    sql = new StringBuilder();
                }
                sql.append("\n");
                sql.append(line);
                if (checkStatementEnds(endStatus, checkStatementEndsIndex, sql.toString())) {
                    String toExecute = sql.substring(0, sql.lastIndexOf(delimiter));
                    toExecute = prepareForExecute(toExecute);
                    if (StringUtils.isNotBlank(toExecute)) {
                        return toExecute;
                    } else {
                        sql.setLength(0);
                    }
                } else {
                    checkStatementEndsIndex = sql.length();
                }
                line = readLine();
            } while (line != null);
            String toExecute = sql.toString();
            if (StringUtils.isNotBlank(toExecute)) {
                return prepareForExecute(toExecute);
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (IOException ex) {
        throw new IoException(ex);
    }
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 43 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class CsvUtils method write.

public static int write(Writer writer, String... data) {
    try {
        StringBuilder buffer = new StringBuilder();
        for (String string : data) {
            buffer.append(string);
        }
        writer.write(buffer.toString());
        if (log.isDebugEnabled()) {
            log.debug(buffer.toString());
        }
        return buffer.length();
    } catch (IOException ex) {
        throw new IoException(ex);
    }
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 44 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class SymXmlDataReader method readNext.

protected Object readNext() {
    try {
        Map<String, String> rowData = new LinkedHashMap<String, String>();
        String columnName = null;
        Table lastTable = this.table;
        int eventType = parser.next();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch(eventType) {
                case XmlPullParser.TEXT:
                    if (columnName != null) {
                        rowData.put(columnName, parser.getText());
                        columnName = null;
                    }
                    break;
                case XmlPullParser.START_TAG:
                    String name = parser.getName();
                    if ("row".equalsIgnoreCase(name)) {
                        table = new Table();
                        data = new CsvData();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if (attributeName.equalsIgnoreCase("entity")) {
                                table.setName(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("catalog")) {
                                table.setCatalog(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("schema")) {
                                table.setSchema(attributeValue);
                            } else if (attributeName.equalsIgnoreCase("dml")) {
                                if (attributeValue.equals("I")) {
                                    data.setDataEventType(DataEventType.INSERT);
                                } else if (attributeValue.equals("U")) {
                                    data.setDataEventType(DataEventType.UPDATE);
                                } else if (attributeValue.equals("D")) {
                                    data.setDataEventType(DataEventType.DELETE);
                                } else if (attributeValue.equals("C")) {
                                    data.setDataEventType(DataEventType.CREATE);
                                } else if (attributeValue.equals("S")) {
                                    data.setDataEventType(DataEventType.SQL);
                                } else if (attributeValue.equals("B")) {
                                    data.setDataEventType(DataEventType.BSH);
                                } else if (attributeValue.equals("R")) {
                                    data.setDataEventType(DataEventType.RELOAD);
                                }
                            }
                        }
                    } else if ("data".equalsIgnoreCase(name)) {
                        boolean nullValue = false;
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if ("key".equalsIgnoreCase(attributeName)) {
                                columnName = attributeValue;
                            } else if ("xsi:nil".equalsIgnoreCase(attributeName)) {
                                nullValue = true;
                            }
                        }
                        if (nullValue) {
                            rowData.put(columnName, null);
                            columnName = null;
                        }
                    } else if ("batch".equalsIgnoreCase(name)) {
                        batch = new Batch();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if ("binary".equalsIgnoreCase(attributeName)) {
                                batch.setBinaryEncoding(BinaryEncoding.valueOf(attributeValue));
                            } else if ("nodeid".equalsIgnoreCase(attributeName)) {
                                batch.setSourceNodeId(attributeValue);
                            }
                        }
                        return batch;
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if ("row".equalsIgnoreCase(name)) {
                        String[] columnNames = rowData.keySet().toArray(new String[rowData.keySet().size()]);
                        for (String colName : columnNames) {
                            table.addColumn(new Column(colName));
                        }
                        String[] columnValues = rowData.values().toArray(new String[rowData.values().size()]);
                        data.putParsedData(CsvData.ROW_DATA, columnValues);
                        rowData = new LinkedHashMap<String, String>();
                        if (lastTable == null || !lastTable.equals(table)) {
                            return table;
                        } else {
                            return data;
                        }
                    } else if ("data".equalsIgnoreCase(name)) {
                        columnName = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
        return null;
    } catch (IOException ex) {
        throw new IoException(ex);
    } catch (XmlPullParserException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Table(org.jumpmind.db.model.Table) IOException(java.io.IOException) CsvData(org.jumpmind.symmetric.io.data.CsvData) LinkedHashMap(java.util.LinkedHashMap) Batch(org.jumpmind.symmetric.io.data.Batch) Column(org.jumpmind.db.model.Column) IoException(org.jumpmind.exception.IoException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 45 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class StagedResource method setState.

public void setState(State state) {
    if (file != null && file.exists()) {
        File newFile = buildFile(state);
        if (!newFile.equals(file)) {
            closeInternal();
            if (newFile.exists()) {
                if (writer != null || outputStream != null) {
                    throw new IoException("Could not write '{}' it is currently being written to", newFile.getAbsolutePath());
                }
                if (!FileUtils.deleteQuietly(newFile)) {
                    log.warn("Failed to delete '{}' in preparation for renaming '{}'", newFile.getAbsolutePath(), file.getAbsoluteFile());
                    if (readers != null && readers.size() > 0) {
                        for (Thread thread : readers.keySet()) {
                            BufferedReader reader = readers.get(thread);
                            log.warn("Closing unwanted reader for '{}' that had been created on thread '{}'", newFile.getAbsolutePath(), thread.getName());
                            IOUtils.closeQuietly(reader);
                        }
                    }
                    readers = null;
                    if (!FileUtils.deleteQuietly(newFile)) {
                        log.warn("Failed to delete '{}' for a second time", newFile.getAbsolutePath());
                    }
                }
            }
            if (!file.renameTo(newFile)) {
                String msg = String.format("Had trouble renaming file.  The current name is %s and the desired state was %s", file.getAbsolutePath(), state);
                log.warn(msg);
                throw new IllegalStateException(msg);
            }
        }
    }
    refreshLastUpdateTime();
    this.state = state;
    this.file = buildFile(state);
}
Also used : BufferedReader(java.io.BufferedReader) IoException(org.jumpmind.exception.IoException) File(java.io.File)

Aggregations

IoException (org.jumpmind.exception.IoException)48 IOException (java.io.IOException)41 File (java.io.File)8 Table (org.jumpmind.db.model.Table)8 BufferedReader (java.io.BufferedReader)6 Column (org.jumpmind.db.model.Column)6 BufferedWriter (java.io.BufferedWriter)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 OutputStreamWriter (java.io.OutputStreamWriter)4 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SymmetricException (org.jumpmind.symmetric.SymmetricException)3 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)3 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 BufferedInputStream (java.io.BufferedInputStream)2