protected void finallyClose(Runnable r) { try { r.run(); } catch (Exception e) { LOGGER.error(e); throw e; } finally { close(); } }
private void commitSilently(ConnectionInfo connectionInfo) { try { if (connectionInfo != null) { connectionInfo.ifNotInTransaction(c -> c.setAutoCommit(true)); } } catch (SQLException e) { LOGGER.error(e, "Failed to commit connection upon close"); } }
private void closeSilently(final AutoCloseable closeable) { actionSilently(closeable, AutoCloseable::close, "closing"); try { if (closeable != null) { closeable.close(); } } catch (Exception e) { LOGGER.error(e, "Error closing " + closeable); // Just log the error. No re-throw } }
private <T> void actionSilently(final T actionTarget, ThrowingConsumer<T> action, String actionLabel) { try { if (actionTarget != null) { action.accept(actionTarget); } } catch (Exception e) { LOGGER.error(e, "Error " + actionLabel + " " + actionTarget); // Just log the error. No re-throw } }
protected boolean finallyCloseBoolean(BooleanSupplier bs) { try { return bs.getAsBoolean(); } catch (Exception e) { LOGGER.error(e); throw e; } finally { close(); } }
protected <T> T finallyCloseReference(Supplier<T> s) { try { return s.get(); } catch (Exception e) { LOGGER.error(e); throw e; } finally { close(); } }
protected void executeInTransaction( final Dbms dbms, final Connection conn, final List<? extends SqlStatement> sqlStatementList ) throws SQLException { requireNonNull(dbms); requireNonNull(conn); requireNonNull(sqlStatementList); assertNotClosed(); final AtomicReference<SqlStatement> lastSqlStatement = new AtomicReference<>(); try { executeSqlStatementList(sqlStatementList, lastSqlStatement, dbms, conn); postSuccessfulTransaction(sqlStatementList); } catch (SQLException sqlEx) { LOGGER.error("SqlStatementList: " + sqlStatementList); LOGGER.error("SQL: " + lastSqlStatement); LOGGER.error(sqlEx, sqlEx.getMessage()); throw sqlEx; } }
public static Properties loadProperties(Logger logger, File configFile) { final Properties properties = new Properties(); if (configFile.exists() && configFile.canRead()) { try (final InputStream in = new FileInputStream(configFile)) { properties.load(in); } catch (final IOException ex) { final String err = "Error loading default settings from " + configFile.getAbsolutePath() + "-file."; logger.error(ex, err); throw new RuntimeException(err, ex); } } else { logger.info( "No configuration file '" + configFile.getAbsolutePath() + "' found." ); } return properties; }
private void discard(PoolableConnection connection) { requireNonNull(connection); LOGGER_CONNECTION.debug("Discard: %s", connection); try { connection.rawClose(); } catch (SQLException sqle) { LOGGER_CONNECTION.error(sqle, "Error closing a connection."); } }
private boolean isValidOrNull(PoolableConnection connection) { // connection nullable try { return connection == null || (connection.getExpires() > System.currentTimeMillis() && !connection.isClosed()); } catch (SQLException sqle) { LOGGER_CONNECTION.error(sqle, "Error while checking if a connection is closed."); return false; } }
"' in class '" + value.getClass().getName() + "' of type '" + field.getType() + "'."; LOGGER_INSTANCE.error(ex, err); throw new RuntimeException(err, ex);
protected long finallyCloseLong(LongSupplier lp) { try { return lp.getAsLong(); } catch (Exception e) { LOGGER.error(e); throw e; } finally { close(); } }
protected int finallyCloseInt(IntSupplier is) { try { return is.getAsInt(); } catch (Exception e) { LOGGER.error(e); throw e; } finally { close(); } }
protected double finallyCloseDouble(DoubleSupplier ds) { try { return ds.getAsDouble(); } catch (Exception e) { LOGGER.error(e); throw e; } finally { close(); } }
public void writeToFile(TranslatorManager delegator, Path codePath, String content, boolean overwriteExisting) { requireNonNulls(codePath, content); try { if (overwriteExisting || !codePath.toFile().exists()) { final Path hashPath = codePath.getParent() .resolve(secretFolderName()) .resolve(HASH_PREFIX + codePath.getFileName().toString() + HASH_SUFFIX); write(hashPath, HashUtil.md5(content), true); write(codePath, content, false); fileCounter.incrementAndGet(); } } catch (final IOException ex) { LOGGER.error(ex, "Failed to write file " + codePath); } LOGGER.trace("*** BEGIN File:" + codePath); Stream.of(content.split(Formatting.nl())).forEachOrdered(LOGGER::trace); LOGGER.trace("*** END File:" + codePath); }
@Override public Connection newConnection( final String uri, final String username, final char[] password ) { try { final Connection connection = DriverManager.getConnection(uri, username, charsToString(password)); LOGGER_CONNECTION.debug("New external connection: %s", connection); return connection; } catch (final SQLException ex) { final String msg = "Unable to get connection using url \"" + uri + "\", user = \"" + username + "\", password = \"********\"."; LOGGER_CONNECTION.error(ex, msg); throw new SpeedmentException(msg, ex); } }
@Override public void execute(Speedment speedment) throws MojoExecutionException, MojoFailureException { getLog().info("Generating code using JSON configuration file: '" + configLocation().toAbsolutePath() + "'."); if (hasConfigFile()) { try { final Project project = speedment.getOrThrow(ProjectComponent.class).getProject(); speedment.getOrThrow(TranslatorManager.class).accept(project); // after generating the speedment code, the package location needs to be added as a source folder if (!mavenProject.getCompileSourceRoots().contains(mavenProject.getBasedir().getAbsolutePath() + "/" + project.getPackageLocation())) { System.out.println("adding new source location"); mavenProject.addCompileSourceRoot(mavenProject.getBasedir().getAbsolutePath() + "/" + project.getPackageLocation()); } } catch (final Exception ex) { final String err = "Error parsing configFile file."; LOGGER.error(ex, err); getLog().error(err); throw new MojoExecutionException(err, ex); } } else { final String err = "To run speedment:generate a valid configFile needs to be specified."; getLog().error(err); throw new MojoExecutionException(err); } }
@Override public void newProject() { try { MainApp.setInjector(injector.newBuilder().build()); final MainApp app = new MainApp(); final Stage newStage = new Stage(); app.start(newStage); } catch (final Exception e) { LOGGER.error(e); showError("Could not create empty project", e.getMessage(), e); } }
protected void validateRuntimeConfig(Injector injector) { LOGGER.debug("Validating Runtime Configuration"); final Project project = injector.getOrThrow(ProjectComponent.class) .getProject(); if (project == null) { throw new SpeedmentException("No project defined"); } project.dbmses().forEach(d -> { final String typeName = d.getTypeName(); final Optional<DbmsType> oDbmsType = injector.getOrThrow( DbmsHandlerComponent.class).findByName(typeName); if (!oDbmsType.isPresent()) { throw new SpeedmentException("The database type " + typeName + " is not registered with the " + DbmsHandlerComponent.class.getSimpleName()); } final DbmsType dbmsType = oDbmsType.get(); if (!dbmsType.isSupported()) { LOGGER.error("The database driver class " + dbmsType.getDriverName() + " is not available. Make sure to include it in your " + "class path (e.g. in the POM file)" ); } }); }
/** * Retrieves data about he window settings from the previous session and applies * them to the stage. These settings include window size, position and if * it was maximized or not. * * @param stage the stage to apply these settings to * @param name the name under which we stored the settings */ public static void applyStoredDisplaySettings(Stage stage, String name){ try { if( PREFERENCES.nodeExists(name) ){ Preferences stagePreferences = PREFERENCES.node(name); boolean wasMaximized = stagePreferences.getBoolean(WINDOW_MAXIMIZED, false); if( wasMaximized ){ stage.setMaximized(true); } else { stage.setX( stagePreferences.getDouble(WINDOW_X_POS, DEFUALT_X)); stage.setY( stagePreferences.getDouble(WINDOW_Y_POS, DEFUALT_Y)); stage.setWidth( stagePreferences.getDouble(WINDOW_WIDTH, DEFUALT_WIDTH)); stage.setHeight(stagePreferences.getDouble(WINDOW_HEIGHT, DEFUALT_HEIHGT)); } } } catch (BackingStoreException ex) { LOGGER.error(ex, "Could not access preferences for window " + name); } }