File tree Expand file tree Collapse file tree 7 files changed +53
-11
lines changed
jcp/src/main/java/com/igormaznitsa/jcp Expand file tree Collapse file tree 7 files changed +53
-11
lines changed Original file line number Diff line number Diff line change 5353import com .igormaznitsa .jcp .containers .FileInfoContainer ;
5454import com .igormaznitsa .jcp .context .PreprocessingState ;
5555import com .igormaznitsa .jcp .context .PreprocessorContext ;
56+ import com .igormaznitsa .jcp .context .PreprocessorContextAware ;
5657import com .igormaznitsa .jcp .directives .ExcludeIfDirectiveHandler ;
5758import com .igormaznitsa .jcp .exceptions .FilePositionInfo ;
5859import com .igormaznitsa .jcp .exceptions .PreprocessorException ;
@@ -319,7 +320,21 @@ private List<PreprocessingState.ExcludeIfInfo> processGlobalDirectives(
319320 private Statistics preprocessFiles (final Collection <FileInfoContainer > files ,
320321 final boolean notifyProcessors ) throws IOException {
321322 if (notifyProcessors ) {
322- context .fireNotificationStart ();
323+ final List <PreprocessorContextAware > successfullyNotified = new ArrayList <>();
324+ try {
325+ context .fireNotificationStart (successfullyNotified );
326+ } catch (final Exception ex ) {
327+ context .logError ("Error during init of context aware processors: " + ex .getMessage ());
328+ successfullyNotified .forEach (x -> {
329+ try {
330+ x .onContextStopped (context , ex );
331+ } catch (Exception err ) {
332+ context .logError ("Error: " + err .getMessage ());
333+ }
334+ });
335+ throw new IllegalStateException ("Exception during notification of context aware listeners" ,
336+ ex );
337+ }
323338 }
324339
325340 int preprocessedCounter = 0 ;
Original file line number Diff line number Diff line change 2727import com .igormaznitsa .jcp .context .CommentTextProcessor ;
2828import com .igormaznitsa .jcp .context .PreprocessingState ;
2929import com .igormaznitsa .jcp .context .PreprocessorContext ;
30+ import com .igormaznitsa .jcp .context .PreprocessorContextAware ;
3031import com .igormaznitsa .jcp .directives .AbstractDirectiveHandler ;
3132import com .igormaznitsa .jcp .directives .AfterDirectiveProcessingBehaviour ;
3233import com .igormaznitsa .jcp .directives .DirectiveArgumentType ;
@@ -516,7 +517,21 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
516517 final boolean notifyProcessors )
517518 throws IOException {
518519 if (!context .isCloned () && notifyProcessors ) {
519- context .fireNotificationStart ();
520+ final List <PreprocessorContextAware > successfullyNotified = new ArrayList <>();
521+ try {
522+ context .fireNotificationStart (successfullyNotified );
523+ } catch (final Exception ex ) {
524+ context .logError ("Error during init of context aware processors: " + ex .getMessage ());
525+ successfullyNotified .forEach (x -> {
526+ try {
527+ x .onContextStopped (context , ex );
528+ } catch (Exception err ) {
529+ context .logError ("Error: " + err .getMessage ());
530+ }
531+ });
532+ throw new IllegalStateException ("Exception during notification of context aware listeners" ,
533+ ex );
534+ }
520535 }
521536
522537 PreprocessingState theState = null ;
Original file line number Diff line number Diff line change 1313 *
1414 * @since 7.2.0
1515 */
16- public interface CommentTextProcessor extends PreprocessorContextListener , ExecutionAllowable {
16+ public interface CommentTextProcessor extends PreprocessorContextAware , ExecutionAllowable {
1717
1818 /**
1919 * Processes uncommented text detected in `//$` or `//$$` sections.
Original file line number Diff line number Diff line change @@ -296,13 +296,23 @@ public Set<FileInfoContainer> findPreprocessedResources() {
296296
297297 /**
298298 * Send notification about context start to all registered listeners.
299- *
299+ * @param initedList list accumulating successfully processed listeners, must not be immutable and must not be null
300300 * @since 7.2.0
301301 */
302- public void fireNotificationStart () {
303- this .getCommentTextProcessors ().forEach (x -> x .onContextStarted (this ));
302+ public void fireNotificationStart (final List <PreprocessorContextAware > initedList ) {
303+ this .getCommentTextProcessors ().forEach (x -> {
304+ x .onContextStarted (this );
305+ initedList .add (x );
306+ });
304307 this .getMapVariableNameToSpecialVarProcessor ()
305- .values ().stream ().flatMap (Collection ::stream ).forEach (x -> x .onContextStarted (this ));
308+ .values ().stream ().flatMap (Collection ::stream ).forEach (x -> {
309+ x .onContextStarted (this );
310+ initedList .add (x );
311+ });
312+ this .getPreprocessorExtensions ().forEach (x -> {
313+ x .onContextStarted (this );
314+ initedList .add (x );
315+ });
306316 }
307317
308318 /**
@@ -316,6 +326,7 @@ public void fireNotificationStop(final Throwable error) {
316326 this .getMapVariableNameToSpecialVarProcessor ()
317327 .values ().stream ().flatMap (Collection ::stream )
318328 .forEach (x -> x .onContextStopped (this , error ));
329+ this .getPreprocessorExtensions ().forEach (x -> x .onContextStopped (this , error ));
319330 }
320331
321332 /**
Original file line number Diff line number Diff line change 33/**
44 * Listener for preprocessor context work states.
55 *
6- * @since 7.2 .0
6+ * @since 7.3 .0
77 */
8- public interface PreprocessorContextListener {
8+ public interface PreprocessorContextAware {
99 /**
1010 * Called when context started.
1111 *
Original file line number Diff line number Diff line change 3232 *
3333 * @author Igor Maznitsa ([email protected] ) 3434 */
35- public interface SpecialVariableProcessor extends PreprocessorContextListener ,
35+ public interface SpecialVariableProcessor extends PreprocessorContextAware ,
3636 ExecutionAllowable {
3737
3838 /**
Original file line number Diff line number Diff line change 2525import com .igormaznitsa .jcp .context .ExecutionAllowable ;
2626import com .igormaznitsa .jcp .context .PreprocessingState ;
2727import com .igormaznitsa .jcp .context .PreprocessorContext ;
28+ import com .igormaznitsa .jcp .context .PreprocessorContextAware ;
2829import com .igormaznitsa .jcp .exceptions .FilePositionInfo ;
2930import com .igormaznitsa .jcp .expression .Value ;
3031
3435 *
3536 * @author Igor Maznitsa ([email protected] ) 3637 */
37- public interface PreprocessorExtension extends ExecutionAllowable {
38+ public interface PreprocessorExtension extends PreprocessorContextAware , ExecutionAllowable {
3839
3940 /**
4041 * Undefined arity. In case of functions it means to check only name.
You can’t perform that action at this time.
0 commit comments