/** * Applies a function for each file from a folder. The folder should contain at least one file to process, otherwise * an exception will be thrown. * * @param folder * {@link File} representing the folder where the files will be used from processing. * @param function * {@link Function} to apply on each found file. */ public static void forEachFileInFolder(final File folder, final Function<File, Void> function) { Validate.notNull(function); final Collection<File> files = FileUtils.listFiles(folder, TrueFileFilter.TRUE, FalseFileFilter.INSTANCE); int processedNumber = 0; for (final File file : files) { try { function.apply(file); } catch (final Exception e) { throw new RuntimeException("Problem while applying function on file: " + file, e); } processedNumber++; } logSuccess(processedNumber); }
/** * Applies a function for each file from a folder. The folder should contain at least one file to process, otherwise * an exception will be thrown. * * @param folder * {@link File} representing the folder where the files will be used from processing. * @param function * {@link Function} to apply on each found file. */ public static void forEachFileInFolder(final File folder, final Function<File, Void> function) { Validate.notNull(function); final Collection<File> files = FileUtils.listFiles(folder, TrueFileFilter.TRUE, FalseFileFilter.INSTANCE); int processedNumber = 0; for (final File file : files) { try { function.apply(file); } catch (final Exception e) { throw new RuntimeException("Problem while applying function on file: " + file, e); } processedNumber++; } logSuccess(processedNumber); }
/** * Uses the wildcardExpanderHandler to process all found files and directories. * * @param files * a collection of all files and folders found during wildcard matching. * @VisibleForTestOnly */ void triggerWildcardExpander(final Collection<File> allFiles, final WildcardContext wildcardContext) throws IOException { LOG.debug("wildcard resources: {}", allFiles); if (allFiles.isEmpty()) { final String message = String.format("No resource found for wildcard: %s", wildcardContext.getWildcard()); LOG.warn(message); throw new IOException(message); } if (wildcardExpanderHandler != null) { try { wildcardExpanderHandler.apply(allFiles); } catch(final IOException e) { // preserve exception type if the exception is already an IOException throw e; } catch (final Exception e) { LOG.debug("wildcard expanding error. Reporting original exception", e); throw new IOException("Exception during expanding wildcard: " + e.getMessage()); } } }
/** * Uses the wildcardExpanderHandler to process all found files and directories. * * @param files * a collection of all files and folders found during wildcard matching. * @VisibleForTestOnly */ void triggerWildcardExpander(final Collection<File> allFiles, final WildcardContext wildcardContext) throws IOException { LOG.debug("wildcard resources: {}", allFiles); if (allFiles.isEmpty()) { final String message = String.format("No resource found for wildcard: %s", wildcardContext.getWildcard()); LOG.warn(message); throw new IOException(message); } if (wildcardExpanderHandler != null) { try { wildcardExpanderHandler.apply(allFiles); } catch(final IOException e) { // preserve exception type if the exception is already an IOException throw e; } catch (final Exception e) { LOG.debug("wildcard expanding error. Reporting original exception", e); throw new IOException("Exception during expanding wildcard: " + e.getMessage()); } } }
private void forEachCallbackDo(final Function<LifecycleCallback, Void> func) { for (final LifecycleCallback callback : getCallbacks()) { try { func.apply(callback); } catch (final Exception e) { LOG.error("Problem invoking callback", e); onException(e); } finally { map.remove(Context.getCorrelationId()); } } }
private void forEachCallbackDo(final Function<LifecycleCallback, Void> func) { for (final LifecycleCallback callback : getCallbacks()) { try { func.apply(callback); } catch (final Exception e) { LOG.error("Problem invoking callback", e); onException(e); } finally { map.remove(Context.getCorrelationId()); } } }
/** * A blocking operation which waits for the provided function to be true. If the function doesn't return true after a * timeout period (expressed in milliseconds), it will throw a {@link RuntimeException}. * * @param function * a {@link Function} responsible for expressing the until expression. It must return true if the wait * blocking operation should stop. * @param timeout * number of milliseconds to wait until expression is true. If this timeout is exceeded, the exception will * be thrown. */ public static void waitUntil(final Function<Void, Boolean> function, final long timeout) { final long start = System.currentTimeMillis(); try { while (!function.apply(null)) { if (System.currentTimeMillis() - start > timeout) { throw new WroRuntimeException("Timeout of " + timeout + "ms exceeded."); } } } catch (final Exception e) { throw WroRuntimeException.wrap(e); } }
/** * A blocking operation which waits for the provided function to be true. If the function doesn't return true after a * timeout period (expressed in milliseconds), it will throw a {@link RuntimeException}. * * @param function * a {@link Function} responsible for expressing the until expression. It must return true if the wait * blocking operation should stop. * @param timeout * number of milliseconds to wait until expression is true. If this timeout is exceeded, the exception will * be thrown. */ public static void waitUntil(final Function<Void, Boolean> function, final long timeout) { final long start = System.currentTimeMillis(); try { while (!function.apply(null)) { if (System.currentTimeMillis() - start > timeout) { throw new WroRuntimeException("Timeout of " + timeout + "ms exceeded."); } } } catch (final Exception e) { throw WroRuntimeException.wrap(e); } }
@Override protected void onImportDetected(final String importedUri) { getLog().debug("Found @import " + importedUri); try { final ChangeStatus status = func.apply(importedUri); getLog().debug("ChangeStatus for " + importedUri + ": " + status); if (ChangeStatus.NOT_CHANGED.equals(status)) { remember(Resource.create(importedUri, ResourceType.CSS)); } } catch (final Exception e) { getLog().error("Cannot apply a function on @import resource: " + importedUri + ". Ignoring it.", e); } remember(Resource.create(importedUri, ResourceType.CSS)); }
@Override protected void onImportDetected(final String importedUri) { getLog().debug("Found @import " + importedUri); try { final ChangeStatus status = func.apply(importedUri); getLog().debug("ChangeStatus for " + importedUri + ": " + status); if (ChangeStatus.NOT_CHANGED.equals(status)) { remember(Resource.create(importedUri, ResourceType.CSS)); } } catch (final Exception e) { getLog().error("Cannot apply a function on @import resource: " + importedUri + ". Ignoring it.", e); } remember(Resource.create(importedUri, ResourceType.CSS)); }
/** * Creates a report which handles the adaptation of type <F> to {@link LintItem}. * * @param lintReport * {@link LintReport} containing all lints. * @param formatterType * the type of formatter to use. * @param adapter * a {@link Function} responsible for adapting a type <F> into {@link LintItem} */ private static <F> ReportXmlFormatter createInternal(final LintReport<F> lintReport, final FormatterType formatterType, final Function<F, LintItem> adapter) { Validate.notNull(lintReport); final LintReport<LintItem> report = new LintReport<LintItem>(); for (final ResourceLintReport<F> reportItem : lintReport.getReports()) { final Collection<LintItem> lintItems = new ArrayList<LintItem>(); for (final F lint : reportItem.getLints()) { try { LOG.debug("Adding lint: {}", lint); lintItems.add(adapter.apply(lint)); } catch (final Exception e) { throw WroRuntimeException.wrap(e, "Problem while adapting lint item"); } } report.addReport(ResourceLintReport.create(reportItem.getResourcePath(), lintItems)); } return new ReportXmlFormatter(report, formatterType); }
/** * Creates a report which handles the adaptation of type <F> to {@link LintItem}. * * @param lintReport * {@link LintReport} containing all lints. * @param formatterType * the type of formatter to use. * @param adapter * a {@link Function} responsible for adapting a type <F> into {@link LintItem} */ private static <F> ReportXmlFormatter createInternal(final LintReport<F> lintReport, final FormatterType formatterType, final Function<F, LintItem> adapter) { Validate.notNull(lintReport); final LintReport<LintItem> report = new LintReport<LintItem>(); for (final ResourceLintReport<F> reportItem : lintReport.getReports()) { final Collection<LintItem> lintItems = new ArrayList<LintItem>(); for (final F lint : reportItem.getLints()) { try { LOG.debug("Adding lint: {}", lint); lintItems.add(adapter.apply(lint)); } catch (final Exception e) { throw WroRuntimeException.wrap(e, "Problem while adapting lint item"); } } report.addReport(ResourceLintReport.create(reportItem.getResourcePath(), lintItems)); } return new ReportXmlFormatter(report, formatterType); }