Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.vcs.ade.treestamp; |
43 |
|
44 |
import java.io.BufferedReader; |
45 |
import java.io.File; |
46 |
import java.io.IOException; |
47 |
import java.io.InputStreamReader; |
48 |
import java.util.Collections; |
49 |
import java.util.HashMap; |
50 |
import java.util.HashSet; |
51 |
import java.util.Map; |
52 |
import java.util.Set; |
53 |
import java.util.concurrent.atomic.AtomicReference; |
54 |
import java.util.logging.Level; |
55 |
import java.util.logging.Logger; |
56 |
import java.util.regex.Matcher; |
57 |
import java.util.regex.Pattern; |
58 |
import org.openide.filesystems.FileObject; |
59 |
import org.openide.filesystems.FileUtil; |
60 |
import org.openide.filesystems.TreeStamp; |
61 |
import org.openide.util.Exceptions; |
62 |
|
63 |
/** |
64 |
* |
65 |
* @author Tomas Zezula |
66 |
*/ |
67 |
public final class ADETreeStampProvider extends TreeStamp.Provider { |
68 |
|
69 |
private static final String LABEL = "BASE_LABEL:"; //NOI18N |
70 |
private static final String PWV_LABEL = "VIEW_LABEL"; //NOI18N |
71 |
private static final String CHANGED_FILES = "ALL AFFECTED FILES:"; //NOI18N |
72 |
private static final String NEW_FILES = "NEW FILES:"; //NOI18N |
73 |
private static final String REMOVED_FILES = "RMNAMED (REMOVED) FILES:"; //NOI18N |
74 |
private static final Pattern CHANGE = Pattern.compile("\\s*\\d+\\.\\s+(.*)"); //NOI18N |
75 |
|
76 |
private static final Logger LOG = Logger.getLogger(ADETreeStampProvider.class.getName()); |
77 |
|
78 |
private final AtomicReference<File> adeRoot = new AtomicReference<File>(); |
79 |
|
80 |
private ADETreeStampProvider() {} |
81 |
|
82 |
@Override |
83 |
protected Map<FileObject, TreeStamp> stamps(Map<FileObject, String> rootsAndBaseIds) { |
84 |
final Map<FileObject, TreeStamp> result = new HashMap<FileObject, TreeStamp>(); |
85 |
Transaction tx = null; |
86 |
boolean described = false; |
87 |
for (Map.Entry<FileObject,String> entry : rootsAndBaseIds.entrySet()) { |
88 |
final FileObject root = entry.getKey(); |
89 |
final String base = entry.getValue(); |
90 |
final String adeRootPath = getADERootPath(); |
91 |
final String rootDirPath = getPath(FileUtil.toFile(root)); |
92 |
if (adeRootPath != null && |
93 |
rootDirPath != null && |
94 |
rootDirPath.startsWith(adeRootPath)) { //NOI18N |
95 |
if (!described) { |
96 |
described = true; |
97 |
tx = describeTrans(getADERoot()); |
98 |
} |
99 |
if (tx != null && (base == null || base.equals(tx.getLabel()))) { |
100 |
final Set<String> paths = new HashSet<String>(); |
101 |
for (String cf : tx.getChanges()) { |
102 |
if (cf.startsWith(rootDirPath)) { |
103 |
paths.add(cf.substring(rootDirPath.length())); |
104 |
} |
105 |
} |
106 |
result.put(root, create(tx.getLabel(), null, paths)); |
107 |
LOG.log( |
108 |
Level.FINE, |
109 |
"Root: {0}, Label: {1}, Changes: {2}", //NOI18N |
110 |
new Object[] { |
111 |
rootDirPath, |
112 |
tx.getLabel(), |
113 |
paths |
114 |
}); |
115 |
} |
116 |
} |
117 |
} |
118 |
return result; |
119 |
} |
120 |
|
121 |
|
122 |
File getADERoot() { |
123 |
File adeRootDir = adeRoot.get(); |
124 |
if (adeRootDir == null) { |
125 |
final String adeRootStr = System.getenv("ADE_VIEW_ROOT"); //NOI18N |
126 |
if (adeRootStr != null) { |
127 |
adeRootDir = FileUtil.normalizeFile(new File(adeRootStr)); |
128 |
adeRoot.compareAndSet(null, adeRootDir); |
129 |
} |
130 |
} |
131 |
return adeRootDir; |
132 |
} |
133 |
|
134 |
String getADERootPath() { |
135 |
return getPath(getADERoot()); |
136 |
} |
137 |
|
138 |
private static String getPath(final File file) { |
139 |
if (file == null) { |
140 |
return null; |
141 |
} |
142 |
String path = file.getAbsolutePath(); |
143 |
if (!path.endsWith(File.separator)) { |
144 |
path = path + File.separatorChar; |
145 |
} |
146 |
return path; |
147 |
} |
148 |
|
149 |
|
150 |
private Transaction describeTrans(final File rootDir) { |
151 |
try { |
152 |
final Process process = new ProcessBuilder("ade","describetrans","-short").start(); //NOI18N |
153 |
final BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream())); |
154 |
String label = null; |
155 |
Set<String> changedFiles = new HashSet<String>(); |
156 |
try { |
157 |
int mode = 0; |
158 |
String line; |
159 |
while ((line=in.readLine())!=null) { |
160 |
if (mode == 0) { |
161 |
if (line.startsWith(LABEL)) { |
162 |
String[] parts = line.split("\\s*:\\s*"); //NOI18N |
163 |
if (parts.length == 2) { |
164 |
label = parts[1]; |
165 |
} |
166 |
} else if (line.startsWith(CHANGED_FILES)) { |
167 |
mode = 1; |
168 |
} else if (line.startsWith(NEW_FILES)) { |
169 |
mode = 1; |
170 |
} else if (line.startsWith(REMOVED_FILES)) { |
171 |
mode = 1; |
172 |
} |
173 |
} else if (mode == 1) { |
174 |
if (line.startsWith(CHANGED_FILES)) { |
175 |
mode = 1; |
176 |
} else if (line.startsWith(NEW_FILES)) { |
177 |
mode = 1; |
178 |
} else if (line.startsWith(REMOVED_FILES)) { |
179 |
mode = 1; |
180 |
} else { |
181 |
final Matcher m = CHANGE.matcher(line); |
182 |
if (m.matches()) { |
183 |
String path = m.group(1); |
184 |
changedFiles.add( |
185 |
FileUtil.normalizeFile(new File(rootDir,path.trim())).getAbsolutePath()); |
186 |
} else { |
187 |
mode = 0; |
188 |
} |
189 |
} |
190 |
} |
191 |
} |
192 |
} finally { |
193 |
in.close(); |
194 |
} |
195 |
if (label != null) { |
196 |
return new Transaction(label, changedFiles); |
197 |
} else { |
198 |
final Process process2 = new ProcessBuilder("ade","pwv").start(); //NOI18N |
199 |
final BufferedReader in2 = new BufferedReader( new InputStreamReader(process2.getInputStream())); |
200 |
try { |
201 |
String line; |
202 |
while ((line=in2.readLine())!=null) { |
203 |
if (line.startsWith(PWV_LABEL)) { |
204 |
String[] parts = line.split("\\s*:\\s*"); //NOI18N |
205 |
if (parts.length == 2) { |
206 |
label = parts[1]; |
207 |
} |
208 |
} |
209 |
} |
210 |
} finally { |
211 |
in2.close(); |
212 |
} |
213 |
if (label != null) { |
214 |
return new Transaction(label, Collections.<String>emptySet()); |
215 |
} |
216 |
} |
217 |
} catch (IOException ioe) { |
218 |
Exceptions.printStackTrace(ioe); |
219 |
} |
220 |
return null; |
221 |
} |
222 |
|
223 |
static ADETreeStampProvider getInstance() { |
224 |
return Holder.instance; |
225 |
} |
226 |
|
227 |
private static class Transaction { |
228 |
|
229 |
private final String label; |
230 |
private final Set<? extends String> changedFiles; |
231 |
|
232 |
private Transaction( |
233 |
final String label, |
234 |
final Set<? extends String> changedFiles) { |
235 |
this.label = label; |
236 |
this.changedFiles = changedFiles; |
237 |
} |
238 |
|
239 |
public String getLabel() { |
240 |
return label; |
241 |
} |
242 |
|
243 |
public Set<? extends String> getChanges() { |
244 |
return changedFiles; |
245 |
} |
246 |
|
247 |
} |
248 |
|
249 |
private static class Holder { |
250 |
private static final ADETreeStampProvider instance = new ADETreeStampProvider(); |
251 |
} |
252 |
|
253 |
} |