View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 pw999
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 net.sf.oqt.core;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.FileWriter;
22  import java.io.IOException;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  
26  import net.sf.oqt.model.ResultVO;
27  
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * The class centralizes the serialization process of the ResultVO.
32   * <p>
33   * In order to transfer the information from the plugin to the reporting plugin, the data is serialized to disk. This
34   * also allows developers to deserialize the object and use the information in other plugins.
35   * 
36   * @author phillip
37   * 
38   */
39  public final class Serializer {
40      /**
41       * Where the file is saved.
42       */
43      private final File resultFile;
44      
45      private final File outputDir;
46  
47      /**
48       * Creates a new Serializer object. This will initialize the file where the ResultVO will be stored.
49       * <p>
50       * The directory where the object will be saved will also be created.
51       * 
52       * @param project a reference to the Maven project.
53       * @throws IOException if failed to create the output directory
54       */
55      public Serializer(final MavenProject project) throws IOException {
56          File buildDir = new File(project.getBuild().getDirectory());
57          if (!buildDir.exists() && !buildDir.mkdir()) {   //if running from command line and target folder doesn't exist
58              throw new IOException("Failed to create build dir at " + buildDir.getAbsolutePath());
59          }
60          resultFile = new File(project.getBuild().getDirectory() + "/querytranslator/output.ser");
61          outputDir = new File(project.getBuild().getDirectory() + "/querytranslator/");
62          if (!outputDir.exists() && !outputDir.mkdir()) {
63              throw new IOException("Failed to create the output directory in " + outputDir.getAbsolutePath());
64          }
65      }
66  
67      /**
68       * Serialized the ResultVO to disk (the location is determined in the constructor).
69       * 
70       * @param result the ResultVO to serialize.
71       * @throws IOException if failed to serialize.
72       */
73      public void serialize(final ResultVO result) throws IOException {
74          if (resultFile.exists()) {
75              if (!resultFile.delete()) {
76                  throw new IOException("Failed to delete old report at " + resultFile.getAbsolutePath());
77              }
78          }
79          if (!resultFile.createNewFile()) {
80              throw new IOException("Failed to create report file at " + resultFile.getAbsolutePath());
81          }
82          FileOutputStream fos = null;
83          ObjectOutputStream out = null;
84          fos = new FileOutputStream(resultFile);
85          out = new ObjectOutputStream(fos);
86          out.writeObject(result);
87          out.close();
88      }
89  
90      /**
91       * Deserialized the ResultVO from disk (the location is determined in the constructor).
92       * 
93       * @return the deserialized ResultVO or null if the file doesn't exist.
94       * @throws IOException if failed to deserialize.
95       * @throws ClassNotFoundException if the ResultVO is not found on the classpath (should never occur).
96       */
97      public ResultVO deserialize() throws IOException, ClassNotFoundException {
98          FileInputStream fis = null;
99          ObjectInputStream in = null;
100         if (!resultFile.exists()) {
101             return null;
102         }
103         fis = new FileInputStream(resultFile);
104         in = new ObjectInputStream(fis);
105         final Object o = in.readObject();
106         in.close();
107         return (ResultVO) o;
108     }
109     
110     public void textOutput(String fileName, String stringOutput) throws IOException {
111         File outputFile = new File(outputDir.getAbsolutePath() + "/" + fileName);
112         FileWriter writer = new FileWriter(outputFile);
113         writer.write(stringOutput);
114         writer.flush();
115         writer.close();
116     }
117 }