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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.jk.status;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
23 import org.apache.catalina.ant.AbstractCatalinaTask;
24 import org.apache.tools.ant.BuildException;
27 * Ant task that implements the <code>/status</code> command, supported by the
28 * mod_jk status (1.2.13) application.
31 * @author Peter Rossbach
32 * @version $Revision: 611693 $
36 public class JkStatusUpdateTask extends AbstractCatalinaTask {
39 * The descriptive information about this implementation.
41 private static final String info = "org.apache.jk.status.JkStatusUpdateTask/1.1";
43 private String worker = "lb";
45 private String workerType = "lb";
47 private int internalid = 0;
49 private Integer lbRetries;
51 private Integer lbRecovertime;
53 private Boolean lbStickySession = Boolean.TRUE;
55 private Boolean lbForceSession = Boolean.FALSE;
57 private Integer workerLoadFactor;
59 private String workerJvmRoute ;
61 private int workerDistance = -1;
63 private String workerRedirect;
65 private String workerClusterDomain;
67 private Boolean workerDisabled ;
69 private Boolean workerStopped ;
71 private int workerActivation = -1;
73 private boolean isLBMode = true;
77 private String workerLb;
80 * Return descriptive information about this implementation and the
81 * corresponding version number, in the format
82 * <code><description>/<version></code>.
84 public String getInfo() {
93 public JkStatusUpdateTask() {
95 setUrl("http://localhost/jkstatus");
99 * @return Returns the workerDistance.
101 public int getWorkerDistance() {
102 return workerDistance;
106 * @param workerDistance The workerDistance to set.
108 public void setWorkerDistance(int workerDistance) {
109 this.workerDistance = workerDistance;
113 * @return Returns the workerJvmRoute.
115 public String getWorkerJvmRoute() {
116 return workerJvmRoute;
120 * @param workerJvmRoute The workerJvmRoute to set.
122 public void setWorkerJvmRoute(String workerJvmRoute) {
123 this.workerJvmRoute = workerJvmRoute;
127 * @return Returns the internalid.
129 public int getInternalid() {
135 * The internalid to set.
137 public void setInternalid(int internalid) {
138 this.internalid = internalid;
142 * @return Returns the lbForceSession.
144 public Boolean getLbForceSession() {
145 return lbForceSession;
149 * @param lbForceSession
150 * The lbForceSession to set.
152 public void setLbForceSession(Boolean lbForceSession) {
153 this.lbForceSession = lbForceSession;
157 * @return Returns the lbRecovertime.
159 public Integer getLbRecovertime() {
160 return lbRecovertime;
164 * @param lbRecovertime
165 * The lbRecovertime to set.
167 public void setLbRecovertime(Integer lbRecovertime) {
168 this.lbRecovertime = lbRecovertime;
172 * @return Returns the lbRetries.
174 public Integer getLbRetries() {
180 * The lbRetries to set.
182 public void setLbRetries(Integer lbRetries) {
183 this.lbRetries = lbRetries;
187 * @return Returns the lbStickySession.
189 public Boolean getLbStickySession() {
190 return lbStickySession;
194 * @param lbStickySession
195 * The lbStickySession to set.
197 public void setLbStickySession(Boolean lbStickySession) {
198 this.lbStickySession = lbStickySession;
202 * @return Returns the worker.
204 public String getWorker() {
212 public void setWorker(String worker) {
213 this.worker = worker;
217 * @return Returns the workerType.
219 public String getWorkerType() {
225 * The workerType to set.
227 public void setWorkerType(String workerType) {
228 this.workerType = workerType;
232 * @return Returns the workerLb.
234 public String getWorkerLb() {
240 * The workerLb to set.
242 public void setWorkerLb(String workerLb) {
243 this.workerLb = workerLb;
247 * @return Returns the workerClusterDomain.
249 public String getWorkerClusterDomain() {
250 return workerClusterDomain;
254 * @param workerClusterDomain
255 * The workerClusterDomain to set.
257 public void setWorkerClusterDomain(String workerClusterDomain) {
258 this.workerClusterDomain = workerClusterDomain;
262 * @return Returns the workerDisabled.
264 public Boolean getWorkerDisabled() {
265 return workerDisabled;
269 * @param workerDisabled
270 * The workerDisabled to set.
272 public void setWorkerDisabled(Boolean workerDisabled) {
273 this.workerDisabled = workerDisabled;
277 * @return Returns the workerActivation.
279 public int getWorkerActivation() {
280 return workerActivation;
286 * <li>1 disabled</li>
289 * @param workerActivation The workerActivation to set.
292 public void setWorkerActivation(int workerActivation) {
293 this.workerActivation = workerActivation;
297 * @return Returns the workerStopped.
299 public Boolean getWorkerStopped() {
300 return workerStopped;
304 * @param workerStopped The workerStopped to set.
306 public void setWorkerStopped(Boolean workerStopped) {
307 this.workerStopped = workerStopped;
311 * @return Returns the workerLoadFactor.
313 public Integer getWorkerLoadFactor() {
314 return workerLoadFactor;
318 * @param workerLoadFactor
319 * The workerLoadFactor to set.
321 public void setWorkerLoadFactor(Integer workerLoadFactor) {
322 this.workerLoadFactor = workerLoadFactor;
326 * @return Returns the workerRedirect.
328 public String getWorkerRedirect() {
329 return workerRedirect;
333 * @param workerRedirect
334 * The workerRedirect to set.
336 public void setWorkerRedirect(String workerRedirect) {
337 this.workerRedirect = workerRedirect;
341 * Execute the requested operation.
343 * @exception BuildException
346 public void execute() throws BuildException {
350 StringBuffer sb = createLink();
351 execute(sb.toString(), null, null, -1);
356 * Create JkStatus link
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
364 * <li>vwa=0 active</li>
365 * <li>vwa=1 disabled</li>
366 * <li>vwa=2 stopped</li>
371 * <br/>Loadbalacing parameter:
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>
381 * <br/>Tcp worker parameter:
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>
396 * @return create jkstatus link
398 private StringBuffer createLink() {
400 StringBuffer sb = new StringBuffer();
402 sb.append("?cmd=update&mime=txt");
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
410 sb.append(lbRetries);
412 if ((lbRecovertime != null)) { // > 59
414 sb.append(lbRecovertime);
416 if ((lbStickySession != null)) {
418 sb.append(lbStickySession);
420 if ((lbForceSession != null)) {
422 sb.append(lbForceSession);
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()));
430 sb.append(URLEncoder.encode(worker, getCharset()));
431 if (workerLoadFactor != null) { // >= 1
433 sb.append(workerLoadFactor);
435 if (workerJvmRoute != null) {
437 sb.append(URLEncoder.encode(workerJvmRoute, getCharset()));
439 if (workerDisabled != null) {
441 sb.append(workerDisabled);
443 if (workerStopped != null) {
445 sb.append(workerStopped);
447 if (workerActivation >= 0 && workerActivation < 3) {
449 sb.append(workerActivation);
451 if (workerDistance >= 0) {
453 sb.append(workerDistance);
455 if (workerRedirect != null) { // other worker conrecte lb's
457 sb.append(URLEncoder.encode(workerRedirect,
460 if (workerClusterDomain != null) {
462 sb.append(URLEncoder.encode(workerClusterDomain,
467 } catch (UnsupportedEncodingException e) {
468 throw new BuildException("Invalid 'charset' attribute: "
475 * check correct lb and worker pararmeter
477 protected void checkParameter() {
478 if (worker == null) {
479 throw new BuildException("Must specify 'worker' attribute");
481 if (workerType == null) {
482 throw new BuildException("Must specify 'workerType' attribute");
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");
490 if (lbStickySession == null || lbForceSession == null) {
491 throw new BuildException("Must specify at a lb worker either"
492 + "'lbStickySession' and 'lbForceSession' attribute");
494 if (null != lbRecovertime && 60 < lbRecovertime.intValue()) {
495 throw new BuildException(
496 "The 'lbRecovertime' must be greater than 59");
498 if (null != lbRetries && 1 < lbRetries.intValue()) {
499 throw new BuildException(
500 "The 'lbRetries' must be greater than 1");
503 } else if ("worker".equals(workerType)) {
504 if (workerLoadFactor == null ) {
505 throw new BuildException(
506 "Must specify at a node worker 'workerLoadFactor' attribute");
508 if (workerClusterDomain == null) {
509 throw new BuildException(
510 "Must specify at a node worker 'workerClusterDomain' attribute");
512 if (workerRedirect == null) {
513 throw new BuildException(
514 "Must specify at a node worker 'workerRedirect' attribute");
516 if (workerLb == null) {
517 throw new BuildException("Must specify 'workerLb' attribute");
519 if (workerLoadFactor.intValue() < 1) {
520 throw new BuildException(
521 "The 'workerLoadFactor' must be greater or equal 1");
525 throw new BuildException(
526 "Only 'lb' and 'worker' supported as workerType attribute");