Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 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;
 
 
Abstract implementation of a Task object.

Author(s):
Jean-Francois Arcand
 
 public abstract class TaskBase implements TaskTaskListener{

    
    
This number represent a specific implementation of a Task instance.
 
     protected int type;
    

    
List of listeners
 
     protected ArrayList<TaskListenerlisteners;
      
    
    
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 ;
    }

    
    
Set the SelectorThread object.
    public void setSelectorThread(SelectorThread selectorThread){
        this. = selectorThread;
    }
    
    
    
Return the SelectorThread
        return ;
    }
    
    
    
Set the pipeline on which Worker Threads will synchronize.
    public void setPipeline(Pipeline pipeline){
        this. = pipeline;
    }
    
    
    
Return the pipeline used by this object.
    public Pipeline getPipeline(){
        return ;
    }
   

    
Set the SelectionKey
    public void setSelectionKey(SelectionKey key){
        this. = key;
    }


    
Return the SelectionKey associated with this task.
    public SelectionKey getSelectionKey(){
        return ;
    }


    
Gets the RequestGroupInfo from this task.
    
        return ( != null?
                .getRequestGroupInfo() : null);
    }


    
Returns true if monitoring has been enabled, false otherwise.
    public boolean isMonitoringEnabled(){
        return ( != null ?
                .isMonitoringEnabled() : false);
    }


    
Gets the KeepAliveStats associated with this task.
    public KeepAliveStats getKeepAliveStats() {
        return ( != null?
                .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 ( != null){
            .addTask(this);
        } else {
            run();
        }
    }
    
    
    //------------------------------------------------------Task Listener ----//
    private void initListener(){
        if (  == null ){
             = new ArrayList<TaskListener>();
        }
    }
    
    
    
Add the given TaskListener to this Task.
    public void addTaskListener(TaskListener task){
        initListener();
        .add(task);
    }

    
    
Remove the given TaskListener/code> from this Task.
    public void removeTaskListener(TaskListener task){
        if ( == nullreturn;
        .remove(task);
    }
    
    
    
Clean all the listeners of this Task
    public void clearTaskListeners(){
        if ( == nullreturn;
        .clear();
    }
    
    
    
Notify listeners.
    protected void fireTaskEvent(TaskEvent<?> event){
        if ( == nullreturn;        
        for (int i=0; i < .size(); i++){
            .get(i).taskEvent(event);
        }
    }
    
    
    
Recycle internal state.
    public void recycle(){
    }
    
    
    
Return all listeners of this Task.

Returns:
ArrayList containing all TaskListener instances registered with this Task
    public ArrayList getTaskListeners(){
        initListener();
        return ;
    }
    

    
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;
    }
   
    
    
Return true if this Task is recyclable.
    public boolean getRecycle(){
        return ;
    }


    
Return the current Socket used by this instance

Returns:
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 (  == null ) {
            return getSocket().getChannel();
        } else {
            return (SocketChannel).channel();
        }
    }
     

    
Cancel the task.

Parameters:
message the HTTP message to included within the html page
code The http code to use. If null, automatically close the connection without sending an error page.
    public void cancelTask(String messageString code){
        SocketChannel channel = getChannel(); 
        if (code != null) {
            SelectorThread.logger().log(.,message);
            try {
                ByteBuffer byteBuffer = HtmlHelper.getErrorPage(messagecode);
                OutputWriter.flushChannel(channel,byteBuffer);
            } catch (IOException ex){
                SelectorThread.logger().log(.,"CancelTask failed"ex);
            }
        }
        
        if ( .isEnableNioLogging() ){
            SelectorThread.logger().log(."Cancelling SocketChannel " 
                    + getChannel());
        }
        
        if (  != null){
            .cancelKey();
        } 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
    }
 
New to GrepCode? Check out our FAQ X