Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2b36d591f11b1565a4e06982941bb53ec0ac114 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.repo;

import java.io.*;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.core.caches.ICacheSource;
import org.eclipse.team.internal.core.caches.LocalReplica;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.XMLMemento;

/**
 * A local replica that uses IMemento to persist and restore the local replica.
 */
public abstract class XMLLocalReplica extends LocalReplica {

    /**
     * Create an IMemento based local replica.
     * @param remote the remote cache source being replicated
     */
    protected XMLLocalReplica(ICacheSource remote) {
        super(remote);
    }

    /* (non-Javadoc)
     * @see org.eclipse.team.internal.core.caches.LocalReplica#save(java.lang.Object, org.eclipse.core.runtime.IProgressMonitor)
     */
    protected void save(Object o, IProgressMonitor monitor) throws CoreException {
        XMLMemento memento = XMLMemento.createWriteRoot(getType());
        save(o, memento, monitor);
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getFile())));
            memento.save(writer);
        } catch (FileNotFoundException e) {
            throw new TeamException("Could not create file " + getFile().getAbsolutePath(), e);
        } catch (IOException e) {
            throw new TeamException("Could not write to file " + getFile().getAbsolutePath(), e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e1) {
                    // Ignore
                }
            }
        }

    }

    private String getType() {
        return "replica"; //$NON-NLS-1$
    }

    /* (non-Javadoc)
     * @see org.eclipse.team.internal.core.caches.LocalReplica#load(org.eclipse.core.runtime.IProgressMonitor)
     */
    protected Object load(int flags, IProgressMonitor monitor) throws CoreException {
        // reload from the disk cache
        Reader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(getFile())));
            XMLMemento memento = XMLMemento.createReadRoot(reader);
            return load(flags, memento, monitor);
        } catch (FileNotFoundException e) {
            // No file on disk so fetch from the remote source
            return fetchRemote(flags, monitor);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    // Ignore
                }
            }
        }
    }

    /**
     * Return the file where the local replica is to be persisted.
     * @return the file where the local replica is to be persisted
     */
    protected abstract File getFile();
    
    /**
     * Save the object to the given memento which will be used to
     * persist the locla replica to disk.
     * @param o the object being cached
     * @param memento the memento used to cache the object
     * @param monitor a progress monitor
     */
    protected abstract void save(Object o, IMemento memento, IProgressMonitor monitor) throws CoreException;
    
    /**
     * Load the object from the given memento.
     * @param flags the flags passed to the cache reference
     * @param memento the memento containing the replica
     * @param monitor a progress monitor
     * @return the object build from the replica
     */
    protected abstract Object load(int flags, IMemento memento, IProgressMonitor monitor) throws CoreException;

}

Back to the top