Skip to content

Commit 19cf119

Browse files
committed
* Renamed ProcessScope to JvmScope.
* Fixed tests that were using MainApplicationScope instead of TestApplicationScope. * Updated dependencies.
1 parent cb28ea7 commit 19cf119

Some content is hidden

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

44 files changed

+2512
-937
lines changed

java/src/main/java/com/github/cowwoc/requirements10/java/internal/scope/AbstractApplicationScope.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package com.github.cowwoc.requirements10.java.internal.scope;
66

7+
import com.github.cowwoc.pouch.core.AbstractScope;
78
import com.github.cowwoc.pouch.core.ConcurrentLazyReference;
89
import com.github.cowwoc.pouch.core.Reference;
910
import com.github.cowwoc.requirements10.java.GlobalConfiguration;
@@ -16,9 +17,10 @@
1617
/**
1718
* ApplicationScope for the main and test codebases.
1819
*/
19-
public abstract class AbstractApplicationScope implements ApplicationScope
20+
public abstract class AbstractApplicationScope extends AbstractScope
21+
implements ApplicationScope
2022
{
21-
private final ProcessScope parent;
23+
protected final JvmScope parent;
2224
/**
2325
* The global configuration.
2426
*/
@@ -35,7 +37,7 @@ public abstract class AbstractApplicationScope implements ApplicationScope
3537
* @param globalConfiguration the global configuration
3638
* @throws NullPointerException if any of the arguments are null
3739
*/
38-
protected AbstractApplicationScope(ProcessScope parent, GlobalConfiguration globalConfiguration)
40+
protected AbstractApplicationScope(JvmScope parent, GlobalConfiguration globalConfiguration)
3941
{
4042
assert (parent != null);
4143
assert (globalConfiguration != null);

java/src/main/java/com/github/cowwoc/requirements10/java/internal/scope/ApplicationScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* The configuration of an application. A process may contain multiple applications.
1212
*/
13-
public interface ApplicationScope extends ProcessScope
13+
public interface ApplicationScope extends JvmScope
1414
{
1515
/**
1616
* @return the global configuration inherited by all validators
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2016 Gili Tzabari
3+
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
4+
*/
5+
package com.github.cowwoc.requirements10.java.internal.scope;
6+
7+
import com.github.cowwoc.pouch.core.AbstractScope;
8+
import com.github.cowwoc.requirements10.java.internal.terminal.Terminal;
9+
10+
import java.time.Duration;
11+
import java.util.concurrent.atomic.AtomicBoolean;
12+
13+
/**
14+
* The default implementation of JvmScope.
15+
* <p>
16+
* This class ensures that globals, such as slf4j, are initialized in a thread-safe manner.
17+
*/
18+
public final class DefaultJvmScope extends AbstractScope
19+
implements JvmScope
20+
{
21+
/**
22+
* The singleton instance.
23+
*/
24+
public static final DefaultJvmScope INSTANCE = new DefaultJvmScope();
25+
/**
26+
* The maximum amount of time to wait for child scopes to close.
27+
*/
28+
public static final Duration CLOSE_TIMEOUT = Duration.ofSeconds(10);
29+
private final Terminal terminal = new Terminal();
30+
private final AtomicBoolean closed = new AtomicBoolean();
31+
32+
private DefaultJvmScope()
33+
{
34+
}
35+
36+
@Override
37+
public Terminal getTerminal()
38+
{
39+
return terminal;
40+
}
41+
42+
@Override
43+
public boolean isClosed()
44+
{
45+
return closed.get();
46+
}
47+
48+
@Override
49+
public void close()
50+
{
51+
if (!closed.compareAndSet(false, true))
52+
return;
53+
children.shutdown(CLOSE_TIMEOUT);
54+
}
55+
}

java/src/main/java/com/github/cowwoc/requirements10/java/internal/scope/DefaultProcessScope.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

java/src/main/java/com/github/cowwoc/requirements10/java/internal/scope/ProcessScope.java renamed to java/src/main/java/com/github/cowwoc/requirements10/java/internal/scope/JvmScope.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
*/
55
package com.github.cowwoc.requirements10.java.internal.scope;
66

7+
import com.github.cowwoc.pouch.core.Scope;
78
import com.github.cowwoc.requirements10.java.internal.terminal.Terminal;
89

910
/**
10-
* The process configuration.
11+
* Values specific to the lifetime of a JVM.
1112
* <p>
12-
* <b>Thread-safety</b>: Implementations must be thread-safe.
13+
* Implementations must be thread-safe.
1314
*/
14-
public interface ProcessScope extends AutoCloseable
15+
public interface JvmScope extends Scope
1516
{
1617
/**
17-
* @return the terminal attached to the process
18+
* @return the terminal that the JVM will output to
1819
*/
1920
Terminal getTerminal();
2021

java/src/main/java/com/github/cowwoc/requirements10/java/internal/scope/MainApplicationScope.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
*/
55
package com.github.cowwoc.requirements10.java.internal.scope;
66

7+
import java.util.concurrent.atomic.AtomicBoolean;
8+
9+
import static com.github.cowwoc.requirements10.java.internal.scope.DefaultJvmScope.CLOSE_TIMEOUT;
10+
711
/**
812
* ApplicationScope for the main codebase.
913
*/
@@ -12,21 +16,33 @@ public final class MainApplicationScope extends AbstractApplicationScope
1216
/**
1317
* The singleton instance.
1418
*/
15-
public static final MainApplicationScope INSTANCE = new MainApplicationScope(DefaultProcessScope.INSTANCE);
19+
public static final MainApplicationScope INSTANCE = new MainApplicationScope(DefaultJvmScope.INSTANCE);
20+
private final AtomicBoolean closed = new AtomicBoolean();
1621

1722
/**
1823
* Creates a new application scope.
1924
*
2025
* @param parent the parent scope
2126
* @throws NullPointerException if {@code parent} is null
2227
*/
23-
private MainApplicationScope(ProcessScope parent)
28+
private MainApplicationScope(JvmScope parent)
2429
{
2530
super(parent, new MainGlobalConfiguration(parent.getTerminal()));
31+
parent.addChild(this);
32+
}
33+
34+
@Override
35+
public boolean isClosed()
36+
{
37+
return closed.get();
2638
}
2739

2840
@Override
2941
public void close()
3042
{
43+
if (!closed.compareAndSet(false, true))
44+
return;
45+
parent.removeChild(this);
46+
children.shutdown(CLOSE_TIMEOUT);
3147
}
3248
}

java/src/main/java/com/github/cowwoc/requirements10/java/internal/scope/MainGlobalConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public MainGlobalConfiguration(Terminal terminal)
3030
@Override
3131
public Set<TerminalEncoding> supportedTerminalEncodings()
3232
{
33-
return terminal.getSupportedTypes();
33+
return terminal.getSupportedEncodings();
3434
}
3535

3636
@Override

java/src/main/java/com/github/cowwoc/requirements10/java/internal/terminal/Processes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
final class Processes
1515
{
16-
private static final String NEWLINE = System.getProperty("line.separator");
16+
private static final String NEWLINE = System.lineSeparator();
1717

1818
/**
1919
* Prevent construction.

java/src/main/java/com/github/cowwoc/requirements10/java/internal/terminal/Terminal.java

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@
2121
import static com.github.cowwoc.requirements10.java.TerminalEncoding.XTERM_8_COLORS;
2222

2323
/**
24-
* The terminal associated with the process.
24+
* The terminal that the JVM is outputting to.
2525
*/
2626
public final class Terminal
2727
{
2828
private final AtomicReference<TerminalEncoding> encoding = new AtomicReference<>();
2929
private final Reference<Set<TerminalEncoding>> supportedTypes =
30-
ConcurrentLazyReference.create(this::getSupportedTypesImpl);
31-
private final Reference<Boolean> connectedToStdout =
32-
ConcurrentLazyReference.create(this::isConnectedToStdoutImpl);
30+
ConcurrentLazyReference.create(this::getSupportedEncodingsImpl);
3331

3432
/**
3533
* Creates a new instance.
@@ -41,31 +39,31 @@ public Terminal()
4139
/**
4240
* @return the ANSI escape codes supported by the terminal
4341
*/
44-
public Set<TerminalEncoding> getSupportedTypes()
42+
public Set<TerminalEncoding> getSupportedEncodings()
4543
{
4644
return supportedTypes.getValue();
4745
}
4846

4947
/**
5048
* @return the ANSI escape codes supported by the terminal
5149
*/
52-
private Set<TerminalEncoding> getSupportedTypesImpl()
50+
private Set<TerminalEncoding> getSupportedEncodingsImpl()
5351
{
5452
return switch (OperatingSystem.detected().type)
5553
{
56-
case WINDOWS -> getSupportedTypesForWindows();
57-
case LINUX, MAC -> getSupportedTypesForLinuxOrMac();
54+
case WINDOWS -> getSupportedEncodingsForWindows();
55+
case LINUX, MAC -> getSupportedEncodingsForLinuxOrMac();
5856
};
5957
}
6058

61-
private Set<TerminalEncoding> getSupportedTypesForLinuxOrMac()
59+
private Set<TerminalEncoding> getSupportedEncodingsForLinuxOrMac()
6260
{
6361
String term = System.getenv("TERM");
6462
if (term == null)
6563
return Set.of(NONE);
6664
// Following the approach set out in http://stackoverflow.com/a/39033815/14731, we don't attempt to
67-
// support all possible terminal types. Instead, we support mainstream types and require the terminal
68-
// to support or emulate them.
65+
// support all possible terminal encodings. Instead, we support mainstream encodings and require the
66+
// terminal to support or emulate them.
6967
Set<TerminalEncoding> result = EnumSet.of(NONE);
7068
switch (term)
7169
{
@@ -99,8 +97,9 @@ private Set<TerminalEncoding> getSupportedTypesForLinuxOrMac()
9997
return result;
10098
}
10199

102-
private Set<TerminalEncoding> getSupportedTypesForWindows()
100+
private Set<TerminalEncoding> getSupportedEncodingsForWindows()
103101
{
102+
// WT_SESSION indicates that we are running in Windows Terminal.
104103
if (System.getenv("WT_SESSION") == null)
105104
return Set.of(NONE);
106105
return Set.of(NONE, XTERM_8_COLORS, XTERM_16_COLORS, XTERM_256_COLORS, RGB_888_COLORS);
@@ -126,7 +125,7 @@ private void setEncodingImpl(TerminalEncoding encoding, boolean force)
126125
this.encoding.set(NONE);
127126
return;
128127
}
129-
if (!getSupportedTypes().contains(encoding) && !force)
128+
if (!getSupportedEncodings().contains(encoding) && !force)
130129
{
131130
this.encoding.set(NONE);
132131
return;
@@ -141,10 +140,10 @@ private void setEncodingImpl(TerminalEncoding encoding, boolean force)
141140
*/
142141
public void useBestEncoding()
143142
{
144-
Set<TerminalEncoding> supportedTypes = getSupportedTypes();
145-
List<TerminalEncoding> sortedTypes = new ArrayList<>(supportedTypes);
146-
sortedTypes.sort(TerminalEncoding.sortByDecreasingRank());
147-
setEncodingImpl(sortedTypes.getFirst(), false);
143+
Set<TerminalEncoding> supportedEncodings = getSupportedEncodings();
144+
List<TerminalEncoding> sortedEncodings = new ArrayList<>(supportedEncodings);
145+
sortedEncodings.sort(TerminalEncoding.sortByDecreasingRank());
146+
setEncodingImpl(sortedEncodings.getFirst(), false);
148147
}
149148

150149
/**
@@ -176,21 +175,11 @@ public void setEncoding(TerminalEncoding encoding)
176175
}
177176

178177
/**
179-
* @return true if stdout is connected to the terminal
178+
* @return true if stdout is connected to a terminal
180179
*/
181180
public boolean isConnectedToStdout()
182181
{
183-
return connectedToStdout.getValue();
184-
}
185-
186-
/**
187-
* @return true if stdout is connected to the terminal
188-
*/
189-
private boolean isConnectedToStdoutImpl()
190-
{
191-
// System.console() is not as accurate as the native library in that it returns null when stdin
192-
// is redirected (which we don't care about). We try using System.console() and if we need more
193-
// information, we send a follow-up query to the native library.
182+
// Reminder: System.console() returns null if stdin is redirected
194183
return System.console() != null;
195184
}
196185
}

0 commit comments

Comments
 (0)