List of usage examples for java.util.function Supplier get
T get();
From source file:org.talend.daikon.security.access.RequiresAuthorityAspect.java
/** * The interceptor method for method annotated with {@link RequiresAuthority}. * * @param pjp The method invocation.//from w w w .j a va2s . c om * @return The object * @throws Throwable Throws {@link org.springframework.security.access.AccessDeniedException} in case of denied * access to the invoked method. */ @Around("@annotation(org.talend.daikon.security.access.RequiresAuthority)") public Object requires(ProceedingJoinPoint pjp) throws Throwable { final Authentication authentication = ofNullable(getContext().getAuthentication()).orElse(ANONYMOUS); LOGGER.debug("Checking @Required access on {} for user {}.", pjp, authentication); final MethodSignature methodSignature = (MethodSignature) pjp.getSignature(); final Method method = methodSignature.getMethod(); final RequiresAuthority annotation = method.getAnnotation(RequiresAuthority.class); if (annotation == null) { throw new IllegalArgumentException("Missing @RequiresAuthority annotation."); // Rather unexpected } final String[] authorityArray = annotation.authority(); final Supplier<Stream<String>> authorityStreamSupplier = () -> Stream.of(authorityArray) .filter(StringUtils::isNotBlank); final String[] valueArray = annotation.value(); final Supplier<Stream<String>> valueStreamSupplier = () -> Stream.of(valueArray) .filter(StringUtils::isNotBlank); Supplier<Stream<String>> streamSupplier = null; if (authorityStreamSupplier.get().count() > 0) { streamSupplier = authorityStreamSupplier; } else if (valueStreamSupplier.get().count() > 0) { streamSupplier = valueStreamSupplier; } if (streamSupplier != null && streamSupplier.get().noneMatch(RequiresAuthorityAspect::isAllowed)) { LOGGER.debug("Access denied for user {} on {}.", authentication, method); final Class<? extends AccessDenied> onDeny = annotation.onDeny(); final AccessDenied accessDenied; try { accessDenied = onDeny.newInstance(); return accessDenied.onDeny(annotation, method, pjp.getArgs()); } catch (InstantiationException noInstance) { LOGGER.error("Unable to use on deny custom class {}", onDeny.getName(), noInstance); throw new AccessDeniedException("Access denied for " + method.getName() + ".", noInstance); } } LOGGER.debug("Access allowed for user {} on {}.", authentication, method); return pjp.proceed(); }
From source file:org.wildfly.security.credential.store.KeystorePasswordStoreTest.java
private void testAccessFromMultipleCredentialStores(final ExecutorService executor, Supplier<Callable<Object>> csTask) { try {/* ww w . j a v a 2 s . c om*/ Callable<Object> task = csTask.get(); Future<Object> task1Future = executor.submit(task); task1Future.get(15, TimeUnit.SECONDS); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:spimedb.SpimeDB.java
public <X> X run(String id, Supplier<X> r) { return locker.locked(id, (ii) -> { try {/*from w w w. j av a 2 s. com*/ return r.get(); } catch (Throwable t) { logger.error("{} {} {}", id, r, t); return null; } }); }
From source file:src.main.java.com.vgorcinschi.assignmentone.utils.StringValidator.java
public static String stringValidator(Predicate<String> cond, Supplier<String> validationMsg, String type, Scanner sc) {/* ww w .ja v a2 s . co m*/ String response = null; boolean responseReady = false; System.out.println("Please enter your " + type); while (!responseReady) { String unsanitized = sc.nextLine(); if (!isNotBlank(unsanitized)) { log.error("An invalid input has been provided while asking for " + type); System.out.println("You cannot enter blank name. Please try again."); } else if (ofNullable(cond).isPresent()) { if (!cond.test(unsanitized)) { log.error("An invalid input has been provided(" + unsanitized + ") " + "while asking for " + type + ". Additional requirements failed: " + validationMsg.get()); System.out.println(isNotBlank(validationMsg.get()) ? validationMsg.get() : "You haven't " + "meat caller's additional requirements. Unfortunately he hasn't provided " + "any validation details."); } else { responseReady = true; response = unsanitized; log.info("A successful response has been provided for " + type + ": " + response); } } else { responseReady = true; response = unsanitized; log.info("A successful response has been provided for " + type + ": " + response); } } return response; }
From source file:src.main.java.com.vgorcinschi.assignmenttwo.util.StringValidator.java
public static String stringValidator(Predicate<String> cond, Supplier<String> validationMsg, Supplier<String> type, Scanner sc) { String response = null;/*from w ww .ja v a 2 s. c o m*/ boolean responseReady = false; System.out.println("Please enter your " + type.get()); while (!responseReady) { String unsanitized = sc.nextLine(); if (!isNotBlank(unsanitized)) { log.error("An invalid input has been provided while asking for " + type.get()); System.out.println("You cannot enter blank name. Please try again."); } else if (ofNullable(cond).isPresent()) { if (!cond.test(unsanitized)) { log.error("An invalid input has been provided(" + unsanitized + ") " + "while asking for " + type.get() + ". Additional requirements failed: " + validationMsg.get()); System.out.println(isNotBlank(validationMsg.get()) ? validationMsg.get() : "You haven't " + "meat caller's additional requirements. Unfortunately he hasn't provided " + "any validation details."); } else { responseReady = true; response = unsanitized; log.info("A successful response has been provided for " + type.get() + ": " + response); } } else { responseReady = true; response = unsanitized; log.info("A successful response has been provided for " + type.get() + ": " + response); } } return response; }
From source file:utybo.branchingstorytree.swing.OpenBSTGUI.java
/** * Load and parse a file, using appropriate dialogs if an error occurs to * inform the user and even give him the option to reload the file * * @param file/*from ww w. j a va2s. c o m*/ * The file to load * @param client * The BST Client. This is required for parsing the file * @return */ public void loadFile(final File file, final TabClient client, Consumer<BranchingStory> callback) { SwingWorker<BranchingStory, Object> worker = new SwingWorker<BranchingStory, Object>() { @Override protected BranchingStory doInBackground() throws Exception { try { LOG.trace("Parsing story"); String ext = FilenameUtils.getExtension(file.getName()); BranchingStory bs = null; if (ext.equals("bsp")) { bs = BSTPackager.fromPackage(new ProgressMonitorInputStream(instance, "Opening " + file.getName() + "...", new FileInputStream(file)), client); } else { bs = parser .parse(new BufferedReader(new InputStreamReader( new ProgressMonitorInputStream(instance, "Opening " + file.getName() + "...", new FileInputStream(file)), StandardCharsets.UTF_8)), new Dictionary(), client, "<main>"); client.setBRMHandler(new BRMFileClient(file, client, bs)); } callback.accept(bs); return bs; } catch (final IOException e) { LOG.error("IOException caught", e); showException(Lang.get("file.error").replace("$e", e.getClass().getSimpleName()).replace("$m", e.getMessage()), e); return null; } catch (final BSTException e) { LOG.error("BSTException caught", e); String s = "<html>" + Lang.get("file.bsterror.1"); s += Lang.get("file.bsterror.2"); s += Lang.get("file.bsterror.3").replace("$l", "" + e.getWhere()).replace("$f", "[main]"); if (e.getCause() != null) { s += Lang.get("file.bsterror.4").replace("$e", e.getCause().getClass().getSimpleName()) .replace("$m", e.getCause().getMessage()); } s += Lang.get("file.bsterror.5").replace("$m", "" + e.getMessage()); s += Lang.get("file.bsterror.6"); String s2 = s; if (doAndReturn(() -> Messagers.showConfirm(instance, s2, Messagers.OPTIONS_YES_NO, Messagers.TYPE_ERROR, Lang.get("bsterror"))) == Messagers.OPTION_YES) { LOG.debug("Reloading"); return doInBackground(); } return null; } catch (final Exception e) { LOG.error("Random exception caught", e); showException(Lang.get("file.crash"), e); return null; } } private <T> T doAndReturn(Supplier<T> supplier) { ArrayList<T> l = new ArrayList<>(); invokeSwingAndWait(() -> { l.add(supplier.get()); }); return l.size() == 0 ? null : l.get(0); } @Override protected void done() { try { get(); } catch (InterruptedException e) { // Shouldn't happen } catch (ExecutionException e) { LOG.error("Random exception caught", e); Messagers.showException(instance, Lang.get("file.crash"), e); } } }; worker.execute(); }