FileDocCategorySizeDatePackage
IsolatedTask.javaAPI DocGlassfish v2 API9046Fri May 04 22:37:02 BST 2007com.sun.enterprise.web.ara

IsolatedTask

public class IsolatedTask extends TaskWrapper implements com.sun.enterprise.web.connector.grizzly.TaskListener
This task is used to configure an instance of .ReadTask based on the Rule implementation.
author
Jeanfrancois Arcand

Fields Summary
public static final int
ISOLATED_TASK
protected com.sun.enterprise.web.connector.grizzly.StreamAlgorithm
algorithm
The algorithm used to determine the context-root, the HTTP method, the protocol etc.
protected RulesExecutor
rulesExecutor
The RuleExecutor used to apply Rule
protected ArrayList
listeners
List of listeners
protected int
initialBytePosition
The ByteBuffer initial position before applying the Algorithm
protected int
initialByteLimit
The ByteBuffer initial limit before applying the Algorithm
protected com.sun.enterprise.web.connector.grizzly.TaskEvent
taskEvent
The TaskEvent used between this task and it's attached ReadTask
protected com.sun.enterprise.web.connector.grizzly.Pipeline
pipeline
The Thread Pool wrapper.
private static ConcurrentHashMap
cacheKey
Cache the SelectionKey to avoid parsing the requests bytes more than once.
Constructors Summary
public IsolatedTask()

    
    
     
        taskEvent = new TaskEvent<IsolatedTask>();
        taskEvent.attach(this);      
        taskEvent.setStatus(TaskEvent.COMPLETED);
    
Methods Summary
public voidaddTaskListener(com.sun.enterprise.web.connector.grizzly.TaskListener task)
Add the given TaskListener to this Task.

        listeners.add(task);
    
public voidclearTaskListeners()
Clean all the listeners of this Task

        listeners.clear();
    
public voiddoTask()
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.

   
        try {
            ReadTask readTask = (ReadTask)wrappedTask;
            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 (algorithm.parse(byteBuffer)) {
                execute = rulesExecutor.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(taskEvent);
                }   
            } else {
                // Failed to read the URI. Close the connections.
                readTask.terminate(false);
                fireTaskEvent(taskEvent);
            }
        } catch (Exception ex){
            SelectorThread.logger()
                .log(Level.SEVERE,"IsolatedTask logic exception.",ex);
        }  
    
public voidexecute()
Execute that task using the current Thread.

        run();
    
protected voidfireTaskEvent(com.sun.enterprise.web.connector.grizzly.TaskEvent event)
Notify listeners of that class that the processing has completed.

        for (int i=0; i < listeners.size(); i++){
            listeners.get(i).taskEvent(event);
        }
    
public com.sun.enterprise.web.connector.grizzly.PipelinegetPipeline()
Get the Thread pool wrapper.

        return pipeline;
    
public intgetType()
This task type.

        return ISOLATED_TASK;
    
public voidremoveTaskListener(com.sun.enterprise.web.connector.grizzly.TaskListener task)
Remove the given TaskListener/code> from this Task.

        listeners.remove(task);
    
public voidrun()
Execute the logic required to isolate the task.

        try{
            doTask();
        } catch (IOException ex){
            throw new RuntimeException(ex);
        };
    
public voidsetAlgorithm(com.sun.enterprise.web.connector.grizzly.StreamAlgorithm algorithm)
Set the Algorithm used by this task.

        this.algorithm = algorithm;
    
public voidsetPipeline(com.sun.enterprise.web.connector.grizzly.Pipeline pipeline)
Set the Thread pool wrapper.

        this.pipeline = pipeline;
    
public voidsetRulesExecutor(RulesExecutor rulesExecutor)
Set the RuleExecutor instance used by this task.

        this.rulesExecutor = rulesExecutor;
    
public voidtaskEvent(com.sun.enterprise.web.connector.grizzly.TaskEvent event)
Remove the SelectionKey from the cache.

        wrappedTask = null;
        fireTaskEvent(taskEvent);
        ((ReadTask)event.attachement()).setPipeline(pipeline);      
    
public com.sun.enterprise.web.ara.IsolatedTaskwrap(com.sun.enterprise.web.connector.grizzly.Task task)
Wrao the Task with this task.

        wrappedTask = task;
        return this;