View Javadoc

1   /*
2    * Copyright 2008-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.maven.issues.cache;
17  
18  import java.io.File;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import javax.xml.stream.XMLInputFactory;
23  import javax.xml.stream.XMLOutputFactory;
24  import javax.xml.stream.XMLStreamException;
25  import javax.xml.stream.XMLStreamReader;
26  import javax.xml.stream.XMLStreamWriter;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.eclipse.mylyn.tasks.core.data.TaskData;
31  
32  /**
33   * Implementation of the data store that writes task data to XML files.
34   */
35  public class XmlTaskDataStore extends AbstractFileTaskDataStore
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * Reference to the logger for this class.
43     */
44    private static final Log LOG = LogFactory.getLog(XmlTaskDataStore.class);
45  
46    // --- members --------------------------------------------------------------
47  
48    /**
49     * The character encoding to read and write files.
50     */
51    private final String encoding;
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default constructor.
59     *
60     * @param encoding the character encoding to read and write files.
61     * @param persistentStoreDirectory the reference to the directory to store the
62     *          individual task files.
63     */
64    public XmlTaskDataStore(final String encoding,
65        final File persistentStoreDirectory)
66    {
67      super(persistentStoreDirectory, ".xml");
68      this.encoding = encoding;
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    // --- business -------------------------------------------------------------
80  
81    /**
82     * Reads the task from the given input stream.
83     *
84     * @param inputStream the input stream to read from.
85     * @return the task information read from the given input stream.
86     * @throws PersistenceException if the task data cannot be read from the given
87     *           input stream.
88     */
89    protected TaskData readTask(final InputStream inputStream)
90      throws PersistenceException
91    {
92      final XMLInputFactory factory = XMLInputFactory.newInstance();
93      XmlTaskReader taskReader = null;
94      ;
95      try
96      {
97        final XMLStreamReader xmlReader =
98            factory.createXMLStreamReader(inputStream, encoding);
99        taskReader = new XmlTaskReader(xmlReader);
100       return taskReader.readTask();
101     }
102     catch (final XMLStreamException e)
103     {
104       throw new PersistenceException("Cannot read task from XML stream.", e);
105     }
106     finally
107     {
108       if (taskReader != null)
109       {
110         taskReader.close();
111       }
112     }
113   }
114 
115   /**
116    * Persists the task to the given output stream. This method takes
117    * responsibility of closing the stream.
118    *
119    * @param outputStream the output stream to write to.
120    * @param task the task data to store.
121    * @throws PersistenceException if the task data cannot be written to the
122    *           given output stream.
123    */
124   protected void writeTask(final OutputStream outputStream, final TaskData task)
125     throws PersistenceException
126   {
127     final XMLOutputFactory factory = XMLOutputFactory.newInstance();
128     XmlTaskWriter taskWriter = null;
129     try
130     {
131       final XMLStreamWriter xmlWriter =
132           factory.createXMLStreamWriter(outputStream, encoding);
133       xmlWriter.writeStartDocument(encoding, "1.0");
134       taskWriter = new XmlTaskWriter(xmlWriter);
135       taskWriter.writeTask(task);
136       xmlWriter.writeEndDocument();
137     }
138     catch (final XMLStreamException e)
139     {
140       final String message =
141           "Cannot persist task data with ID '" + task.getTaskId() + "'.";
142       LOG.warn(message, e);
143       throw new PersistenceException(message, e);
144     }
145     finally
146     {
147       if (taskWriter != null)
148       {
149         taskWriter.close();
150       }
151     }
152   }
153 
154   // --- object basics --------------------------------------------------------
155 
156 }