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.HashMap; |
49 |
import java.util.HashSet; |
50 |
import java.util.Map; |
51 |
import java.util.Set; |
52 |
import java.util.concurrent.atomic.AtomicReference; |
53 |
import java.util.logging.Level; |
54 |
import java.util.logging.Logger; |
55 |
import java.util.regex.Matcher; |
56 |
import java.util.regex.Pattern; |
57 |
import org.openide.filesystems.FileObject; |
58 |
import org.openide.filesystems.FileUtil; |
59 |
import org.openide.filesystems.TreeStamp; |
60 |
import org.openide.util.Exceptions; |
61 |
|
62 |
/** |
63 |
* |
64 |
* @author Tomas Zezula |
65 |
*/ |
66 |
public final class ADETreeStampProvider extends TreeStamp.Provider { |
67 |
|
68 |
private static final String LABEL = "BASE_LABEL:"; //NOI18N |
69 |
private static final String CHANGED_FILES = "ALL AFFECTED FILES:"; //NOI18N |
70 |
private static final String NEW_FILES = "NEW FILES:"; //NOI18N |
71 |
private static final String REMOVED_FILES = "RMNAMED (REMOVED) FILES:"; //NOI18N |
72 |
private static final Pattern CHANGE = Pattern.compile("\\s*\\d+\\.\\s+(.*)"); //NOI18N |
73 |
|
74 |
private static final Logger LOG = Logger.getLogger(ADETreeStampProvider.class.getName()); |
75 |
|
76 |
private final AtomicReference<File> adeRoot = new AtomicReference<File>(); |
77 |
|
78 |
private ADETreeStampProvider() {} |
79 |
|
80 |
@Override |
81 |
protected Map<FileObject, TreeStamp> stamps(Map<FileObject, String> rootsAndBaseIds) { |
82 |
final Map<FileObject, TreeStamp> result = new HashMap<FileObject, TreeStamp>(); |
83 |
Transaction tx = null; |
84 |
boolean described = false; |
85 |
for (Map.Entry<FileObject,String> entry : rootsAndBaseIds.entrySet()) { |
86 |
final FileObject root = entry.getKey(); |
87 |
final String base = entry.getValue(); |
88 |
final String adeRootPath = getADERootPath(); |
89 |
final String rootDirPath = getPath(FileUtil.toFile(root)); |
90 |
if (adeRootPath != null && |
91 |
rootDirPath != null && |
92 |
rootDirPath.startsWith(adeRootPath)) { //NOI18N |
93 |
if (!described) { |
94 |
described = true; |
95 |
tx = describeTrans(getADERoot()); |
96 |
} |
97 |
if (tx != null && (base == null || base.equals(tx.getLabel()))) { |
98 |
final Set<String> paths = new HashSet<String>(); |
99 |
for (String cf : tx.getChanges()) { |
100 |
if (cf.startsWith(rootDirPath)) { |
101 |
paths.add(cf.substring(rootDirPath.length())); |
102 |
} |
103 |
} |
104 |
result.put(root, create(tx.getLabel(), null, paths)); |
105 |
LOG.log( |
106 |
Level.FINE, |
107 |
"Root: {0}, Label: {1}, Changes: {2}", //NOI18N |
108 |
new Object[] { |
109 |
rootDirPath, |
110 |
tx.getLabel(), |
111 |
paths |
112 |
}); |
113 |
} |
114 |
} |
115 |
} |
116 |
return result; |
117 |
} |
118 |
|
119 |
|
120 |
File getADERoot() { |
121 |
File adeRootDir = adeRoot.get(); |
122 |
if (adeRootDir == null) { |
123 |
final String adeRootStr = System.getenv("ADE_VIEW_ROOT"); //NOI18N |
124 |
if (adeRootStr != null) { |
125 |
adeRootDir = FileUtil.normalizeFile(new File(adeRootStr)); |
126 |
adeRoot.compareAndSet(null, adeRootDir); |
127 |
} |
128 |
} |
129 |
return adeRootDir; |
130 |
} |
131 |
|
132 |
String getADERootPath() { |
133 |
return getPath(getADERoot()); |
134 |
} |
135 |
|
136 |
private static String getPath(final File file) { |
137 |
if (file == null) { |
138 |
return null; |
139 |
} |
140 |
String path = file.getAbsolutePath(); |
141 |
if (!path.endsWith(File.separator)) { |
142 |
path = path + File.separatorChar; |
143 |
} |
144 |
return path; |
145 |
} |
146 |
|
147 |
|
148 |
private Transaction describeTrans(final File rootDir) { |
149 |
try { |
150 |
final Process process = new ProcessBuilder("ade","describetrans","-short").start(); //NOI18N |
151 |
final BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream())); |
152 |
try { |
153 |
String label = null; |
154 |
Set<String> changedFiles = new HashSet<String>(); |
155 |
int mode = 0; |
156 |
String line; |
157 |
while ((line=in.readLine())!=null) { |
158 |
if (mode == 0) { |
159 |
if (line.startsWith(LABEL)) { |
160 |
String[] parts = line.split("\\s*:\\s*"); //NOI18N |
161 |
if (parts.length == 2) { |
162 |
label = parts[1]; |
163 |
} |
164 |
} else if (line.startsWith(CHANGED_FILES)) { |
165 |
mode = 1; |
166 |
} else if (line.startsWith(NEW_FILES)) { |
167 |
mode = 1; |
168 |
} else if (line.startsWith(REMOVED_FILES)) { |
169 |
mode = 1; |
170 |
} |
171 |
} else if (mode == 1) { |
172 |
if (line.startsWith(CHANGED_FILES)) { |
173 |
mode = 1; |
174 |
} else if (line.startsWith(NEW_FILES)) { |
175 |
mode = 1; |
176 |
} else if (line.startsWith(REMOVED_FILES)) { |
177 |
mode = 1; |
178 |
} else { |
179 |
final Matcher m = CHANGE.matcher(line); |
180 |
if (m.matches()) { |
181 |
String path = m.group(1); |
182 |
changedFiles.add( |
183 |
FileUtil.normalizeFile(new File(rootDir,path.trim())).getAbsolutePath()); |
184 |
} else { |
185 |
mode = 0; |
186 |
} |
187 |
} |
188 |
} |
189 |
} |
190 |
if (label != null) { |
191 |
return new Transaction(label, changedFiles); |
192 |
} |
193 |
} finally { |
194 |
in.close(); |
195 |
} |
196 |
} catch (IOException ioe) { |
197 |
Exceptions.printStackTrace(ioe); |
198 |
} |
199 |
return null; |
200 |
} |
201 |
|
202 |
static ADETreeStampProvider getInstance() { |
203 |
return Holder.instance; |
204 |
} |
205 |
|
206 |
private static class Transaction { |
207 |
|
208 |
private final String label; |
209 |
private final Set<? extends String> changedFiles; |
210 |
|
211 |
private Transaction( |
212 |
final String label, |
213 |
final Set<? extends String> changedFiles) { |
214 |
this.label = label; |
215 |
this.changedFiles = changedFiles; |
216 |
} |
217 |
|
218 |
public String getLabel() { |
219 |
return label; |
220 |
} |
221 |
|
222 |
public Set<? extends String> getChanges() { |
223 |
return changedFiles; |
224 |
} |
225 |
|
226 |
} |
227 |
|
228 |
private static class Holder { |
229 |
private static final ADETreeStampProvider instance = new ADETreeStampProvider(); |
230 |
} |
231 |
|
232 |
} |