KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ant > JKStatusUpdateTask


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.catalina.ant;
19
20 import java.io.UnsupportedEncodingException JavaDoc;
21 import java.net.URLEncoder JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24
25 /**
26  * Ant task that implements the <code>/status</code> command, supported by the
27  * mod_jk status (1.2.9) application.
28  *
29  * @author Peter Rossbach
30  * @version $Revision: 467222 $
31  * @since 5.5.9
32  */

33 public class JKStatusUpdateTask extends AbstractCatalinaTask {
34
35     private String JavaDoc worker = "lb";
36
37     private String JavaDoc workerType = "lb";
38
39     private int internalid = 0;
40
41     private Integer JavaDoc lbRetries;
42
43     private Integer JavaDoc lbRecovertime;
44
45     private Boolean JavaDoc lbStickySession = Boolean.TRUE;
46
47     private Boolean JavaDoc lbForceSession = Boolean.FALSE;
48
49     private Integer JavaDoc workerLoadFactor;
50
51     private String JavaDoc workerRedirect;
52
53     private String JavaDoc workerClusterDomain;
54
55     private Boolean JavaDoc workerDisabled = Boolean.FALSE;
56
57     private Boolean JavaDoc workerStopped = Boolean.FALSE;
58     
59     private boolean isLBMode = true;
60
61     private String JavaDoc workerLb;
62
63     /**
64      *
65      */

66     public JKStatusUpdateTask() {
67         super();
68         setUrl("http://localhost/status");
69     }
70
71     /**
72      * @return Returns the internalid.
73      */

74     public int getInternalid() {
75         return internalid;
76     }
77
78     /**
79      * @param internalid
80      * The internalid to set.
81      */

82     public void setInternalid(int internalid) {
83         this.internalid = internalid;
84     }
85
86     /**
87      * @return Returns the lbForceSession.
88      */

89     public Boolean JavaDoc getLbForceSession() {
90         return lbForceSession;
91     }
92
93     /**
94      * @param lbForceSession
95      * The lbForceSession to set.
96      */

97     public void setLbForceSession(Boolean JavaDoc lbForceSession) {
98         this.lbForceSession = lbForceSession;
99     }
100
101     /**
102      * @return Returns the lbRecovertime.
103      */

104     public Integer JavaDoc getLbRecovertime() {
105         return lbRecovertime;
106     }
107
108     /**
109      * @param lbRecovertime
110      * The lbRecovertime to set.
111      */

112     public void setLbRecovertime(Integer JavaDoc lbRecovertime) {
113         this.lbRecovertime = lbRecovertime;
114     }
115
116     /**
117      * @return Returns the lbRetries.
118      */

119     public Integer JavaDoc getLbRetries() {
120         return lbRetries;
121     }
122
123     /**
124      * @param lbRetries
125      * The lbRetries to set.
126      */

127     public void setLbRetries(Integer JavaDoc lbRetries) {
128         this.lbRetries = lbRetries;
129     }
130
131     /**
132      * @return Returns the lbStickySession.
133      */

134     public Boolean JavaDoc getLbStickySession() {
135         return lbStickySession;
136     }
137
138     /**
139      * @param lbStickySession
140      * The lbStickySession to set.
141      */

142     public void setLbStickySession(Boolean JavaDoc lbStickySession) {
143         this.lbStickySession = lbStickySession;
144     }
145
146     /**
147      * @return Returns the worker.
148      */

149     public String JavaDoc getWorker() {
150         return worker;
151     }
152
153     /**
154      * @param worker
155      * The worker to set.
156      */

157     public void setWorker(String JavaDoc worker) {
158         this.worker = worker;
159     }
160
161     /**
162      * @return Returns the workerType.
163      */

164     public String JavaDoc getWorkerType() {
165         return workerType;
166     }
167
168     /**
169      * @param workerType
170      * The workerType to set.
171      */

172     public void setWorkerType(String JavaDoc workerType) {
173         this.workerType = workerType;
174     }
175
176     /**
177      * @return Returns the workerLb.
178      */

179     public String JavaDoc getWorkerLb() {
180         return workerLb;
181     }
182
183     /**
184      * @param workerLb
185      * The workerLb to set.
186      */

187     public void setWorkerLb(String JavaDoc workerLb) {
188         this.workerLb = workerLb;
189     }
190
191     /**
192      * @return Returns the workerClusterDomain.
193      */

194     public String JavaDoc getWorkerClusterDomain() {
195         return workerClusterDomain;
196     }
197
198     /**
199      * @param workerClusterDomain
200      * The workerClusterDomain to set.
201      */

202     public void setWorkerClusterDomain(String JavaDoc workerClusterDomain) {
203         this.workerClusterDomain = workerClusterDomain;
204     }
205
206     /**
207      * @return Returns the workerDisabled.
208      */

209     public Boolean JavaDoc getWorkerDisabled() {
210         return workerDisabled;
211     }
212
213     /**
214      * @param workerDisabled
215      * The workerDisabled to set.
216      */

217     public void setWorkerDisabled(Boolean JavaDoc workerDisabled) {
218         this.workerDisabled = workerDisabled;
219     }
220
221     /**
222      * @return Returns the workerStopped.
223      */

224     public Boolean JavaDoc getWorkerStopped() {
225         return workerStopped;
226     }
227     
228     /**
229      * @param workerStopped The workerStopped to set.
230      */

231     public void setWorkerStopped(Boolean JavaDoc workerStopped) {
232         this.workerStopped = workerStopped;
233     }
234     
235     /**
236      * @return Returns the workerLoadFactor.
237      */

238     public Integer JavaDoc getWorkerLoadFactor() {
239         return workerLoadFactor;
240     }
241
242     /**
243      * @param workerLoadFactor
244      * The workerLoadFactor to set.
245      */

246     public void setWorkerLoadFactor(Integer JavaDoc workerLoadFactor) {
247         this.workerLoadFactor = workerLoadFactor;
248     }
249
250     /**
251      * @return Returns the workerRedirect.
252      */

253     public String JavaDoc getWorkerRedirect() {
254         return workerRedirect;
255     }
256
257     /**
258      * @param workerRedirect
259      * The workerRedirect to set.
260      */

261     public void setWorkerRedirect(String JavaDoc workerRedirect) {
262         this.workerRedirect = workerRedirect;
263     }
264
265     /**
266      * Execute the requested operation.
267      *
268      * @exception BuildException
269      * if an error occurs
270      */

271     public void execute() throws BuildException {
272
273         super.execute();
274         checkParameter();
275         StringBuffer JavaDoc sb = createLink();
276         execute(sb.toString(), null, null, -1);
277
278     }
279
280     /**
281      * Create JkStatus link
282      * <ul>
283      * <li><b>load balance example:
284      * </b>http://localhost/status?cmd=update&mime=txt&w=lb&lf=false&ls=true</li>
285      * <li><b>worker example:
286      * </b>http://localhost/status?cmd=update&mime=txt&w=node1&l=lb&wf=1&wd=false&ws=false
287      * </li>
288      * </ul>
289      *
290      * @return create jkstatus link
291      */

292     private StringBuffer JavaDoc createLink() {
293         // Building URL
294
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
295         try {
296             sb.append("?cmd=update&mime=txt");
297             sb.append("&w=");
298             sb.append(URLEncoder.encode(worker, getCharset()));
299
300             if (isLBMode) {
301                 //http://localhost/status?cmd=update&mime=txt&w=lb&lf=false&ls=true
302
if ((lbRetries != null)) { // > 0
303
sb.append("&lr=");
304                     sb.append(lbRetries);
305                 }
306                 if ((lbRecovertime != null)) { // > 59
307
sb.append("&lt=");
308                     sb.append(lbRecovertime);
309                 }
310                 if ((lbStickySession != null)) {
311                     sb.append("&ls=");
312                     sb.append(lbStickySession);
313                 }
314                 if ((lbForceSession != null)) {
315                     sb.append("&lf=");
316                     sb.append(lbForceSession);
317                 }
318             } else {
319                 //http://localhost/status?cmd=update&mime=txt&w=node1&l=lb&wf=1&wd=false&ws=false
320
if ((workerLb != null)) { // must be configured
321
sb.append("&l=");
322                     sb.append(URLEncoder.encode(workerLb, getCharset()));
323                 }
324                 if ((workerLoadFactor != null)) { // >= 1
325
sb.append("&wf=");
326                     sb.append(workerLoadFactor);
327                 }
328                 if ((workerDisabled != null)) {
329                     sb.append("&wd=");
330                     sb.append(workerDisabled);
331                 }
332                 if ((workerStopped != null)) {
333                     sb.append("&ws=");
334                     sb.append(workerStopped);
335                 }
336                 if ((workerRedirect != null)) { // other worker conrecte lb's
337
sb.append("&wr=");
338                 }
339                 if ((workerClusterDomain != null)) {
340                     sb.append("&wc=");
341                     sb.append(URLEncoder.encode(workerClusterDomain,
342                             getCharset()));
343                 }
344             }
345
346         } catch (UnsupportedEncodingException JavaDoc e) {
347             throw new BuildException("Invalid 'charset' attribute: "
348                     + getCharset());
349         }
350         return sb;
351     }
352
353     /**
354      * check correct lb and worker pararmeter
355      */

356     protected void checkParameter() {
357         if (worker == null) {
358             throw new BuildException("Must specify 'worker' attribute");
359         }
360         if (workerType == null) {
361             throw new BuildException("Must specify 'workerType' attribute");
362         }
363         if ("lb".equals(workerType)) {
364             if (lbRecovertime == null && lbRetries == null) {
365                 throw new BuildException(
366                         "Must specify at a lb worker either 'lbRecovertime' or"
367                                 + "'lbRetries' attribute");
368             }
369             if (lbStickySession == null || lbForceSession == null) {
370                 throw new BuildException("Must specify at a lb worker either"
371                         + "'lbStickySession' and 'lbForceSession' attribute");
372             }
373             if (null != lbRecovertime && 60 < lbRecovertime.intValue()) {
374                 throw new BuildException(
375                         "The 'lbRecovertime' must be greater than 59");
376             }
377             if (null != lbRetries && 1 < lbRetries.intValue()) {
378                 throw new BuildException(
379                         "The 'lbRetries' must be greater than 1");
380             }
381             isLBMode = true;
382         } else if ("worker".equals(workerType)) {
383             if (workerDisabled == null) {
384                 throw new BuildException(
385                         "Must specify at a node worker 'workerDisabled' attribute");
386             }
387             if (workerStopped == null) {
388                 throw new BuildException(
389                         "Must specify at a node worker 'workerStopped' attribute");
390             }
391             if (workerLoadFactor == null ) {
392                 throw new BuildException(
393                         "Must specify at a node worker 'workerLoadFactor' attribute");
394             }
395             if (workerClusterDomain == null) {
396                 throw new BuildException(
397                         "Must specify at a node worker 'workerClusterDomain' attribute");
398             }
399             if (workerRedirect == null) {
400                 throw new BuildException(
401                         "Must specify at a node worker 'workerRedirect' attribute");
402             }
403             if (workerLb == null) {
404                 throw new BuildException("Must specify 'workerLb' attribute");
405             }
406             if (workerLoadFactor.intValue() < 1) {
407                 throw new BuildException(
408                         "The 'workerLoadFactor' must be greater or equal 1");
409             }
410             isLBMode = false;
411         } else {
412             throw new BuildException(
413                     "Only 'lb' and 'worker' supported as workerType attribute");
414         }
415     }
416 }
Popular Tags