~verterok/bzr-eclipse/fix-license

« back to all changes in this revision

Viewing changes to org.vcs.bazaar.eclipse.core/src/org/vcs/bazaar/eclipse/core/repository/BranchLocationManager.java

  • Committer: Guillermo Gonzalez
  • Date: 2013-10-23 12:11:03 UTC
  • mfrom: (357.1.14 trunk)
  • Revision ID: guillo.gonzo@gmail.com-20131023121103-2mpxnmkop5em0rzg
merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************************
2
 
 * Copyright (c) 2006 Software Balm Consulting Inc.
3
 
 *
4
 
 * TODO: LICENSE DETAILS
5
 
 *******************************************************************************/
6
 
package org.vcs.bazaar.eclipse.core.repository;
7
 
 
8
 
import java.io.File;
9
 
import java.io.FileInputStream;
10
 
import java.io.FileOutputStream;
11
 
import java.io.ObjectInputStream;
12
 
import java.io.ObjectOutputStream;
13
 
import java.util.Iterator;
14
 
import java.util.Set;
15
 
import java.util.SortedSet;
16
 
import java.util.TreeSet;
17
 
 
18
 
import org.vcs.bazaar.eclipse.EclipseBazaarCore;
19
 
import org.vcs.bazaar.eclipse.core.model.IBzrBranch;
20
 
 
21
 
/*
22
 
 * A manager for all Bazaar repository locations.
23
 
 *
24
 
 * TODO: Need a way to delete these branches. Repo Explorer perspective a la
25
 
 * subclipse? TODO: Should probably use a text based format for the locations.
26
 
 * This is just a crude object dump right now.
27
 
 */
28
 
public class BranchLocationManager {
29
 
        private static final SortedSet<IBzrBranch> branches = new TreeSet<IBzrBranch>();// final ensures that its reference will remain the same.
30
 
        private static final String BRANCH_LOCATION_FILE = "repo_locations.txt";
31
 
 
32
 
 
33
 
        //This will only get instantiated once don't need to synchronize the constructor.
34
 
        private static final BranchLocationManager instance = new BranchLocationManager();
35
 
 
36
 
        private BranchLocationManager(){}
37
 
        /**
38
 
         * @return The manager instance
39
 
         */
40
 
        public static BranchLocationManager getInstance() {
41
 
                return instance;
42
 
        }
43
 
 
44
 
        /*
45
 
         * Return a <code>File</code> object representing the location file. The
46
 
         * file may or may not exist and must be checked before use.
47
 
         */
48
 
        private File getLocationFile() {
49
 
                return EclipseBazaarCore.getDefault().getStateLocation().append(BRANCH_LOCATION_FILE).toFile();
50
 
        }
51
 
 
52
 
        /*
53
 
         * Load all saved repository locations from the plug-in's default area.
54
 
         */
55
 
        public void start() throws Exception {
56
 
 
57
 
                File locationFile = getLocationFile();
58
 
 
59
 
                // If the file doesn't exist, then there's nothing to do.
60
 
                if (!locationFile.exists())
61
 
                        return;
62
 
 
63
 
                FileInputStream istream = new FileInputStream(locationFile);
64
 
                ObjectInputStream p = new ObjectInputStream(istream);
65
 
 
66
 
                try {
67
 
                        while (true) {
68
 
                                // String uri = (String) p.readObject();
69
 
                                IBzrBranch branch = (IBzrBranch) p.readObject();
70
 
                                addRepoLocation(branch);
71
 
                        }
72
 
                } catch (Exception e) {
73
 
                        // Expected exception when there are no more records. Is this the only reason for an ioexception ??
74
 
                } finally {
75
 
                        p.close();
76
 
                        istream.close();
77
 
                }
78
 
        }
79
 
 
80
 
        /*
81
 
         * Flush all repository locations out to a file in the plug-in's default
82
 
         * area.
83
 
         */
84
 
        public void stop() throws Exception {
85
 
                // Determine if there's anything that we need to save out to file.
86
 
                if (getAllBranches().size() == 0)
87
 
                        return;
88
 
 
89
 
                File locationFile = getLocationFile();
90
 
 
91
 
                // If the file doesn't exist, then create it.
92
 
                if (!locationFile.exists()) {
93
 
                        locationFile.createNewFile();
94
 
                }
95
 
 
96
 
                FileOutputStream ostream = new FileOutputStream(locationFile);
97
 
                ObjectOutputStream p = new ObjectOutputStream(ostream);
98
 
 
99
 
                // Add any previously existing URI's to the combo box for ease of use.
100
 
                Iterator<IBzrBranch> iter = getAllBranches().iterator();
101
 
                while (iter.hasNext()) {
102
 
                        IBzrBranch branch = (iter.next());
103
 
                        p.writeObject(branch);
104
 
 
105
 
                }
106
 
 
107
 
                p.flush();
108
 
                ostream.close();
109
 
        }
110
 
 
111
 
        /*
112
 
         * Return an ordered list of all repository locations that are presently
113
 
         * known.
114
 
         */
115
 
        public Set<IBzrBranch> getAllBranches() {
116
 
                return new TreeSet<IBzrBranch>(branches);
117
 
        }
118
 
 
119
 
        /*
120
 
         * Add a repository location to the database.
121
 
         */
122
 
        public boolean addRepoLocation(IBzrBranch branch) {
123
 
                if (branch == null || branch.getBranchLocation() == null) return false;
124
 
                return branches.add(branch);
125
 
        }
126
 
 
127
 
        /*
128
 
         * Add a repository location to the database.
129
 
         */
130
 
        public boolean removeRepoLocation(IBzrBranch branch) {
131
 
                if (branch == null) return false;
132
 
                return branches.remove(branch);
133
 
        }
134
 
}