|
| 1 | +/* |
| 2 | + * Logback: the reliable, generic, fast and flexible logging framework. |
| 3 | + * Copyright (C) 1999-2024, QOS.ch. All rights reserved. |
| 4 | + * |
| 5 | + * This program and the accompanying materials are dual-licensed under |
| 6 | + * either the terms of the Eclipse Public License v1.0 as published by |
| 7 | + * the Eclipse Foundation |
| 8 | + * |
| 9 | + * or (per the licensee's choosing) |
| 10 | + * |
| 11 | + * under the terms of the GNU Lesser General Public License version 2.1 |
| 12 | + * as published by the Free Software Foundation. |
| 13 | + */ |
| 14 | + |
| 15 | +package ch.qos.logback.classic.tyler; |
| 16 | + |
| 17 | +import ch.qos.logback.core.Context; |
| 18 | +import ch.qos.logback.core.joran.action.ActionUtil; |
| 19 | +import ch.qos.logback.core.model.ModelConstants; |
| 20 | +import ch.qos.logback.core.model.PropertyModel; |
| 21 | +import ch.qos.logback.core.model.util.PropertyModelUtil; |
| 22 | +import ch.qos.logback.core.model.util.VariableSubstitutionsHelper; |
| 23 | +import ch.qos.logback.core.spi.ContextAwareBase; |
| 24 | +import ch.qos.logback.core.util.ContextUtil; |
| 25 | +import ch.qos.logback.core.util.Loader; |
| 26 | +import ch.qos.logback.core.util.OptionHelper; |
| 27 | + |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.FileNotFoundException; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.net.URL; |
| 33 | +import java.util.Properties; |
| 34 | + |
| 35 | +public class VariableModelHelper extends ContextAwareBase { |
| 36 | + |
| 37 | + TylerConfiguratorBase tylerConfiguratorBase; |
| 38 | + VariableSubstitutionsHelper variableSubstitutionsHelper; |
| 39 | + |
| 40 | + VariableModelHelper(Context context, TylerConfiguratorBase tylerConfiguratorBase) { |
| 41 | + super( tylerConfiguratorBase); |
| 42 | + this.context = context; |
| 43 | + this.tylerConfiguratorBase = tylerConfiguratorBase; |
| 44 | + this.variableSubstitutionsHelper = new VariableSubstitutionsHelper(context); |
| 45 | + } |
| 46 | + |
| 47 | + void updateProperties(PropertyModel propertyModel) { |
| 48 | + |
| 49 | + ActionUtil.Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr()); |
| 50 | + if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) { |
| 51 | + String file = propertyModel.getFile(); |
| 52 | + file = tylerConfiguratorBase.subst(file); |
| 53 | + try (FileInputStream istream = new FileInputStream(file)) { |
| 54 | + loadAndSetProperties(istream, scope); |
| 55 | + } catch (FileNotFoundException e) { |
| 56 | + addError("Could not find properties file [" + file + "]."); |
| 57 | + } catch (IOException |IllegalArgumentException e1) { // IllegalArgumentException is thrown in case the file |
| 58 | + // is badly malformed, i.e a binary. |
| 59 | + addError("Could not read properties file [" + file + "].", e1); |
| 60 | + } |
| 61 | + } else if (PropertyModelUtil.checkResourceAttributeSanity(propertyModel)) { |
| 62 | + String resource = propertyModel.getResource(); |
| 63 | + resource = tylerConfiguratorBase.subst(resource); |
| 64 | + URL resourceURL = Loader.getResourceBySelfClassLoader(resource); |
| 65 | + if (resourceURL == null) { |
| 66 | + addError("Could not find resource [" + resource + "]."); |
| 67 | + } else { |
| 68 | + try ( InputStream istream = resourceURL.openStream();) { |
| 69 | + loadAndSetProperties(istream, scope); |
| 70 | + } catch (IOException e) { |
| 71 | + addError("Could not read resource file [" + resource + "].", e); |
| 72 | + } |
| 73 | + } |
| 74 | + } else if (PropertyModelUtil.checkValueNameAttributesSanity(propertyModel)) { |
| 75 | + // earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no |
| 76 | + // need to do this. See RegularEscapeUtil.__UNUSED__basicEscape |
| 77 | + String value = propertyModel.getValue(); |
| 78 | + |
| 79 | + // now remove both leading and trailing spaces |
| 80 | + value = value.trim(); |
| 81 | + value = tylerConfiguratorBase.subst(value); |
| 82 | + setProperty(propertyModel.getName(), value, scope); |
| 83 | + |
| 84 | + } else { |
| 85 | + addError(ModelConstants.INVALID_ATTRIBUTES); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + void loadAndSetProperties(InputStream istream, ActionUtil.Scope scope) throws IOException { |
| 90 | + Properties props = new Properties(); |
| 91 | + props.load(istream); |
| 92 | + setProperties(props, scope); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public void setProperties(Properties props, ActionUtil.Scope scope) { |
| 97 | + switch (scope) { |
| 98 | + case LOCAL: |
| 99 | + variableSubstitutionsHelper.addSubstitutionProperties(props); |
| 100 | + break; |
| 101 | + case CONTEXT: |
| 102 | + ContextUtil cu = new ContextUtil(getContext()); |
| 103 | + cu.addProperties(props); |
| 104 | + break; |
| 105 | + case SYSTEM: |
| 106 | + OptionHelper.setSystemProperties(this, props); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public void setProperty(String key, String value, ActionUtil.Scope scope) { |
| 111 | + switch (scope) { |
| 112 | + case LOCAL: |
| 113 | + variableSubstitutionsHelper.addSubstitutionProperty(key, value); |
| 114 | + break; |
| 115 | + case CONTEXT: |
| 116 | + getContext().putProperty(key, value); |
| 117 | + break; |
| 118 | + case SYSTEM: |
| 119 | + OptionHelper.setSystemProperty(this, key, value); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments