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.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.logging.log4j.util.StringBuilderFormattable;
24  
25  /**
26   * A collection of StructuredDataMessages.
27   */
28  public class StructuredDataCollectionMessage implements StringBuilderFormattable,
29          MessageCollectionMessage<StructuredDataMessage> {
30  
31      private List<StructuredDataMessage> structuredDataMessageList;
32  
33      public StructuredDataCollectionMessage(List<StructuredDataMessage> messages) {
34          this.structuredDataMessageList = messages;
35      }
36  
37      @Override
38      public Iterator<StructuredDataMessage> iterator() {
39          return structuredDataMessageList.iterator();
40      }
41  
42      @Override
43      public String getFormattedMessage() {
44          StringBuilder sb = new StringBuilder();
45          formatTo(sb);
46          return sb.toString();
47      }
48  
49      @Override
50      public String getFormat() {
51          StringBuilder sb = new StringBuilder();
52          for (StructuredDataMessage msg : structuredDataMessageList) {
53              if (msg.getFormat() != null) {
54                  if (sb.length() > 0) {
55                      sb.append(", ");
56                  }
57                  sb.append(msg.getFormat());
58              }
59          }
60          return sb.toString();
61      }
62  
63      @Override
64      public void formatTo(StringBuilder buffer) {
65          for (StructuredDataMessage msg : structuredDataMessageList) {
66              msg.formatTo(buffer);
67          }
68      }
69  
70      @Override
71      public Object[] getParameters() {
72          List<Object[]> objectList = new ArrayList<>();
73          int count = 0;
74          for (StructuredDataMessage msg : structuredDataMessageList) {
75              Object[] objects = msg.getParameters();
76              if (objects != null) {
77                  objectList.add(objects);
78                  count += objects.length;
79              }
80          }
81          Object[] objects = new Object[count];
82          int index = 0;
83          for (Object[] objs : objectList) {
84             for (Object obj : objs) {
85                 objects[index++] = obj;
86             }
87          }
88          return objects;
89      }
90  
91      @Override
92      public Throwable getThrowable() {
93          for (StructuredDataMessage msg : structuredDataMessageList) {
94              Throwable t = msg.getThrowable();
95              if (t != null) {
96                  return t;
97              }
98          }
99          return null;
100     }
101 }