001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005-2006 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Benoit Pelletier
022: * --------------------------------------------------------------------------
023: * $Id: ModJk.java 8383 2006-05-24 08:30:06Z pelletib $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.ant.cluster;
026:
027: import java.io.File;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import org.objectweb.jonas.ant.jonasbase.JEcho;
033: import org.objectweb.jonas.ant.jonasbase.JMkdir;
034: import org.objectweb.jonas.ant.jonasbase.JTouch;
035: import org.objectweb.jonas.ant.jonasbase.Tasks;
036:
037: /**
038: * Allow to configure the mod_jk load balancer for Apache
039: * @author Benoit Pelletier
040: */
041: public class ModJk extends Tasks {
042:
043: /**
044: * Info for the logger
045: */
046: private static final String INFO = "[ModJk] ";
047:
048: /**
049: * Name of the worker file
050: */
051: private static final String MOD_JK_WORKER_FILE = "workers.properties";
052:
053: /**
054: * Name of the tomcat_jk file
055: */
056: private static final String MOD_JK_TOMCAT_FILE = "tomcat_jk.conf";
057:
058: /**
059: * Name of the httpd.conf file
060: */
061: private static final String HTTPD_CONF_FILE = "httpd.conf";
062:
063: /**
064: * Worker file
065: */
066: private File fileWorker = null;
067:
068: /**
069: * Tomcat config file
070: */
071: private File fileTomcat = null;
072:
073: /**
074: * Httpd config file
075: */
076: private File fileHttpd = null;
077:
078: /**
079: * Workers list
080: */
081: private List workersList = null;
082:
083: /**
084: * sticky session
085: */
086: private boolean stickySession = false;
087:
088: /**
089: * root dir
090: */
091: private String rootDir = null;
092:
093: /**
094: * mod_jk dir
095: */
096: private String dir = null;
097:
098: /**
099: * Default constructor
100: */
101: public ModJk() {
102: super ();
103: workersList = new ArrayList();
104: }
105:
106: /**
107: * Creation of the mod_jk files
108: */
109: public void createFiles() {
110: String modJkDir = rootDir + File.separator + dir;
111: JMkdir mkdir = new JMkdir();
112: mkdir.setDestDir(new File(modJkDir));
113: addTask(mkdir);
114:
115: JTouch touchWorker = new JTouch();
116: fileWorker = new File(modJkDir + "/" + MOD_JK_WORKER_FILE);
117: touchWorker.setDestDir(fileWorker);
118: addTask(touchWorker);
119:
120: JTouch touchTomcat = new JTouch();
121: fileTomcat = new File(modJkDir + "/" + MOD_JK_TOMCAT_FILE);
122: touchTomcat.setDestDir(fileTomcat);
123: addTask(touchTomcat);
124:
125: // JTouch touchHttpd = new JTouch();
126: // fileHttpd = new File(modJkDir + "/" + HTTPD_CONF_FILE);
127: // touchHttpd.setDestDir(fileHttpd);
128: // addTask(touchHttpd);
129:
130: }
131:
132: /**
133: * Add a worker
134: * @param portNumber port number
135: * @param lbFactor load balancing factor
136: * @return worker name
137: */
138: public String addWorker(String portNumber, String lbFactor) {
139: Worker worker = new Worker();
140: worker.setPortNumber(portNumber);
141: worker.setLbFactor(lbFactor);
142: int index = workersList.size() + 1;
143: worker.setName("worker" + index);
144: workersList.add(worker);
145: return worker.getName();
146: }
147:
148: /**
149: * get worker definition
150: * @param worker worker to define
151: * @return definition of the worker
152: */
153: private String getWorkerDef(Worker worker) {
154: String workerDef = "\n" + "# -----------------------" + "\n"
155: + "# "
156: + worker.getName()
157: + "\n"
158: + "# -----------------------"
159: + "\n"
160: + "worker."
161: + worker.getName()
162: + ".port="
163: + worker.getPortNumber()
164: + "\n"
165: + "worker."
166: + worker.getName()
167: + ".host=localhost"
168: + "\n"
169: + "worker."
170: + worker.getName()
171: + ".type=ajp13"
172: + "\n"
173: + "# Load balance factor"
174: + "\n"
175: + "worker."
176: + worker.getName()
177: + ".lbfactor="
178: + worker.getLbFactor()
179: + "\n"
180: + "# Define prefered failover node for "
181: + worker.getName()
182: + "\n"
183: + "#worker."
184: + worker.getName()
185: + ".redirect=worker2"
186: + "\n"
187: + "# Disable "
188: + worker.getName()
189: + " for all requests except failover"
190: + "\n"
191: + "#worker."
192: + worker.getName()
193: + ".disabled=True"
194: + "\n";
195: return workerDef;
196: }
197:
198: /**
199: * creation of the worker file
200: */
201: private void flushWorkerFile() {
202: JEcho echo = new JEcho();
203: echo.setDestDir(fileWorker);
204: String workersNameList = "";
205: String workersDefs = "";
206: for (Iterator it = this .workersList.iterator(); it.hasNext();) {
207: Worker worker = (Worker) it.next();
208:
209: if (workersNameList.compareTo("") == 0) {
210: workersNameList = workersNameList + worker.getName();
211: } else {
212: workersNameList = workersNameList + ","
213: + worker.getName();
214: }
215: workersDefs = workersDefs + getWorkerDef(worker);
216: }
217:
218: String contentFile = "\n" + "# -----------------------" + "\n"
219: + "# List the workers name" + "\n"
220: + "# -----------------------" + "\n"
221: + "worker.list=loadbalancer,jkstatus" + "\n"
222: + workersDefs
223: + "\n"
224: + "# -----------------------"
225: + "\n"
226: + "# Load Balancer worker"
227: + "\n"
228: + "# -----------------------"
229: + "\n"
230: + "worker.loadbalancer.type=lb"
231: + "\n"
232: + "worker.loadbalancer.balanced_workers="
233: + workersNameList
234: + "\n"
235: + "worker.loadbalancer.sticky_session="
236: + stickySession
237: + "\n"
238: + "# -----------------------"
239: + "\n"
240: + "# jkstatus worker"
241: + "\n"
242: + "# -----------------------"
243: + "\n"
244: + "worker.jkstatus.type=status" + "\n";
245:
246: echo.setMessage(contentFile);
247: echo.setLogInfo(INFO + "Flushing Configuration in '"
248: + fileWorker + "'");
249: addTask(echo);
250: }
251:
252: /**
253: * creation of the tomcat-jk file
254: */
255: private void flushTomcatFile() {
256: JEcho echo = new JEcho();
257: echo.setDestDir(fileTomcat);
258:
259: String contentFile = "\n"
260: + "LoadModule jk_module modules/mod_jk.so" + "\n"
261: + "# Location of the worker file" + "\n"
262: + "JkWorkersFile "
263: + this .dir
264: + File.separator
265: + MOD_JK_WORKER_FILE
266: + "\n"
267: + "# Location of the log file"
268: + "\n"
269: + "JkLogFile "
270: + this .dir
271: + File.separator
272: + "mod_jk.log"
273: + "\n"
274: + "# Log level : debug, info, error or emerg"
275: + "\n"
276: + "JkLogLevel emerg"
277: + "\n"
278: + "# Shared Memory Filename ( Only for Unix platform ) required by loadbalancer"
279: + "\n"
280: + "JkShmFile "
281: + this .dir
282: + File.separator
283: + "jk.shm"
284: + "\n"
285: + "# Assign specific URL to Tomcat workers"
286: + "\n"
287: + "JkMount /sampleCluster2 loadbalancer"
288: + "\n"
289: + "JkMount /sampleCluster2/* loadbalancer"
290: + "\n"
291: + "# A mount point to the status worker"
292: + "\n"
293: + "JkMount /jkmanager jkstatus"
294: + "\n"
295: + "JkMount /jkmanager/* jkstatus"
296: + "\n"
297: + "# Enable the Jk manager access only from localhost"
298: + "\n"
299: + "<Location /jkmanager/>"
300: + "\n"
301: + " JkMount jkstatus"
302: + "\n"
303: + " Order deny,allow"
304: + "\n"
305: + " Deny from all"
306: + "\n"
307: + " Allow from 127.0.0.1"
308: + "\n"
309: + "</Location>" + "\n";
310:
311: echo.setMessage(contentFile);
312: echo.setLogInfo(INFO + "Flushing Configuration in '"
313: + fileTomcat + "'");
314: addTask(echo);
315: }
316:
317: /**
318: * creation of the httpd.conf file
319: */
320: private void flushHttpdFile() {
321: JEcho echo = new JEcho();
322: echo.setDestDir(fileHttpd);
323:
324: String contentFile = "\n" + "Include " + this .dir
325: + File.separator + MOD_JK_TOMCAT_FILE + "\n";
326:
327: echo.setMessage(contentFile);
328: echo.setLogInfo(INFO + "Flushing Configuration in '"
329: + fileHttpd + "'");
330: addTask(echo);
331: }
332:
333: /**
334: * Generation of the config files
335: */
336: public void flushFiles() {
337: flushWorkerFile();
338: flushTomcatFile();
339: // flushHttpdFile();
340: }
341:
342: /**
343: * Set sticky Session
344: * @param stickySession to set
345: **/
346: public void setStickySession(boolean stickySession) {
347: this .stickySession = stickySession;
348: }
349:
350: /**
351: * Define an inner class for workers
352: * @author Benoit Pelletier
353: */
354: public class Worker {
355:
356: /**
357: * port number
358: */
359: private String portNumber = null;
360:
361: /**
362: * load balancing factor
363: */
364: private String lbFactor = null;
365:
366: /**
367: * name
368: */
369: private String name = null;
370:
371: /**
372: * get port number
373: * @return port number
374: */
375: public String getPortNumber() {
376: return portNumber;
377: }
378:
379: /**
380: * set port number
381: * @param portNumber port number
382: */
383: public void setPortNumber(String portNumber) {
384: this .portNumber = portNumber;
385: }
386:
387: /**
388: * get load balancing factor
389: * @return load balancing factor
390: */
391: public String getLbFactor() {
392: return lbFactor;
393: }
394:
395: /**
396: * set load balancing factor
397: * @param lbFactor load balancing factor
398: */
399: public void setLbFactor(String lbFactor) {
400: this .lbFactor = lbFactor;
401: }
402:
403: /**
404: * get name
405: * @return name
406: */
407: public String getName() {
408: return name;
409: }
410:
411: /**
412: * set name
413: * @param name name to set
414: */
415: public void setName(String name) {
416: this .name = name;
417: }
418: }
419:
420: /**
421: * Set the mod_jk directory
422: * @param dir directory
423: */
424: public void setDir(String dir) {
425: this .dir = dir;
426: }
427:
428: /**
429: * Set the root directory
430: * @param dir directory
431: */
432: public void setRootDir(String dir) {
433: this.rootDir = dir;
434: }
435: }
|