com.sun.grizzly.http.TaskBase Maven / Gradle / Ivy
/*
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2007-2008 Sun Microsystems, Inc. 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.html
* or glassfish/bootstrap/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 glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [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 com.sun.grizzly.http;
import com.sun.grizzly.Pipeline;
import com.sun.grizzly.PipelineFullException;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.logging.Level;
import com.sun.grizzly.tcp.RequestGroupInfo;
import com.sun.grizzly.util.OutputWriter;
/**
* Abstract implementation of a {@link Task} object.
*
* @author Jean-Francois Arcand
*/
public abstract class TaskBase implements Task, TaskListener{
/**
* This number represent a specific implementation of a {@link Task}
* instance.
*/
protected int type;
/**
* List of listeners
*/
protected ArrayList listeners;
/**
* The {@link Pipeline} object associated with this
* {@link Task}
*/
protected Pipeline pipeline;
/**
* The {@link SelectionKey} used by this task.
*/
protected SelectionKey key;
/**
* Recycle this task
*/
protected boolean recycle = true;
/**
* The {@link SelectorThread} who created this task.
*/
protected SelectorThread selectorThread;
// ------------------------------------------------------------------//
public int getType(){
return type;
}
/**
* Set the {@link SelectorThread} object.
*/
public void setSelectorThread(SelectorThread selectorThread){
this.selectorThread = selectorThread;
}
/**
* Return the {@link SelectorThread}
*/
public SelectorThread getSelectorThread(){
return selectorThread;
}
/**
* Set the pipeline on which Worker Threads will synchronize.
*/
public void setPipeline(Pipeline pipeline){
this.pipeline = pipeline;
}
/**
* Return the pipeline used by this object.
*/
public Pipeline getPipeline(){
return pipeline;
}
/**
* Set the {@link SelectionKey}
*/
public void setSelectionKey(SelectionKey key){
this.key = key;
}
/**
* Return the {@link SelectionKey} associated with this task.
*/
public SelectionKey getSelectionKey(){
return key;
}
/**
* Gets the {@link RequestGroupInfo} from this task.
*/
public RequestGroupInfo getRequestGroupInfo() {
return (selectorThread != null?
selectorThread.getRequestGroupInfo() : null);
}
/**
* Returns true if monitoring has been enabled, false
* otherwise.
*/
public boolean isMonitoringEnabled(){
return (selectorThread != null ?
selectorThread.isMonitoringEnabled() : false);
}
/**
* Gets the KeepAliveStats
associated with this task.
*/
public KeepAliveStats getKeepAliveStats() {
return (selectorThread != null?
selectorThread.getKeepAliveStats() : null);
}
/**
* Execute the task based on its {@link Pipeline}. If the
* {@link Pipeline} is null, then execute the task on using the
* calling thread.
*/
public void execute(){
if (pipeline != null){
try{
pipeline.execute(this);
} catch (PipelineFullException ex){
throw new RuntimeException(ex);
}
} else {
run();
}
}
//------------------------------------------------------Task Listener ----//
private void initListener(){
if ( listeners == null ){
listeners = new ArrayList();
}
}
/**
* Add the given {@link TaskListener} to this {@link Task}.
*/
public void addTaskListener(TaskListener task){
initListener();
listeners.add(task);
}
/**
* Remove the given TaskListener/code> from this
* {@link Task}.
*/
public void removeTaskListener(TaskListener task){
if (listeners == null) return;
listeners.remove(task);
}
/**
* Clean all the listeners of this {@link Task}
*/
public void clearTaskListeners(){
if (listeners == null) return;
listeners.clear();
}
/**
* Notify listeners.
*/
protected void fireTaskEvent(TaskEvent event){
if (listeners == null) return;
for (int i=0; i < listeners.size(); i++){
listeners.get(i).taskEvent(event);
}
}
/**
* Recycle internal state.
*/
public void recycle(){
}
/**
* Return all listeners of this {@link Task}.
*
* @return ArrayList containing all {@link TaskListener}
* instances registered with this {@link Task}
*/
public ArrayList getTaskListeners(){
initListener();
return listeners;
}
/**
* Some {@link Pipeline} implementation requires a instance of
* Runnable
instance.
*/
public void run(){
try{
doTask();
} catch (IOException ex){
throw new RuntimeException(ex);
}
}
/**
* Declare whether this {@link Task} is recyclable. If so, this
* {@link Task} will be recycled after every invocation of
* doTask()
.
*/
public void setRecycle(boolean recycle){
this.recycle = recycle;
}
/**
* Return true if this {@link Task} is recyclable.
*/
public boolean getRecycle(){
return recycle;
}
/**
* Return the current {@link Socket} used by this instance
* @return socket the current {@link Socket} used by this instance
*/
public Socket getSocket(){
return null;
}
/**
* Return the underlying {@link Channel}, independent of the NIO
* mode we are using.
*/
private SocketChannel getChannel(){
if ( key == null ) {
return getSocket().getChannel();
} else {
return (SocketChannel)key.channel();
}
}
/**
* Cancel the task.
* @param message the HTTP message to included within the html page
* @param code The http code to use. If null, automatically close the
* connection without sending an error page.
*/
public void cancelTask(String message, String code){
SocketChannel channel = getChannel();
if (code != null) {
SelectorThread.logger().log(Level.WARNING,message);
try {
ByteBuffer byteBuffer = HtmlHelper.getErrorPage(message, code);
OutputWriter.flushChannel(channel,byteBuffer);
} catch (IOException ex){
SelectorThread.logger().log(Level.FINE,"CancelTask failed", ex);
}
}
if ( selectorThread.isEnableNioLogging() ){
SelectorThread.logger().log(Level.INFO, "Cancelling SocketChannel "
+ getChannel());
}
if ( key != null){
selectorThread.cancelKey(key);
} else if ( getSocket() != null ){
try{
getSocket().close();
} catch (IOException ex){
}
}
}
/**
* By default, do nothing when a Callable
is invoked.
*/
public Object call() throws Exception{
doTask();
return null;
}
/**
* Base implementation.
*/
public void taskEvent(TaskEvent event){
// Do nothing
}
}