KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 JavaDoc;
36
37 import java.util.concurrent.ConcurrentLinkedQueue JavaDoc;
38 import java.util.List JavaDoc;
39
40
41 /**
42  * Customized <code>Pipeline</code> which wrap the original <code>Task</code>
43  * instance with an instance of <code>IsolatedTask</code>
44  *
45  * @author Jeanfrancois Arcand
46  */

47 public class IsolationPipeline extends LinkedListPipeline
48         implements TaskListener{
49     
50     private final static String JavaDoc ALGORITHM_CLASS =
51          "com.sun.enterprise.web.ara.isolationPipeline.algorithm";
52     private final static String JavaDoc RULE_EXECUTOR_CLASS =
53          "com.sun.enterprise.web.ara.isolationPipeline.ruleExecutor";
54             
55             
56     /**
57      * Cache instance of <code>IsolatedTask</code>
58      */

59     private ConcurrentLinkedQueue JavaDoc<IsolatedTask> isolatedTasks;
60     
61       
62     // ------------------------------------------------------ Constructor ---/
63

64     
65     public IsolationPipeline(){
66     }
67     
68     
69     /**
70      * Initialize this pipeline by first initializing its parent, and then by
71      * creating the caches and the rule executor engine.
72      */

73     public void initPipeline(){
74         // 1. first, init this pipeline.
75
super.initPipeline();
76         
77         // 2. Create cache
78
isolatedTasks = new ConcurrentLinkedQueue JavaDoc<IsolatedTask>();
79         
80         // 3. Cache IsolatedTask
81
for (int i=0; i < maxThreads; i++){
82             isolatedTasks.offer(newIsolatedTask());
83         }
84     }
85     
86     
87     /**
88      * Execute the wrapped <code>Task</code>
89      */

90     public void addTask(Task task) {
91         // SSL not yet supported.
92
if (task.getType() == Task.READ_TASK){
93             super.addTask(wrap(task));
94         } else {
95             super.addTask(task);
96         }
97     }
98     
99
100     /**
101      * Wrap the current <code>Task</code> using an <code>IsolatedTask</code>
102      */

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     /**
114      * Create a new <code>IsolatedTask</code>
115      */

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     /**
128      * Create a new <code>StreamAlgorithm</code>.
129      */

130     private StreamAlgorithm newAlgorithm(){
131         return (StreamAlgorithm)loadInstance(ALGORITHM_CLASS);
132     }
133     
134     
135     /**
136      * Create the new <code>RulesExecutor</code>
137      */

138     private RulesExecutor newRulesExecutor(){
139         return (IsolationRulesExecutor)loadInstance(RULE_EXECUTOR_CLASS);
140     }
141     
142        
143     // ----------------------------------------------- Task Listener ---------//
144

145     public void taskStarted(TaskEvent event) {
146         ; // Do nothing.
147
}
148
149     
150     /**
151      * Return the <code>IsolatedTask</code> to the pool.
152      */

153     public void taskEvent(TaskEvent event) {
154         if ( event.getStatus() == TaskEvent.COMPLETED)
155             isolatedTasks.offer((IsolatedTask)event.attachement());
156     }
157     
158     // ----------------------------------------------- Util ------------------//
159

160     /**
161      * Instanciate a class based on a property.
162      */

163     private Object JavaDoc loadInstance(String JavaDoc property){
164         Class JavaDoc className = null;
165         Pipeline pipeline = null;
166         try{
167             className = Class.forName(property);
168             return className.newInstance();
169         } catch (ClassNotFoundException JavaDoc ex){
170         } catch (InstantiationException JavaDoc ex){
171         } catch (IllegalAccessException JavaDoc ex){
172         }
173         
174         // Default
175
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 JavaDoc();
181     }
182     
183 }
184
Popular Tags