KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > LocalHistoryVCSInterceptor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.localhistory;
20
21 import java.util.HashMap JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.modules.localhistory.store.LocalHistoryStore;
28 import org.netbeans.modules.versioning.spi.VCSInterceptor;
29
30 /**
31  *
32  * Listens to file system operations from the IDE and eventually handles them synchronously
33  *
34  * @author Tomas Stupka
35  */

36 class LocalHistoryVCSInterceptor extends VCSInterceptor {
37         
38     private class StorageMoveHandler {
39         private long ts = -1;
40         
41         private final File JavaDoc from;
42         private final File JavaDoc to;
43         
44         StorageMoveHandler(File JavaDoc from, File JavaDoc to) {
45             this.from = from;
46             this.to = to;
47         }
48         
49         public void delete() {
50             getStore().fileDeleteFromMove(from, to, ts);
51         }
52         
53         public void create() {
54             ts = to.lastModified();
55             getStore().fileCreateFromMove(from, to, ts);
56         }
57     };
58     
59     private LocalHistoryStore getStore() {
60         return LocalHistory.getInstance().getLocalHistoryStore();
61     }
62     
63     private Map JavaDoc<String JavaDoc, StorageMoveHandler> moveHandlerMap;
64
65     // XXX reconsider this. is there realy no other way? is it robust enough?
66
private Set JavaDoc<File JavaDoc> toBeDeleted = new HashSet JavaDoc<File JavaDoc>();
67     private Set JavaDoc<File JavaDoc> toBeCreated = new HashSet JavaDoc<File JavaDoc>();
68     private Set JavaDoc<File JavaDoc> wasJustCreated = new HashSet JavaDoc<File JavaDoc>();
69         
70     /** Creates a new instance of LocalHistoryVCSInterceptor */
71     public LocalHistoryVCSInterceptor() {
72         
73     }
74     
75     // ==================================================================================================
76
// DELETE
77
// ==================================================================================================
78
public boolean beforeDelete(File JavaDoc file) {
79         if(!accept(file)) {
80             return false;
81         }
82         toBeDeleted.add(file); // XXX do this with a hanlder, get the correct ts
83
storeFile(file); // will be stored in the history if there is no actuall entry yet
84
return false;
85     }
86     
87     public void doDelete(File JavaDoc file) throws IOException JavaDoc {
88         // do nothing
89
}
90
91     public void afterDelete(File JavaDoc file) {
92         if(!toBeDeleted.remove(file)) {
93             // do nothing if the file wasn't marked
94
// as to be deleted
95
return;
96         }
97         
98         String JavaDoc key = file.getAbsolutePath();
99         if(getMoveHandlerMap().containsKey(key)) {
100             StorageMoveHandler handler = getMoveHandlerMap().get(key);
101             try {
102                 handler.delete();
103             } finally {
104                 getMoveHandlerMap().remove(key);
105             }
106         } else {
107             getStore().fileDelete(file, System.currentTimeMillis());
108         }
109     }
110     
111     // ==================================================================================================
112
// MOVE
113
// ==================================================================================================
114

115     public boolean beforeMove(final File JavaDoc from, final File JavaDoc to) {
116         if(!accept(from)) {
117             return false;
118         }
119                 
120         // moving a package comes either like
121
// - create(to) and delete(from)
122
// - or the files from the package come like move(from, to)
123
StorageMoveHandler handler = new StorageMoveHandler(from, to);
124         getMoveHandlerMap().put(to.getAbsolutePath(), handler);
125         getMoveHandlerMap().put(from.getAbsolutePath(), handler);
126         return false;
127     }
128
129     public void doMove(File JavaDoc from, File JavaDoc to) throws IOException JavaDoc {
130         // do nothing
131
}
132
133     public void afterMove(File JavaDoc from, File JavaDoc to) {
134         String JavaDoc key = to.getAbsolutePath();
135         if(getMoveHandlerMap().containsKey(key)) {
136             StorageMoveHandler handler = getMoveHandlerMap().get(key);
137             try {
138                 handler.create();
139                 handler.delete();
140             } finally {
141                 getMoveHandlerMap().remove(key);
142                 getMoveHandlerMap().remove(from.getAbsolutePath());
143             }
144         }
145     }
146     
147     // ==================================================================================================
148
// CREATE
149
// ==================================================================================================
150

151     public boolean beforeCreate(File JavaDoc file, boolean isDirectory) {
152         toBeCreated.add(file);
153         return false;
154     }
155
156     public void doCreate(File JavaDoc file, boolean isDirectory) throws IOException JavaDoc {
157         // do nothing
158
}
159
160     public void afterCreate(File JavaDoc file) {
161         toBeCreated.remove(file);
162         if(file.isFile()) {
163             // no change events for folders seen yet
164
wasJustCreated.add(file);
165         }
166         
167         String JavaDoc key = file.getAbsolutePath();
168         if(getMoveHandlerMap().containsKey(key)) {
169             StorageMoveHandler handler = getMoveHandlerMap().get(key);
170             try {
171                 handler.create();
172             } finally {
173                 getMoveHandlerMap().remove(key);
174             }
175         }
176     }
177     
178     // ==================================================================================================
179
// CHANGE
180
// ==================================================================================================
181

182     public void beforeChange(File JavaDoc file) {
183         if(toBeCreated.contains(file) ||
184            wasJustCreated.remove(file))
185         {
186             // ignore change events
187
// if they happen in scope of a create
188
// or just after a create
189
return;
190         }
191         if(!accept(file)) {
192             return;
193         }
194         storeFile(file);
195     }
196     
197     public void afterChange(File JavaDoc file) {
198         // just in case
199
wasJustCreated.remove(file);
200     }
201     
202     private void storeFile(File JavaDoc file) {
203         getStore().fileChange(file, file.lastModified());
204     }
205         
206     private Map JavaDoc<String JavaDoc, StorageMoveHandler> getMoveHandlerMap() {
207         if(moveHandlerMap == null) {
208             moveHandlerMap = new HashMap JavaDoc<String JavaDoc, StorageMoveHandler>();
209         }
210         return moveHandlerMap;
211     }
212     
213     /**
214      *
215      * Decides if a file has to be stored in the Local History or not.
216      *
217      * @param file the file to be stored
218      * @return true if the file has to be stored in the Local History, otherwise false
219      */

220     private boolean accept(File JavaDoc file) {
221         if(!LocalHistory.getInstance().isManaged(file)) {
222             return false;
223         }
224         return true;
225     }
226     
227 }
228
Popular Tags