1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache license, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the license for the specific language governing permissions and
15  * limitations under the license.
16  */

17 package org.apache.logging.log4j.message;
18
19 import java.io.Serializable;
20
21 /**
22  * Default factory for flow messages.
23  *
24  * @since 2.6
25  */

26 public class DefaultFlowMessageFactory implements FlowMessageFactory, Serializable {
27
28     private static final String EXIT_DEFAULT_PREFIX = "Exit";
29     private static final String ENTRY_DEFAULT_PREFIX = "Enter";
30     private static final long serialVersionUID = 8578655591131397576L;
31
32     private final String entryText;
33     private final String exitText;
34
35     /**
36      * Constructs a message factory with {@code "Enter"} and {@code "Exit"} as the default flow strings.
37      */

38     public DefaultFlowMessageFactory() {
39         this(ENTRY_DEFAULT_PREFIX, EXIT_DEFAULT_PREFIX);
40     }
41
42     /**
43      * Constructs a message factory with the given entry and exit strings.
44      * @param entryText the text to use for trace entry, like {@code "Enter"}.
45      * @param exitText the text to use for trace exit, like {@code "Exit"}.
46      */

47     public DefaultFlowMessageFactory(final String entryText, final String exitText) {
48         this.entryText = entryText;
49         this.exitText = exitText;
50     }
51
52     private static class AbstractFlowMessage implements FlowMessage {
53
54         private static final long serialVersionUID = 1L;
55         private final Message message;
56         private final String text;
57
58         AbstractFlowMessage(final String text, final Message message) {
59             this.message = message;
60             this.text = text;
61         }
62
63         @Override
64         public String getFormattedMessage() {
65             if (message != null) {
66                 return text + " " + message.getFormattedMessage();
67             }
68             return text;
69         }
70
71         @Override
72         public String getFormat() {
73             if (message != null) {
74                 return text + ": " + message.getFormat();
75             }
76             return text;
77         }
78
79         @Override
80         public Object[] getParameters() {
81             if (message != null) {
82                 return message.getParameters();
83             }
84             return null;
85         }
86
87         @Override
88         public Throwable getThrowable() {
89             if (message != null) {
90                 return message.getThrowable();
91             }
92             return null;
93         }
94
95         @Override
96         public Message getMessage() {
97             return message;
98         }
99
100         @Override
101         public String getText() {
102             return text;
103         }
104     }
105
106     private static final class SimpleEntryMessage extends AbstractFlowMessage implements EntryMessage {
107
108         private static final long serialVersionUID = 1L;
109
110         SimpleEntryMessage(final String entryText, final Message message) {
111             super(entryText, message);
112         }
113
114     }
115
116     private static final class SimpleExitMessage extends AbstractFlowMessage implements ExitMessage {
117
118         private static final long serialVersionUID = 1L;
119
120         private final Object result;
121         private final boolean isVoid;
122
123         SimpleExitMessage(final String exitText, final EntryMessage message) {
124             super(exitText, message.getMessage());
125             this.result = null;
126             isVoid = true;
127         }
128
129         SimpleExitMessage(final String exitText, final Object result, final EntryMessage message) {
130             super(exitText, message.getMessage());
131             this.result = result;
132             isVoid = false;
133         }
134
135         SimpleExitMessage(final String exitText, final Object result, final Message message) {
136             super(exitText, message);
137             this.result = result;
138             isVoid = false;
139         }
140
141         @Override
142         public String getFormattedMessage() {
143             final String formattedMessage = super.getFormattedMessage();
144             if (isVoid) {
145                 return formattedMessage;
146             }
147             return formattedMessage + ": " + result;
148         }
149     }
150
151     /**
152      * Gets the entry text.
153      * @return the entry text.
154      */

155     public String getEntryText() {
156         return entryText;
157     }
158
159     /**
160      * Gets the exit text.
161      * @return the exit text.
162      */

163     public String getExitText() {
164         return exitText;
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.apache.logging.log4j.message.MessageFactory#newEntryMessage(org.apache.logging.log4j.message.Message)
171      */

172     @Override
173     public EntryMessage newEntryMessage(final Message message) {
174         return new SimpleEntryMessage(entryText, makeImmutable(message));
175     }
176
177     private Message makeImmutable(final Message message) {
178         if (!(message instanceof ReusableMessage)) {
179             return message;
180         }
181         return new SimpleMessage(message.getFormattedMessage());
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see org.apache.logging.log4j.message.FlowMessageFactory#newExitMessage(org.apache.logging.log4j.message.EntryMessage)
188      */

189     @Override
190     public ExitMessage newExitMessage(final EntryMessage message) {
191         return new SimpleExitMessage(exitText, message);
192     }
193
194     /*
195      * (non-Javadoc)
196      *
197      * @see org.apache.logging.log4j.message.FlowMessageFactory#newExitMessage(java.lang.Object, org.apache.logging.log4j.message.EntryMessage)
198      */

199     @Override
200     public ExitMessage newExitMessage(final Object result, final EntryMessage message) {
201         return new SimpleExitMessage(exitText, result, message);
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see org.apache.logging.log4j.message.FlowMessageFactory#newExitMessage(java.lang.Object, org.apache.logging.log4j.message.Message)
208      */

209     @Override
210     public ExitMessage newExitMessage(final Object result, final Message message) {
211         return new SimpleExitMessage(exitText, result, message);
212     }
213 }
214