View Javadoc
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          super();
49          this.entryText = entryText;
50          this.exitText = exitText;
51      }
52  
53      private static class AbstractFlowMessage implements FlowMessage {
54  
55          private static final long serialVersionUID = 1L;
56          private final Message message;
57          private final String text;
58  
59          AbstractFlowMessage(final String text, final Message message) {
60              this.message = message;
61              this.text = text;
62          }
63  
64          @Override
65          public String getFormattedMessage() {
66              if (message != null) {
67                  return text + " " + message.getFormattedMessage();
68              }
69              return text;
70          }
71  
72          @Override
73          public String getFormat() {
74              if (message != null) {
75                  return text + ": " + message.getFormat();
76              }
77              return text;
78          }
79  
80          @Override
81          public Object[] getParameters() {
82              if (message != null) {
83                  return message.getParameters();
84              }
85              return null;
86          }
87  
88          @Override
89          public Throwable getThrowable() {
90              if (message != null) {
91                  return message.getThrowable();
92              }
93              return null;
94          }
95  
96          @Override
97          public Message getMessage() {
98              return message;
99          }
100 
101         @Override
102         public String getText() {
103             return text;
104         }
105     }
106 
107     private static final class SimpleEntryMessage extends AbstractFlowMessage implements EntryMessage {
108 
109         private static final long serialVersionUID = 1L;
110 
111         SimpleEntryMessage(final String entryText, final Message message) {
112             super(entryText, message);
113         }
114 
115     }
116 
117     private static final class SimpleExitMessage extends AbstractFlowMessage implements ExitMessage {
118 
119         private static final long serialVersionUID = 1L;
120 
121         private final Object result;
122         private final boolean isVoid;
123 
124         SimpleExitMessage(final String exitText, final EntryMessage message) {
125             super(exitText, message.getMessage());
126             this.result = null;
127             isVoid = true;
128         }
129 
130         SimpleExitMessage(final String exitText, final Object result, final EntryMessage message) {
131             super(exitText, message.getMessage());
132             this.result = result;
133             isVoid = false;
134         }
135 
136         SimpleExitMessage(final String exitText, final Object result, final Message message) {
137             super(exitText, message);
138             this.result = result;
139             isVoid = false;
140         }
141 
142         @Override
143         public String getFormattedMessage() {
144             final String formattedMessage = super.getFormattedMessage();
145             if (isVoid) {
146                 return formattedMessage;
147             }
148             return formattedMessage + ": " + result;
149         }
150     }
151 
152     /**
153      * Gets the entry text.
154      * @return the entry text.
155      */
156     public String getEntryText() {
157         return entryText;
158     }
159 
160     /**
161      * Gets the exit text.
162      * @return the exit text.
163      */
164     public String getExitText() {
165         return exitText;
166     }
167 
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.apache.logging.log4j.message.MessageFactory#newEntryMessage(org.apache.logging.log4j.message.Message)
172      */
173     @Override
174     public EntryMessage newEntryMessage(final Message message) {
175         return new SimpleEntryMessage(entryText, makeImmutable(message));
176     }
177 
178     private Message makeImmutable(final Message message) {
179         if (!(message instanceof ReusableMessage)) {
180             return message;
181         }
182         return new SimpleMessage(message.getFormattedMessage());
183     }
184 
185     /*
186      * (non-Javadoc)
187      *
188      * @see org.apache.logging.log4j.message.FlowMessageFactory#newExitMessage(org.apache.logging.log4j.message.EntryMessage)
189      */
190     @Override
191     public ExitMessage newExitMessage(final EntryMessage message) {
192         return new SimpleExitMessage(exitText, message);
193     }
194 
195     /*
196      * (non-Javadoc)
197      *
198      * @see org.apache.logging.log4j.message.FlowMessageFactory#newExitMessage(java.lang.Object, org.apache.logging.log4j.message.EntryMessage)
199      */
200     @Override
201     public ExitMessage newExitMessage(final Object result, final EntryMessage message) {
202         return new SimpleExitMessage(exitText, result, message);
203     }
204 
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.apache.logging.log4j.message.FlowMessageFactory#newExitMessage(java.lang.Object, org.apache.logging.log4j.message.Message)
209      */
210     @Override
211     public ExitMessage newExitMessage(final Object result, final Message message) {
212         return new SimpleExitMessage(exitText, result, message);
213     }
214 }