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      private static final long serialVersionUID = 5725337076388822924L;
31  
32      private final List<StructuredDataMessage> structuredDataMessageList;
33  
34      public StructuredDataCollectionMessage(final List<StructuredDataMessage> messages) {
35          this.structuredDataMessageList = messages;
36      }
37  
38      @Override
39      public Iterator<StructuredDataMessage> iterator() {
40          return structuredDataMessageList.iterator();
41      }
42  
43      @Override
44      public String getFormattedMessage() {
45          final StringBuilder sb = new StringBuilder();
46          formatTo(sb);
47          return sb.toString();
48      }
49  
50      @Override
51      public String getFormat() {
52          final StringBuilder sb = new StringBuilder();
53          for (final StructuredDataMessage msg : structuredDataMessageList) {
54              if (msg.getFormat() != null) {
55                  if (sb.length() > 0) {
56                      sb.append(", ");
57                  }
58                  sb.append(msg.getFormat());
59              }
60          }
61          return sb.toString();
62      }
63  
64      @Override
65      public void formatTo(final StringBuilder buffer) {
66          for (final StructuredDataMessage msg : structuredDataMessageList) {
67              msg.formatTo(buffer);
68          }
69      }
70  
71      @Override
72      public Object[] getParameters() {
73          final List<Object[]> objectList = new ArrayList<>();
74          int count = 0;
75          for (final StructuredDataMessage msg : structuredDataMessageList) {
76              final Object[] objects = msg.getParameters();
77              if (objects != null) {
78                  objectList.add(objects);
79                  count += objects.length;
80              }
81          }
82          final Object[] objects = new Object[count];
83          int index = 0;
84          for (final Object[] objs : objectList) {
85             for (final Object obj : objs) {
86                 objects[index++] = obj;
87             }
88          }
89          return objects;
90      }
91  
92      @Override
93      public Throwable getThrowable() {
94          for (final StructuredDataMessage msg : structuredDataMessageList) {
95              final Throwable t = msg.getThrowable();
96              if (t != null) {
97                  return t;
98              }
99          }
100         return null;
101     }
102 }