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