1 23 package com.sun.enterprise.web.ara; 24 25 import com.sun.enterprise.web.connector.grizzly.HtmlHelper; 26 import com.sun.enterprise.web.connector.grizzly.ReadTask; 27 import com.sun.enterprise.web.connector.grizzly.Rule; 28 import com.sun.enterprise.web.connector.grizzly.SelectorThread; 29 30 import java.util.ArrayList ; 31 import java.util.StringTokenizer ; 32 import java.util.logging.Level ; 33 34 import com.sun.enterprise.web.ara.rules.ThreadRatioRule; 35 36 41 public class IsolationRulesExecutor implements RulesExecutor<IsolatedTask> { 42 43 public final static int RULE_OK = 0; 44 public final static int RULE_DELAY = 1; 45 public final static int RULE_BLOCKED = 2; 46 public final static int RULE_OK_NOCACHE = 3; 47 public final static int RULE_CONTINUE = 4; 48 49 50 private final static String RULE_CLASS = 51 "com.sun.enterprise.web.ara.rules"; 52 53 54 private final static String DELAY_VALUE = 55 "com.sun.enterprise.web.ara.delay"; 56 57 58 61 private final static int INITIAL_RULE_COUNT = 5; 62 63 64 67 protected ArrayList <Rule> rules = new ArrayList <Rule>(); 68 69 70 73 private static int currentThreadCount; 74 75 76 79 private static long delayValue = 5 * 1000; 80 81 82 85 private boolean isCachingAllowed = true; 86 87 89 90 public IsolationRulesExecutor() { 91 loadRules(); 92 93 if ( System.getProperty(DELAY_VALUE) != null){ 94 delayValue = Long.valueOf(System.getProperty(DELAY_VALUE)); 95 } 96 } 97 98 99 102 protected void loadRules(){ 103 if ( System.getProperty(RULE_CLASS) != null){ 104 StringTokenizer st = new StringTokenizer ( 105 System.getProperty(RULE_CLASS),","); 106 while (st.hasMoreTokens()){ 107 rules.add(loadInstance(st.nextToken())); 108 } 109 } 110 111 if ( rules.size() == 0){ 112 rules.add(new ThreadRatioRule()); 113 } 114 } 115 116 117 122 public boolean execute(IsolatedTask isolatedTask) { 123 ReadTask task = (ReadTask)isolatedTask.getWrappedTask(); 124 125 Integer status = 0; 126 int i = 0; 127 isCachingAllowed = true; 128 while(true) { 129 rules.get(i).attach(task); 130 131 try{ 132 status = (Integer )rules.get(i).call(); 133 } catch(Exception ex) { 134 SelectorThread.logger().log(Level.SEVERE,"Rule exception",ex); 135 return true; 136 } 137 138 isCachingAllowed = (status == RULE_OK_NOCACHE ? false:true); 139 140 if (status == RULE_DELAY){ 141 142 try{ 145 Thread.sleep(delayValue); 146 } catch (InterruptedException ex) { 147 SelectorThread.logger(). 148 log(Level.SEVERE,"Rule delay exception",ex); 149 } 150 i = 0; 151 continue; 152 } else if ( status == RULE_BLOCKED ){ 153 task.cancelTask("No resources available.", HtmlHelper.OK); 154 return true; 155 } 156 157 i++; 158 if (i == rules.size()){ 159 break; 160 } 161 } 162 163 return (status == RULE_OK || status == RULE_OK_NOCACHE); 164 165 } 166 167 168 171 public boolean isCachingAllowed(){ 172 return isCachingAllowed; 173 } 174 175 176 178 181 private Rule loadInstance(String property){ 182 Class className = null; 183 try{ 184 className = Class.forName(property); 185 return (Rule)className.newInstance(); 186 } catch (ClassNotFoundException ex){ 187 } catch (InstantiationException ex){ 188 } catch (IllegalAccessException ex){ 189 } 190 return new ThreadRatioRule(); 191 } 192 } 193 | Popular Tags |