Skip to content

Commit 63cfcc2

Browse files
authored
Tutorials edits - Terminate on configuration error - Terminal Color (#2368)
* refactor: minor * feat: terminal colors * feat: reorganized utility classes into 'text' and 'xml' packages, added new `TerminalColors` and `XMLTextUtil` classes - Moved text-related utilities to `com.predic8.membrane.core.util.text` package. - Introduced `TerminalColors` class for ANSI terminal color handling. - Added `XMLTextUtil` class with utilities for XML formatting and validation. - Updated class references and imports across the codebase to reflect package restructuring. - Enhanced logging configuration to support color highlighting. * refactor: cleanup unused methods, enhance type-checking, and suppress warnings - Removed redundant methods no longer in use (`waitForAsynchronousInitialization`, `isAsynchronousInitialization`). - Improved type-checking with modern Java features (e.g., `instanceof` with variable binding). - Refined helper method signatures to reduce unnecessary parameters. - Suppressed unused warning in `TerminalColors`. - Updated exception types to match method requirements. * refactor: adjust default behavior in `TerminalColors` and improve code consistency - Changed fallback return value in `TerminalColors` to `false` for safer behavior. - Updated exception type in `XMLTextUtil` to match method operations. - Refined imports in `GrammarMock` for conciseness. - Removed unused exceptions in `HttpServerHandler` to simplify method signatures. * fix: improve OS detection logic and update roadmap for 7.1.0 prep - Corrected OS detection for Windows by using full "windows" string match. - Added 7.1.0 section to the roadmap, noting JSON Schema registration. * refactor: Fixed OSUtil win vs. windows, consolidate `StringUtil` and `TextUtil` usage, reorganize utility imports, and fix minor inconsistencies * refactor: simplify imports in `TextUtilTest` and `TerminalColors` by consolidating wildcard usage * feat: enhance logging with terminal color support and improve OS utility methods - Added colorized logging for different log levels using `TerminalColors`. - Refined `OSUtil` with new constants, IntelliJ/VSCode detection, and JVM argument helper. - Simplified ANSI support detection logic in `TerminalColors`. * refactor: streamline OS utility methods and terminal color logic - Removed redundant imports and simplified method calls in `TerminalColors` and `OSUtil`. - Unified OS detection patterns for macOS, Windows, and Linux/Unix systems. - Improved readability and consistency in terminal color support logic. * feat: enhance terminal color support and fix OS detection issues - Added system property `MEMBRANE_COLORS` to enable/disable terminal color logging explicitly. - Fixed null-safe access in OS detection methods. - Corrected method naming inconsistencies in `OSUtil` and `TerminalColors`. * feat: improve terminal environment handling and simplify ANSI support detection - Added new constants for terminal environment variables in `OSUtil`. - Introduced `isDumbTerminal` method for enhanced Linux/Unix support. - Refactored `detectAnsiSupport` in `TerminalColors` for better readability and logic reuse. * refactor: rename terminal color environment variable and property for consistency - Updated `MEMBRANE_COLORS` to `MEMBRANE_TERM_COLORS` and added new system property `membrane.term.colors`. - Adjusted references in `LogInterceptor`, `TerminalColors`, and `Constants`. - Improved logic in `TerminalColors` for explicit fallback handling. * feat: add support for disabling terminal colors via environment variable and property - Introduced `MEMBRANE_DISABLE_TERM_COLORS` environment variable and `membrane.disable.term.colors` system property. - Updated `TerminalColors`, `Constants`, and log4j2 configuration files to support new variables. - Adjusted ANSI support detection logic to accommodate the disable feature. * refactor: remove `isDumbTerminal` method and inline logic in `TerminalColors` - Simplified terminal color detection by inlining `isDumbTerminal` logic directly into `TerminalColors`. - Removed unused method from `OSUtil`. * feat: enhance and standardize terminal color support - Implemented detailed environment-based terminal color detection in shell and batch scripts. - Deprecated redundant methods in `OSUtil` while streamlining color detection logic in `TerminalColors`. - Updated log configuration files to support standardized `membrane.disable.term.colors` system property. - Improved user control over terminal colors with clear case-insensitive environment overrides. * feat: add `membrane.disable.term.colors` system property to batch script - Extended `start_router.cmd` to include `JAVA_OPTS` for disabling terminal colors. * feat: extend terminal compatibility detection in shell script - Added support for WezTerm, Alacritty, and Kitty in `start_router.sh`. - Minor formatting adjustment in `TerminalColors`. * feat: extend terminal compatibility detection in shell script - Added support for WezTerm, Alacritty, and Kitty in `start_router.sh`. - Minor formatting adjustment in `TerminalColors`.
1 parent f5a416c commit 63cfcc2

47 files changed

Lines changed: 603 additions & 100 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

annot/src/main/java/com/predic8/membrane/annot/generator/JsonSchemaGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
public class JsonSchemaGenerator extends AbstractGrammar {
4141

42+
public static final String MEMBRANE_SCHEMA_JSON_FILENAME = "membrane.schema.json";
43+
4244
private final Map<String, Boolean> topLevelAdded = new HashMap<>();
4345

4446
public JsonSchemaGenerator(ProcessingEnvironment processingEnv) {
@@ -166,7 +168,7 @@ private FileObject createFile(MainInfo main) throws IOException {
166168
.createResource(
167169
CLASS_OUTPUT,
168170
main.getAnnotation().outputPackage().replaceAll("\\.spring$", ".json"),
169-
"membrane.schema.json",
171+
MEMBRANE_SCHEMA_JSON_FILENAME,
170172
sources.toArray(new Element[0])
171173
);
172174
}

annot/src/main/java/com/predic8/membrane/annot/yaml/BeanRegistryImplementation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ private void activationRun() {
143143
uidsToRemove.add(bd.getUid());
144144
} catch (Exception e) {
145145
log.error("Could not handle {} {}/{}", bd.getAction(), bd.getNamespace(), bd.getName(), e);
146+
throw new RuntimeException(e);
146147
}
147148
}
148149
for (String uid : uidsToRemove)

core/src/main/java/com/predic8/membrane/core/Constants.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ public class Constants {
2525

2626
public static final String MEMBRANE_HOME = "MEMBRANE_HOME";
2727

28-
public static final String CRLF = "" + ((char) 13) + ((char) 10);
28+
// Java System Properties
29+
30+
/**
31+
* System property set by start script based on MEMBRANE_DISABLE_TERM_COLORS detection.
32+
* Users should use the environment variable instead.
33+
*/
34+
public static final String MEMBRANE_DISABLE_TERM_COLORS_PROPERTY = "membrane.disable.term.colors";
35+
36+
public static final String CRLF = "" + ((char) 13) + ((char) 10);
2937

3038
public static final byte[] CRLF_BYTES = { 13, 10 };
3139

@@ -42,7 +50,7 @@ public class Constants {
4250
Properties p = new Properties(); // Development
4351
p.load(new FileInputStream("target/maven-archiver/pom.properties"));
4452
version = p.getProperty("version") + " - DEVELOPMENT";
45-
} catch (Exception e2) {
53+
} catch (Exception ignored) {
4654
}
4755
}
4856
VERSION = version;

core/src/main/java/com/predic8/membrane/core/Router.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.predic8.membrane.core.util.DNSCache;
5353
import com.predic8.membrane.core.util.TimerManager;
5454
import com.predic8.membrane.core.util.URIFactory;
55+
import com.predic8.membrane.core.util.text.*;
5556
import org.slf4j.Logger;
5657
import org.slf4j.LoggerFactory;
5758
import org.springframework.beans.BeansException;
@@ -67,10 +68,11 @@
6768
import java.util.*;
6869
import java.util.concurrent.ExecutorService;
6970

70-
import static com.predic8.membrane.core.Constants.PRODUCT_NAME;
71-
import static com.predic8.membrane.core.Constants.VERSION;
71+
import static com.predic8.membrane.core.Constants.*;
7272
import static com.predic8.membrane.core.jmx.JmxExporter.JMX_EXPORTER_NAME;
7373
import static com.predic8.membrane.core.util.DLPUtil.displayTraceWarning;
74+
import static com.predic8.membrane.core.util.text.TerminalColors.BRIGHT_CYAN;
75+
import static com.predic8.membrane.core.util.text.TerminalColors.RESET;
7476
import static java.util.concurrent.Executors.newSingleThreadExecutor;
7577

7678
/**
@@ -355,7 +357,11 @@ public void start() {
355357

356358
ApiInfo.logInfosAboutStartedProxies(ruleManager);
357359
if (!asynchronousInitialization)
358-
log.info("{} {} up and running!", PRODUCT_NAME, VERSION);
360+
logStartupMessage();
361+
}
362+
363+
private static void logStartupMessage() {
364+
log.info("{}{} {} up and running!{}", BRIGHT_CYAN(), PRODUCT_NAME, VERSION, RESET());
359365
}
360366

361367
private void startJmx() {
@@ -662,40 +668,25 @@ public GlobalInterceptor getGlobalInterceptor() {
662668
return globalInterceptor;
663669
}
664670

665-
public synchronized boolean isAsynchronousInitialization() {
666-
return asynchronousInitialization;
667-
}
668-
669671
public synchronized void setAsynchronousInitialization(boolean asynchronousInitialization) {
670672
this.asynchronousInitialization = asynchronousInitialization;
671673
notifyAll();
672674
}
673675

674-
public synchronized void waitForAsynchronousInitialization() {
675-
while (asynchronousInitialization) {
676-
try {
677-
wait();
678-
} catch (InterruptedException e) {
679-
Thread.currentThread().interrupt();
680-
}
681-
}
682-
}
683-
684676
public void handleAsynchronousInitializationResult(boolean success) {
685677
if (!success && !retryInit)
686678
System.exit(1);
687679
ApiInfo.logInfosAboutStartedProxies(ruleManager);
688-
log.info("{} {} up and running!", PRODUCT_NAME, VERSION);
680+
logStartupMessage();
689681
setAsynchronousInitialization(false);
690682
}
691683

692684
@Override
693685
public void handleBeanEvent(BeanDefinition bd, Object bean, Object oldBean) throws IOException {
694-
if (!(bean instanceof Proxy)) {
686+
if (!(bean instanceof Proxy newProxy)) {
695687
throw new IllegalArgumentException("Bean must be a Proxy instance, but got: " + bean.getClass().getName());
696688
}
697689

698-
Proxy newProxy = (Proxy) bean;
699690
if (newProxy.getName() == null)
700691
newProxy.setName(bd.getName());
701692

core/src/main/java/com/predic8/membrane/core/exchangestore/FileExchangeStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.util.*;
3030
import java.util.concurrent.atomic.*;
3131

32-
import static com.predic8.membrane.core.util.TextUtil.*;
32+
import static com.predic8.membrane.core.util.xml.XMLTextUtil.*;
3333
import static java.nio.charset.StandardCharsets.*;
3434
import static java.util.Calendar.*;
3535

core/src/main/java/com/predic8/membrane/core/graphql/GraphQLProtectionInterceptor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.predic8.membrane.core.http.*;
2222
import com.predic8.membrane.core.interceptor.*;
2323
import com.predic8.membrane.core.util.*;
24+
import com.predic8.membrane.core.util.text.*;
2425
import org.slf4j.*;
2526

2627
import java.security.*;
@@ -29,6 +30,7 @@
2930
import static com.predic8.membrane.core.http.Request.METHOD_GET;
3031
import static com.predic8.membrane.core.http.Request.METHOD_POST;
3132
import static com.predic8.membrane.core.interceptor.Outcome.*;
33+
import static com.predic8.membrane.core.util.text.TextUtil.*;
3234

3335
/**
3436
* @description Check GraphQL-over-HTTP requests, enforcing several limits and/or restrictions. This effectively helps to reduce
@@ -187,7 +189,7 @@ public String getLongDescription() {
187189
"graphql-over-http/blob/a1e6d8ca248c9a19eb59a2eedd988c204909ee3f/spec/GraphQLOverHTTP.md\">" +
188190
"GraphQL-over-HTTP</a> specs).<br/>" +
189191
"GraphQL extensions: " + (allowExtensions ? "Allowed." : "Forbidden.") + "<br/>" +
190-
"Allowed HTTP verbs: " + TextUtil.toEnglishList("and", allowedMethods.toArray(new String[0])) + ".<br/>" +
192+
"Allowed HTTP verbs: " + toEnglishList("and", allowedMethods.toArray(new String[0])) + ".<br/>" +
191193
"Maximum allowed nested query levels: " + maxDepth + "<br/>" +
192194
"Maximum allowed recursion levels (nested repetitions of the same word): " + maxRecursion + ".</div>";
193195
}

core/src/main/java/com/predic8/membrane/core/http/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import static com.predic8.membrane.core.http.Header.*;
2626
import static com.predic8.membrane.core.util.ContentTypeDetector.EffectiveContentType.*;
2727
import static com.predic8.membrane.core.util.ContentTypeDetector.*;
28-
import static com.predic8.membrane.core.util.TextUtil.getCharset;
28+
import static com.predic8.membrane.core.util.text.TextUtil.getCharset;
2929

3030
/**
3131
* A HTTP message (request or response).

core/src/main/java/com/predic8/membrane/core/interceptor/acl/Resource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import com.predic8.membrane.core.Router;
2929
import com.predic8.membrane.core.config.AbstractXmlElement;
3030
import com.predic8.membrane.core.config.GenericComplexElement;
31-
import com.predic8.membrane.core.util.TextUtil;
31+
import com.predic8.membrane.core.util.text.TextUtil;
3232

3333
public class Resource extends AbstractXmlElement {
3434

core/src/main/java/com/predic8/membrane/core/interceptor/acl/matchers/GlobMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import com.predic8.membrane.core.interceptor.acl.TypeMatcher;
1717

18-
import static com.predic8.membrane.core.util.TextUtil.globToRegExp;
18+
import static com.predic8.membrane.core.util.text.TextUtil.globToRegExp;
1919
import static java.util.regex.Pattern.compile;
2020

2121
public class GlobMatcher implements TypeMatcher {

core/src/main/java/com/predic8/membrane/core/interceptor/administration/AdminPageBuilder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.predic8.membrane.core.proxies.*;
2525
import com.predic8.membrane.core.proxies.Proxy;
2626
import com.predic8.membrane.core.transport.http.*;
27-
import com.predic8.membrane.core.util.*;
2827
import org.apache.commons.text.*;
2928

3029
import java.io.*;
@@ -36,6 +35,7 @@
3635
import static com.predic8.membrane.core.interceptor.balancer.BalancerUtil.*;
3736
import static com.predic8.membrane.core.interceptor.balancer.Node.Status.*;
3837
import static com.predic8.membrane.core.util.URLParamUtil.*;
38+
import static com.predic8.membrane.core.util.xml.XMLTextUtil.isValidXMLSnippet;
3939
import static java.nio.charset.StandardCharsets.*;
4040
import static java.util.Objects.requireNonNull;
4141
import static org.apache.commons.lang3.time.DurationFormatUtils.*;
@@ -618,12 +618,12 @@ private void createInterceptorVisualization(Interceptor i, int columnSpan, Strin
618618
String longDescription = i.getLongDescription();
619619
boolean same = longDescription.equals(shortDescription);
620620

621-
if (!TextUtil.isValidXMLSnippet(shortDescription)) {
621+
if (!isValidXMLSnippet(shortDescription)) {
622622
shortDescription = StringEscapeUtils.escapeHtml4(shortDescription).replace("\n", "<br/>");
623623
if (same)
624624
longDescription = shortDescription;
625625
}
626-
if (!same && !TextUtil.isValidXMLSnippet(longDescription)) {
626+
if (!same && !isValidXMLSnippet(longDescription)) {
627627
longDescription = StringEscapeUtils.escapeHtml4(longDescription).replace("\n", "<br/>");
628628
}
629629

@@ -636,9 +636,9 @@ private void createInterceptorVisualization(Interceptor i, int columnSpan, Strin
636636

637637
String iid = "i" + id;
638638
div().id("i" + id);
639-
createHelpIcon(i, id);
639+
createHelpIcon(i);
640640
if (!shortDescription.isEmpty() && !longDescription.equals(shortDescription)) {
641-
createExpandIcon(i, id);
641+
createExpandIcon(id);
642642
}
643643
end();
644644
createShowIconsScript(did, iid);
@@ -700,7 +700,7 @@ private void createInterceptorVisualization(Interceptor i, int columnSpan, Strin
700700
end();
701701
}
702702

703-
private void createExpandIcon(Interceptor i, String id) {
703+
private void createExpandIcon(String id) {
704704
div().style("float:right;");
705705
span().id("e" + id).classAttr("ui-icon ui-icon-triangle-1-w").title("expand").end();
706706
end();
@@ -709,7 +709,7 @@ private void createExpandIcon(Interceptor i, String id) {
709709
end();
710710
}
711711

712-
private void createHelpIcon(Interceptor i, String id) {
712+
private void createHelpIcon(Interceptor i) {
713713
String helpId = i.getHelpId();
714714
if (helpId != null) {
715715
div().style("float:right;");

0 commit comments

Comments
 (0)