Search in sources :

Example 1 with HopException

use of org.apache.hop.core.exception.HopException in project hop by apache.

the class Database method execStatementsFromFile.

/**
 * Execute an SQL statement inside a file on the database connection (has to be open)
 *
 * @param filename the file containing the SQL to execute
 * @param sendSinglestatement set to true if you want to send the whole file as a single
 *     statement. If false separate statements will be isolated and executed.
 * @return a Result object indicating the number of lines read, deleted, inserted, updated, ...
 * @throws HopDatabaseException in case anything goes wrong.
 * @sendSinglestatement send one statement
 */
public Result execStatementsFromFile(String filename, boolean sendSinglestatement) throws HopException {
    FileObject sqlFile = null;
    InputStream is = null;
    InputStreamReader bis = null;
    try {
        if (Utils.isEmpty(filename)) {
            throw new HopException("Filename is missing!");
        }
        sqlFile = HopVfs.getFileObject(filename);
        if (!sqlFile.exists()) {
            throw new HopException("We can not find file [" + filename + "]!");
        }
        is = HopVfs.getInputStream(sqlFile);
        bis = new InputStreamReader(new BufferedInputStream(is, 500));
        StringBuilder lineStringBuilder = new StringBuilder(256);
        lineStringBuilder.setLength(0);
        BufferedReader buff = new BufferedReader(bis);
        String sLine = null;
        String sql = Const.CR;
        while ((sLine = buff.readLine()) != null) {
            if (Utils.isEmpty(sLine)) {
                sql = sql + Const.CR;
            } else {
                sql = sql + Const.CR + sLine;
            }
        }
        if (sendSinglestatement) {
            return execStatement(sql);
        } else {
            return execStatements(sql);
        }
    } catch (Exception e) {
        throw new HopException(e);
    } finally {
        try {
            if (sqlFile != null) {
                sqlFile.close();
            }
            if (is != null) {
                is.close();
            }
            if (bis != null) {
                bis.close();
            }
        } catch (Exception e) {
        // Ignore
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HopException(org.apache.hop.core.exception.HopException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FileObject(org.apache.commons.vfs2.FileObject) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 2 with HopException

use of org.apache.hop.core.exception.HopException in project hop by apache.

the class ConfigFileSerializer method writeToFile.

@Override
public void writeToFile(String filename, Map<String, Object> configMap) throws HopException {
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        String niceJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(configMap);
        // Write to a new new file...
        // 
        FileObject newFile = HopVfs.getFileObject(filename + ".new");
        if (newFile.exists() && !newFile.delete()) {
            throw new HopException("Unable to delete new config file " + newFile.getName().getURI());
        }
        // Write to the new file (hop.config.new)
        // 
        OutputStream outputStream = HopVfs.getOutputStream(newFile, false);
        outputStream.write(niceJson.getBytes(StandardCharsets.UTF_8));
        outputStream.close();
        // if this worked, delete the old file  (hop.config.old)
        // 
        FileObject oldFile = HopVfs.getFileObject(filename + ".old");
        if (oldFile.exists() && !oldFile.delete()) {
            throw new HopException("Unable to delete old config file " + oldFile.getName().getURI());
        }
        // If this worked, rename the file to the old file  (hop.config -> hop.config.old)
        // 
        FileObject file = HopVfs.getFileObject(filename);
        if (file.exists() && !file.canRenameTo(oldFile)) {
            // could be a new file
            throw new HopException("Unable to rename config file to .old : " + file.getName().getURI());
        }
        // Now rename the new file to the final value...
        // 
        newFile.moveTo(file);
    } catch (Exception e) {
        throw new HopException("Error writing to Hop configuration file : " + filename, e);
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) OutputStream(java.io.OutputStream) FileObject(org.apache.commons.vfs2.FileObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HopException(org.apache.hop.core.exception.HopException)

Example 3 with HopException

use of org.apache.hop.core.exception.HopException in project hop by apache.

the class ConfigFileSerializer method readFromFile.

@Override
public Map<String, Object> readFromFile(String filename) throws HopException {
    try {
        FileObject file = HopVfs.getFileObject(filename);
        if (!file.exists()) {
            // 
            return new HashMap<>();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
        };
        try (InputStream inputStream = HopVfs.getInputStream(file)) {
            HashMap<String, Object> configMap = objectMapper.readValue(inputStream, typeRef);
            return configMap;
        }
    } catch (Exception e) {
        throw new HopException("Error reading Hop configuration file " + filename, e);
    }
}
Also used : HashMap(java.util.HashMap) HopException(org.apache.hop.core.exception.HopException) InputStream(java.io.InputStream) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HopException(org.apache.hop.core.exception.HopException)

Example 4 with HopException

use of org.apache.hop.core.exception.HopException in project hop by apache.

the class ConfigFile method readFromFile.

public void readFromFile() throws HopException {
    try {
        if (new File(getConfigFilename()).exists()) {
            // Let's write to the file
            // 
            this.serializer = new ConfigFileSerializer();
        } else {
            boolean createWhenMissing = "Y".equalsIgnoreCase(System.getProperty(Const.HOP_AUTO_CREATE_CONFIG, "N"));
            if (createWhenMissing) {
                System.out.println("Creating new default Hop configuration file: " + getConfigFilename());
                this.serializer = new ConfigFileSerializer();
            } else {
                // Doesn't serialize anything really, reads an empty map with an empty file
                // 
                System.out.println("Hop configuration file not found, not serializing: " + getConfigFilename());
                this.serializer = new ConfigNoFileSerializer();
            }
        }
        configMap = serializer.readFromFile(getConfigFilename());
    } catch (Exception e) {
        throw new HopException("Unable to read config file '" + getConfigFilename() + "'", e);
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) File(java.io.File) HopException(org.apache.hop.core.exception.HopException)

Example 5 with HopException

use of org.apache.hop.core.exception.HopException in project hop by apache.

the class HopServer method getNextServerSequenceValue.

public long getNextServerSequenceValue(IVariables variables, String serverSequenceName, long incrementValue) throws HopException {
    try {
        String xml = execService(variables, NextSequenceValueServlet.CONTEXT_PATH + "/" + "?" + NextSequenceValueServlet.PARAM_NAME + "=" + URLEncoder.encode(serverSequenceName, "UTF-8") + "&" + NextSequenceValueServlet.PARAM_INCREMENT + "=" + Long.toString(incrementValue));
        Document doc = XmlHandler.loadXmlString(xml);
        Node seqNode = XmlHandler.getSubNode(doc, NextSequenceValueServlet.XML_TAG);
        String nextValueString = XmlHandler.getTagValue(seqNode, NextSequenceValueServlet.XML_TAG_VALUE);
        String errorString = XmlHandler.getTagValue(seqNode, NextSequenceValueServlet.XML_TAG_ERROR);
        if (!Utils.isEmpty(errorString)) {
            throw new HopException(errorString);
        }
        if (Utils.isEmpty(nextValueString)) {
            throw new HopException("No value retrieved from server sequence '" + serverSequenceName + "' on server " + toString());
        }
        long nextValue = Const.toLong(nextValueString, Long.MIN_VALUE);
        if (nextValue == Long.MIN_VALUE) {
            throw new HopException("Incorrect value '" + nextValueString + "' retrieved from server sequence '" + serverSequenceName + "' on server " + toString());
        }
        return nextValue;
    } catch (Exception e) {
        throw new HopException("There was a problem retrieving a next sequence value from server sequence '" + serverSequenceName + "' on server " + toString(), e);
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) ClientProtocolException(org.apache.http.client.ClientProtocolException) HopException(org.apache.hop.core.exception.HopException)

Aggregations

HopException (org.apache.hop.core.exception.HopException)1038 IRowMeta (org.apache.hop.core.row.IRowMeta)289 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)197 IValueMeta (org.apache.hop.core.row.IValueMeta)177 PipelineMeta (org.apache.hop.pipeline.PipelineMeta)152 HopTransformException (org.apache.hop.core.exception.HopTransformException)145 TransformMeta (org.apache.hop.pipeline.transform.TransformMeta)132 IVariables (org.apache.hop.core.variables.IVariables)122 FileObject (org.apache.commons.vfs2.FileObject)113 List (java.util.List)108 BaseTransformMeta (org.apache.hop.pipeline.transform.BaseTransformMeta)107 FormAttachment (org.eclipse.swt.layout.FormAttachment)103 FormData (org.eclipse.swt.layout.FormData)103 FormLayout (org.eclipse.swt.layout.FormLayout)102 BaseMessages (org.apache.hop.i18n.BaseMessages)97 SWT (org.eclipse.swt.SWT)95 IOException (java.io.IOException)91 Const (org.apache.hop.core.Const)89 BaseTransformDialog (org.apache.hop.ui.pipeline.transform.BaseTransformDialog)88 org.eclipse.swt.widgets (org.eclipse.swt.widgets)88