private void executeOneByOne(Statement statement) throws SQLException { for (String sqlStatement : source.get()) { statementCount++; try { statement.execute(sqlStatement); } catch (Exception e) { log.error(String.format("Can't execute sql statement (%d): %s", statementCount, sqlStatement), e); throw e; } } }
public static <O> SingletonSupplier<O> of(Supplier<O> supplier) { return of(supplier, true); }
public void close() { if (closed || closing) { return; } closing = true; try { if (context.withValue()) { context.get().close(); } if (executor.withValue()) { executor.get().close(); } } catch (Exception e) { throw new RuntimeException("Can't close boot for reason: " + e.getMessage(), e); } finally { cleanMemory(); configuration.clear(); context.clear(); executor.clear(); closed = true; } }
@Override public O get() { return cache == null ? cache = load() : cache; }
public static <O> SingletonSupplier<O> of(Supplier<O> supplier,boolean requireNonNull) { return new SingletonSupplier<>(supplier, requireNonNull); }
/** * Get the {@link ConfigModelReader} for the given {@code format}. * * @param format format of reader to get * @return the found reader * @throws IllegalArgumentException if no reader found for the given format * @throws NullPointerException if format is null */ static ConfigModelReader reader(String format) { Map<String, ConfigModelReader> readers = READERS.get(); ConfigModelReader reader = readers.get(Objects.requireNonNull(format)); if (reader == null) { throw new IllegalArgumentException(String.format("Can't find reader for format: %s, available formats: %s", format, readers.keySet())); } return reader; }
public SqlScriptConsumer build() { return new SqlScriptConsumer(SingletonSupplier.of(source::build), batchSize, blobsContainers.build()); } }
/** * Get the {@link ApplicationConfigReader} for the given {@code format}. * * @param format format of reader to get * @return the found reader * @throws IllegalArgumentException if no reader found for the given format * @throws NullPointerException if format is null */ static ApplicationConfigReader reader(String format) { Map<String, ApplicationConfigReader> readers = READERS.get(); ApplicationConfigReader reader = readers.get(Objects.requireNonNull(format)); if (reader == null) { throw new IllegalArgumentException(String.format("Can't find reader for format: %s, available formats: %s", format, readers.keySet())); } return reader; }
public SqlScriptReader build() { if (source != null) { InputStream stream = source.get(); try { if (gzip) { stream = new GZIPInputStream(this.source.get()); } } catch (IOException e) { throw new IllegalStateException("Can't get gzip input stream", e); } InputStream finalStream = stream; return new SqlScriptReader(SingletonSupplier.of(() -> new BufferedReader(new InputStreamReader(finalStream, encoding))), skipComment, skipTrim, gzip); } return new SqlScriptReader(sourceReader, skipComment, skipTrim, gzip); } }
/** * Get the {@link ApplicationConfigWriter} for the given {@code format}. * * @param format format of writer to get * @return the found writer * @throws IllegalArgumentException if no writer found for the given format * @throws NullPointerException if format is null */ static ApplicationConfigWriter writer(String format) { Map<String, ApplicationConfigWriter> writers = WRITERS.get(); ApplicationConfigWriter writer = writers.get(Objects.requireNonNull(format)); if (writer == null) { throw new IllegalArgumentException(String.format("Can't find writer for format: %s, available formats: %s", format, writers.keySet())); } return writer; }
private ApplicationBoot(ApplicationBootInitializer<Config, Context> initializer) { this.initializer = initializer; this.initializer.initOnce(); this.configuration = SingletonSupplier.of(() -> this.initializer.createConfiguration(this)); this.context = SingletonSupplier.of(() -> this.initializer.createContext(this, getConfiguration())); this.threadFactory = SingletonSupplier.of(ApplicationThreadFactory::new); this.executor = SingletonSupplier.of(() -> this.initializer.createExecutor(this, getConfiguration(), getContext())); INSTANCE = this; }
/** * Get the {@link ConfigModelWriter} for the given {@code format}. * * @param format format of writer to get * @return the found writer * @throws IllegalArgumentException if no writer found for the given format * @throws NullPointerException if format is null */ static ConfigModelWriter writer(String format) { Map<String, ConfigModelWriter> writers = WRITERS.get(); ConfigModelWriter writer = writers.get(Objects.requireNonNull(format)); if (writer == null) { throw new IllegalArgumentException(String.format("Can't find writer for format: %s, available formats: %s", format, writers.keySet())); } return writer; }
public ApplicationThreadFactory getThreadFactory() { checkNotClosed("threadFactory"); return threadFactory.get(); }
private ActionExecutor getExecutor() { checkNotClosed("executor"); return executor.get(); }
public Context getContext() { checkNotClosed("context"); return context.get(); }
public Config getConfiguration() { checkNotClosed("configuration"); return configuration.get(); }
private void executeByBatch(Statement statement) throws SQLException { for (String sqlStatement : source.get()) { statementCount++; try { statement.addBatch(sqlStatement); statements.add(sqlStatement); } catch (Exception e) { log.error(String.format("Can't add sql statement (%d) in batch: %s", statementCount, sqlStatement), e); throw e; } if (statementCount > 0 && statementCount % batchSize == 0) { flush(statement); } } flush(statement); }
public void startInThread() { Runtime.getRuntime().addShutdownHook(threadFactory.get().newThread(this::shutdown, "shutdown", false)); boolean shutdown = false; while (!shutdown) { Thread thread = threadFactory.get().newThread(this::start, "main", false); thread.start(); try { // lock boot lockBoot(); // boot is free to close or reload log.info(String.format("%s Application boot unlock...", BOOT_LOG_PREFIX)); } catch (InterruptedException e) { log.error(String.format("Main thread was interrupted for reason %s", e.getMessage()), e); } finally { if (reload) { try { close(); } catch (Exception e) { log.error(String.format("Could not close boot, will shutdown application: %s", e.getMessage()), e); // if can't close, then shutdown shutdown = true; } } else { // if no reload, this means shutdown shutdown = true; } } } shutdown(); }