Skip to content

Commit

Permalink
[JBTHR-46] I18n log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Nov 6, 2017
1 parent 70ae42c commit 197346e
Show file tree
Hide file tree
Showing 20 changed files with 359 additions and 112 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
<targetPath>META-INF</targetPath>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jboss/threads/ArrayQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public Iterator<E> iterator() {

public boolean hasNext() {
if (modCnt != modIdx) {
throw new ConcurrentModificationException();
throw Messages.msg.concurrentModification();
}
return pos < size;
}

public E next() {
if (modCnt != modIdx) {
throw new ConcurrentModificationException();
throw Messages.msg.concurrentModification();
}
final int pos = this.pos;
if (pos >= size) {
throw new NoSuchElementException();
throw Messages.msg.noSuchElement();
}
final E[] elements = ArrayQueue.this.elements;
final E value = elements[(tail + pos) % elements.length];
Expand All @@ -71,7 +71,7 @@ public E next() {
}

public void remove() {
throw new UnsupportedOperationException();
throw Assert.unsupported();
}
};
}
Expand Down
68 changes: 34 additions & 34 deletions src/main/java/org/jboss/threads/AsyncFutureTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.wildfly.common.Assert;

/**
* A base class for implementing asynchronous tasks. This class implements
* {@link java.util.concurrent.Future Future} as well as {@link AsyncFuture}, and
Expand Down Expand Up @@ -58,22 +60,6 @@ public void run() {
}
}

private static TimeoutException operationTimedOut() {
return new TimeoutException("Operation timed out");
}

private static CancellationException operationCancelled() {
return new CancellationException("Operation was cancelled");
}

private static ExecutionException operationFailed(final Throwable cause) {
return new ExecutionException("Operation failed", cause);
}

private static IllegalStateException invalidState() {
return new IllegalStateException("Invalid state entered");
}

/**
* Construct a new instance.
*
Expand Down Expand Up @@ -259,11 +245,14 @@ public final Status awaitUninterruptibly(final long timeout, final TimeUnit unit
@SuppressWarnings({ "unchecked" })
public final T get() throws InterruptedException, ExecutionException {
synchronized (AsyncFutureTask.this) {
switch (await()) {
case CANCELLED: throw operationCancelled();
case FAILED: throw operationFailed((Throwable) result);
final Status status = await();
switch (status) {
case CANCELLED:
throw Messages.msg.operationCancelled();
case FAILED:
throw Messages.msg.operationFailed((Throwable) result);
case COMPLETE: return (T) result;
default: throw invalidState();
default: throw Assert.impossibleSwitchCase(status);
}
}
}
Expand All @@ -272,12 +261,16 @@ public final T get() throws InterruptedException, ExecutionException {
@SuppressWarnings({ "unchecked" })
public final T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
synchronized (AsyncFutureTask.this) {
switch (await(timeout, unit)) {
case CANCELLED: throw operationCancelled();
case FAILED: throw operationFailed((Throwable) result);
final Status status = await(timeout, unit);
switch (status) {
case CANCELLED:
throw Messages.msg.operationCancelled();
case FAILED:
throw Messages.msg.operationFailed((Throwable) result);
case COMPLETE: return (T) result;
case WAITING: throw operationTimedOut();
default: throw invalidState();
case WAITING:
throw Messages.msg.operationTimedOut();
default: throw Assert.impossibleSwitchCase(status);
}
}
}
Expand All @@ -286,11 +279,14 @@ public final T get(final long timeout, final TimeUnit unit) throws InterruptedEx
@SuppressWarnings({ "unchecked" })
public final T getUninterruptibly() throws CancellationException, ExecutionException {
synchronized (AsyncFutureTask.this) {
switch (awaitUninterruptibly()) {
case CANCELLED: throw operationCancelled();
case FAILED: throw operationFailed((Throwable) result);
final Status status = awaitUninterruptibly();
switch (status) {
case CANCELLED:
throw Messages.msg.operationCancelled();
case FAILED:
throw Messages.msg.operationFailed((Throwable) result);
case COMPLETE: return (T) result;
default: throw invalidState();
default: throw Assert.impossibleSwitchCase(status);
}
}
}
Expand All @@ -299,12 +295,16 @@ public final T getUninterruptibly() throws CancellationException, ExecutionExcep
@SuppressWarnings({ "unchecked" })
public final T getUninterruptibly(final long timeout, final TimeUnit unit) throws CancellationException, ExecutionException, TimeoutException {
synchronized (AsyncFutureTask.this) {
switch (awaitUninterruptibly(timeout, unit)) {
case CANCELLED: throw operationCancelled();
case FAILED: throw operationFailed((Throwable) result);
final Status status = awaitUninterruptibly(timeout, unit);
switch (status) {
case CANCELLED:
throw Messages.msg.operationCancelled();
case FAILED:
throw Messages.msg.operationFailed((Throwable) result);
case COMPLETE: return (T) result;
case WAITING: throw operationTimedOut();
default: throw invalidState();
case WAITING:
throw Messages.msg.operationTimedOut();
default: throw Assert.impossibleSwitchCase(status);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/threads/AtomicArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static <T, V> AtomicArray<T, V> create(AtomicReferenceFieldUpdater<T, V[]
Assert.checkNotNullParam("updater", updater);
Assert.checkNotNullParam("emptyArray", emptyArray);
if (emptyArray.length > 0) {
throw new IllegalArgumentException("Empty array parameter is not empty");
throw Messages.msg.arrayNotEmpty();
}
return new AtomicArray<>(updater, emptyArray);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/threads/BalancingExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void execute(final Runnable command) throws RejectedExecutionException {
final Executor[] executors = this.executors;
final int len = executors.length;
if (len == 0) {
throw new RejectedExecutionException("No executors available to run task");
throw Messages.msg.noExecutorsAvailable();
}
executors[ThreadLocalRandom.current().nextInt(len)].execute(command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public boolean awaitTermination(final long timeout, final TimeUnit unit) throws
}

public void shutdown() {
throw new SecurityException("shutdown() not allowed on container-managed executor");
throw Messages.msg.notAllowedContainerManaged("shutdown");
}

public List<Runnable> shutdownNow() {
throw new SecurityException("shutdownNow() not allowed on container-managed executor");
throw Messages.msg.notAllowedContainerManaged("shutdownNow");
}

public void executeBlocking(final Runnable task) throws RejectedExecutionException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public boolean awaitTermination(final long timeout, final TimeUnit unit) throws
}

public void shutdown() {
throw new SecurityException("shutdown() not allowed on container-managed executor");
throw Messages.msg.notAllowedContainerManaged("shutdown");
}

public List<Runnable> shutdownNow() {
throw new SecurityException("shutdownNow() not allowed on container-managed executor");
throw Messages.msg.notAllowedContainerManaged("shutdownNow");
}

public static ExecutorService directExecutorService() {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/jboss/threads/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import org.jboss.logging.Logger;
import org.wildfly.common.Assert;

/**
* A task which depends on other tasks, and which may have tasks depending upon it. Such a task is automatically
* run when using a provided executor when all its dependencies are satisfied.
*/
public final class Dependency {
private static final Logger log = Logger.getLogger(Dependency.class);

private static final AtomicIntegerFieldUpdater<Dependency> depUpdater = AtomicIntegerFieldUpdater.newUpdater(Dependency.class, "remainingDependencies");

Expand Down Expand Up @@ -61,7 +60,7 @@ void addDependent(Dependency task) {
case RUNNING: runner.dependents.add(task); return;
case FAILED: return;
case DONE: break; // fall out of lock
default: throw new IllegalStateException();
default: throw Assert.impossibleSwitchCase(state);
}
}
task.dependencyFinished();
Expand All @@ -77,7 +76,7 @@ void dependencyFinished() {
executor.execute(runner);
state = State.RUNNING;
} catch (RejectedExecutionException e) {
log.errorf(e, "Error submitting task %s to executor", runner.runnable);
Messages.msg.taskSubmitFailed(e, runner.runnable);
state = State.FAILED;
// clear stuff out since this object will likely be kept alive longer than these objects need to be
runner.runnable = null;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jboss/threads/EnhancedQueueExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
*/
@Contended
public final class EnhancedQueueExecutor extends AbstractExecutorService implements ManageableThreadPoolExecutorService {
static {
Version.getVersionString();
}

/*
┌──────────────────────────┐
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/threads/JBossExecutors.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ public static <R extends Runnable, A> void run(R task, DirectExecutor directExec
throw t;
} catch (Throwable t) {
failed(notifier, t, task, attachment);
throw new RuntimeException("Unknown throwable received", t);
throw Messages.msg.unknownThrowable(t);
} finally {
if (ok) finished(notifier, task, attachment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public RejectedExecutionHandler getDelegate() {
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
rejectCount.incrementAndGet();
if (isShutdown()) {
throw new StoppedExecutorException();
throw Messages.msg.shutDownInitiated();
}
delegate.rejectedExecution(r, executor);
}
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/org/jboss/threads/JBossThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

import org.jboss.logging.Logger;
import org.wildfly.common.Assert;
import org.wildfly.common.function.ExceptionBiConsumer;
import org.wildfly.common.function.ExceptionBiFunction;
Expand All @@ -39,8 +38,10 @@
* A JBoss thread. Supports logging and extra operations.
*/
public class JBossThread extends Thread {
private static final Logger log = Logger.getLogger("org.jboss.threads");
private static final Logger ihlog = Logger.getLogger("org.jboss.threads.interrupt-handler");

static {
Version.getVersionString();
}

private volatile InterruptHandler interruptHandler;
private ThreadNameInfo threadNameInfo;
Expand Down Expand Up @@ -150,7 +151,7 @@ public void interrupt() {
oldVal = stateRef.get();
if (oldVal == STATE_INTERRUPT_PENDING || oldVal == STATE_INTERRUPT_IN_PROGRESS) {
// already set
ihlog.tracef("Interrupting thread \"%s\" (already interrupted)", this);
Messages.msg.tracef("Interrupting thread \"%s\" (already interrupted)", this);
return;
} else if (oldVal == STATE_INTERRUPT_DEFERRED) {
newVal = STATE_INTERRUPT_PENDING;
Expand All @@ -169,13 +170,13 @@ public void interrupt() {
LockSupport.unpark(this);
}
} else {
ihlog.tracef("Interrupting thread \"%s\" (deferred)", this);
Messages.intMsg.tracef("Interrupting thread \"%s\" (deferred)", this);
}
}

private void doInterrupt() {
if (isInterrupted()) return;
ihlog.tracef("Interrupting thread \"%s\"", this);
Messages.msg.tracef("Interrupting thread \"%s\"", this);
try {
super.interrupt();
} finally {
Expand All @@ -184,7 +185,7 @@ private void doInterrupt() {
try {
interruptHandler.handleInterrupt(this);
} catch (Throwable t) {
ihlog.errorf(t, "Interrupt handler %s threw an exception", interruptHandler);
Messages.msg.interruptHandlerThrew(t, interruptHandler);
}
}
}
Expand Down Expand Up @@ -478,11 +479,11 @@ private static boolean registerDeferral(final JBossThread thread) {
* Execute the thread's {@code Runnable}. Logs a trace message at the start and end of execution.
*/
public void run() {
log.tracef("Thread \"%s\" starting execution", this);
Messages.msg.tracef("Thread \"%s\" starting execution", this);
try {
super.run();
} finally {
log.tracef("Thread \"%s\" exiting", this);
Messages.msg.tracef("Thread \"%s\" exiting", this);
}
}

Expand All @@ -503,7 +504,7 @@ public static JBossThread currentThread() {
*/
public void start() {
super.start();
log.tracef("Started thread \"%s\"", this);
Messages.msg.tracef("Started thread \"%s\"", this);
}

/**
Expand All @@ -513,7 +514,7 @@ public void start() {
*/
public void setUncaughtExceptionHandler(final UncaughtExceptionHandler eh) {
super.setUncaughtExceptionHandler(eh);
log.tracef("Changed uncaught exception handler for \"%s\" to %s", this, eh);
Messages.msg.tracef("Changed uncaught exception handler for \"%s\" to %s", this, eh);
}

/**
Expand All @@ -534,7 +535,7 @@ public void setUncaughtExceptionHandler(final UncaughtExceptionHandler eh) {
public static InterruptHandler getAndSetInterruptHandler(final InterruptHandler newInterruptHandler) {
final JBossThread thread = currentThread();
if (thread == null) {
throw new IllegalStateException("The current thread does not support interrupt handlers");
throw Messages.msg.noInterruptHandlers();
}
try {
return thread.interruptHandler;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jboss/threads/JBossThreadPoolExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.atomic.AtomicInteger;
import org.jboss.threads.management.BoundedQueueThreadPoolExecutorMBean;
import org.wildfly.common.Assert;

/**
* @deprecated Use {@link EnhancedQueueExecutor} instead.
Expand Down Expand Up @@ -126,7 +127,7 @@ public boolean isBlocking() {
}

public void setBlocking(final boolean blocking) {
throw new UnsupportedOperationException();
throw Assert.unsupported();
}

public RejectedExecutionHandler getRejectedExecutionHandler() {
Expand Down Expand Up @@ -160,7 +161,7 @@ public RejectedExecutionHandler getDelegate() {
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
rejectCount.incrementAndGet();
if (isShutdown()) {
throw new StoppedExecutorException();
throw Messages.msg.shutDownInitiated();
}
delegate.rejectedExecution(r, executor);
}
Expand Down
Loading

0 comments on commit 197346e

Please sign in to comment.