View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: SerializationPersistenceProvider.java
13   * Created: 2009-08-28
14   * Author: tmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.data.persistence.serialize;
19  
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.util.List;
25  import java.util.UUID;
26  
27  import org.apache.log4j.Logger;
28  
29  import pl.edu.agh.cast.Activator;
30  import pl.edu.agh.cast.data.model.IDataSet;
31  import pl.edu.agh.cast.data.model.IElement;
32  import pl.edu.agh.cast.data.persistence.runtime.RuntimePersistenceProvider;
33  
34  /**
35   * A persistence provider which uses Java serialization for data persistence.
36   *
37   * @author AGH CAST Team
38   */
39  public class SerializationPersistenceProvider extends RuntimePersistenceProvider {
40  
41  	private static final Logger LOG = Activator.getLogger();
42  
43  	/**
44  	 * {@inheritDoc}
45  	 *
46  	 * @see pl.edu.agh.cast.data.persistence.runtime.RuntimePersistenceProvider#destroy()
47  	 */
48  	@Override
49  	public void destroy() {
50  		serialize();
51  		super.destroy();
52  	}
53  
54  	/**
55  	 * {@inheritDoc}
56  	 *
57  	 * @see pl.edu.agh.cast.data.persistence.runtime.RuntimePersistenceProvider#initialize()
58  	 */
59  	@Override
60  	public void initialize() {
61  		super.initialize();
62  		deserialize();
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 *
68  	 * @see pl.edu.agh.cast.data.persistence.runtime.RuntimePersistenceProvider
69  	 *      #saveDataSet(pl.edu.agh.cast.data.model.IDataSet)
70  	 */
71  	@Override
72  	public UUID saveDataSet(IDataSet<? extends IElement> dataSet) {
73  		UUID saveDataSetId = super.saveDataSet(dataSet);
74  		serialize();
75  		return saveDataSetId;
76  	}
77  
78  	private void serialize() {
79  		if (getConfig() == null) {
80  			return;
81  		}
82  		List<IDataSet<? extends IElement>> dataSets = getAllDataSets();
83  
84  		ObjectOutputStream oos = null;
85  
86  		try {
87  			LOG.info("Serializing persistence provider"); //$NON-NLS-1$
88  			oos = new ObjectOutputStream(new FileOutputStream(getConfig()));
89  			oos.writeObject(dataSets);
90  			LOG.info("Serialization of persistence provider completed"); //$NON-NLS-1$
91  		} catch (Exception e) {
92  			LOG.error("Failed to serialize persistence provider", e); //$NON-NLS-1$
93  		} finally {
94  			try {
95  				if (oos != null) {
96  					oos.close();
97  				}
98  			} catch (Exception e) {
99  				LOG.error("Failed to close file after serialization of persistence provider", e); //$NON-NLS-1$
100 			}
101 		}
102 	}
103 
104 	@SuppressWarnings("unchecked")
105 	private void deserialize() {
106 		if (getConfig() == null) {
107 			return;
108 		}
109 		List<IDataSet<? extends IElement>> dataSets = null;
110 
111 		ObjectInputStream ois = null;
112 
113 		try {
114 			LOG.info("Deserializing persistence provider"); //$NON-NLS-1$
115 			ois = new ObjectInputStream(new FileInputStream(getConfig()));
116 			dataSets = (List<IDataSet<? extends IElement>>)ois.readObject();
117 			if (dataSets != null && dataSets.size() > 0) {
118 				clearDataSets();
119 				for (IDataSet<? extends IElement> dataSet : dataSets) {
120 					addDataSet(dataSet);
121 				}
122 			}
123 			LOG.info("Deserialization of persistence provider completed"); //$NON-NLS-1$
124 		} catch (Exception e) {
125 			LOG.error("Failed to deserialize persistence provider", e); //$NON-NLS-1$
126 		} finally {
127 			try {
128 				if (ois != null) {
129 					ois.close();
130 				}
131 			} catch (Exception e) {
132 				LOG.error("Failed to close file after deserialization of persistence provider", e); //$NON-NLS-1$
133 			}
134 		}
135 	}
136 
137 }