1 23 package com.sun.enterprise.web.ara; 24 25 26 import com.sun.enterprise.web.connector.grizzly.LinkedListPipeline; 27 import com.sun.enterprise.web.connector.grizzly.Pipeline; 28 import com.sun.enterprise.web.connector.grizzly.StreamAlgorithm; 29 import com.sun.enterprise.web.connector.grizzly.Task; 30 import com.sun.enterprise.web.connector.grizzly.TaskEvent; 31 import com.sun.enterprise.web.connector.grizzly.TaskListener; 32 import com.sun.enterprise.web.ara.algorithms.ContextRootAlgorithm; 33 import com.sun.enterprise.web.ara.rules.ThreadRatioRule; 34 35 import java.util.ArrayList ; 36 37 import java.util.concurrent.ConcurrentLinkedQueue ; 38 import java.util.List ; 39 40 41 47 public class IsolationPipeline extends LinkedListPipeline 48 implements TaskListener{ 49 50 private final static String ALGORITHM_CLASS = 51 "com.sun.enterprise.web.ara.isolationPipeline.algorithm"; 52 private final static String RULE_EXECUTOR_CLASS = 53 "com.sun.enterprise.web.ara.isolationPipeline.ruleExecutor"; 54 55 56 59 private ConcurrentLinkedQueue <IsolatedTask> isolatedTasks; 60 61 62 64 65 public IsolationPipeline(){ 66 } 67 68 69 73 public void initPipeline(){ 74 super.initPipeline(); 76 77 isolatedTasks = new ConcurrentLinkedQueue <IsolatedTask>(); 79 80 for (int i=0; i < maxThreads; i++){ 82 isolatedTasks.offer(newIsolatedTask()); 83 } 84 } 85 86 87 90 public void addTask(Task task) { 91 if (task.getType() == Task.READ_TASK){ 93 super.addTask(wrap(task)); 94 } else { 95 super.addTask(task); 96 } 97 } 98 99 100 103 private Task wrap(Task task){ 104 IsolatedTask isolatedTask = isolatedTasks.poll(); 105 if ( isolatedTask == null){ 106 isolatedTask = newIsolatedTask(); 107 } 108 isolatedTask.wrap(task); 109 return isolatedTask; 110 } 111 112 113 116 private IsolatedTask newIsolatedTask(){ 117 IsolatedTask task = new IsolatedTask(); 118 119 task.setAlgorithm(newAlgorithm()); 120 task.setRulesExecutor(newRulesExecutor()); 121 task.addTaskListener(this); 122 123 return task; 124 } 125 126 127 130 private StreamAlgorithm newAlgorithm(){ 131 return (StreamAlgorithm)loadInstance(ALGORITHM_CLASS); 132 } 133 134 135 138 private RulesExecutor newRulesExecutor(){ 139 return (IsolationRulesExecutor)loadInstance(RULE_EXECUTOR_CLASS); 140 } 141 142 143 145 public void taskStarted(TaskEvent event) { 146 ; } 148 149 150 153 public void taskEvent(TaskEvent event) { 154 if ( event.getStatus() == TaskEvent.COMPLETED) 155 isolatedTasks.offer((IsolatedTask)event.attachement()); 156 } 157 158 160 163 private Object loadInstance(String property){ 164 Class className = null; 165 Pipeline pipeline = null; 166 try{ 167 className = Class.forName(property); 168 return className.newInstance(); 169 } catch (ClassNotFoundException ex){ 170 } catch (InstantiationException ex){ 171 } catch (IllegalAccessException ex){ 172 } 173 174 if ( property.equals(ALGORITHM_CLASS)){ 176 return new ContextRootAlgorithm(); 177 } else if ( property.equals(RULE_EXECUTOR_CLASS)){ 178 return new IsolationRulesExecutor(); 179 } 180 throw new IllegalStateException (); 181 } 182 183 } 184 | Popular Tags |