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
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations