Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 094e58aa738af557f6a2ce15735abbcf1c814174 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*****************************************************************************
 * Copyright (c) 2014,2015 ASML Netherlands B.V. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  ASML Netherlands B.V. - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.m2m.internal.qvt.oml.tools.coverage.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.eclipse.m2m.qvt.oml.tools.coverage.Activator;

public class CoverageDataPersistor {

    private static final String DIR_SEP = System.getProperty("file.separator");
    private static final String TMP_DIR = System.getProperty("java.io.tmpdir");
    private static final String DIR_PATH = TMP_DIR + DIR_SEP + "CoverageData" + DIR_SEP;
    
    private CoverageDataPersistor() {
    }

	public static CoverageData load() {
        try {
            CoverageData data = new CoverageData();

            // Since transDatas were saved separately, reattach them now
            File folder = new File(DIR_PATH);
            for (File transDataFile : folder.listFiles()) {
                ObjectInputStream stream = new ObjectInputStream(new FileInputStream(transDataFile));
                TransformationCoverageData transData = (TransformationCoverageData) stream.readObject();
                stream.close();
                data.add(transData);
            }
            return data;
        } catch (Exception e) {
        	Activator.error("Failed to load coverage data", e);
        }

        return null;
    }

	public static void save(CoverageData data) {
        try {
            prepareDirectories();

            // Save any transdatas in a new file
            for (TransformationCoverageData transData : data.getData()) {
            	if (!transData.isModified()) {
            		continue;
            	}
            	
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(DIR_PATH + transData.hashCode()));
                os.writeObject(transData);
                os.close();
                
                transData.setModified(false);
            }
        } catch (Exception e) {
        	Activator.error("Failed to persist coverage data", e);
        }
    }

    public static void cleanupDirectories() {
    	prepareDirectories();
    	
        for (File file : new File(DIR_PATH).listFiles()) {
            file.delete();
        }
    }

    private static void prepareDirectories() {
        // Make sure directory exists, and if not, create it
        File dir = new File(DIR_PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

}

Back to the top