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

File JobStatusSerializer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

0
14
5
1
120
51
5
0.36
2.8
5
1

Classes

Class Line # Actions
JobStatusSerializer 46 14 0% 5 2
0.894736889.5%
 

Contributing tests

This file is covered by 127 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;
21   
22    import java.io.File;
23    import java.io.FileOutputStream;
24    import java.io.IOException;
25    import java.io.InputStream;
26    import java.io.OutputStream;
27    import java.io.OutputStreamWriter;
28    import java.nio.file.Files;
29    import java.nio.file.StandardCopyOption;
30   
31    import javax.xml.parsers.ParserConfigurationException;
32   
33    import org.apache.commons.io.FileUtils;
34    import org.apache.commons.io.IOUtils;
35    import org.xwiki.job.event.status.JobStatus;
36    import org.xwiki.job.internal.xstream.SafeXStream;
37   
38    import com.thoughtworks.xstream.XStream;
39   
40    /**
41    * Serialize/unserialize tool for job statuses.
42    *
43    * @version $Id: b5f42da96365fc8e60ae61fd7536fce91c28bc9b $
44    * @since 5.2M2
45    */
 
46    public class JobStatusSerializer
47    {
48    /**
49    * Encoding used for file content and names.
50    */
51    private static final String DEFAULT_ENCODING = "UTF-8";
52   
53    /**
54    * Used to serialize and unserialize status.
55    */
56    private XStream xstream;
57   
58    /**
59    * Default constructor.
60    *
61    * @throws ParserConfigurationException when failing to initialize
62    */
 
63  194 toggle public JobStatusSerializer() throws ParserConfigurationException
64    {
65  194 this.xstream = new SafeXStream();
66    }
67   
68    /**
69    * @param status the status to serialize
70    * @param file the file to serialize the status to
71    * @throws IOException when failing to serialize the status
72    */
 
73  107 toggle public void write(JobStatus status, File file) throws IOException
74    {
75  107 File tempFile = File.createTempFile(file.getName(), ".tmp");
76   
77  107 FileOutputStream stream = FileUtils.openOutputStream(tempFile);
78   
79  107 try {
80  107 write(status, stream);
81    } finally {
82  107 IOUtils.closeQuietly(stream);
83    }
84   
85    // Copy the file to its final destination
86  107 file.mkdirs();
87  107 Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
88    }
89   
90    /**
91    * @param status the status to serialize
92    * @param stream the stream to serialize the status to
93    * @throws IOException when failing to serialize the status
94    */
 
95  107 toggle public void write(JobStatus status, OutputStream stream) throws IOException
96    {
97  107 OutputStreamWriter writer = new OutputStreamWriter(stream, DEFAULT_ENCODING);
98  107 writer.write("<?xml version=\"1.0\" encoding=\"" + DEFAULT_ENCODING + "\"?>\n");
99  107 this.xstream.toXML(status, writer);
100  107 writer.flush();
101    }
102   
103    /**
104    * @param file the file to read
105    * @return the status
106    */
 
107  57 toggle public JobStatus read(File file)
108    {
109  57 return (JobStatus) this.xstream.fromXML(file);
110    }
111   
112    /**
113    * @param stream the stream to read
114    * @return the status
115    */
 
116  0 toggle public JobStatus read(InputStream stream)
117    {
118  0 return (JobStatus) this.xstream.fromXML(stream);
119    }
120    }