-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDefaultBuildContext.java
More file actions
216 lines (187 loc) · 7.19 KB
/
DefaultBuildContext.java
File metadata and controls
216 lines (187 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Copyright (c) 2008 Sonatype, Inc. All rights reserved.
This program is licensed to you under the Apache License Version 2.0,
and you may not use this file except in compliance with the Apache License Version 2.0.
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing,
software distributed under the Apache License Version 2.0 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.codehaus.plexus.build;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.maven.plugin.LegacySupport;
import org.codehaus.plexus.build.connect.BuildConnection;
import org.codehaus.plexus.build.connect.messages.RefreshMessage;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.Scanner;
import org.codehaus.plexus.util.io.CachingOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Filesystem based non-incremental build context implementation which behaves
* as if all files were just created. More specifically,
*
* <ol>
* <li>hasDelta returns <code>true</code> for all paths</li>
* <li>newScanner returns Scanner that scans all files under provided
* basedir</li>
* <li>newDeletedScanner always returns empty scanner</li>
* <li>isIncremental returns <code>false</code></li>
* <li>getValue always returns the last set value in this session and only
* stores to memory</li>
* </ol>
*/
@Named("default")
@Singleton
public class DefaultBuildContext implements BuildContext {
private final Logger logger = LoggerFactory.getLogger(DefaultBuildContext.class);
// the legacy API requires the AbstractLogEnabled we just have it here to get
// compile errors in case it is missing from the classpath!
@SuppressWarnings("unused")
private static final AbstractLogEnabled DUMMY = null;
private final Map<String, Object> contextMap = new ConcurrentHashMap<>();
private org.sonatype.plexus.build.incremental.BuildContext legacy;
private BuildConnection connection;
private LegacySupport legacySupport;
/**
* @param legacy the legacy API we delegate to by default, this allow us
* to support "older" plugins and implementors of the API
* while still having a way to move forward!
* @param connection the connection we use to forward refresh events
* @param legacySupport legacy support to get the current session
*/
@Inject
public DefaultBuildContext(
org.sonatype.plexus.build.incremental.BuildContext legacy,
BuildConnection connection,
LegacySupport legacySupport) {
this.legacy = legacy;
this.connection = connection;
this.legacySupport = legacySupport;
}
/** {@inheritDoc} */
public boolean hasDelta(String relpath) {
return legacy.hasDelta(relpath);
}
/**
* <p>hasDelta.</p>
*
* @param file a {@link java.io.File} object.
* @return a boolean.
*/
public boolean hasDelta(File file) {
return legacy.hasDelta(file);
}
/**
* <p>hasDelta.</p>
*
* @param relpaths a {@link java.util.List} object.
* @return a boolean.
*/
public boolean hasDelta(List<String> relpaths) {
return legacy.hasDelta(relpaths);
}
/** {@inheritDoc} */
public OutputStream newFileOutputStream(File file) throws IOException {
if (isDefaultImplementation()) {
return new CachingOutputStream(file.toPath());
}
return legacy.newFileOutputStream(file);
}
/**
* @return <code>true</code> if the legacy is the default implementation and we
* can safely override/change behavior here, or <code>false</code> if a
* custom implementation is used and full delegation is required.
*/
private boolean isDefaultImplementation() {
return legacy.getClass().equals(org.sonatype.plexus.build.incremental.DefaultBuildContext.class);
}
/** {@inheritDoc} */
public Scanner newScanner(File basedir) {
return legacy.newScanner(basedir);
}
/** {@inheritDoc} */
public void refresh(File file) {
legacy.refresh(file);
connection.send(new RefreshMessage(file.toPath()), legacySupport.getSession());
}
/** {@inheritDoc} */
public Scanner newDeleteScanner(File basedir) {
return legacy.newDeleteScanner(basedir);
}
/** {@inheritDoc} */
public Scanner newScanner(File basedir, boolean ignoreDelta) {
return legacy.newScanner(basedir, ignoreDelta);
}
/**
* <p>isIncremental.</p>
*
* @return a boolean.
*/
public boolean isIncremental() {
return legacy.isIncremental();
}
/** {@inheritDoc} */
public Object getValue(String key) {
return contextMap.get(key);
}
/** {@inheritDoc} */
public void setValue(String key, Object value) {
contextMap.put(key, value);
}
/** {@inheritDoc} */
public void addError(File file, int line, int column, String message, Throwable cause) {
addMessage(file, line, column, message, Severity.ERROR, cause);
}
/** {@inheritDoc} */
public void addWarning(File file, int line, int column, String message, Throwable cause) {
addMessage(file, line, column, message, Severity.WARNING, cause);
}
private String getMessage(File file, int line, int column, String message) {
return file.getAbsolutePath() + " [" + line + ':' + column + "]: " + message;
}
/** {@inheritDoc} */
@Override
public void addMessage(File file, int line, int column, String message, Severity severity, Throwable cause) {
if (isDefaultImplementation()) {
switch (severity) {
case ERROR:
logger.error(getMessage(file, line, column, message), cause);
return;
case WARNING:
logger.warn(getMessage(file, line, column, message), cause);
return;
default:
logger.debug(getMessage(file, line, column, message), cause);
return;
}
}
legacy.addMessage(file, line, column, message, severity.getValue(), cause);
}
/** {@inheritDoc} */
@Override
@Deprecated
public void addMessage(File file, int line, int column, String message, int severity, Throwable cause) {
addMessage(file, line, column, message, Severity.fromValue(severity), cause);
}
/** {@inheritDoc} */
public void removeMessages(File file) {
if (isDefaultImplementation()) {
return;
}
legacy.removeMessages(file);
}
/** {@inheritDoc} */
public boolean isUptodate(File target, File source) {
return legacy.isUptodate(target, source);
}
}