View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.adapter;
17  
18  import java.io.BufferedInputStream;
19  import java.io.BufferedOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  import java.io.OutputStream;
29  
30  import javax.tools.FileObject;
31  
32  import org.apache.commons.io.IOUtils;
33  
34  import de.smartics.testdoc.core.doc.UnitTestDoc;
35  
36  /**
37   * Provides means to persist the state of collected test documentation
38   * information.
39   *
40   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
41   * @version $Revision:591 $
42   */
43  public final class PersistenceUtils
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    // --- members --------------------------------------------------------------
50  
51    // ****************************** Initializer *******************************
52  
53    // ****************************** Constructors ******************************
54  
55    // ****************************** Inner Classes *****************************
56  
57    // ********************************* Methods ********************************
58  
59    // --- init -----------------------------------------------------------------
60  
61    // --- get&set --------------------------------------------------------------
62  
63    // --- business -------------------------------------------------------------
64  
65    /**
66     * Serializes the index.
67     *
68     * @param instance the instance to serialize.
69     * @param out the stream to write to.
70     * @throws IllegalStateException if the index cannot be serialized.
71     */
72    public void serialize(final Object instance, final OutputStream out)
73      throws IllegalStateException
74    {
75      ObjectOutputStream output = null;
76      try
77      {
78        output = new ObjectOutputStream(new BufferedOutputStream(out));
79        output.writeObject(instance);
80      }
81      catch (final IOException e)
82      {
83        throw new IllegalStateException("Cannot persist instance of type '"
84                                        + instance.getClass().getName() + "'.", e);
85      }
86      finally
87      {
88        IOUtils.closeQuietly(output);
89      }
90    }
91  
92    /**
93     * Serializes the instance to the given file.
94     *
95     * @param instance the instance to serialize.
96     * @param file the file to write to.
97     * @throws IllegalStateException if the index cannot be serialized.
98     */
99    public void serialize(final Object instance, final FileObject file)
100     throws IllegalStateException
101   {
102     try
103     {
104       removeFile(file);
105       serialize(instance, file.openOutputStream());
106     }
107     catch (final IOException e)
108     {
109       throw new IllegalStateException(
110           "Cannot load instance from APT filer source output folder.", e);
111     }
112   }
113 
114   private void removeFile(final FileObject file)
115   {
116     try
117     {
118       file.delete();
119     }
120     catch (final IllegalStateException e)
121     {
122       // eclipse throws this exception, but we will ignore it.
123       // We want the file to be deleted to make sure the
124       // seralization process does not encounter a problem
125       // because of an old file lingering around. If
126       // we cannot delete it, we hope we have no problems
127       // overwriting it later.
128     }
129   }
130 
131   /**
132    * Serializes the test doc instance to the given file.
133    *
134    * @param testDoc the instance to serialize.
135    * @param file the file to write to.
136    * @throws IllegalStateException if the index cannot be serialized.
137    */
138   public void serialize(final UnitTestDoc testDoc, final File file)
139     throws IllegalStateException
140   {
141     try
142     {
143       final OutputStream output = new FileOutputStream(file);
144       serialize(testDoc, output);
145     }
146     catch (final FileNotFoundException e)
147     {
148       throw new IllegalStateException("Cannot write instance of type '"
149                                       + testDoc.getClass().getName()
150                                       + "' to file '" + file.getAbsolutePath()
151                                       + "'.", e);
152     }
153   }
154 
155   /**
156    * Deserializes a single object from the given stream.
157    *
158    * @param in the stream to read from.
159    * @return the object read.
160    */
161   public Object deserialize(final InputStream in)
162   {
163     ObjectInputStream input = null;
164     try
165     {
166       input = new ObjectInputStream(new BufferedInputStream(in));
167       final Object instance = input.readObject();
168       return instance;
169     }
170     catch (final IOException e)
171     {
172       throw new IllegalStateException("Cannot load instance from stream.", e);
173     }
174     catch (final ClassNotFoundException e)
175     {
176       throw new IllegalStateException(
177           "Cannot find class for instance to read serialized form from stream.",
178           e);
179     }
180     finally
181     {
182       IOUtils.closeQuietly(input);
183     }
184   }
185 
186   /**
187    * Deserializes a single object from the given file.
188    *
189    * @param file the file to read from.
190    * @return the object read.
191    */
192   public Object deserialize(final File file)
193   {
194     try
195     {
196       final InputStream input = new FileInputStream(file);
197       return deserialize(input);
198     }
199     catch (final FileNotFoundException e)
200     {
201       throw new IllegalStateException("Cannot load instance from file '"
202                                       + file.getAbsolutePath() + "'.", e);
203     }
204   }
205 
206   /**
207    * Deserializes a single object from the given file.
208    *
209    * @param file the file to read from.
210    * @return the object read.
211    */
212   public Object deserialize(final FileObject file)
213   {
214     try
215     {
216       final InputStream input = file.openInputStream();
217       return deserialize(input);
218     }
219     catch (final IOException e)
220     {
221       throw new IllegalStateException("Cannot load instance from file '"
222                                       + file.getName() + "'.", e);
223     }
224   }
225 
226   // --- object basics --------------------------------------------------------
227 
228 }