Skip to content

Commit 45d2a8c

Browse files
committed
refactoring of service processing
1 parent 99438fb commit 45d2a8c

File tree

7 files changed

+53
-11
lines changed

7 files changed

+53
-11
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/JcpPreprocessor.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.igormaznitsa.jcp.containers.FileInfoContainer;
5454
import com.igormaznitsa.jcp.context.PreprocessingState;
5555
import com.igormaznitsa.jcp.context.PreprocessorContext;
56+
import com.igormaznitsa.jcp.context.PreprocessorContextAware;
5657
import com.igormaznitsa.jcp.directives.ExcludeIfDirectiveHandler;
5758
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
5859
import 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;

jcp/src/main/java/com/igormaznitsa/jcp/containers/FileInfoContainer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.igormaznitsa.jcp.context.CommentTextProcessor;
2828
import com.igormaznitsa.jcp.context.PreprocessingState;
2929
import com.igormaznitsa.jcp.context.PreprocessorContext;
30+
import com.igormaznitsa.jcp.context.PreprocessorContextAware;
3031
import com.igormaznitsa.jcp.directives.AbstractDirectiveHandler;
3132
import com.igormaznitsa.jcp.directives.AfterDirectiveProcessingBehaviour;
3233
import 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;

jcp/src/main/java/com/igormaznitsa/jcp/context/CommentTextProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
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.

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessorContext.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff 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
/**

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessorContextListener.java renamed to jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessorContextAware.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
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
*

jcp/src/main/java/com/igormaznitsa/jcp/context/SpecialVariableProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author Igor Maznitsa ([email protected])
3434
*/
35-
public interface SpecialVariableProcessor extends PreprocessorContextListener,
35+
public interface SpecialVariableProcessor extends PreprocessorContextAware,
3636
ExecutionAllowable {
3737

3838
/**

jcp/src/main/java/com/igormaznitsa/jcp/extension/PreprocessorExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.igormaznitsa.jcp.context.ExecutionAllowable;
2626
import com.igormaznitsa.jcp.context.PreprocessingState;
2727
import com.igormaznitsa.jcp.context.PreprocessorContext;
28+
import com.igormaznitsa.jcp.context.PreprocessorContextAware;
2829
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
2930
import com.igormaznitsa.jcp.expression.Value;
3031

@@ -34,7 +35,7 @@
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.

0 commit comments

Comments
 (0)