1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.job.internal

File JobStatusSerializer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

2
19
5
1
131
59
10
0.53
3.8
5
2

Classes

Class Line # Actions
JobStatusSerializer 46 19 0% 10 6
0.769230876.9%
 

Contributing tests

This file is covered by 141 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.FileAlreadyExistsException;
29    import java.nio.file.Files;
30    import java.nio.file.StandardCopyOption;
31   
32    import javax.xml.parsers.ParserConfigurationException;
33   
34    import org.apache.commons.io.FileUtils;
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: aee79a82092fc1a768c6a7aa438d7aad3286f0f1 $
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  219 toggle public JobStatusSerializer() throws ParserConfigurationException
64    {
65  219 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  21431 toggle public void write(JobStatus status, File file) throws IOException
74    {
75  21433 File tempFile = File.createTempFile(file.getName(), ".tmp");
76   
77  21435 try (FileOutputStream stream = FileUtils.openOutputStream(tempFile)) {
78  21422 write(status, stream);
79    }
80   
81    // Copy the file to its final destination
82  21003 file.mkdirs();
83  21427 for (int i = 0; i < 10; ++i) {
84  21396 try {
85  21395 Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
86   
87    // Stop the retry loop if it succeeded
88  21222 break;
89    } catch (FileAlreadyExistsException e) {
90    // Yes it sounds pretty weird but it can happen so we try 10 times before giving up
91    // Wait a bit before retrying
92  0 try {
93  0 Thread.sleep(1000);
94    } catch (InterruptedException ei) {
95  0 throw e;
96    }
97    }
98    }
99    }
100   
101    /**
102    * @param status the status to serialize
103    * @param stream the stream to serialize the status to
104    * @throws IOException when failing to serialize the status
105    */
 
106  21413 toggle public void write(JobStatus status, OutputStream stream) throws IOException
107    {
108  21430 OutputStreamWriter writer = new OutputStreamWriter(stream, DEFAULT_ENCODING);
109  21426 writer.write("<?xml version=\"1.0\" encoding=\"" + DEFAULT_ENCODING + "\"?>\n");
110  21430 this.xstream.toXML(status, writer);
111  21418 writer.flush();
112    }
113   
114    /**
115    * @param file the file to read
116    * @return the status
117    */
 
118  56 toggle public JobStatus read(File file)
119    {
120  56 return (JobStatus) this.xstream.fromXML(file);
121    }
122   
123    /**
124    * @param stream the stream to read
125    * @return the status
126    */
 
127  0 toggle public JobStatus read(InputStream stream)
128    {
129  0 return (JobStatus) this.xstream.fromXML(stream);
130    }
131    }