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); } } }
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 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 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 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; } }
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); } }
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; }
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; } }