private Object createConfiguration(Class<?> type, Object extension) { if (!(extension instanceof IGlobalExtension)) { throw new ExtensionException("Extension '%s' references unknown configuration class '%s'") .withArgs(extension.getClass(), type); } try { return type.newInstance(); } catch (InstantiationException e) { throw new ExtensionException("Cannot instantiate configuration class %s").withArgs(type); } catch (IllegalAccessException e) { throw new ExtensionException("Configuration class '%s' has no public no-arg constructor").withArgs(type); } } }
private Closure createCondition(Class<? extends Closure> clazz) { try { return clazz.getConstructor(Object.class, Object.class).newInstance(null, null); } catch (Exception e) { throw new ExtensionException("Failed to instantiate @IgnoreIf condition", e); } }
private Closure createCondition(Class<? extends Closure> clazz) { try { return clazz.getConstructor(Object.class, Object.class).newInstance(null, null); } catch (Exception e) { throw new ExtensionException("Failed to instantiate @Requires condition", e); } }
private Class<?> loadExtensionClass(String className) { try { return RunContext.class.getClassLoader().loadClass(className); } catch (Exception e) { throw new ExtensionException("Failed to load extension class '%s'", e).withArgs(className); } } }
public void start() { try { socket = new Socket(reportServerAddress, reportServerPort); } catch (IOException e) { throw new ExtensionException(String.format("Error opening connection to report server. " + "Server address: %s Server port: %d", reportServerAddress, reportServerPort), e); } }
private IGlobalExtension instantiateGlobalExtension(Class<?> clazz) { try { return (IGlobalExtension) clazz.newInstance(); } catch (Exception e) { throw new ExtensionException("Failed to instantiate extension '%s'", e).withArgs(clazz.getName()); } }
private List<URL> locateDescriptors(String descriptorPath) { try { return Collections.list(RunContext.class.getClassLoader().getResources(descriptorPath)); } catch (Exception e) { throw new ExtensionException("Failed to locate extension descriptors", e); } }
private void verifyGlobalExtension(Class<?> clazz) { if (!IGlobalExtension.class.isAssignableFrom(clazz)) throw new ExtensionException( "Class '%s' is not a valid global extension because it is not derived from '%s'" ).withArgs(clazz.getName(), IGlobalExtension.class.getName()); }
private static Closure createCondition(Class<? extends Closure> clazz) { if (clazz.equals(Closure.class)) { return null; } try { return clazz.getConstructor(Object.class, Object.class).newInstance(null, null); } catch (Exception e) { throw new ExtensionException("Failed to instantiate @Retry condition", e); } }
private IAnnotationDrivenExtension getOrCreateExtension(Class<? extends IAnnotationDrivenExtension> clazz) { IAnnotationDrivenExtension extension = localExtensions.get(clazz); if (extension == null) { try { extension = clazz.newInstance(); } catch (InstantiationException e) { throw new ExtensionException("Failed to instantiate extension '%s'", e).withArgs(clazz); } catch (IllegalAccessException e) { throw new ExtensionException("No-arg constructor of extension '%s' is not public", e).withArgs(clazz); } configurationRegistry.configureExtension(extension); localExtensions.put(clazz, extension); } return extension; } }
public void start() { logFile.getParentFile().mkdirs(); try { logFile.createNewFile(); fileWriter = new OutputStreamWriter(new FileOutputStream(logFile), "utf-8"); } catch (IOException e) { throw new ExtensionException("Error creating report log file: " + logFile, e); } jsonWriter = new JsonWriter(fileWriter); jsonWriter.setPrettyPrint(prettyPrint); }
private List<String> readDescriptor(URL url) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(url.openStream())); List<String> lines = new ArrayList<>(); String line = reader.readLine(); while (line != null) { line = line.trim(); if (line.length() > 0 && !line.startsWith("#")) lines.add(line); line = reader.readLine(); } return lines; } catch (IOException e) { throw new ExtensionException("Failed to read extension descriptor '%s'", e).withArgs(url); } finally { IoUtil.closeQuietly(reader); } }
private void writeLog(Map log) { try { fileWriter.write(prefix); fileWriter.write("["); jsonWriter.write(log); fileWriter.write("]"); fileWriter.write(postfix); if (liveUpdate) { fileWriter.flush(); } } catch (IOException e) { throw new ExtensionException("Error writing to report log file: " + logFile, e); } } }
public List<Class<?>> loadClasses(String descriptorPath) { Map<String, URL> discoveredClasses = new HashMap<>(); List<Class<?>> extClasses = new ArrayList<>(); for (URL url : locateDescriptors(descriptorPath)) { for (String className : readDescriptor(url)) { if (discoveredClasses.containsKey(className)) { throw new ExtensionException("Duplicated Extension declaration for [%s]\nSource 1: %s\nSource 2: %s\n" + "This is most likely caused by having two different version of a library on the classpath." ).withArgs(className, url, discoveredClasses.get(className)); } discoveredClasses.put(className, url); extClasses.add(loadExtensionClass(className)); } } return extClasses; }
private Object evaluateCondition(Closure condition) { condition.setDelegate(new PreconditionContext()); condition.setResolveStrategy(Closure.DELEGATE_ONLY); try { return condition.call(); } catch (Exception e) { throw new ExtensionException("Failed to evaluate @IgnoreIf condition", e); } } }
protected Object getRuleInstance(FieldInfo field, Object fieldTarget) { Object rule = field.readValue(fieldTarget); if (rule == null) { try { rule = field.getType().newInstance(); field.writeValue(fieldTarget, rule); } catch (Exception e) { throw new ExtensionException("Auto-instantiating @Rule field '%s' failed. You may have to instantiate it manually.", e).withArgs(field.getName()); } } return rule; } }
private Object evaluateCondition(Closure condition) { condition.setDelegate(new PreconditionContext()); condition.setResolveStrategy(Closure.DELEGATE_ONLY); try { return condition.call(); } catch (Exception e) { throw new ExtensionException("Failed to evaluate @Requires condition", e); } } }
IoUtil.closeQuietly(socket); socket = null; throw new ExtensionException("Error sending data to report server. " + "Server address: $reportServerAddress Server port: $reportServerPort", e);
private boolean satisfiesCondition(IMethodInvocation invocation, Throwable failure) { if (condition == null) { return true; } condition.setDelegate(new RetryConditionContext(invocation, failure)); condition.setResolveStrategy(Closure.DELEGATE_ONLY); try { return GroovyRuntimeUtil.isTruthy(condition.call()); } catch (Exception e) { throw new ExtensionException("Failed to evaluate @Retry condition", e); } }