@Override public void close() throws IOException { try { if (conn != null) { conn.close(); } } catch (final SQLException e) { throw new MorphExecutionException("sqlmap: could not close db connection", e); } }
@Override public String process(final String value) { try { return URLEncoder.encode(value, "UTF-8"); } catch (final UnsupportedEncodingException e) { throw new MorphExecutionException("urlencode: unsupported encoding UTF-8", e); } }
private Connection getMySqlConnection() { try { Class.forName(driver); conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database + "?" + "user=" + login + "&" + "password=" + password); } catch (final ClassNotFoundException | SQLException e) { throw new MorphExecutionException("sqlmap: cannot create db connection", e); } return conn; }
public void setDatasource(final String name) { try { this.datasource = (DataSource) new InitialContext().lookup(name); } catch (final NamingException e) { throw new MorphExecutionException( "jndisqlmap: lookup of data source failed", e); } }
@Override public String process(final String value) { final Object obj; try { obj = invocable.invokeFunction(invoke, value); return obj.toString(); } catch (final ScriptException e) { throw new MorphExecutionException( "Error in script while evaluating 'process' method", e); } catch (final NoSuchMethodException e) { throw new MorphExecutionException("'process' method is missing in script", e); } }
public void init() { try { preparedStatement = getMySqlConnection().prepareStatement(query); } catch (final SQLException e) { throw new MorphExecutionException( "sqlmap: could not create prepared statement for query", e); } isUninitialized = false; }
public void setFile(final String file) { try ( final InputStream stream = openStream(file); final BufferedReader reader = new BufferedReader( new InputStreamReader(stream, StandardCharsets.UTF_8)) ) { String line; while ((line = reader.readLine()) != null) { if (line.isEmpty()) { continue; } final String[] parts = split.split(line); if (parts.length == 2) { map.put(parts[0], parts[1]); } } } catch (final IOException | UncheckedIOException e) { throw new MorphExecutionException("filemap: cannot read map file", e); } }
@Override public String get(final Object key) { String resultString = null; try( final Connection connection = datasource.getConnection(); final PreparedStatement statement = connection.prepareStatement(query); ) { statement.setString(1, key.toString()); try(final ResultSet resultSet = statement.executeQuery()) { if (resultSet.first()) { resultString = resultSet.getString(1); } } } catch (final SQLException e) { throw new MorphExecutionException( "jndisqlmap: execution of sql query failed", e); } return resultString; }
private InputStream openStream(String file) { return openAsFile(file) .orElseGet(() -> openAsResource(file) .orElseGet(() -> openAsUrl(file) .orElseThrow(() -> new MorphExecutionException( "File not found: " + file)))); }
@Override public String get(final Object key) { if (isUninitialized) { init(); } String resultString = null; final ResultSet resultSet; try { preparedStatement.setString(1, key.toString()); resultSet = preparedStatement.executeQuery(); if (resultSet.first()) { resultString = resultSet.getString(1); } resultSet.close(); } catch (final SQLException e) { throw new MorphExecutionException( "sqlmap: execution of prepared statement failed", e); } return resultString; }