13
13
14
14
package org .codehaus .plexus .build ;
15
15
16
+ import javax .inject .Inject ;
16
17
import javax .inject .Named ;
17
18
import javax .inject .Singleton ;
18
19
23
24
import java .util .Map ;
24
25
import java .util .concurrent .ConcurrentHashMap ;
25
26
26
- import org .codehaus .plexus .util . DirectoryScanner ;
27
+ import org .codehaus .plexus .logging . AbstractLogEnabled ;
27
28
import org .codehaus .plexus .util .Scanner ;
28
29
import org .codehaus .plexus .util .io .CachingOutputStream ;
29
30
import org .slf4j .Logger ;
47
48
@ Singleton
48
49
public class DefaultBuildContext implements BuildContext {
49
50
50
- private final Map <String , Object > contextMap = new ConcurrentHashMap <>();
51
51
private final Logger logger = LoggerFactory .getLogger (DefaultBuildContext .class );
52
+ // the legacy API requires the AbstractLogEnabled we just have it here to get
53
+ // compile errors in case it is missing from the classpath!
54
+ @ SuppressWarnings ("unused" )
55
+ private static final AbstractLogEnabled DUMMY = null ;
56
+
57
+ private final Map <String , Object > contextMap = new ConcurrentHashMap <>();
58
+ private org .sonatype .plexus .build .incremental .BuildContext legacy ;
59
+
60
+ /**
61
+ * @param legacy the legacy API we delegate to by default, this allow us to
62
+ * support "older" plugins and implementors of the API while still
63
+ * having a way to move forward!
64
+ */
65
+ @ Inject
66
+ public DefaultBuildContext (org .sonatype .plexus .build .incremental .BuildContext legacy ) {
67
+ this .legacy = legacy ;
68
+ }
69
+
52
70
/** {@inheritDoc} */
53
71
public boolean hasDelta (String relpath ) {
54
- return true ;
72
+ return legacy . hasDelta ( relpath ) ;
55
73
}
56
74
57
75
/**
@@ -61,7 +79,7 @@ public boolean hasDelta(String relpath) {
61
79
* @return a boolean.
62
80
*/
63
81
public boolean hasDelta (File file ) {
64
- return true ;
82
+ return legacy . hasDelta ( file ) ;
65
83
}
66
84
67
85
/**
@@ -71,34 +89,44 @@ public boolean hasDelta(File file) {
71
89
* @return a boolean.
72
90
*/
73
91
public boolean hasDelta (List <String > relpaths ) {
74
- return true ;
92
+ return legacy . hasDelta ( relpaths ) ;
75
93
}
76
94
77
95
/** {@inheritDoc} */
78
96
public OutputStream newFileOutputStream (File file ) throws IOException {
79
- return new CachingOutputStream (file .toPath ());
97
+ if (isDefaultImplementation ()) {
98
+ return new CachingOutputStream (file .toPath ());
99
+ }
100
+ return legacy .newFileOutputStream (file );
101
+ }
102
+
103
+ /**
104
+ * @return <code>true</code> if the legacy is the default implementation and we
105
+ * can safely override/change behavior here, or <code>false</code> if a
106
+ * custom implementation is used and full delegation is required.
107
+ */
108
+ private boolean isDefaultImplementation () {
109
+ return legacy .getClass ().equals (org .sonatype .plexus .build .incremental .DefaultBuildContext .class );
80
110
}
81
111
82
112
/** {@inheritDoc} */
83
113
public Scanner newScanner (File basedir ) {
84
- DirectoryScanner ds = new DirectoryScanner ();
85
- ds .setBasedir (basedir );
86
- return ds ;
114
+ return legacy .newScanner (basedir );
87
115
}
88
116
89
117
/** {@inheritDoc} */
90
118
public void refresh (File file ) {
91
- // do nothing
119
+ legacy . refresh ( file );
92
120
}
93
121
94
122
/** {@inheritDoc} */
95
123
public Scanner newDeleteScanner (File basedir ) {
96
- return new EmptyScanner (basedir );
124
+ return legacy . newDeleteScanner (basedir );
97
125
}
98
126
99
127
/** {@inheritDoc} */
100
128
public Scanner newScanner (File basedir , boolean ignoreDelta ) {
101
- return newScanner (basedir );
129
+ return legacy . newScanner (basedir , ignoreDelta );
102
130
}
103
131
104
132
/**
@@ -107,7 +135,7 @@ public Scanner newScanner(File basedir, boolean ignoreDelta) {
107
135
* @return a boolean.
108
136
*/
109
137
public boolean isIncremental () {
110
- return false ;
138
+ return legacy . isIncremental () ;
111
139
}
112
140
113
141
/** {@inheritDoc} */
@@ -120,10 +148,6 @@ public void setValue(String key, Object value) {
120
148
contextMap .put (key , value );
121
149
}
122
150
123
- private String getMessage (File file , int line , int column , String message ) {
124
- return file .getAbsolutePath () + " [" + line + ':' + column + "]: " + message ;
125
- }
126
-
127
151
/** {@inheritDoc} */
128
152
public void addError (File file , int line , int column , String message , Throwable cause ) {
129
153
addMessage (file , line , column , message , SEVERITY_ERROR , cause );
@@ -134,28 +158,38 @@ public void addWarning(File file, int line, int column, String message, Throwabl
134
158
addMessage (file , line , column , message , SEVERITY_WARNING , cause );
135
159
}
136
160
161
+ private String getMessage (File file , int line , int column , String message ) {
162
+ return file .getAbsolutePath () + " [" + line + ':' + column + "]: " + message ;
163
+ }
164
+
137
165
/** {@inheritDoc} */
138
166
public void addMessage (File file , int line , int column , String message , int severity , Throwable cause ) {
139
- switch (severity ) {
140
- case BuildContext .SEVERITY_ERROR :
141
- logger .error (getMessage (file , line , column , message ), cause );
142
- return ;
143
- case BuildContext .SEVERITY_WARNING :
144
- logger .warn (getMessage (file , line , column , message ), cause );
145
- return ;
167
+ if (isDefaultImplementation ()) {
168
+ switch (severity ) {
169
+ case BuildContext .SEVERITY_ERROR :
170
+ logger .error (getMessage (file , line , column , message ), cause );
171
+ return ;
172
+ case BuildContext .SEVERITY_WARNING :
173
+ logger .warn (getMessage (file , line , column , message ), cause );
174
+ return ;
175
+ default :
176
+ logger .debug (getMessage (file , line , column , message ), cause );
177
+ return ;
178
+ }
146
179
}
147
- throw new IllegalArgumentException ( "severity=" + severity );
180
+ legacy . addMessage ( file , line , column , message , severity , cause );
148
181
}
149
182
150
183
/** {@inheritDoc} */
151
- public void removeMessages (File file ) {}
184
+ public void removeMessages (File file ) {
185
+ if (isDefaultImplementation ()) {
186
+ return ;
187
+ }
188
+ legacy .removeMessages (file );
189
+ }
152
190
153
191
/** {@inheritDoc} */
154
192
public boolean isUptodate (File target , File source ) {
155
- return target != null
156
- && target .exists ()
157
- && source != null
158
- && source .exists ()
159
- && target .lastModified () > source .lastModified ();
193
+ return legacy .isUptodate (target , source );
160
194
}
161
195
}
0 commit comments