View Javadoc

1   /*
2    * #%L
3    * JavaHg
4    * %%
5    * Copyright (C) 2011 aragost Trifork ag
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
25   */
26  package com.aragost.javahg;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.List;
31  
32  import com.aragost.javahg.internals.GenericCommand;
33  
34  public class Bundle {
35  
36      private final BaseRepository baseRepository;
37  
38      private OverlayRepository overlayRepository;
39  
40      private final File file;
41  
42      private boolean manageFile;
43  
44      private List<Changeset> changesets;
45  
46      /**
47       * Create a new bundle. The bundle file will not be deleted on close.
48       * 
49       * @param baseRepository
50       *            The base repository this bundle applies to
51       * @param file
52       *            The bundle file.
53       */
54      public Bundle(BaseRepository baseRepository, File file) {
55          this(baseRepository, file, false);
56      }
57  
58      /**
59       * Create a new bundle.
60       * 
61       * @param baseRepository
62       *            The base repository this bundle applies to
63       * @param file
64       *            The bundle file
65       * @param manageFile
66       *            Whether to delete the bundle file when this bundle is closed.
67       */
68      public Bundle(BaseRepository baseRepository, File file, boolean manageFile) {
69          this.baseRepository = baseRepository;
70          this.overlayRepository = new OverlayRepository(baseRepository, this);
71          this.file = file;
72          this.manageFile = manageFile;
73      }
74  
75      /**
76       * @return the underlying file for the bundle
77       */
78      public File getFile() {
79          return file;
80      }
81  
82      /**
83       * @param manageFile
84       *            Whether close should delete the bundle file
85       */
86      public void setManageFile(boolean manageFile) {
87          this.manageFile = manageFile;
88      }
89  
90      /**
91       * Do the final initialization of the bundle.
92       * <p>
93       * This method is only public so it can be called from
94       * IncomingCommand
95       * 
96       * @param changesets
97       *            the changesets contained in this Bundle
98       */
99      public void init(List<Changeset> changesets) {
100         this.changesets = changesets;
101     }
102 
103     /**
104      * Return the changeset for this bundle. The are sorted from low
105      * revisions to high revisions.
106      * 
107      * @return The changesets
108      */
109     public List<Changeset> getChangesets() {
110         return changesets;
111     }
112 
113     public void close() {
114         Repository repo = this.overlayRepository;
115         repo.close();
116 
117         if (manageFile) {
118             getFile().delete();
119         }
120 
121         this.overlayRepository = null;
122     }
123 
124     public BaseRepository getBaseRepository() {
125         return baseRepository;
126     }
127 
128     public OverlayRepository getOverlayRepository() {
129         return overlayRepository;
130     }
131 
132     /**
133      * Push the changesets in the bundle to its base repository.
134      * 
135      * @throws IOException
136      */
137     public void pushToRepository() throws IOException {
138         final BaseRepository base = getBaseRepository();
139         GenericCommand cmd = new GenericCommand(base, "pull");
140         cmd.execute(getFile().getPath());
141     }
142 }