Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2008-2014 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.grizzly.filterchain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Appendable;
import org.glassfish.grizzly.Appender;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.Closeable;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.Context;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.IOEvent;
import org.glassfish.grizzly.ProcessorExecutor;
import org.glassfish.grizzly.ReadResult;
import org.glassfish.grizzly.ThreadCache;
import org.glassfish.grizzly.WriteResult;
import org.glassfish.grizzly.asyncqueue.MessageCloner;
import org.glassfish.grizzly.attributes.AttributeHolder;
import org.glassfish.grizzly.attributes.AttributeStorage;
import org.glassfish.grizzly.memory.Buffers;
import org.glassfish.grizzly.memory.MemoryManager;
import org.glassfish.grizzly.utils.Holder;
/**
* {@link FilterChain} {@link Context} implementation.
*
* @see Context
* @see FilterChain
*
* @author Alexey Stashok
*/
@SuppressWarnings("deprecation")
public class FilterChainContext implements AttributeStorage {
private static final Logger logger = Grizzly.logger(FilterChainContext.class);
public enum State {
RUNNING, SUSPEND
}
public enum Operation {
NONE, ACCEPT, CONNECT, READ, WRITE, EVENT, CLOSE
}
private static final ThreadCache.CachedTypeIndex CACHE_IDX =
ThreadCache.obtainIndex(FilterChainContext.class, 8);
public static FilterChainContext create(final Connection connection) {
return create(connection, connection);
}
public static FilterChainContext create(final Connection connection,
final Closeable closeable) {
FilterChainContext context = ThreadCache.takeFromCache(CACHE_IDX);
if (context == null) {
context = new FilterChainContext();
}
context.setConnection(connection);
context.setCloseable(closeable);
context.getTransportContext().isBlocking = connection.isBlocking();
return context;
}
public static final int NO_FILTER_INDEX = Integer.MIN_VALUE;
/**
* Cached {@link NextAction} instance for "Invoke action" implementation
*/
private static final NextAction INVOKE_ACTION = new InvokeAction();
/**
* Cached {@link NextAction} instance for "Stop action" implementation
*/
private static final NextAction STOP_ACTION = new StopAction();
/**
* Cached {@link NextAction} instance for "Suspend action" implementation
*/
private static final NextAction SUSPEND_ACTION = new SuspendAction();
/**
* Cached {@link NextAction} instance for "Rerun filter action" implementation
*/
private static final NextAction RERUN_FILTER_ACTION = new RerunFilterAction();
final InternalContextImpl internalContext = new InternalContextImpl(this);
final TransportContext transportFilterContext = new TransportContext();
/**
* Context task state
*/
private volatile State state;
private Operation operation = Operation.NONE;
/**
* {@link CompletionHandler}, which will be notified, when operation will be
* complete. For WRITE it means the data will be written on wire, for other
* operations - the last Filter has finished the processing.
*/
protected CompletionHandler operationCompletionHandler;
private final Runnable contextRunnable;
/**
* Context associated message
*/
private Object message;
/**
* The {@link Closeable} object associated with the filter chain processing
*/
private Closeable closeable;
/**
* Context associated event, if EVENT operation
*/
protected FilterChainEvent event;
/**
* {@link Holder}, which contains address, associated with the
* current {@link org.glassfish.grizzly.IOEvent} processing.
*/
private Holder addressHolder;
NextAction predefinedNextAction;
/**
* Index of the currently executing {@link Filter} in
* the {@link FilterChainContext} list.
*/
private int filterIdx;
private int startIdx;
private int endIdx;
private final StopAction cachedStopAction = new StopAction();
private final InvokeAction cachedInvokeAction = new InvokeAction();
private final List completionListeners =
new ArrayList(2);
private final List copyListeners =
new ArrayList(2);
public FilterChainContext() {
filterIdx = NO_FILTER_INDEX;
contextRunnable = new Runnable() {
@Override
public void run() {
try {
if (state == State.SUSPEND) {
state = State.RUNNING;
}
ProcessorExecutor.execute(FilterChainContext.this.internalContext);
} catch (Exception e) {
logger.log(Level.FINE, "Exception during running Processor", e);
}
}
};
}
/**
* Suspend processing of the current task
*/
public Runnable suspend() {
internalContext.suspend();
this.state = State.SUSPEND;
return getRunnable();
}
/**
* Resume processing of the current task
*/
public void resume() {
internalContext.resume();
getRunnable().run();
}
/**
* Resume processing of the current task starting from the Filter, which
* follows the Filter, which suspend the processing.
*/
public void resumeNext() {
resume(getInvokeAction());
}
/**
* Resume the current FilterChain task processing by completing the current
* {@link Filter} (the Filter, which suspended the processing) with the
* passed {@link NextAction}.
*
* @param nextAction the {@link NextAction}, based on which {@link FilterChain}
* will continue processing.
*/
public void resume(final NextAction nextAction) {
internalContext.resume();
try {
if (state == State.SUSPEND) {
state = State.RUNNING;
}
predefinedNextAction = nextAction;
ProcessorExecutor.execute(internalContext);
} catch (Exception e) {
logger.log(Level.FINE, "Exception during running Processor", e);
}
}
/**
* This method invocation suggests the {@link FilterChain} to create a
* copy of this {@link FilterChainContext} and resume/fork its execution
* starting from the current {@link Filter}.
*/
public void fork() {
fork(null);
}
/**
* This method invocation suggests the {@link FilterChain} to create a
* copy of this {@link FilterChainContext} and resume/fork its execution
* starting from the current {@link Filter}.
*
* If nextAction parameter is not null - then its value is treated
* as a result of the current {@link Filter} execution on the forked
* {@link FilterChain} processing. So during the forked {@link FilterChain}
* processing the current {@link Filter} will not be invoked, but
* the processing will be continued as if the current {@link Filter}
* returned nextAction as a result.
* For example if we call fork(ctx.getInvokeAction()); the forked
* {@link FilterChain} processing will start with the next {@link Filter} in
* chain.
*/
public void fork(final NextAction nextAction) {
try {
predefinedNextAction = getForkAction(nextAction);
ProcessorExecutor.execute(internalContext);
} catch (Exception e) {
logger.log(Level.FINE, "Exception during running Processor", e);
}
}
/**
* Get the current processing task state.
* @return the current processing task state.
*/
public State state() {
return state;
}
public int nextFilterIdx() {
return ++filterIdx;
}
public int previousFilterIdx() {
return --filterIdx;
}
public int getFilterIdx() {
return filterIdx;
}
public void setFilterIdx(int index) {
this.filterIdx = index;
}
public int getStartIdx() {
return startIdx;
}
public void setStartIdx(int startIdx) {
this.startIdx = startIdx;
}
public int getEndIdx() {
return endIdx;
}
public void setEndIdx(int endIdx) {
this.endIdx = endIdx;
}
/**
* Get {@link FilterChain}, which runs the {@link Filter}.
*
* @return {@link FilterChain}, which runs the {@link Filter}.
*/
public FilterChain getFilterChain() {
return (FilterChain) internalContext.getProcessor();
}
/**
* Get the {@link Connection}, associated with the current processing.
*
* @return {@link Connection} object, associated with the current processing.
*/
public Connection getConnection() {
return internalContext.getConnection();
}
/**
* Set the {@link Connection}, associated with the current processing.
*
* @param connection {@link Connection} object, associated with the current processing.
*/
void setConnection(final Connection connection) {
internalContext.setConnection(connection);
}
/**
* Get the {@link Closeable}, associated with the current processing.
* The {@link Closeable} object might be used to close/terminate an entity
* associated with the {@link FilterChain} processing in response to an
* error occurred during the processing.
* If not customized the {@link Closeable} object represents a {@link Connection}
* associated with the {@link FilterChain} processing.
*
* @return {@link Closeable} object, associated with the current processing.
*/
public Closeable getCloseable() {
return closeable;
}
/**
* Set the {@link Closeable}, associated with the current processing.
* The {@link Closeable} object might be used to close/terminate an entity
* associated with the {@link FilterChain} processing in response to an
* error occurred during the processing.
* If not customized the {@link Closeable} object represents a {@link Connection}
* associated with the {@link FilterChain} processing.
*
* @param closeable {@link Closeable} object, associated with the current processing.
* If null the {@link #getConnection()} value will be assigned
*/
void setCloseable(final Closeable closeable) {
this.closeable = closeable != null ? closeable : getConnection();
}
/**
* Get message object, associated with the current processing.
*
* Usually {@link FilterChain} represents sequence of parser and process
* {@link Filter}s. Each parser can change the message representation until
* it will come to processor {@link Filter}.
*
* @return message object, associated with the current processing.
*/
@SuppressWarnings("unchecked")
public T getMessage() {
return (T) message;
}
/**
* Set message object, associated with the current processing.
*
* Usually {@link FilterChain} represents sequence of parser and process
* {@link Filter}s. Each parser can change the message representation until
* it will come to processor {@link Filter}.
*
* @param message message object, associated with the current processing.
*/
public void setMessage(Object message) {
this.message = message;
}
/**
* Get a {@link Holder}, which contains address, associated with the
* current {@link org.glassfish.grizzly.IOEvent} processing.
*
* When we process {@link org.glassfish.grizzly.IOEvent#READ} event - it represents sender address,
* or when process {@link org.glassfish.grizzly.IOEvent#WRITE} - address of receiver.
*
* @return {@link Holder}, which contains address, associated with the
* current {@link org.glassfish.grizzly.IOEvent} processing.
*/
public Holder getAddressHolder() {
return addressHolder;
}
/**
* Set address, associated with the current {@link org.glassfish.grizzly.IOEvent} processing.
* When we process {@link org.glassfish.grizzly.IOEvent#READ} event - it represents sender address,
* or when process {@link org.glassfish.grizzly.IOEvent#WRITE} - address of receiver.
*
* @param addressHolder {@link Holder}, which contains address, associated with the current {@link org.glassfish.grizzly.IOEvent} processing.
*/
public void setAddressHolder(final Holder addressHolder) {
this.addressHolder = addressHolder;
}
/**
* Get address, associated with the current {@link org.glassfish.grizzly.IOEvent} processing.
* When we process {@link org.glassfish.grizzly.IOEvent#READ} event - it represents sender address,
* or when process {@link org.glassfish.grizzly.IOEvent#WRITE} - address of receiver.
*
* @return address, associated with the current {@link org.glassfish.grizzly.IOEvent} processing.
*/
public Object getAddress() {
return addressHolder != null ? addressHolder.get() : null;
}
/**
* Set address, associated with the current {@link org.glassfish.grizzly.IOEvent} processing.
* When we process {@link org.glassfish.grizzly.IOEvent#READ} event - it represents sender address,
* or when process {@link org.glassfish.grizzly.IOEvent#WRITE} - address of receiver.
*
* @param address address, associated with the current {@link org.glassfish.grizzly.IOEvent} processing.
*/
public void setAddress(final Object address) {
addressHolder = Holder.