package org.glassfish.grizzly.asyncqueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Cacheable;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.localization.LogMessages;
import org.glassfish.grizzly.utils.DebugPoint;
public abstract class AsyncQueueRecord<R> implements Cacheable {
private final static Logger LOGGER = Grizzly.logger(AsyncQueue.class);
protected Connection connection;
protected Object message;
protected CompletionHandler completionHandler;
protected boolean isRecycled = false;
protected DebugPoint recycleTrack;
protected AsyncQueueRecord() {
}
public AsyncQueueRecord(final Connection connection,
final Object message, final CompletionHandler completionHandler) {
set(connection, message, completionHandler);
}
protected final void set(final Connection connection,
final Object message, final CompletionHandler completionHandler) {
checkRecycled();
this.connection = connection;
this.message = message;
this.completionHandler = completionHandler;
}
public Connection getConnection() {
return connection;
}
@SuppressWarnings("unchecked")
public final <T> T getMessage() {
checkRecycled();
return (T) message;
}
public final void setMessage(final Object message) {
checkRecycled();
this.message = message;
}
public abstract R getCurrentResult();
public void notifyFailure(final Throwable e) {
if (completionHandler != null) {
completionHandler.failed(e);
} else {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE,
LogMessages.FINE_GRIZZLY_ASYNCQUEUE_ERROR_NOCALLBACK_ERROR(e));
}
}
}
@SuppressWarnings("unchecked")
public final void notifyIncomplete() {
if (completionHandler != null) {
completionHandler.updated(getCurrentResult());
}
}
protected final void checkRecycled() {
if (Grizzly.isTrackingThreadCache() && isRecycled) {
final DebugPoint track = recycleTrack;
if (track != null) {
throw new IllegalStateException("AsyncReadQueueRecord has been recycled at: " + track);
} else {
throw new IllegalStateException("AsyncReadQueueRecord has been recycled");
}
}
}
}