bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / jkstatus / src / share / org / apache / jk / status / JkStatusUpdateTask.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.jk.status;
19
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22
23 import org.apache.catalina.ant.AbstractCatalinaTask;
24 import org.apache.tools.ant.BuildException;
25
26 /**
27  * Ant task that implements the <code>/status</code> command, supported by the
28  * mod_jk status (1.2.13) application.
29  * 
30  * 
31  * @author Peter Rossbach
32  * @version $Revision: 611693 $
33  * @since 5.5.10
34  * @deprecated
35  */
36 public class JkStatusUpdateTask extends AbstractCatalinaTask {
37
38     /**
39      * The descriptive information about this implementation.
40      */
41     private static final String info = "org.apache.jk.status.JkStatusUpdateTask/1.1";
42
43     private String worker = "lb";
44
45     private String workerType = "lb";
46
47     private int internalid = 0;
48
49     private Integer lbRetries;
50
51     private Integer lbRecovertime;
52
53     private Boolean lbStickySession = Boolean.TRUE;
54
55     private Boolean lbForceSession = Boolean.FALSE;
56
57     private Integer workerLoadFactor;
58
59     private String workerJvmRoute ;
60     
61     private int workerDistance = -1;
62     
63     private String workerRedirect;
64
65     private String workerClusterDomain;
66
67     private Boolean workerDisabled ;
68
69     private Boolean workerStopped ;
70     
71     private int workerActivation = -1;
72       
73     private boolean isLBMode = true;
74     
75     
76
77     private String workerLb;
78
79     /**
80      * Return descriptive information about this implementation and the
81      * corresponding version number, in the format
82      * <code>&lt;description&gt;/&lt;version&gt;</code>.
83      */
84     public String getInfo() {
85
86         return (info);
87
88     }
89     
90     /**
91      *  
92      */
93     public JkStatusUpdateTask() {
94         super();
95         setUrl("http://localhost/jkstatus");
96     }
97
98     /**
99      * @return Returns the workerDistance.
100      */
101     public int getWorkerDistance() {
102         return workerDistance;
103     }
104
105     /**
106      * @param workerDistance The workerDistance to set.
107      */
108     public void setWorkerDistance(int workerDistance) {
109         this.workerDistance = workerDistance;
110     }
111
112     /**
113      * @return Returns the workerJvmRoute.
114      */
115     public String getWorkerJvmRoute() {
116         return workerJvmRoute;
117     }
118
119     /**
120      * @param workerJvmRoute The workerJvmRoute to set.
121      */
122     public void setWorkerJvmRoute(String workerJvmRoute) {
123         this.workerJvmRoute = workerJvmRoute;
124     }
125
126     /**
127      * @return Returns the internalid.
128      */
129     public int getInternalid() {
130         return internalid;
131     }
132
133     /**
134      * @param internalid
135      *            The internalid to set.
136      */
137     public void setInternalid(int internalid) {
138         this.internalid = internalid;
139     }
140
141     /**
142      * @return Returns the lbForceSession.
143      */
144     public Boolean getLbForceSession() {
145         return lbForceSession;
146     }
147
148     /**
149      * @param lbForceSession
150      *            The lbForceSession to set.
151      */
152     public void setLbForceSession(Boolean lbForceSession) {
153         this.lbForceSession = lbForceSession;
154     }
155
156     /**
157      * @return Returns the lbRecovertime.
158      */
159     public Integer getLbRecovertime() {
160         return lbRecovertime;
161     }
162
163     /**
164      * @param lbRecovertime
165      *            The lbRecovertime to set.
166      */
167     public void setLbRecovertime(Integer lbRecovertime) {
168         this.lbRecovertime = lbRecovertime;
169     }
170
171     /**
172      * @return Returns the lbRetries.
173      */
174     public Integer getLbRetries() {
175         return lbRetries;
176     }
177
178     /**
179      * @param lbRetries
180      *            The lbRetries to set.
181      */
182     public void setLbRetries(Integer lbRetries) {
183         this.lbRetries = lbRetries;
184     }
185
186     /**
187      * @return Returns the lbStickySession.
188      */
189     public Boolean getLbStickySession() {
190         return lbStickySession;
191     }
192
193     /**
194      * @param lbStickySession
195      *            The lbStickySession to set.
196      */
197     public void setLbStickySession(Boolean lbStickySession) {
198         this.lbStickySession = lbStickySession;
199     }
200
201     /**
202      * @return Returns the worker.
203      */
204     public String getWorker() {
205         return worker;
206     }
207
208     /**
209      * @param worker
210      *            The worker to set.
211      */
212     public void setWorker(String worker) {
213         this.worker = worker;
214     }
215
216     /**
217      * @return Returns the workerType.
218      */
219     public String getWorkerType() {
220         return workerType;
221     }
222
223     /**
224      * @param workerType
225      *            The workerType to set.
226      */
227     public void setWorkerType(String workerType) {
228         this.workerType = workerType;
229     }
230
231     /**
232      * @return Returns the workerLb.
233      */
234     public String getWorkerLb() {
235         return workerLb;
236     }
237
238     /**
239      * @param workerLb
240      *            The workerLb to set.
241      */
242     public void setWorkerLb(String workerLb) {
243         this.workerLb = workerLb;
244     }
245
246     /**
247      * @return Returns the workerClusterDomain.
248      */
249     public String getWorkerClusterDomain() {
250         return workerClusterDomain;
251     }
252
253     /**
254      * @param workerClusterDomain
255      *            The workerClusterDomain to set.
256      */
257     public void setWorkerClusterDomain(String workerClusterDomain) {
258         this.workerClusterDomain = workerClusterDomain;
259     }
260
261     /**
262      * @return Returns the workerDisabled.
263      */
264     public Boolean getWorkerDisabled() {
265         return workerDisabled;
266     }
267
268     /**
269      * @param workerDisabled
270      *            The workerDisabled to set.
271      */
272     public void setWorkerDisabled(Boolean workerDisabled) {
273         this.workerDisabled = workerDisabled;
274     }
275
276     /**
277      * @return Returns the workerActivation.
278      */
279     public int getWorkerActivation() {
280         return workerActivation;
281     }
282     
283     /**
284      * <ul>
285      * <li>0 active</li>
286      * <li>1 disabled</li>
287      * <li>2 stopped</li>
288      * </ul>
289      * @param workerActivation The workerActivation to set.
290      * 
291      */
292     public void setWorkerActivation(int workerActivation) {
293         this.workerActivation = workerActivation;
294     }
295     
296     /**
297      * @return Returns the workerStopped.
298      */
299     public Boolean getWorkerStopped() {
300         return workerStopped;
301     }
302     
303     /**
304      * @param workerStopped The workerStopped to set.
305      */
306     public void setWorkerStopped(Boolean workerStopped) {
307         this.workerStopped = workerStopped;
308     }
309     
310     /**
311      * @return Returns the workerLoadFactor.
312      */
313     public Integer getWorkerLoadFactor() {
314         return workerLoadFactor;
315     }
316
317     /**
318      * @param workerLoadFactor
319      *            The workerLoadFactor to set.
320      */
321     public void setWorkerLoadFactor(Integer workerLoadFactor) {
322         this.workerLoadFactor = workerLoadFactor;
323     }
324
325     /**
326      * @return Returns the workerRedirect.
327      */
328     public String getWorkerRedirect() {
329         return workerRedirect;
330     }
331
332     /**
333      * @param workerRedirect
334      *            The workerRedirect to set.
335      */
336     public void setWorkerRedirect(String workerRedirect) {
337         this.workerRedirect = workerRedirect;
338     }
339
340     /**
341      * Execute the requested operation.
342      * 
343      * @exception BuildException
344      *                if an error occurs
345      */
346     public void execute() throws BuildException {
347
348         super.execute();
349         checkParameter();
350         StringBuffer sb = createLink();
351         execute(sb.toString(), null, null, -1);
352
353     }
354
355     /**
356      * Create JkStatus link
357      * <ul>
358      * <li><b>load balance example:
359      * </b>http://localhost/jkstatus?cmd=update&mime=txt&w=lb&vlf=false&vls=true</li>
360      * <li><b>worker example:
361      * </b>http://localhost/jkstatus?cmd=update&mime=txt&w=lb&sw=node1&vwn=node01&vwf=1&vwa=2&vwx=0
362      * <br/>
363      * <ul>
364      * <li>vwa=0 active</li>
365      * <li>vwa=1 disabled</li>
366      * <li>vwa=2 stopped</li>
367      * </ul>
368      * </li>
369      * </ul>
370      *
371      * <br/>Loadbalacing parameter:
372      * <br/>
373      * <ul>
374      * <li><b>w:<b/> name lb worker</li>
375      * <li><b>vlr:<b/> Number of Retries</li>
376      * <li><b>vlt:<b/> recover wait time</li>
377      * <li><b>vlf:<b/> Force Sticky Session</li>
378      * <li><b>vls:<b/> Sticky session</li>
379      * </ul>
380      * 
381      * <br/>Tcp worker parameter:
382      * <br/>
383      * <ul>
384      * <li><b>w:<b/> name worker</li>
385      * <li><b>sw:<b/> name lb sub worker</li>
386      * <li><b>vwf:<b/> load factor</li>
387      * <li><b>vwn:<b/> jvm route</li>
388      * <li><b>vwx:<b/> distance</li>
389      * <li><b>vwa:<b/> activation state</li>
390      * <li><b>vwr:<b/> redirect route</li>
391      * <li><b>vwd:<b/> cluster domain</li>
392      * <li><b>ws:<b/> stopped deprecated 1.2.16</li>
393      * <li><b>wd:<b/> disabled deprecated 1.2.16</li>
394      * </ul>
395      * 
396      * @return create jkstatus link
397      */
398     private StringBuffer createLink() {
399         // Building URL
400         StringBuffer sb = new StringBuffer();
401         try {
402             sb.append("?cmd=update&mime=txt");
403             sb.append("&w=");
404
405             if (isLBMode) {
406                 sb.append(URLEncoder.encode(worker, getCharset()));
407                 //http://localhost/jkstatus?cmd=update&mime=txt&w=lb&vlf=false&vls=true
408                 if ((lbRetries != null)) { // > 0
409                     sb.append("&vlr=");
410                     sb.append(lbRetries);
411                 }
412                 if ((lbRecovertime != null)) { // > 59
413                     sb.append("&vlt=");
414                     sb.append(lbRecovertime);
415                 }
416                 if ((lbStickySession != null)) {
417                     sb.append("&vls=");
418                     sb.append(lbStickySession);
419                 }
420                 if ((lbForceSession != null)) {
421                     sb.append("&vlf=");
422                     sb.append(lbForceSession);
423                 }
424             } else {
425                 //http://localhost/status?cmd=update&mime=txt&sw=node1&w=lb&vwf=1&wd=false&ws=false
426                 if (workerLb != null) { // must be configured
427                     sb.append(URLEncoder.encode(workerLb, getCharset()));
428                 }
429                 sb.append("&sw=");
430                 sb.append(URLEncoder.encode(worker, getCharset()));
431                 if (workerLoadFactor != null) { // >= 1
432                     sb.append("&vwf=");
433                     sb.append(workerLoadFactor);
434                 }
435                 if (workerJvmRoute != null) {
436                     sb.append("&vwn=");
437                     sb.append(URLEncoder.encode(workerJvmRoute, getCharset()));
438                 } 
439                 if (workerDisabled != null) {
440                     sb.append("&vwd=");
441                     sb.append(workerDisabled);
442                 }
443                 if (workerStopped != null) {
444                     sb.append("&vws=");
445                     sb.append(workerStopped);
446                 }
447                 if (workerActivation >= 0 && workerActivation < 3) {
448                     sb.append("&vwa=");
449                     sb.append(workerActivation);
450                 } 
451                 if (workerDistance >= 0) {
452                     sb.append("&vwx=");
453                     sb.append(workerDistance);
454                 }
455                 if (workerRedirect != null) { // other worker conrecte lb's
456                     sb.append("&vwr=");
457                     sb.append(URLEncoder.encode(workerRedirect,
458                             getCharset()));
459                 }
460                 if (workerClusterDomain != null) {
461                     sb.append("&vwc=");
462                     sb.append(URLEncoder.encode(workerClusterDomain,
463                             getCharset()));
464                 }
465             }
466
467         } catch (UnsupportedEncodingException e) {
468             throw new BuildException("Invalid 'charset' attribute: "
469                     + getCharset());
470         }
471         return sb;
472     }
473
474     /**
475      * check correct lb and worker pararmeter
476      */
477     protected void checkParameter() {
478         if (worker == null) {
479             throw new BuildException("Must specify 'worker' attribute");
480         }
481         if (workerType == null) {
482             throw new BuildException("Must specify 'workerType' attribute");
483         }
484         if ("lb".equals(workerType)) {
485             if (lbRecovertime == null && lbRetries == null) {
486                 throw new BuildException(
487                         "Must specify at a lb worker either 'lbRecovertime' or"
488                                 + "'lbRetries' attribute");
489             }
490             if (lbStickySession == null || lbForceSession == null) {
491                 throw new BuildException("Must specify at a lb worker either"
492                         + "'lbStickySession' and 'lbForceSession' attribute");
493             }
494             if (null != lbRecovertime && 60 < lbRecovertime.intValue()) {
495                 throw new BuildException(
496                         "The 'lbRecovertime' must be greater than 59");
497             }
498             if (null != lbRetries && 1 < lbRetries.intValue()) {
499                 throw new BuildException(
500                         "The 'lbRetries' must be greater than 1");
501             }
502             isLBMode = true;
503         } else if ("worker".equals(workerType)) {
504             if (workerLoadFactor == null ) {
505                 throw new BuildException(
506                         "Must specify at a node worker 'workerLoadFactor' attribute");
507             }
508             if (workerClusterDomain == null) {
509                 throw new BuildException(
510                         "Must specify at a node worker 'workerClusterDomain' attribute");
511             }
512             if (workerRedirect == null) {
513                 throw new BuildException(
514                         "Must specify at a node worker 'workerRedirect' attribute");
515             }
516             if (workerLb == null) {
517                 throw new BuildException("Must specify 'workerLb' attribute");
518             }
519             if (workerLoadFactor.intValue() < 1) {
520                 throw new BuildException(
521                         "The 'workerLoadFactor' must be greater or equal 1");
522             }
523             isLBMode = false;
524         } else {
525             throw new BuildException(
526                     "Only 'lb' and 'worker' supported as workerType attribute");
527         }
528     }
529 }