Skip to content

Commit

Permalink
more fine tuning of propertyModelHandler and co
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Feb 27, 2024
1 parent a21c6b3 commit 8f3a304
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,37 @@
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.LevelUtil;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.PropertyContainer;
import ch.qos.logback.core.spi.ScanException;
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
import ch.qos.logback.core.status.OnConsoleStatusListener;
import ch.qos.logback.core.util.OptionHelper;
import ch.qos.logback.core.util.StatusListenerConfigHelper;
import ch.qos.logback.core.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

public class TylerConfiguratorBase extends ContextAwareBase implements PropertyContainer {
public class TylerConfiguratorBase extends ContextAwareBase implements ContextAwarePropertyContainer {

public static final String SET_CONTEXT_NAME = "setContextName";
public static final String SET_CONTEXT_METHOD_NAME = "setContext";
public static final String SET_CONTEXT_NAME_METHOD_NAME = "setContextName";
public static final String SETUP_LOGGER_METHOD_NAME = "setupLogger";
public static final String VARIABLE_SUBSTITUTIONS_HELPER_FIELD_NAME = "variableSubstitutionsHelper";
public static final String PROPERTY_MODEL_HANDLER_HELPER_FIELD_NAME = "propertyModelHandlerHelper";

VariableSubstitutionsHelper variableSubstitutionsHelper;
// initialized via #setContext
protected VariableSubstitutionsHelper variableSubstitutionsHelper;
// context set in #setContext
protected PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this);

private Logger setupLogger(String loggerName, Level level, String levelString, Boolean additivity) {
protected Logger setupLogger(String loggerName, String levelString, Boolean additivity) {
LoggerContext loggerContext = (LoggerContext) context;
Logger logger = loggerContext.getLogger(loggerName);
if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelString)) {
Level level = LevelUtil.levelStringToLevel(levelString);
logger.setLevel(level);
}
if (additivity != null) {
Expand All @@ -48,6 +56,13 @@ private Logger setupLogger(String loggerName, Level level, String levelString, B
return logger;
}

@Override
public void setContext(Context context) {
super.setContext(context);
variableSubstitutionsHelper = new VariableSubstitutionsHelper(context);
propertyModelHandlerHelper.setContext(context);
}

protected void setContextName(String name) {
if(StringUtil.isNullOrEmpty(name)) {
addError("Cannot set context name to null or empty string");
Expand All @@ -72,17 +87,9 @@ protected void addOnConsoleStatusListener() {
* @param ref
* @return
*/
@Override
public String subst(String ref) {
if (ref == null) {
return null;
}

try {
return OptionHelper.substVars(ref, this, context);
} catch (ScanException | IllegalArgumentException e) {
addError("Problem while parsing [" + ref + "]", e);
return ref;
}
return variableSubstitutionsHelper.subst(ref);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ch.qos.logback.core.joran.action.ActionUtil;
import ch.qos.logback.core.model.ModelConstants;
import ch.qos.logback.core.model.PropertyModel;
import ch.qos.logback.core.model.util.PropertyModelUtil;
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.util.ContextUtil;
Expand Down Expand Up @@ -47,7 +47,7 @@ public class VariableModelHelper extends ContextAwareBase {
void updateProperties(PropertyModel propertyModel) {

ActionUtil.Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr());
if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) {
if (PropertyModelHandlerHelper.checkFileAttributeSanity(propertyModel)) {
String file = propertyModel.getFile();
file = tylerConfiguratorBase.subst(file);
try (FileInputStream istream = new FileInputStream(file)) {
Expand All @@ -58,7 +58,7 @@ void updateProperties(PropertyModel propertyModel) {
// is badly malformed, i.e a binary.
addError("Could not read properties file [" + file + "].", e1);
}
} else if (PropertyModelUtil.checkResourceAttributeSanity(propertyModel)) {
} else if (PropertyModelHandlerHelper.checkResourceAttributeSanity(propertyModel)) {
String resource = propertyModel.getResource();
resource = tylerConfiguratorBase.subst(resource);
URL resourceURL = Loader.getResourceBySelfClassLoader(resource);
Expand All @@ -71,7 +71,7 @@ void updateProperties(PropertyModel propertyModel) {
addError("Could not read resource file [" + resource + "].", e);
}
}
} else if (PropertyModelUtil.checkValueNameAttributesSanity(propertyModel)) {
} else if (PropertyModelHandlerHelper.checkValueNameAttributesSanity(propertyModel)) {
// earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no
// need to do this. See RegularEscapeUtil.__UNUSED__basicEscape
String value = propertyModel.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package ch.qos.logback.core.joran.action;

import ch.qos.logback.core.model.processor.ModelInterpretationContext;
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
import ch.qos.logback.core.util.OptionHelper;

public class ActionUtil {
Expand Down Expand Up @@ -50,7 +51,7 @@ static public Scope stringToScope(String scopeStr) {
// }
// }

static public void setProperty(ModelInterpretationContext ic, String key, String value, Scope scope) {
static public void setProperty(ContextAwarePropertyContainer ic, String key, String value, Scope scope) {
switch (scope) {
case LOCAL:
ic.addSubstitutionProperty(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ protected Model buildCurrentModel(SaxEventInterpretationContext interpretationCo
PropertyModel propertyModel = new PropertyModel();
propertyModel.setName(attributes.getValue(NAME_ATTRIBUTE));
propertyModel.setValue(attributes.getValue(VALUE_ATTRIBUTE));
propertyModel.setScopeStr(attributes.getValue(SCOPE_ATTRIBUTE));
propertyModel.setFile(attributes.getValue(FILE_ATTRIBUTE));
propertyModel.setResource(attributes.getValue(RESOURCE_ATTRIBUTE));
propertyModel.setScopeStr(attributes.getValue(SCOPE_ATTRIBUTE));
return propertyModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
import ch.qos.logback.core.model.InsertFromJNDIModel;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.util.PropertyModelUtil;
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
import ch.qos.logback.core.util.JNDIUtil;
import ch.qos.logback.core.util.OptionHelper;

Expand Down Expand Up @@ -61,7 +61,7 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
addError("[" + envEntryName + "] has null or empty value");
} else {
addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope");
PropertyModelUtil.setProperty(mic, asKey, envEntryValue, scope);
PropertyModelHandlerHelper.setProperty(mic, asKey, envEntryValue, scope);
}
} catch (NamingException e) {
addError("Failed to lookup JNDI env-entry [" + envEntryName + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.action.ActionUtil;
import ch.qos.logback.core.joran.action.ActionUtil.Scope;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.ModelConstants;
import ch.qos.logback.core.model.PropertyModel;
import ch.qos.logback.core.model.util.PropertyModelUtil;
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
import ch.qos.logback.core.util.Loader;

public class PropertyModelHandler extends ModelHandlerBase {
Expand All @@ -32,55 +31,12 @@ protected Class<PropertyModel> getSupportedModelClass() {
}

@Override
public void handle(ModelInterpretationContext interpretationContext, Model model) {
public void handle(ModelInterpretationContext mic, Model model) {

PropertyModel propertyModel = (PropertyModel) model;

Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr());

if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) {
String file = propertyModel.getFile();
file = interpretationContext.subst(file);
try (FileInputStream istream = new FileInputStream(file)) {
loadAndSetProperties(interpretationContext, istream, scope);
} catch (FileNotFoundException e) {
addError("Could not find properties file [" + file + "].");
} catch (IOException|IllegalArgumentException e1) { // IllegalArgumentException is thrown in case the file
// is badly malformed, i.e a binary.
addError("Could not read properties file [" + file + "].", e1);
}
} else if (PropertyModelUtil.checkResourceAttributeSanity(propertyModel)) {
String resource = propertyModel.getResource();
resource = interpretationContext.subst(resource);
URL resourceURL = Loader.getResourceBySelfClassLoader(resource);
if (resourceURL == null) {
addError("Could not find resource [" + resource + "].");
} else {
try ( InputStream istream = resourceURL.openStream();) {
loadAndSetProperties(interpretationContext, istream, scope);
} catch (IOException e) {
addError("Could not read resource file [" + resource + "].", e);
}
}
} else if (PropertyModelUtil.checkValueNameAttributesSanity(propertyModel)) {
// earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no
// need to do this. See RegularEscapeUtil.__UNUSED__basicEscape
String value = propertyModel.getValue();

// now remove both leading and trailing spaces
value = value.trim();
value = interpretationContext.subst(value);
ActionUtil.setProperty(interpretationContext, propertyModel.getName(), value, scope);

} else {
addError(ModelConstants.INVALID_ATTRIBUTES);
}
}

void loadAndSetProperties(ModelInterpretationContext mic, InputStream istream, Scope scope) throws IOException {
Properties props = new Properties();
props.load(istream);
PropertyModelUtil.setProperties(mic, props, scope);
PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this);
propertyModelHandlerHelper.setContext(context);
propertyModelHandlerHelper.handlePropertyModel(mic, propertyModel);
}

}
Loading

0 comments on commit 8f3a304

Please sign in to comment.