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   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
36   * @version $Revision:591 $
37   */
38  public class XmlTaskDataStore extends AbstractFileTaskDataStore
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * Reference to the logger for this class.
46     */
47    private static final Log LOG = LogFactory.getLog(XmlTaskDataStore.class);
48  
49    // --- members --------------------------------------------------------------
50  
51    // ****************************** Initializer *******************************
52  
53    // ****************************** Constructors ******************************
54  
55    /**
56     * Default constructor.
57     *
58     * @param persistentStoreDirectory the reference to the directory to store the
59     *          individual task files.
60     */
61    public XmlTaskDataStore(final File persistentStoreDirectory)
62    {
63      super(persistentStoreDirectory, ".xml");
64    }
65  
66    // ****************************** Inner Classes *****************************
67  
68    // ********************************* Methods ********************************
69  
70    // --- init -----------------------------------------------------------------
71  
72    // --- get&set --------------------------------------------------------------
73  
74    // --- business -------------------------------------------------------------
75  
76    /**
77     * Reads the task from the given input stream.
78     *
79     * @param inputStream the input stream to read from.
80     * @return the task information read from the given input stream.
81     * @throws PersistenceException if the task data cannot be read from the given
82     *           input stream.
83     */
84    protected TaskData readTask(final InputStream inputStream)
85      throws PersistenceException
86    {
87      final XMLInputFactory factory = XMLInputFactory.newInstance();
88      XMLStreamReader xmlReader = null;
89      try
90      {
91        xmlReader = factory.createXMLStreamReader(inputStream);
92        final XmlTaskReader taskReader = new XmlTaskReader(xmlReader);
93        return taskReader.readTask();
94      }
95      catch (final XMLStreamException e)
96      {
97        throw new PersistenceException("Cannot read task from XML stream.", e);
98      }
99      finally
100     {
101       if (xmlReader != null)
102       {
103         try
104         {
105           xmlReader.close();
106         }
107         catch (final XMLStreamException e)
108         {
109           // Ignore
110           if (LOG.isErrorEnabled())
111           {
112             LOG.error("Cannot close input stream. Continue...", e);
113           }
114         }
115       }
116     }
117   }
118 
119   /**
120    * Persists the task to the given output stream. This method takes
121    * responsibility of closing the stream.
122    *
123    * @param outputStream the output stream to write to.
124    * @param task the task data to store.
125    * @throws PersistenceException if the task data cannot be written to the
126    *           given output stream.
127    */
128   protected void writeTask(final OutputStream outputStream, final TaskData task)
129     throws PersistenceException
130   {
131     final XMLOutputFactory factory = XMLOutputFactory.newInstance();
132     XMLStreamWriter xmlWriter = null;
133     try
134     {
135       xmlWriter = factory.createXMLStreamWriter(outputStream);
136       xmlWriter.writeStartDocument();
137       final XmlTaskWriter taskWriter = new XmlTaskWriter(xmlWriter);
138       taskWriter.writeTask(task);
139       xmlWriter.writeEndDocument();
140     }
141     catch (final XMLStreamException e)
142     {
143       final String message =
144           "Cannot persist task data with ID '" + task.getTaskId() + "'.";
145       LOG.warn(message, e);
146       throw new PersistenceException(message, e);
147     }
148     finally
149     {
150       if (xmlWriter != null)
151       {
152         try
153         {
154           xmlWriter.close();
155         }
156         catch (final XMLStreamException e)
157         {
158           // Ignore
159           if (LOG.isErrorEnabled())
160           {
161             LOG.error("Cannot close output stream. Continue...", e);
162           }
163         }
164       }
165     }
166   }
167 
168   // --- object basics --------------------------------------------------------
169 
170 }