![JAR search and dependency download from the Maven repository](/logo.png)
com.sun.enterprise.web.ara.IsolatedTask Maven / Gradle / Ivy
/*
* 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;
import com.sun.enterprise.web.connector.grizzly.Pipeline;
import com.sun.enterprise.web.connector.grizzly.ReadTask;
import com.sun.enterprise.web.connector.grizzly.SelectorThread;
import com.sun.enterprise.web.connector.grizzly.StreamAlgorithm;
import com.sun.enterprise.web.connector.grizzly.Task;
import com.sun.enterprise.web.connector.grizzly.TaskEvent;
import com.sun.enterprise.web.connector.grizzly.TaskListener;
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.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
/**
* This task is used to configure an instance of .ReadTask
based
* on the Rule
implementation.
*
* @author 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 listeners = new ArrayList();
/**
* 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 taskEvent;
/**
* The Thread Pool wrapper.
*/
protected Pipeline pipeline;
/**
* Cache the SelectionKey
to avoid parsing the
* requests bytes more than once.
*/
private static ConcurrentHashMap cacheKey =
new ConcurrentHashMap();
public IsolatedTask(){
taskEvent = new TaskEvent();
taskEvent.attach(this);
taskEvent.setStatus(TaskEvent.COMPLETED);
}
/**
* Apply a set of Rule
s 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)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);
}
}
/**
* Set the RuleExecutor
instance used by this task.
*/
public void setRulesExecutor(RulesExecutor rulesExecutor){
this.rulesExecutor = rulesExecutor;
}
/**
* Set the Algorithm
used by this task.
*/
public void setAlgorithm(StreamAlgorithm algorithm){
this.algorithm = algorithm;
}
/**
* Wrao the Task
with this task.
*/
public IsolatedTask wrap(Task task){
wrappedTask = 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){
listeners.add(task);
}
/**
* Remove the given TaskListener/code> from this
* Task
.
*/
@Override
public void removeTaskListener(TaskListener task){
listeners.remove(task);
}
/**
* Clean all the listeners of this Task
*/
@Override
public void clearTaskListeners(){
listeners.clear();
}
/**
* Notify listeners of that class that the processing has completed.
*/
protected void fireTaskEvent(TaskEvent> event){
for (int i=0; i < listeners.size(); i++){
listeners.get(i).taskEvent(event);
}
}
/**
* Remove the SelectionKey
from the cache.
*/
public void taskEvent(TaskEvent event) {
wrappedTask = null;
fireTaskEvent(taskEvent);
((ReadTask)event.attachement()).setPipeline(pipeline);
}
/**
* This task type.
*/
public int getType(){
return ISOLATED_TASK;
}
/**
* Set the Thread
pool wrapper.
*/
public void setPipeline(Pipeline pipeline) {
this.pipeline = pipeline;
}
/**
* Get the Thread
pool wrapper.
*/
public Pipeline getPipeline() {
return pipeline;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy