package org.glassfish.grizzly.strategies;
import java.io.IOException;
import java.util.concurrent.Executor;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.IOEvent;
import org.glassfish.grizzly.IOEventLifeCycleListener;
import org.glassfish.grizzly.Processor;
import java.util.logging.Logger;
public final class WorkerThreadIOStrategy extends AbstractIOStrategy {
private static final WorkerThreadIOStrategy INSTANCE = new WorkerThreadIOStrategy();
private static final Logger logger = Grizzly.logger(WorkerThreadIOStrategy.class);
private WorkerThreadIOStrategy() { }
public static WorkerThreadIOStrategy getInstance() {
return INSTANCE;
}
@Override
public boolean executeIoEvent(final Connection connection,
final IOEvent ioEvent, final boolean isIoEventEnabled)
throws IOException {
final boolean isReadOrWriteEvent = isReadWrite(ioEvent);
final IOEventLifeCycleListener listener;
if (isReadOrWriteEvent) {
if (isIoEventEnabled) {
connection.disableIOEvent(ioEvent);
}
listener = ENABLE_INTEREST_LIFECYCLE_LISTENER;
} else {
listener = null;
}
final Executor threadPool = getThreadPoolFor(connection, ioEvent);
if (threadPool != null) {
threadPool.execute(
new WorkerThreadRunnable(connection, ioEvent, listener));
} else {
run0(connection, ioEvent, listener);
}
return true;
}
private static void run0(final Connection connection,
final IOEvent ioEvent,
final IOEventLifeCycleListener lifeCycleListener) {
fireIOEvent(connection, ioEvent, lifeCycleListener, logger);
}
private static final class WorkerThreadRunnable implements Runnable {
final Connection connection;
final IOEvent ioEvent;
final IOEventLifeCycleListener lifeCycleListener;
private WorkerThreadRunnable(final Connection connection,
final IOEvent ioEvent,
final IOEventLifeCycleListener lifeCycleListener) {
this.connection = connection;
this.ioEvent = ioEvent;
this.lifeCycleListener = lifeCycleListener;
}
@Override
public void run() {
run0(connection, ioEvent, lifeCycleListener);
}
}
}