|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.logging.logback;
|
18 | 18 |
|
19 |
| -import java.util.ArrayList; |
20 |
| -import java.util.List; |
21 |
| - |
22 |
| -import ch.qos.logback.core.joran.action.Action; |
23 |
| -import ch.qos.logback.core.joran.event.InPlayListener; |
24 |
| -import ch.qos.logback.core.joran.event.SaxEvent; |
25 |
| -import ch.qos.logback.core.joran.spi.ActionException; |
26 |
| -import ch.qos.logback.core.joran.spi.InterpretationContext; |
27 |
| -import ch.qos.logback.core.joran.spi.Interpreter; |
28 |
| -import ch.qos.logback.core.util.OptionHelper; |
| 19 | +import ch.qos.logback.core.joran.action.BaseModelAction; |
| 20 | +import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext; |
| 21 | +import ch.qos.logback.core.model.Model; |
29 | 22 | import org.xml.sax.Attributes;
|
30 | 23 |
|
31 |
| -import org.springframework.core.env.Environment; |
32 |
| -import org.springframework.core.env.Profiles; |
33 |
| -import org.springframework.util.Assert; |
34 |
| -import org.springframework.util.StringUtils; |
35 |
| - |
36 | 24 | /**
|
37 |
| - * Logback {@link Action} to support {@code <springProfile>} tags. Allows section of a |
38 |
| - * logback configuration to only be enabled when a specific profile is active. |
| 25 | + * Logback {@link BaseModelAction} for {@code <springProperty>} tags. Allows a section of |
| 26 | + * a Logback configuration to only be enabled when a specific profile is active. |
39 | 27 | *
|
40 | 28 | * @author Phillip Webb
|
41 | 29 | * @author Eddú Meléndez
|
| 30 | + * @author Andy Wilkinson |
| 31 | + * @see SpringProfileModel |
| 32 | + * @see SpringProfileModelHandler |
42 | 33 | */
|
43 |
| -class SpringProfileAction extends Action implements InPlayListener { |
44 |
| - |
45 |
| - private final Environment environment; |
46 |
| - |
47 |
| - private int depth = 0; |
48 |
| - |
49 |
| - private boolean acceptsProfile; |
50 |
| - |
51 |
| - private List<SaxEvent> events; |
52 |
| - |
53 |
| - SpringProfileAction(Environment environment) { |
54 |
| - this.environment = environment; |
55 |
| - } |
56 |
| - |
57 |
| - @Override |
58 |
| - public void begin(InterpretationContext ic, String name, Attributes attributes) throws ActionException { |
59 |
| - this.depth++; |
60 |
| - if (this.depth != 1) { |
61 |
| - return; |
62 |
| - } |
63 |
| - ic.pushObject(this); |
64 |
| - this.acceptsProfile = acceptsProfiles(ic, attributes); |
65 |
| - this.events = new ArrayList<>(); |
66 |
| - ic.addInPlayListener(this); |
67 |
| - } |
68 |
| - |
69 |
| - private boolean acceptsProfiles(InterpretationContext ic, Attributes attributes) { |
70 |
| - if (this.environment == null) { |
71 |
| - return false; |
72 |
| - } |
73 |
| - String[] profileNames = StringUtils |
74 |
| - .trimArrayElements(StringUtils.commaDelimitedListToStringArray(attributes.getValue(NAME_ATTRIBUTE))); |
75 |
| - if (profileNames.length == 0) { |
76 |
| - return false; |
77 |
| - } |
78 |
| - for (int i = 0; i < profileNames.length; i++) { |
79 |
| - profileNames[i] = OptionHelper.substVars(profileNames[i], ic, this.context); |
80 |
| - } |
81 |
| - return this.environment.acceptsProfiles(Profiles.of(profileNames)); |
82 |
| - } |
83 |
| - |
84 |
| - @Override |
85 |
| - public void end(InterpretationContext ic, String name) throws ActionException { |
86 |
| - this.depth--; |
87 |
| - if (this.depth != 0) { |
88 |
| - return; |
89 |
| - } |
90 |
| - ic.removeInPlayListener(this); |
91 |
| - verifyAndPop(ic); |
92 |
| - if (this.acceptsProfile) { |
93 |
| - addEventsToPlayer(ic); |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - private void verifyAndPop(InterpretationContext ic) { |
98 |
| - Object o = ic.peekObject(); |
99 |
| - Assert.state(o != null, "Unexpected null object on stack"); |
100 |
| - Assert.isInstanceOf(SpringProfileAction.class, o, "logback stack error"); |
101 |
| - Assert.state(o == this, "ProfileAction different than current one on stack"); |
102 |
| - ic.popObject(); |
103 |
| - } |
104 |
| - |
105 |
| - private void addEventsToPlayer(InterpretationContext ic) { |
106 |
| - Interpreter interpreter = ic.getJoranInterpreter(); |
107 |
| - this.events.remove(0); |
108 |
| - this.events.remove(this.events.size() - 1); |
109 |
| - interpreter.getEventPlayer().addEventsDynamically(this.events, 1); |
110 |
| - } |
| 34 | +class SpringProfileAction extends BaseModelAction { |
111 | 35 |
|
112 | 36 | @Override
|
113 |
| - public void inPlay(SaxEvent event) { |
114 |
| - this.events.add(event); |
| 37 | + protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String name, |
| 38 | + Attributes attributes) { |
| 39 | + SpringProfileModel model = new SpringProfileModel(); |
| 40 | + model.setName(attributes.getValue(NAME_ATTRIBUTE)); |
| 41 | + return model; |
115 | 42 | }
|
116 | 43 |
|
117 | 44 | }
|
0 commit comments