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.async;
 
Default implementation of the AsyncExecutor. This class will execute a ProcessorTask asynchronously, by interrupting the process based on the logic defined in its associated AsyncFilter If no AsyncFilter are defined, the ProcessorTask will not be interrupted and executed synchronously.

Author(s):
Jeanfrancois Arcand
 
 public class DefaultAsyncExecutor implements AsyncExecutor{
 
     private final static String ASYNC_FILTER = 
             "com.sun.enterprise.web.connector.grizzly.asyncFilters";

    
    
The AsyncTask used to wrap the ProcessorTask
 
     private AsyncTask asyncProcessorTask;
       
    
    
The associated ProcessorTask
 
     private ProcessorTask processorTask;
    
    
    
The AsyncFilter to execute asynchronous operations on a ProcessorTask.
 
     private static String[] sharedAsyncFilters = null;;
    
    
    
The AsyncFilter to execute asynchronous operations on a ProcessorTask.
 
     private ArrayList<AsyncFilterasyncFilters = 
             new ArrayList<AsyncFilter>();
    
    
    
Do we need to invoke filters?
 
    private boolean invokeFilter = true;

    
    
Loads filters implementation.
    static {
        loadFilters();
    }
    
    
    
The AsyncHandler associated with this object.
    private AsyncHandler asyncHandler;

    
Only one execution of every steps are allowed.
    private AtomicBoolean parseHeaderPhase = new AtomicBoolean(false);
    private AtomicBoolean executeAdapterPhase = new AtomicBoolean(false);
    private AtomicBoolean commitResponsePhase = new AtomicBoolean(false);
    
    // --------------------------------------------------------------------- //
    
    public DefaultAsyncExecutor(){
        init();
    }
    
    
    private void init(){
        if ( != null){
            for (String filterName){
                .add(loadInstance(filterName));
            }
        }
    }
    
    // ------------------------------------------------Asynchrounous Execution --/
    
    
Pre-execute a ProcessorTask by parsing the request line.
    public boolean preExecute() throws Exception{
        if (!.getAndSet(true)){
            if (  == null ){
                throw new IllegalStateException("Null ProcessorTask");
            }
            .preProcess();
            .parseRequest();
            return true;
        }
        return false;
    }
    
    
    
Interrupt the ProcessorTask if AsyncFilter has been defined.

Returns:
true if the execution can continue, false if delayed.
    public boolean interrupt() throws Exception{
        if (.isError()) return true;  // if error was detected on prev phase - skip execution
        if (  == null || .size() == 0 ) {
            execute();
            return false;
        } else {
            return invokeFilters();
        }
    }
    
    
    
Interrupt the ProcessorTask if AsyncFilter has been defined.

Returns:
true if the execution can continue, false if delayed.
    public boolean execute() throws Exception{
        if (!.getAndSet(true)){
            .invokeAdapter();
            return true;
        }
        return false;
    }

    
    
Invoke the AsyncFilter
    private boolean invokeFilters(){
        boolean continueExec = true;
        for (AsyncFilter asf){
            continueExec = asf.doFilter(this);
            if ( !continueExec ){
                break;
            }
        }
        return continueExec;
    }
    
    
    
Post-execute the ProcessorTask by preparing the response, flushing the response and then close or keep-alive the connection.
    public boolean postExecute() throws Exception{
        if (!.getAndSet(true)){
            .postResponse();
            .postProcess();
            .terminateProcess();
            // De-reference so under stress we don't have a simili leak.
             = null;
            return false;
        }
        return false;
    }

      
    
Set the AsyncTask.
    public void setAsyncTask(AsyncTask asyncProcessorTask){
        this. = asyncProcessorTask;
    }
    
    
    
Return AsyncTask.
    public AsyncTask getAsyncTask(){
        return ;
    }
    
    
    // --------------------------------------------------------- Util --------//  
    
    
    
Load the list of AsynchFilter.
    protected static void loadFilters(){      
        if ( System.getProperty() != null){
            StringTokenizer st = new StringTokenizer(
                    System.getProperty(),",");
            
             = new String[st.countTokens()];    
            int i = 0;
            while (st.hasMoreTokens()){
                [i++] = st.nextToken();                
            } 
        }   
    }    
    
    
    
Instanciate a class based on a property.
    private static AsyncFilter loadInstance(String property){        
        Class className = null;                               
        try{                              
            className = Class.forName(property);
            return (AsyncFilter)className.newInstance();
        } catch (ClassNotFoundException ex){
            SelectorThread.logger().log(.,ex.getMessage(),ex);
        } catch (InstantiationException ex){
            SelectorThread.logger().log(.,ex.getMessage(),ex);            
        } catch (IllegalAccessException ex){
            SelectorThread.logger().log(.,ex.getMessage(),ex);            
        }
        return null;
    }   

    
    
Add an AsyncFilter
    public void addAsyncFilter(AsyncFilter asyncFilter) {
        .add(asyncFilter);
    }

    
    
Remove an AsyncFilter
    public boolean removeAsyncFilter(AsyncFilter asyncFilter) {
        return .remove(asyncFilter);
    }

    
    
Get the AsyncHandler who drive the asynchronous process.
    public AsyncHandler getAsyncHandler() {
        return ;
    }
    
    
    
Set the AsyncHandler who drive the asynchronous process.
    public void setAsyncHandler(AsyncHandler asyncHandler) {
        this. = asyncHandler;
    }

    
Reset
    void recycle(){
        .getAndSet(false);
        .getAndSet(false);
        .getAndSet(false);
    }
New to GrepCode? Check out our FAQ X