Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LOGMGR-178] Set properties a default value when the property is remo… #142

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private void setPropertyValueExpression(final String propertyName, final ValueEx
if (setter == null && ! constructorProp) {
throw new IllegalArgumentException(String.format("No property \"%s\" setter found for %s \"%s\"", propertyName, getDescription(), getName()));
}
final ValueExpression oldValue = properties.put(propertyName, expression);
final ValueExpression<String> oldValue = properties.put(propertyName, expression);
getConfiguration().addAction(new ConfigAction<ObjectProducer>() {
public ObjectProducer validate() throws IllegalArgumentException {
if (setter == null) {
Expand Down Expand Up @@ -222,10 +222,33 @@ public void applyPostCreate(final ObjectProducer param) {
}

public void rollback() {
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;
}
final ObjectProducer producer;
if (replacement) {
properties.put(propertyName, oldValue);
producer = getConfiguration().getValue(actualClass, propertyName, propertyType, oldValue, true);
} else {
properties.remove(propertyName);
producer = getDefaultValue(propertyType);
}
if (setter != null) {
// Get the reference instance, the old value and reset to the old value
final T instance = getRefs().get(getName());
if (instance != null) {
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());
}
} else {
// If the instance is not available don't keep the property
properties.remove(propertyName);
}
}
}
});
Expand All @@ -239,11 +262,55 @@ public boolean removeProperty(final String propertyName) {
if (isRemoved()) {
throw new IllegalArgumentException(String.format("Cannot remove property \"%s\" on %s \"%s\" (removed)", propertyName, getDescription(), getName()));
}
try {
return properties.containsKey(propertyName);
} finally {
properties.remove(propertyName);
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()));
}
final ValueExpression<String> oldValue = properties.remove(propertyName);
if (oldValue != null) {
getConfiguration().addAction(new ConfigAction<ObjectProducer>() {
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);
}

public void applyPreCreate(final ObjectProducer param) {
addPostConfigurationActions();
}

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

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());
}
}
});
return true;
}
return false;
}

public List<String> getPropertyNames() {
Expand Down Expand Up @@ -453,6 +520,28 @@ static Method getPropertyGetter(Class<?> clazz, String propertyName) {
return null;
}

private static ObjectProducer getDefaultValue(final Class<?> paramType) {
if (paramType == boolean.class) {
return new SimpleObjectProducer(Boolean.FALSE);
} else if (paramType == byte.class) {
return new SimpleObjectProducer((byte) 0x00);
} else if (paramType == short.class) {
return new SimpleObjectProducer((short) 0);
} else if (paramType == int.class) {
return new SimpleObjectProducer(0);
} else if (paramType == long.class) {
return new SimpleObjectProducer(0L);
} else if (paramType == float.class) {
return new SimpleObjectProducer(0.0f);
} else if (paramType == double.class) {
return new SimpleObjectProducer(0.0d);
} else if (paramType == char.class) {
return new SimpleObjectProducer((char) 0x00);
} else {
return SimpleObjectProducer.NULL_PRODUCER;
}
}

static class ModuleFinder {

private ModuleFinder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ public String getPattern() {
* @param pattern the pattern
*/
public void setPattern(final String pattern) {
setSteps(FormatStringParser.getSteps(pattern, colors));
if (pattern == null) {
setSteps(null);
} else {
setSteps(FormatStringParser.getSteps(pattern, colors));
}
this.pattern = pattern;
}

Expand Down