com.sun.enterprise.web.connector.grizzly.TaskBase Maven / Gradle / Ivy
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-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 com.sun.enterprise.web.connector.grizzly;
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 java.util.logging.Logger;
import org.apache.coyote.RequestGroupInfo;
/**
* Abstract implementation of a Task
object.
*
* @author Jean-Francois Arcand
*/
public abstract class TaskBase implements Task, TaskListener {
private static final Logger LOGGER =
Logger.getLogger(TaskBase.class.getName());
private static final Level LOG_LEVEL = Level.FINEST;
/**
* This number represent a specific implementation of a Task
* instance.
*/
protected int type;
/**
* List of listeners
*/
protected ArrayList listeners;
/**
* The Pipeline
object associated with this
* Task
*/
protected Pipeline pipeline;
/**
* The SelectionKey
used by this task.
*/
protected SelectionKey key;
/**
* Recycle this task
*/
protected boolean recycle = true;
/**
* The SelectorThread
who created this task.
*/
protected SelectorThread selectorThread;
// ------------------------------------------------------------------//
public int getType(){
return type;
}
/**
* Set the SelectorThread
object.
*/
public void setSelectorThread(SelectorThread selectorThread){
this.selectorThread = selectorThread;
}
/**
* Return the 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 SelectionKey
*/
public void setSelectionKey(SelectionKey key){
this.key = key;
}
/**
* Return the SelectionKey
associated with this task.
*/
public SelectionKey getSelectionKey(){
return key;
}
/**
* Gets the 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 Pipeline
. If the
* Pipeline
is null, then execute the task on using the
* calling thread.
*/
public void execute() {
if (LOGGER.isLoggable(LOG_LEVEL)) {
LOGGER.log(LOG_LEVEL, "TaskBase.execute tb={0}", new Object[]{this});
}
if (pipeline != null) {
pipeline.addTask(this);
} else {
run();
}
}
//------------------------------------------------------Task Listener ----//
private void initListener(){
if ( listeners == null ){
listeners = new ArrayList();
}
}
/**
* Add the given TaskListener
to this Task
.
*/
public void addTaskListener(TaskListener task){
initListener();
listeners.add(task);
}
/**
* Remove the given TaskListener/code> from this
* Task
.
*/
public void removeTaskListener(TaskListener task){
if (listeners == null) return;
listeners.remove(task);
}
/**
* Clean all the listeners of this 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 Task
.
*
* @return ArrayList containing all TaskListener
* instances registered with this Task
*/
public ArrayList getTaskListeners(){
initListener();
return listeners;
}
/**
* Some Pipeline
implementation requires a instance of
* Runnable
instance.
*/
public void run(){
try{
doTask();
} catch (IOException ex){
throw new RuntimeException(ex);
}
}
/**
* Declare whether this Task
is recyclable. If so, this
* Task
will be recycled after every invocation of
* doTask()
.
*/
public void setRecycle(boolean recycle){
this.recycle = recycle;
}
/**
* Return true
if this Task
is recyclable.
*/
public boolean getRecycle(){
return recycle;
}
/**
* Return the current Socket
used by this instance
* @return socket the current Socket
used by this instance
*/
public Socket getSocket(){
return null;
}
/**
* Return the underlying 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{
return null;
}
public void taskEvent(TaskEvent event){
// Do nothing
}
}