Skip to content

Commit

Permalink
Merge pull request #167 from jboss-logging/revert-165-LOGMGR-188
Browse files Browse the repository at this point in the history
Revert "[LOGMGR-188] Add option for generic properties and child handler name…"
  • Loading branch information
jamezp authored Jan 19, 2018
2 parents 72ac257 + d2d75f9 commit 5a01c6f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 568 deletions.
85 changes: 35 additions & 50 deletions src/main/java/org/jboss/logmanager/PropertyConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.Set;
import java.util.regex.Pattern;

import org.jboss.logmanager.config.ConfigurationProperty;
import org.jboss.logmanager.config.ErrorManagerConfiguration;
import org.jboss.logmanager.config.FilterConfiguration;
import org.jboss.logmanager.config.FormatterConfiguration;
Expand Down Expand Up @@ -138,7 +137,7 @@ public void writeConfiguration(final OutputStream outputStream, final boolean wr
for (String loggerName : loggerNames) {
writeLoggerConfiguration(out, config.getLoggerConfiguration(loggerName), implicitHandlers, implicitFilters, writeExpressions);
}
final List<String> allHandlerNames = config.getPersistableHandlerNames();
final List<String> allHandlerNames = config.getHandlerNames();
final List<String> explicitHandlerNames = new ArrayList<String>(allHandlerNames);
explicitHandlerNames.removeAll(implicitHandlers);
if (!explicitHandlerNames.isEmpty()) {
Expand All @@ -150,7 +149,7 @@ public void writeConfiguration(final OutputStream outputStream, final boolean wr
writeHandlerConfiguration(out, config.getHandlerConfiguration(handlerName), implicitHandlers, implicitFilters,
implicitFormatters, implicitErrorManagers, writeExpressions);
}
final List<String> allFilterNames = config.getPersistableFilterNames();
final List<String> allFilterNames = config.getFilterNames();
final List<String> explicitFilterNames = new ArrayList<String>(allFilterNames);
explicitFilterNames.removeAll(implicitFilters);
if (!explicitFilterNames.isEmpty()) {
Expand All @@ -161,7 +160,7 @@ public void writeConfiguration(final OutputStream outputStream, final boolean wr
for (String filterName : allFilterNames) {
writeFilterConfiguration(out, config.getFilterConfiguration(filterName), writeExpressions);
}
final List<String> allFormatterNames = config.getPersistableFormatterNames();
final List<String> allFormatterNames = config.getFormatterNames();
final ArrayList<String> explicitFormatterNames = new ArrayList<String>(allFormatterNames);
explicitFormatterNames.removeAll(implicitFormatters);
if (!explicitFormatterNames.isEmpty()) {
Expand All @@ -172,7 +171,7 @@ public void writeConfiguration(final OutputStream outputStream, final boolean wr
for (String formatterName : allFormatterNames) {
writeFormatterConfiguration(out, config.getFormatterConfiguration(formatterName), writeExpressions);
}
final List<String> allErrorManagerNames = config.getPersistableErrorManagerNames();
final List<String> allErrorManagerNames = config.getErrorManagerNames();
final ArrayList<String> explicitErrorManagerNames = new ArrayList<String>(allErrorManagerNames);
explicitErrorManagerNames.removeAll(implicitErrorManagers);
if (!explicitErrorManagerNames.isEmpty()) {
Expand All @@ -185,7 +184,7 @@ public void writeConfiguration(final OutputStream outputStream, final boolean wr
}

// Write POJO configurations
final List<String> pojoNames = config.getPersistablePojoNames();
final List<String> pojoNames = config.getPojoNames();
if (!pojoNames.isEmpty()) {
writePropertyComment(out, "POJOs to configure");
writeProperty(out, "pojos", toCsvString(pojoNames));
Expand Down Expand Up @@ -246,7 +245,7 @@ private void writeHandlerConfiguration(final Writer out, final HandlerConfigurat
final Set<String> implicitHandlers, final Set<String> implicitFilters,
final Set<String> implicitFormatters, final Set<String> implicitErrorManagers,
final boolean writeExpressions) throws IOException {
if (handler != null && handler.isPersistable()) {
if (handler != null) {
out.write(NEW_LINE);
final String name = handler.getName();
final String prefix = "handler." + name + ".";
Expand Down Expand Up @@ -298,7 +297,7 @@ private void writeHandlerConfiguration(final Writer out, final HandlerConfigurat
printError("Handler %s is not defined and will not be written to the configuration for handler %s%n", handlerName, name);
}
}
if (!handlerNames.isEmpty() && handler.isHandlerNamesPersistable()) {
if (!handlerNames.isEmpty()) {
writeProperty(out, prefix, "handlers", toCsvString(handlerNames));
}
final List<String> postConfigurationMethods = handler.getPostConfigurationMethods();
Expand All @@ -310,7 +309,7 @@ private void writeHandlerConfiguration(final Writer out, final HandlerConfigurat
}

private static void writeFilterConfiguration(final Writer out, final FilterConfiguration filter, final boolean writeExpressions) throws IOException {
if (filter != null && filter.isPersistable()) {
if (filter != null) {
out.write(NEW_LINE);
final String name = filter.getName();
final String prefix = "filter." + name + ".";
Expand All @@ -329,7 +328,7 @@ private static void writeFilterConfiguration(final Writer out, final FilterConfi
}

private static void writeFormatterConfiguration(final Writer out, final FormatterConfiguration formatter, final boolean writeExpressions) throws IOException {
if (formatter != null && formatter.isPersistable()) {
if (formatter != null) {
out.write(NEW_LINE);
final String name = formatter.getName();
final String prefix = "formatter." + name + ".";
Expand All @@ -348,7 +347,7 @@ private static void writeFormatterConfiguration(final Writer out, final Formatte
}

private static void writeErrorManagerConfiguration(final Writer out, final ErrorManagerConfiguration errorManager, final boolean writeExpressions) throws IOException {
if (errorManager != null && errorManager.isPersistable()) {
if (errorManager != null) {
out.write(NEW_LINE);
final String name = errorManager.getName();
final String prefix = "errorManager." + name + ".";
Expand All @@ -367,7 +366,7 @@ private static void writeErrorManagerConfiguration(final Writer out, final Error
}

private static void writePojoConfiguration(final Writer out, final PojoConfiguration pojo, final boolean writeExpressions) throws IOException {
if (pojo != null && pojo.isPersistable()) {
if (pojo != null) {
out.write(NEW_LINE);
final String name = pojo.getName();
final String prefix = "pojo." + name + ".";
Expand Down Expand Up @@ -427,30 +426,6 @@ private static void writeProperty(final Writer out, final String prefix, final S
out.write(NEW_LINE);
}

/**
* Writes a property to the print stream.
*
* @param out the print stream to write to.
* @param prefix the prefix for the name or {@code null} to use no prefix.
* @param property the property that may be or may not be written to the configuration
* @param writeExpression {@code true} if expressions should be written, {@code false} if the resolved value
* should be written
*/
private static void writeProperty(final Writer out, final String prefix, final ConfigurationProperty property, final boolean writeExpression) throws IOException {
if (property.isPersistable()) {
final String value = property.getValue(writeExpression);
if (!value.isEmpty()) {
if (prefix == null) {
writeKey(out, property.getKey());
} else {
writeKey(out, String.format("%s%s", prefix, property.getKey()));
}
writeValue(out, value);
out.write(NEW_LINE);
}
}
}

/**
* Writes a collection of properties to the print stream. Uses the {@link org.jboss.logmanager.config.PropertyConfigurable#getPropertyValueString(String)}
* to extract the value.
Expand All @@ -462,23 +437,33 @@ private static void writeProperty(final Writer out, final String prefix, final C
* should be written
*/
private static void writeProperties(final Writer out, final String prefix, final PropertyConfigurable propertyConfigurable, final boolean writeExpression) throws IOException {
final List<ConfigurationProperty> props = propertyConfigurable.getProperties();
if (!props.isEmpty()) {
final List<String> names = propertyConfigurable.getPropertyNames();
if (!names.isEmpty()) {
final List<String> ctorProps = propertyConfigurable.getConstructorProperties();
final List<String> names = new ArrayList<>();
for (ConfigurationProperty property : props) {
if (property.isPersistable()) {
names.add(property.getKey());
if (prefix == null) {
writeProperty(out, "properties", toCsvString(names));
if (!ctorProps.isEmpty()) {
writeProperty(out, "constructorProperties", toCsvString(ctorProps));
}
}
if (!names.isEmpty()) {
for (String name : names) {
if (writeExpression) {
writeProperty(out, name, propertyConfigurable.getPropertyValueExpression(name).getValue());
} else {
writeProperty(out, name, propertyConfigurable.getPropertyValueString(name));
}
}
} else {
writeProperty(out, prefix, "properties", toCsvString(names));
}
if (!ctorProps.isEmpty()) {
writeProperty(out, prefix, "constructorProperties", toCsvString(ctorProps));
}
for (ConfigurationProperty property : props) {
writeProperty(out, prefix, property, writeExpression);
if (!ctorProps.isEmpty()) {
writeProperty(out, prefix, "constructorProperties", toCsvString(ctorProps));
}
for (String name : names) {
if (writeExpression) {
writeProperty(out, prefix, name, propertyConfigurable.getPropertyValueExpression(name).getValue());
} else {
writeProperty(out, prefix, name, propertyConfigurable.getPropertyValueString(name));
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ abstract class AbstractPropertyConfiguration<T, C extends AbstractPropertyConfig
private final String moduleName;
private final String className;
private final String[] constructorProperties;
private final Map<String, ConfigurationProperty> properties = new LinkedHashMap<>(0);
private final Map<String, ValueExpression<String>> properties = new LinkedHashMap<String, ValueExpression<String>>(0);
private final Map<String, Method> postConfigurationMethods = new LinkedHashMap<String, Method>();

private boolean persistable;

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;
Expand All @@ -76,7 +74,6 @@ protected AbstractPropertyConfiguration(final Class<T> baseClass, final LogConte
throw new IllegalArgumentException(String.format("Failed to load class \"%s\" for %s \"%s\"", className, getDescription(), name), e);
}
this.actualClass = actualClass;
persistable = true;
}

ConfigAction<T> getConstructAction() {
Expand Down Expand Up @@ -110,8 +107,7 @@ public T validate() throws IllegalArgumentException {
if (! properties.containsKey(property)) {
throw new IllegalArgumentException(String.format("No property named \"%s\" is configured on %s \"%s\"", property, getDescription(), getName()));
}
final ConfigurationProperty configurationProperty = properties.get(property);
final ValueExpression<String> valueExpression = configurationProperty == null ? null : configurationProperty.getValue();
final ValueExpression valueExpression = properties.get(property);
final Object value = getConfiguration().getValue(actualClass, property, paramTypes[i], valueExpression, true).getObject();
params[i] = value;
}
Expand Down Expand Up @@ -150,18 +146,13 @@ static boolean contains(Object[] array, Object val) {
}

public void setPropertyValueString(final String propertyName, final String value) throws IllegalArgumentException {
setPropertyValueExpression(propertyName, value, true);
}

@Override
public void setPropertyValueString(final String propertyName, final String value, final boolean persist) throws IllegalArgumentException {
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 ConfigurationPropertyImpl(propertyName, ValueExpression.STRING_RESOLVER.resolve(value), persist));
setPropertyValueExpression(propertyName, ValueExpression.STRING_RESOLVER.resolve(value));
}

public String getPropertyValueString(final String propertyName) {
Expand All @@ -170,55 +161,39 @@ public String getPropertyValueString(final String propertyName) {

@Override
public ValueExpression<String> getPropertyValueExpression(final String propertyName) {
return properties.containsKey(propertyName) ? properties.get(propertyName).getValue() : ValueExpression.NULL_STRING_EXPRESSION;
return properties.containsKey(propertyName) ? properties.get(propertyName) : ValueExpression.NULL_STRING_EXPRESSION;
}

@Override
public void setPropertyValueExpression(final String propertyName, final String expression) {
setPropertyValueExpression(propertyName, expression, true);
}

@Override
public void setPropertyValueExpression(final String propertyName, final String expression, final boolean persist) {
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 ConfigurationPropertyImpl(propertyName, ValueExpression.STRING_RESOLVER.resolve(expression), persist));
setPropertyValueExpression(propertyName, ValueExpression.STRING_RESOLVER.resolve(expression));
}

@Override
public void setPropertyValueExpression(final String propertyName, final String expression, final String value) {
setPropertyValueExpression(propertyName, expression, value, true);
}

@Override
public void setPropertyValueExpression(final String propertyName, final String expression, final String value, final boolean persist) {
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 ConfigurationPropertyImpl(propertyName, new ValueExpressionImpl<>(expression, value), persist));
setPropertyValueExpression(propertyName, new ValueExpressionImpl<String>(expression, value));
}

@Override
public List<ConfigurationProperty> getProperties() {
return new ArrayList<>(properties.values());
}

private void setPropertyValueExpression(final String propertyName, final ConfigurationProperty configurationProperty) {
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()));
}
final ConfigurationProperty oldProperty = properties.put(propertyName, configurationProperty);
final ValueExpression<String> oldValue = oldProperty == null ? null : oldProperty.getValue();
final ValueExpression<String> oldValue = properties.put(propertyName, expression);
getConfiguration().addAction(new ConfigAction<ObjectProducer>() {
public ObjectProducer validate() throws IllegalArgumentException {
if (setter == null) {
Expand All @@ -228,7 +203,7 @@ public ObjectProducer validate() throws IllegalArgumentException {
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, configurationProperty.getValue(), false);
return getConfiguration().getValue(actualClass, propertyName, propertyType, expression, false);
}

public void applyPreCreate(final ObjectProducer param) {
Expand All @@ -255,7 +230,7 @@ public void rollback() {
}
final ObjectProducer producer;
if (replacement) {
properties.put(propertyName, oldProperty);
properties.put(propertyName, oldValue);
producer = getConfiguration().getValue(actualClass, propertyName, propertyType, oldValue, true);
} else {
properties.remove(propertyName);
Expand Down Expand Up @@ -291,9 +266,8 @@ public boolean removeProperty(final String propertyName) {
if (setter == null) {
throw new IllegalArgumentException(String.format("No property \"%s\" setter found for %s \"%s\"", propertyName, getDescription(), getName()));
}
final ConfigurationProperty oldProperty = properties.remove(propertyName);
if (oldProperty != null) {
final ValueExpression<String> oldValue = oldProperty.getValue();
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);
Expand Down Expand Up @@ -466,16 +440,6 @@ public void rollback() {
return true;
}

@Override
public boolean isPersistable() {
return persistable;
}

@Override
public void setPersistable(final boolean persistable) {
this.persistable = persistable;
}

protected final void addPostConfigurationActions() {
addPostConfigurationActions(false);
}
Expand Down
Loading

0 comments on commit 5a01c6f

Please sign in to comment.