public void applyPreCreate(final ObjectProducer param) { addPostConfigurationActions(); }
@Override public boolean hasConstructorProperty(final String propertyName) { return contains(constructorProperties, propertyName); }
public ObjectProducer validate() throws IllegalArgumentException { if (setter == null) { return ObjectProducer.NULL_PRODUCER; } final Class<?> propertyType = getPropertyType(actualClass, propertyName); if (propertyType == null) { throw new IllegalArgumentException(String.format("No property \"%s\" type could be determined for %s \"%s\"", propertyName, getDescription(), getName())); } return getConfiguration().getValue(actualClass, propertyName, propertyType, expression, false); }
public ObjectProducer validate() throws IllegalArgumentException { final Class<?> propertyType = getPropertyType(actualClass, propertyName); if (propertyType == null) { throw new IllegalArgumentException(String.format("No property \"%s\" type could be determined for %s \"%s\"", propertyName, getDescription(), getName())); } return getDefaultValue(propertyType); }
static Class<?> getConstructorPropertyType(Class<?> clazz, String propertyName) { final Method getter = getPropertyGetter(clazz, propertyName); return getter != null ? getter.getReturnType() : getPropertyType(clazz, propertyName); }
@Override public void setPropertyValueExpression(final String propertyName, final String expression) { if (isRemoved()) { throw new IllegalArgumentException(String.format("Cannot set property \"%s\" on %s \"%s\" (removed)", propertyName, getDescription(), getName())); } if (propertyName == null) { throw new IllegalArgumentException("propertyName is null"); } setPropertyValueExpression(propertyName, ValueExpression.STRING_RESOLVER.resolve(expression)); }
public boolean removeProperty(final String propertyName) { if (isRemoved()) { throw new IllegalArgumentException(String.format("Cannot remove property \"%s\" on %s \"%s\" (removed)", propertyName, getDescription(), getName())); final Method setter = getPropertySetter(actualClass, propertyName); if (setter == null) { throw new IllegalArgumentException(String.format("No property \"%s\" setter found for %s \"%s\"", propertyName, getDescription(), getName())); getConfiguration().addAction(new ConfigAction<ObjectProducer>() { public ObjectProducer validate() throws IllegalArgumentException { final Class<?> propertyType = getPropertyType(actualClass, propertyName);
private void setPropertyValueExpression(final String propertyName, final ValueExpression<String> expression) { final boolean replacement = properties.containsKey(propertyName); final boolean constructorProp = contains(constructorProperties, propertyName); final Method setter = getPropertySetter(actualClass, propertyName); if (setter == null && ! constructorProp) { throw new IllegalArgumentException(String.format("No property \"%s\" setter found for %s \"%s\"", propertyName, getDescription(), getName())); getConfiguration().addAction(new ConfigAction<ObjectProducer>() { public ObjectProducer validate() throws IllegalArgumentException { if (setter == null) {
protected final Deque<?> removePostConfigurationActions() { final String name = className + "." + getName(); return getConfiguration().removePostConfigurationActions(name); }
@Override public boolean removePostConfigurationMethod(final String methodName) { final LogContextConfigurationImpl configuration = getConfiguration(); if (!postConfigurationMethods.containsKey(methodName)) { return false; } final Method method = postConfigurationMethods.get(methodName); postConfigurationMethods.remove(methodName); configuration.addAction(new ConfigAction<Void>() { public Void validate() throws IllegalArgumentException { return null; } public void applyPreCreate(final Void param) { } public void applyPostCreate(final Void param) { } public void rollback() { postConfigurationMethods.put(methodName, method); addPostConfigurationActions(true); } }); return true; }
@Override public T getInstance() { return refs.get(getName()); }
static Class<?> getPropertyType(Class<?> clazz, String propertyName) { final Method setter = getPropertySetter(clazz, propertyName); return setter != null ? setter.getParameterTypes()[0] : null; }
public String getPropertyValueString(final String propertyName) { return getPropertyValueExpression(propertyName).getResolvedValue(); }
protected AbstractPropertyConfiguration(final Class<T> baseClass, final LogContextConfigurationImpl configuration, final Map<String, T> refs, final Map<String, C> configs, final String name, final String moduleName, final String className, final String[] constructorProperties) { super(name, configuration, refs, configs); this.moduleName = moduleName; this.className = className; if (className == null) { throw new IllegalArgumentException("className is null"); } this.constructorProperties = constructorProperties; final ClassLoader classLoader; if (moduleName != null) try { classLoader = ModuleFinder.getClassLoader(moduleName); } catch (Throwable e) { throw new IllegalArgumentException(String.format("Failed to load module \"%s\" for %s \"%s\"", moduleName, getDescription(), name), e); } else { classLoader = getClass().getClassLoader(); } final Class<? extends T> actualClass; try { actualClass = Class.forName(className, true, classLoader).asSubclass(baseClass); } catch (Exception e) { throw new IllegalArgumentException(String.format("Failed to load class \"%s\" for %s \"%s\"", className, getDescription(), name), e); } this.actualClass = actualClass; }
@Override public void setPropertyValueExpression(final String propertyName, final String expression, final String value) { if (isRemoved()) { throw new IllegalArgumentException(String.format("Cannot set property \"%s\" on %s \"%s\" (removed)", propertyName, getDescription(), getName())); } if (propertyName == null) { throw new IllegalArgumentException("propertyName is null"); } setPropertyValueExpression(propertyName, new ValueExpressionImpl<String>(expression, value)); }
public void applyPostCreate(final ObjectProducer param) { final T instance = getRefs().get(getName()); try { setter.invoke(instance, param.getObject()); } catch (Throwable e) { StandardOutputStreams.printError(e, "Failed to invoke setter %s with value %s%n.", setter.getName(), param.getObject()); } }
private void addPostConfigurationActions(final boolean replace) { final String name = className + "." + getName(); final LogContextConfigurationImpl configuration = getConfiguration(); if (!replace && configuration.postConfigurationActionsExist(name)) { return;
@Override public boolean addPostConfigurationMethod(final String methodName) { final LogContextConfigurationImpl configuration = getConfiguration(); if (postConfigurationMethods.containsKey(methodName)) { return false; } configuration.addAction(new ConfigAction<Method>() { public Method validate() throws IllegalArgumentException { try { return actualClass.getMethod(methodName); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("Method '%s' not found on '%s'", methodName, actualClass.getName())); } } public void applyPreCreate(final Method param) { } public void applyPostCreate(final Method param) { postConfigurationMethods.put(methodName, param); // TODO (jrp) this isn't the best for performance addPostConfigurationActions(true); } public void rollback() { postConfigurationMethods.remove(methodName); addPostConfigurationActions(true); } }); return true; }
public void rollback() { // We need to once again determine the property type final Class<?> propertyType = getPropertyType(actualClass, propertyName); if (propertyType == null) { // We don't want the rest of the rollback to fail so we'll just log a message StandardOutputStreams.printError("No property \"%s\" type could be determined for %s \"%s\"", propertyName, getDescription(), getName()); return; } // Get the reference instance, the old value and reset to the old value final T instance = getRefs().get(getName()); final ObjectProducer producer = getConfiguration().getValue(actualClass, propertyName, propertyType, oldValue, true); try { setter.invoke(instance, producer.getObject()); } catch (Throwable e) { StandardOutputStreams.printError(e, "Failed to invoke setter %s with value %s%n.", setter.getName(), producer.getObject()); } } });