Skip to content

Commit

Permalink
Improved examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowwoc committed Dec 30, 2024
1 parent 9388259 commit 9242b18
Show file tree
Hide file tree
Showing 28 changed files with 116 additions and 127 deletions.
1 change: 0 additions & 1 deletion core/src/main/java/com/github/cowwoc/pouch/core/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* {@link #removeChild(Scope) parent.removeChild(this)} at the end of their {@link #close()} method.
* <p>
* Example implementation of the {@code close()} method:
* <p>
* <pre>
* {@code
* public void close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public HelloWorldResource(RequestScope scope)
public String getHello()
{
return "Hello world!\n" +
"HTTP-scoped value : " + scope.getRequestedUri() + "\n" +
"Application-scoped value: " + scope.getMode();
"HTTP-scoped value: " + scope.getRequestedUri() + "\n" +
"JVM-scoped value : " + scope.getMode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Duration getScopeCloseTimeout()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Connection getConnection()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Duration getScopeCloseTimeout()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Code common to all JvmScope implementations.
* Values specific to the lifetime of a JVM.
* <p>
* Implementations must be thread-safe.
*/
abstract class AbstractJvmScope extends AbstractScope
public final class DefaultJvmScope extends AbstractScope
implements JvmScope
{
/**
Expand Down Expand Up @@ -58,13 +60,26 @@ protected void disposeValue(ScheduledExecutorService scheduler)
}
}
};
private final Logger log = LoggerFactory.getLogger(AbstractJvmScope.class);
private final RunMode mode;
private final Logger log = LoggerFactory.getLogger(DefaultJvmScope.class);

/**
* Creates a new application scope.
* Creates a new scope.
*
* @param mode the runtime mode of the JVM
* @throws NullPointerException if {@code mode} is null
*/
protected AbstractJvmScope()
public DefaultJvmScope(RunMode mode)
{
if (mode == null)
throw new NullPointerException("mode may not be null");
this.mode = mode;
}

@Override
public RunMode getMode()
{
return mode;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public DataSource getDataSource()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
public interface JvmScope extends Scope
{
/**
* Returns the execution mode (e.g. "main", "test").
* Returns the runtime mode.
*
* @return the execution mode (e.g. "main", "test")
* @return the runtime mode
* @throws IllegalStateException if the scope is closed
*/
String getMode();
RunMode getMode();

/**
* Returns the scheduler to use for background tasks.
*
* @return the scheduler to use for background tasks
* @throws IllegalStateException if the scope is closed
*/
ScheduledExecutorService getScheduler();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static class JvmScopeFactory implements Factory<JvmScope>
@Override
public JvmScope provide()
{
return new MainJvmScope();
return new DefaultJvmScope(RunMode.RELEASE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.cowwoc.pouch.dropwizard.scope;

/**
* Runtime modes.
*/
public enum RunMode
{
/**
* Optimized for debugging problems (extra runtime checks, logging of the program state, disabled web client
* cache).
*/
DEBUG,
/**
* Optimized for maximum performance.
*/
RELEASE
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.github.cowwoc.pouch.dropwizard.database;

import com.github.cowwoc.pouch.dropwizard.scope.DatabaseScope;
import com.github.cowwoc.pouch.dropwizard.scope.DefaultJvmScope;
import com.github.cowwoc.pouch.dropwizard.scope.JvmScope;
import com.github.cowwoc.pouch.dropwizard.scope.RunMode;
import com.github.cowwoc.pouch.dropwizard.scope.TestDatabaseScope;
import com.github.cowwoc.pouch.dropwizard.scope.TestJvmScope;
import com.github.cowwoc.pouch.dropwizard.scope.TransactionScope;
import org.junit.jupiter.api.Test;

Expand All @@ -15,7 +16,7 @@ public final class TestDatabase
@Test
public void test1() throws SQLException
{
try (JvmScope jvmScope = new TestJvmScope();
try (JvmScope jvmScope = new DefaultJvmScope(RunMode.DEBUG);
DatabaseScope databaseScope = new TestDatabaseScope(jvmScope);
TransactionScope transaction = databaseScope.createTransactionScope())
{
Expand All @@ -27,7 +28,7 @@ public void test1() throws SQLException
@Test
public void test2() throws SQLException
{
try (JvmScope jvmScope = new TestJvmScope();
try (JvmScope jvmScope = new DefaultJvmScope(RunMode.DEBUG);
DatabaseScope databaseScope = new TestDatabaseScope(jvmScope);
TransactionScope transaction = databaseScope.createTransactionScope())
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private static class JvmScopeFactory implements Factory<JvmScope>
@Override
public JvmScope provide()
{
return new TestJvmScope();
return new DefaultJvmScope(RunMode.DEBUG);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions jersey/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public final class HelloWorldResource
public String getHello()
{
return "Hello world!\n" +
"HTTP-scoped value : " + scope.getRequestedUri() + "\n" +
"Application-scoped value: " + scope.getMode();
"HTTP-scoped value: " + scope.getRequestedUri() + "\n" +
"JVM-scoped value : " + scope.getMode();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public HelloWorldResource(RequestScope scope)
public String getHello()
{
return "Hello world!\n" +
"HTTP-scoped value : " + scope.getRequestedUri() + "\n" +
"Application-scoped value: " + scope.getMode();
"HTTP-scoped value: " + scope.getRequestedUri() + "\n" +
"JVM-scoped value : " + scope.getMode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Duration getScopeCloseTimeout()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand All @@ -50,7 +50,7 @@ public ScheduledExecutorService getScheduler()
{
return parent.getScheduler();
}

@Override
public Connection getConnection()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Connection getConnection()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Duration getScopeCloseTimeout()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
import java.util.concurrent.atomic.LongAdder;

/**
* Code common to all JvmScope implementations.
* The default implementation of JvmScope.
* <p>
* This class ensures that globals, such as slf4j, are initialized in a thread-safe manner.
*/
public abstract class AbstractJvmScope extends AbstractScope
public final class DefaultJvmScope extends AbstractScope
implements JvmScope
{
/**
* The maximum amount of time to wait for child scopes to close.
*/
private static final Duration CLOSE_TIMEOUT = Duration.ofSeconds(10);
/**
* {@code true} if the scope has been closed.
*/
private final AtomicBoolean closed = new AtomicBoolean();
private final RunMode mode;
private final Factory<ScheduledExecutorService> schedulerFactory = new ConcurrentLazyFactory<>()
{
@Override
Expand Down Expand Up @@ -70,13 +69,29 @@ protected void disposeValue(ScheduledExecutorService scheduler)
}
}
};
private final Logger log = LoggerFactory.getLogger(AbstractJvmScope.class);
/**
* {@code true} if the scope has been closed.
*/
private final AtomicBoolean closed = new AtomicBoolean();
private final Logger log = LoggerFactory.getLogger(DefaultJvmScope.class);

/**
* Creates a new JVM scope.
* Creates a new scope.
*
* @param mode the runtime mode of the JVM
* @throws NullPointerException if {@code mode} is null
*/
public AbstractJvmScope()
public DefaultJvmScope(RunMode mode)
{
if (mode == null)
throw new NullPointerException("mode may not be null");
this.mode = mode;
}

@Override
public RunMode getMode()
{
return mode;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public DataSource getDataSource()
}

@Override
public String getMode()
public RunMode getMode()
{
return parent.getMode();
}
Expand All @@ -102,7 +102,7 @@ public Connection getConnection()
{
return connection.getValue();
}

@Override
public boolean isClosed()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
public interface JvmScope extends Scope
{
/**
* Returns the execution mode (e.g. "main", "test").
* Returns the runtime mode
*
* @return the execution mode (e.g. "main", "test")
* @return the runtime mode
* @throws IllegalStateException if the scope is closed
*/
String getMode();
RunMode getMode();

/**
* @return the amount of time to wait for scopes to close
Expand All @@ -33,6 +34,7 @@ public interface JvmScope extends Scope
* Returns the scheduler to use for background tasks.
*
* @return the scheduler to use for background tasks
* @throws IllegalStateException if the scope is closed
*/
ScheduledExecutorService getScheduler();
}
Loading

0 comments on commit 9242b18

Please sign in to comment.