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.ara;
 
 
This task is used to configure an instance of .ReadTask based on the Rule implementation.

Author(s):
Jeanfrancois Arcand
 
 public class IsolatedTask extends TaskWrapper implements TaskListener{
 
     public final static int ISOLATED_TASK = 4;
             
    
The algorithm used to determine the context-root, the HTTP method, the protocol etc.
 
     protected StreamAlgorithm algorithm;
    
    
    
The RuleExecutor used to apply Rule
 
     protected RulesExecutor rulesExecutor;
    
   
    
List of listeners
 
     protected ArrayList<TaskListenerlisteners = new ArrayList<TaskListener>();

    
    
The ByteBuffer initial position before applying the Algorithm
 
     protected int initialBytePosition;
    
    
    
The ByteBuffer initial limit before applying the Algorithm
     
    protected int initialByteLimit;
    
    
    
The TaskEvent used between this task and it's attached ReadTask
    protected TaskEvent<IsolatedTasktaskEvent;
    

    
The Thread Pool wrapper.
    protected Pipeline pipeline;    
    
    
    
Cache the SelectionKey to avoid parsing the requests bytes more than once.
    private static ConcurrentHashMap<SelectionKey,PipelinecacheKey = 
            new ConcurrentHashMap<SelectionKey,Pipeline>();
    
    
    public IsolatedTask(){
         = new TaskEvent<IsolatedTask>();
        .attach(this);      
    }
    
    
    
Apply a set of Rules to the current bytes requests using an instance of ReadTask byte buffer. Once the Rule has been successfully applied, execute it.
    public void doTask() throws IOException {   
        try {
            ReadTask readTask = (ReadTask);
            ByteBuffer byteBuffer = readTask.getByteBuffer();     
            SocketChannel socketChannel = 
                        (SocketChannel)readTask.getSelectionKey().channel();
            Socket socket = socketChannel.socket();
           
            socketChannel.read(byteBuffer);
        
            int position = byteBuffer.position();
            int limit = byteBuffer.limit();
            // If we weren't able to parse the token, return to the 
            // SelectorThread
            boolean execute = false;
            
            if (.parse(byteBuffer)) {
                execute = .execute(this);
                if ( execute ){                       
                    // Tell the ReadTask to not load bytes and re-use the one
                    // already loaded.
                    readTask.setBytesAvailable(true);
                    byteBuffer.limit(limit);
                    byteBuffer.position(position);
                    // Get notification once the task has completed.
                    readTask.addTaskListener(this);
                    readTask.execute();
                } else {
                    fireTaskEvent();
                }   
            } else {
                // Failed to read the URI. Close the connections.
                readTask.terminate(false);
                fireTaskEvent();
            }
        } catch (Exception ex){
            SelectorThread.logger()
                .log(.,"IsolatedTask logic exception.",ex);
        }  
    }
        
    
    
Set the RuleExecutor instance used by this task.
    public void setRulesExecutor(RulesExecutor rulesExecutor){
        this. = rulesExecutor;
    }
    
    
Set the Algorithm used by this task.
    public void setAlgorithm(StreamAlgorithm algorithm){
        this. = algorithm;
    }
    
    
    
Wrao the Task with this task.
    public IsolatedTask wrap(Task task){
         = task;
        return this;
    }   
     
    
    // ------------------------------------------------------ Execution -----//
    
    
    
Execute that task using the current Thread.
    public void execute(){
        run();
    }
    
    
    
Execute the logic required to isolate the task.
    public void run(){
        try{
            doTask();
        } catch (IOException ex){
            throw new RuntimeException(ex);
        }
    }
    
    // ----------------------------------------------------- Task Listener ----/
    
    
     
Add the given TaskListener to this Task.
    @Override
    public void addTaskListener(TaskListener task){
        .add(task);
    }

    
    
Remove the given TaskListener/code> from this Task.
    @Override
    public void removeTaskListener(TaskListener task){
        .remove(task);
    }
    
    
    
Clean all the listeners of this Task
    @Override
    public void clearTaskListeners(){
        .clear();
    }

    
    
Notify listeners of that class that the processing has completed.
    protected void fireTaskEvent(TaskEvent<?> event){
        for (int i=0; i < .size(); i++){
            .get(i).taskEvent(event);
        }
    }

    
    
Remove the SelectionKey from the cache.
    public void taskEvent(TaskEvent event) {
         = null;
        fireTaskEvent();
        ((ReadTask)event.attachement()).setPipeline();      
    }

    
    
This task type.
    public int getType(){
        return ;
    }

    
    
Set the Thread pool wrapper.
    public void setPipeline(Pipeline pipeline) {
        this. = pipeline;
    }

    
    
Get the Thread pool wrapper.
    public Pipeline getPipeline() {
        return ;
    }
    
    
New to GrepCode? Check out our FAQ X