parent
05d2f3cc8e
commit
0bfa9cd704
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.logback;
|
||||
|
||||
import ch.qos.logback.core.model.NamedModel;
|
||||
|
||||
/**
|
||||
* Logback {@link NamedModel model} to support {@code <springProfile>} tags.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @see SpringProfileAction
|
||||
* @see SpringProfileModelHandler
|
||||
*/
|
||||
class SpringProfileModel extends NamedModel {
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.logback;
|
||||
|
||||
import ch.qos.logback.core.Context;
|
||||
import ch.qos.logback.core.model.Model;
|
||||
import ch.qos.logback.core.model.processor.ModelHandlerBase;
|
||||
import ch.qos.logback.core.model.processor.ModelHandlerException;
|
||||
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
|
||||
import ch.qos.logback.core.spi.ScanException;
|
||||
import ch.qos.logback.core.util.OptionHelper;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Logback {@link ModelHandlerBase model handler} to support {@code <springProfile>} tags.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Eddú Meléndez
|
||||
* @author Andy Wilkinson
|
||||
* @see SpringProfileModel
|
||||
* @see SpringProfileAction
|
||||
*/
|
||||
class SpringProfileModelHandler extends ModelHandlerBase {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
SpringProfileModelHandler(Context context, Environment environment) {
|
||||
super(context);
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
|
||||
SpringProfileModel profileModel = (SpringProfileModel) model;
|
||||
if (!acceptsProfiles(intercon, profileModel)) {
|
||||
model.markAsSkipped();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean acceptsProfiles(ModelInterpretationContext ic, SpringProfileModel model) {
|
||||
if (this.environment == null) {
|
||||
return false;
|
||||
}
|
||||
String[] profileNames = StringUtils
|
||||
.trimArrayElements(StringUtils.commaDelimitedListToStringArray(model.getName()));
|
||||
if (profileNames.length == 0) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < profileNames.length; i++) {
|
||||
try {
|
||||
profileNames[i] = OptionHelper.substVars(profileNames[i], ic, this.context);
|
||||
}
|
||||
catch (ScanException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
return this.environment.acceptsProfiles(Profiles.of(profileNames));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.logback;
|
||||
|
||||
import ch.qos.logback.core.model.NamedModel;
|
||||
|
||||
/**
|
||||
* Logback {@link NamedModel model} to support {@code <springProperty>} tags. Allows
|
||||
* Logback properties to be sourced from the Spring environment.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @see SpringPropertyAction
|
||||
* @see SpringPropertyModelHandler
|
||||
*/
|
||||
class SpringPropertyModel extends NamedModel {
|
||||
|
||||
private String scope;
|
||||
|
||||
private String defaultValue;
|
||||
|
||||
private String source;
|
||||
|
||||
String getScope() {
|
||||
return this.scope;
|
||||
}
|
||||
|
||||
void setScope(String scope) {
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
String getDefaultValue() {
|
||||
return this.defaultValue;
|
||||
}
|
||||
|
||||
void setDefaultValue(String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
String getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.logback;
|
||||
|
||||
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.ModelUtil;
|
||||
import ch.qos.logback.core.model.processor.ModelHandlerBase;
|
||||
import ch.qos.logback.core.model.processor.ModelHandlerException;
|
||||
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
|
||||
import ch.qos.logback.core.util.OptionHelper;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* Logback {@link ModelHandlerBase model handler} to support {@code <springProperty>}
|
||||
* tags. Allows Logback properties to be sourced from the Spring environment.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Eddú Meléndez
|
||||
* @author Madhura Bhave
|
||||
* @author Andy Wilkinson
|
||||
* @see SpringPropertyAction
|
||||
* @see SpringPropertyModel
|
||||
*/
|
||||
class SpringPropertyModelHandler extends ModelHandlerBase {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
SpringPropertyModelHandler(Context context, Environment environment) {
|
||||
super(context);
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
|
||||
SpringPropertyModel propertyModel = (SpringPropertyModel) model;
|
||||
Scope scope = ActionUtil.stringToScope(propertyModel.getScope());
|
||||
String defaultValue = propertyModel.getDefaultValue();
|
||||
String source = propertyModel.getSource();
|
||||
if (OptionHelper.isNullOrEmpty(propertyModel.getName()) || OptionHelper.isNullOrEmpty(source)) {
|
||||
addError("The \"name\" and \"source\" attributes of <springProperty> must be set");
|
||||
}
|
||||
ModelUtil.setProperty(intercon, propertyModel.getName(), getValue(source, defaultValue), scope);
|
||||
}
|
||||
|
||||
private String getValue(String source, String defaultValue) {
|
||||
if (this.environment == null) {
|
||||
addWarn("No Spring Environment available to resolve " + source);
|
||||
return defaultValue;
|
||||
}
|
||||
return this.environment.getProperty(source, defaultValue);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue