1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.job.internal.xstream

File SafeLogEventConverter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
40
4
1
117
73
11
0.28
10
4
2.75

Classes

Class Line # Actions
SafeLogEventConverter 41 40 0% 11 2
0.9565217595.7%
 

Contributing tests

This file is covered by 189 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.job.internal.xstream;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.apache.commons.lang3.math.NumberUtils;
26    import org.slf4j.Marker;
27    import org.xwiki.logging.LogLevel;
28    import org.xwiki.logging.event.LogEvent;
29   
30    import com.thoughtworks.xstream.converters.MarshallingContext;
31    import com.thoughtworks.xstream.converters.UnmarshallingContext;
32    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
33    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
34   
35    /**
36    * Filter {@link LogEvent} arguments allowed to be serialized.
37    *
38    * @version $Id: 660394d13f7cf2395bbab449c7d3ab26f6a2f505 $
39    * @since 8.4RC1
40    */
 
41    public class SafeLogEventConverter extends SafeMessageConverter
42    {
43    protected static final String FIELD_LEVEL = "level";
44   
45    protected static final String FIELD_TIMESTAMP = "timeStamp";
46   
47    /**
48    * @param xstream the {@link com.thoughtworks.xstream.XStream} instance to use to isolate array element marshaling
49    */
 
50  463 toggle public SafeLogEventConverter(SafeXStream xstream)
51    {
52  463 super(xstream);
53    }
54   
 
55  1857 toggle @Override
56    public boolean canConvert(Class type)
57    {
58  1857 return type == LogEvent.class;
59    }
60   
 
61  370 toggle @Override
62    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context)
63    {
64  370 LogEvent log = (LogEvent) source;
65   
66    // Level
67  370 XStreamUtils.serializeField(FIELD_LEVEL, LogLevel.class, log.getLevel(), writer, context, mapper());
68   
69    // TimeStamp
70  370 writer.startNode(FIELD_TIMESTAMP);
71  370 writer.setValue(String.valueOf(log.getTimeStamp()));
72  370 writer.endNode();
73   
74  370 super.marshal(source, writer, context);
75    }
76   
 
77  126 toggle @Override
78    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
79    {
80  126 LogLevel level = null;
81  126 Marker marker = null;
82  126 String message = "";
83  126 long timeStamp = -1;
84  126 List<Object> arguments = new ArrayList<>();
85  126 Throwable throwable = null;
86   
87  529 while (reader.hasMoreChildren()) {
88  403 reader.moveDown();
89  403 switch (reader.getNodeName()) {
90  126 case FIELD_LEVEL:
91  126 level = read(LogLevel.class, reader, context);
92  126 break;
93  9 case FIELD_MARKER:
94  9 marker = read(Marker.class, reader, context);
95  9 break;
96  126 case FIELD_MESSAGE:
97  126 message = reader.getValue();
98  126 break;
99  27 case FIELD_TIMESTAMP:
100  27 String timeStampString = reader.getValue();
101  27 timeStamp = NumberUtils.toLong(timeStampString, -1);
102  27 break;
103  1 case FIELD_THROWABLE:
104  1 throwable = read(Throwable.class, reader, context);
105  1 break;
106  114 case FIELD_ARGUMENTARRAY:
107  114 arguments = unmarshalArgumentArray(reader, context);
108  114 break;
109  0 default:
110  0 break;
111    }
112  403 reader.moveUp();
113    }
114   
115  126 return new LogEvent(marker, level, message, arguments.toArray(), throwable, timeStamp);
116    }
117    }