View Javadoc

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