@Test public void concurrentPasswordRuleBuilding() { String body = POJOHelper.serialize(createBaseDefaultPasswordRuleConf()); assertTimeoutPreemptively(Duration.ofSeconds(30), () -> { TestImplementation implementation = new TestImplementation(); implementation.setBody(body); ReentrantLock lock = new ReentrantLock(); lock.lock(); AtomicInteger runningThreads = new AtomicInteger(0); AtomicInteger errorCount = new AtomicInteger(0); List<String> errorMessages = Collections.synchronizedList(new LinkedList<>()); for (int i = 0; i < 10; i++) { runningThreads.incrementAndGet(); new Thread(() -> { try { while (lock.isLocked()) { Thread.yield(); } try { ImplementationManager.buildPasswordRule(implementation).orElseThrow(); } catch (Exception e) { errorMessages.add(e.getLocalizedMessage()); errorCount.incrementAndGet(); } } finally { runningThreads.decrementAndGet(); } }).start(); } lock.unlock(); while (runningThreads.get() > 0) { Thread.yield(); } assertTrue( errorMessages.isEmpty(), () -> errorMessages.stream().collect(Collectors.joining(System.lineSeparator()))); }); }
public static Optional<PasswordRule> buildPasswordRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PasswordRule>buildGroovy(impl)); case JAVA: default: PasswordRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PasswordRuleConf.class); Class<? extends PasswordRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPasswordRuleClass(ruleConf.getClass()); PasswordRule rule = buildJavaWithConf(ruleClass); if (rule == null) { LOG.warn("Could not find matching password rule for {}", impl.getClass()); } else { rule.setConf(ruleConf); } return Optional.ofNullable(rule); } }
ImplementationManager { public static Optional<PasswordRule> buildPasswordRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PasswordRule>buildGroovy(impl)); case JAVA: default: PasswordRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PasswordRuleConf.class); Class<? extends PasswordRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPasswordRuleClass(ruleConf.getClass()); PasswordRule rule = buildJavaWithConf(ruleClass); if (rule == null) { LOG.warn("Could not find matching password rule for {}", impl.getClass()); } else { rule.setConf(ruleConf); } return Optional.ofNullable(rule); } } }
ImplementationManager { public static Optional<PasswordRule> buildPasswordRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PasswordRule>buildGroovy(impl)); case JAVA: default: PasswordRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PasswordRuleConf.class); Class<? extends PasswordRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPasswordRuleClass(ruleConf.getClass()); PasswordRule rule = buildJavaWithConf(ruleClass); if (rule == null) { LOG.warn("Could not find matching password rule for {}", impl.getClass()); } else { rule.setConf(ruleConf); } return Optional.ofNullable(rule); } } private  ImplementationManager(); }
ImplementationManager { public static Optional<PasswordRule> buildPasswordRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PasswordRule>buildGroovy(impl)); case JAVA: default: PasswordRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PasswordRuleConf.class); Class<? extends PasswordRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPasswordRuleClass(ruleConf.getClass()); PasswordRule rule = buildJavaWithConf(ruleClass); if (rule == null) { LOG.warn("Could not find matching password rule for {}", impl.getClass()); } else { rule.setConf(ruleConf); } return Optional.ofNullable(rule); } } private  ImplementationManager(); static Optional<Reportlet> buildReportlet(final Implementation impl); static Optional<AccountRule> buildAccountRule(final Implementation impl); static Optional<PasswordRule> buildPasswordRule(final Implementation impl); static Optional<PullCorrelationRule> buildPullCorrelationRule(final Implementation impl); static Optional<PushCorrelationRule> buildPushCorrelationRule(final Implementation impl); static T build(final Implementation impl); static Class<?> purge(final String implementation); }
ImplementationManager { public static Optional<PasswordRule> buildPasswordRule(final Implementation impl) throws InstantiationException, IllegalAccessException { switch (impl.getEngine()) { case GROOVY: return Optional.of(ImplementationManager.<PasswordRule>buildGroovy(impl)); case JAVA: default: PasswordRuleConf ruleConf = POJOHelper.deserialize(impl.getBody(), PasswordRuleConf.class); Class<? extends PasswordRule> ruleClass = ApplicationContextProvider.getApplicationContext(). getBean(ImplementationLookup.class).getPasswordRuleClass(ruleConf.getClass()); PasswordRule rule = buildJavaWithConf(ruleClass); if (rule == null) { LOG.warn("Could not find matching password rule for {}", impl.getClass()); } else { rule.setConf(ruleConf); } return Optional.ofNullable(rule); } } private  ImplementationManager(); static Optional<Reportlet> buildReportlet(final Implementation impl); static Optional<AccountRule> buildAccountRule(final Implementation impl); static Optional<PasswordRule> buildPasswordRule(final Implementation impl); static Optional<PullCorrelationRule> buildPullCorrelationRule(final Implementation impl); static Optional<PushCorrelationRule> buildPushCorrelationRule(final Implementation impl); static T build(final Implementation impl); static Class<?> purge(final String implementation);  }