static void setCached(VerifyContext context, ExcelSheetRuleExtensionRepository repository) { Reference<ExcelSheetRuleExtensionRepository> ref; if (repository.getClass().getClassLoader() == ClassLoader.getSystemClassLoader()) { ref = new SoftReference<>(repository); } else { ref = new WeakReference<>(repository); } CACHE.put(context.getTestContext().getClassLoader(), ref); }
private static ExcelSheetRuleExtensionRepository newInstance(VerifyContext context) { ClassLoader classLoader = context.getTestContext().getClassLoader(); List<ExcelSheetRuleExtension> extensions = new ArrayList<>(); for (ExcelSheetRuleExtension extension : ServiceLoader.load(ExcelSheetRuleExtension.class, classLoader)) { extensions.add(extension); } return new ExcelSheetRuleExtensionRepository(extensions); }
/** * Returns a {@link ExcelSheetRuleExtensionRepository} for the target context. * @param context the target context * @return a repository object * @throws IllegalArgumentException if some parameters were {@code null} */ public static ExcelSheetRuleExtensionRepository get(VerifyContext context) { if (context == null) { throw new IllegalArgumentException("context must not be null"); //$NON-NLS-1$ } Reference<ExcelSheetRuleExtensionRepository> ref = CACHE.get(context.getTestContext().getClassLoader()); ExcelSheetRuleExtensionRepository cached = ref == null ? null : ref.get(); if (cached != null) { return cached; } ExcelSheetRuleExtensionRepository created = newInstance(context); setCached(context, created); return created; }
@Override public <T> Verifier createVerifier( DataModelDefinition<T> definition, VerifyContext context) throws IOException { DataModelSource expected = expectedFactory.createSource(definition, context.getTestContext()); VerifyRule verifyRule = ruleFactory.createRule(definition, context); boolean succeed = false; try { Verifier verifier = new VerifyRuleVerifier(expected, verifyRule); succeed = true; return verifier; } finally { if (succeed == false) { expected.close(); } } } @Override