1313
1414package org .codehaus .plexus .build ;
1515
16+ import javax .inject .Inject ;
1617import javax .inject .Named ;
1718import javax .inject .Singleton ;
1819
2324import java .util .Map ;
2425import java .util .concurrent .ConcurrentHashMap ;
2526
26- import org .codehaus .plexus .util . DirectoryScanner ;
27+ import org .codehaus .plexus .logging . AbstractLogEnabled ;
2728import org .codehaus .plexus .util .Scanner ;
2829import org .codehaus .plexus .util .io .CachingOutputStream ;
2930import org .slf4j .Logger ;
4748@ Singleton
4849public class DefaultBuildContext implements BuildContext {
4950
50- private final Map <String , Object > contextMap = new ConcurrentHashMap <>();
5151 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+
5270 /** {@inheritDoc} */
5371 public boolean hasDelta (String relpath ) {
54- return true ;
72+ return legacy . hasDelta ( relpath ) ;
5573 }
5674
5775 /**
@@ -61,7 +79,7 @@ public boolean hasDelta(String relpath) {
6179 * @return a boolean.
6280 */
6381 public boolean hasDelta (File file ) {
64- return true ;
82+ return legacy . hasDelta ( file ) ;
6583 }
6684
6785 /**
@@ -71,34 +89,44 @@ public boolean hasDelta(File file) {
7189 * @return a boolean.
7290 */
7391 public boolean hasDelta (List <String > relpaths ) {
74- return true ;
92+ return legacy . hasDelta ( relpaths ) ;
7593 }
7694
7795 /** {@inheritDoc} */
7896 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 );
80110 }
81111
82112 /** {@inheritDoc} */
83113 public Scanner newScanner (File basedir ) {
84- DirectoryScanner ds = new DirectoryScanner ();
85- ds .setBasedir (basedir );
86- return ds ;
114+ return legacy .newScanner (basedir );
87115 }
88116
89117 /** {@inheritDoc} */
90118 public void refresh (File file ) {
91- // do nothing
119+ legacy . refresh ( file );
92120 }
93121
94122 /** {@inheritDoc} */
95123 public Scanner newDeleteScanner (File basedir ) {
96- return new EmptyScanner (basedir );
124+ return legacy . newDeleteScanner (basedir );
97125 }
98126
99127 /** {@inheritDoc} */
100128 public Scanner newScanner (File basedir , boolean ignoreDelta ) {
101- return newScanner (basedir );
129+ return legacy . newScanner (basedir , ignoreDelta );
102130 }
103131
104132 /**
@@ -107,7 +135,7 @@ public Scanner newScanner(File basedir, boolean ignoreDelta) {
107135 * @return a boolean.
108136 */
109137 public boolean isIncremental () {
110- return false ;
138+ return legacy . isIncremental () ;
111139 }
112140
113141 /** {@inheritDoc} */
@@ -120,10 +148,6 @@ public void setValue(String key, Object value) {
120148 contextMap .put (key , value );
121149 }
122150
123- private String getMessage (File file , int line , int column , String message ) {
124- return file .getAbsolutePath () + " [" + line + ':' + column + "]: " + message ;
125- }
126-
127151 /** {@inheritDoc} */
128152 public void addError (File file , int line , int column , String message , Throwable cause ) {
129153 addMessage (file , line , column , message , SEVERITY_ERROR , cause );
@@ -134,28 +158,38 @@ public void addWarning(File file, int line, int column, String message, Throwabl
134158 addMessage (file , line , column , message , SEVERITY_WARNING , cause );
135159 }
136160
161+ private String getMessage (File file , int line , int column , String message ) {
162+ return file .getAbsolutePath () + " [" + line + ':' + column + "]: " + message ;
163+ }
164+
137165 /** {@inheritDoc} */
138166 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+ }
146179 }
147- throw new IllegalArgumentException ( "severity=" + severity );
180+ legacy . addMessage ( file , line , column , message , severity , cause );
148181 }
149182
150183 /** {@inheritDoc} */
151- public void removeMessages (File file ) {}
184+ public void removeMessages (File file ) {
185+ if (isDefaultImplementation ()) {
186+ return ;
187+ }
188+ legacy .removeMessages (file );
189+ }
152190
153191 /** {@inheritDoc} */
154192 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 );
160194 }
161195}
0 commit comments