KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > ara > IsolationRulesExecutor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.logging.Level JavaDoc;
33
34 import com.sun.enterprise.web.ara.rules.ThreadRatioRule;
35
36 /**
37  * This class execute sequentialy <code>Rule</code>s on a <code>Task</code>
38  *
39  * @author Jeanfrancois Arcand
40  */

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 JavaDoc RULE_CLASS =
51             "com.sun.enterprise.web.ara.rules";
52     
53     
54     private final static String JavaDoc DELAY_VALUE =
55             "com.sun.enterprise.web.ara.delay";
56     
57     
58     /**
59      * Initial number of <code>Rule</code> to cache.
60      */

61     private final static int INITIAL_RULE_COUNT = 5;
62
63
64     /**
65      * The list of <code>Rule</code> to apply to a <code>Task</code>
66      */

67     protected ArrayList JavaDoc<Rule> rules = new ArrayList JavaDoc<Rule>();
68     
69     
70     /**
71      * The current number of thread used.
72      */

73     private static int currentThreadCount;
74     
75     
76     /**
77      * The time this thread will sleep when a rule is delayed.
78      */

79     private static long delayValue = 5 * 1000;
80     
81     
82     /**
83      * Is caching allowed
84      */

85     private boolean isCachingAllowed = true;
86     
87     // ---------------------------------------------------------------------//
88

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     /**
100      * Load the list of <code>Rules</code> to apply to a <code>Task</code>
101      */

102     protected void loadRules(){
103         if ( System.getProperty(RULE_CLASS) != null){
104             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(
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     /**
118      * Execute the <code>Rule</code>s on the <code>IsolatedTask</code>
119      * @param isolatedTask the task used.
120      * @return true if the request processing can continue.
121      */

122     public boolean execute(IsolatedTask isolatedTask) {
123         ReadTask task = (ReadTask)isolatedTask.getWrappedTask();
124                        
125         Integer JavaDoc status = 0;
126         int i = 0;
127         isCachingAllowed = true;
128         while(true) {
129             rules.get(i).attach(task);
130             
131             try{
132                 status = (Integer JavaDoc)rules.get(i).call();
133             } catch(Exception JavaDoc 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                 // Wait until the delay expire. The current thread will
143
// wait and then re-execute the rules.
144
try{
145                     Thread.sleep(delayValue);
146                 } catch (InterruptedException JavaDoc 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     /**
169      * Is caching of <code>Rule</code> results allowed
170      */

171     public boolean isCachingAllowed(){
172         return isCachingAllowed;
173     }
174
175     
176     // -------------------------------------------------------- Util --------//
177

178      /**
179      * Instanciate a class based on a property.
180      */

181     private Rule loadInstance(String JavaDoc property){
182         Class JavaDoc className = null;
183         try{
184             className = Class.forName(property);
185             return (Rule)className.newInstance();
186         } catch (ClassNotFoundException JavaDoc ex){
187         } catch (InstantiationException JavaDoc ex){
188         } catch (IllegalAccessException JavaDoc ex){
189         }
190         return new ThreadRatioRule();
191     }
192 }
193
Popular Tags