KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > cluster > ModJk


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Benoit Pelletier
22  * --------------------------------------------------------------------------
23  * $Id: ModJk.java,v 1.1 2005/06/07 08:21:27 pelletib Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.ant.cluster;
28
29 import java.io.File JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34
35 import org.objectweb.jonas.ant.jonasbase.JEcho;
36 import org.objectweb.jonas.ant.jonasbase.JMkdir;
37 import org.objectweb.jonas.ant.jonasbase.JTouch;
38 import org.objectweb.jonas.ant.jonasbase.Tasks;
39
40 /**
41  * Allow to configure the mod_jk load balancer for Apache
42  * @author Benoit Pelletier
43  */

44 public class ModJk extends Tasks {
45
46     /**
47      * Info for the logger
48      */

49     private static final String JavaDoc INFO = "[ModJk] ";
50
51     /**
52      * Name of the worker file
53      */

54     private static final String JavaDoc MOD_JK_WORKER_FILE = "workers.properties";
55
56     /**
57      * Name of the tomcat_jk file
58      */

59     private static final String JavaDoc MOD_JK_TOMCAT_FILE = "tomcat-jk.conf";
60
61     /**
62      * Worker file
63      */

64     private File JavaDoc fileWorker = null;
65
66     /**
67      * Tomcat config file
68      */

69     private File JavaDoc fileTomcat = null;
70
71     /**
72      * Workers list
73      */

74     private List JavaDoc workersList = null;
75
76     /**
77      * sticky session
78      */

79     private boolean stickySession = false;
80
81     /**
82      * Default constructor
83      */

84     public ModJk() {
85         super();
86         workersList = new ArrayList JavaDoc();
87     }
88
89     /**
90      * Creation of the mod_jk files
91      * @param destDir destination directory
92      */

93     public void createFiles(String JavaDoc destDir) {
94         JMkdir mkdir = new JMkdir();
95         mkdir.setDestDir(new File JavaDoc(destDir));
96         addTask(mkdir);
97
98         JTouch touchWorker = new JTouch();
99         fileWorker = new File JavaDoc(destDir + "/" + MOD_JK_WORKER_FILE);
100         touchWorker.setDestDir(fileWorker);
101         addTask(touchWorker);
102
103         JTouch touchTomcat = new JTouch();
104         fileTomcat = new File JavaDoc(destDir + "/" + MOD_JK_TOMCAT_FILE);
105         touchTomcat.setDestDir(fileTomcat);
106         addTask(touchTomcat);
107    }
108
109     /**
110      * Add a worker
111      * @param portNumber port number
112      * @param lbFactor load balancing factor
113      */

114     public void addWorker(String JavaDoc portNumber, String JavaDoc lbFactor) {
115         Worker worker = new Worker();
116         worker.setPortNumber(portNumber);
117         worker.setLbFactor(lbFactor);
118         workersList.add(worker);
119     }
120
121     /**
122      * get worker definition
123      * @param worker worker to define
124      * @return definition of the worker
125      */

126     private String JavaDoc getWorkerDef(Worker worker) {
127         String JavaDoc workerDef = "\n"
128             +
129             "# -----------------------" + "\n"
130             +
131             "# " + worker.getName() + "\n"
132             +
133             "# -----------------------" + "\n"
134             +
135             "worker." + worker.getName() + ".port=" + worker.getPortNumber() + "\n"
136             +
137             "worker." + worker.getName() + ".host=localhost" + "\n"
138             +
139             "worker." + worker.getName() + ".type=ajp13" + "\n"
140             +
141             "# Load balance factor" + "\n"
142             +
143             "worker." + worker.getName() + ".lbfactor=" + worker.getLbFactor() + "\n"
144             +
145             "# Define prefered failover node for " + worker.getName() + "\n"
146             +
147             "#worker." + worker.getName() + ".redirect=worker2" + "\n"
148             +
149             "# Disable " + worker.getName() + " for all requests except failover" + "\n"
150             +
151             "#worker." + worker.getName() + ".disabled=True" + "\n";
152         return workerDef;
153     }
154
155     /**
156      * creation of the worker file
157      */

158     private void flushWorkerFile() {
159         JEcho echo = new JEcho();
160         echo.setDestDir(fileWorker);
161         String JavaDoc workersNameList = "";
162         String JavaDoc workersDefs = "";
163         int ind = 1;
164         for (Iterator JavaDoc it = this.workersList.iterator(); it.hasNext();) {
165             Worker worker = (Worker) it.next();
166             worker.setName("worker" + ind);
167
168             if (workersNameList.compareTo("") == 0) {
169                 workersNameList = workersNameList + worker.getName();
170             } else {
171                 workersNameList = workersNameList + "," + worker.getName();
172             }
173             workersDefs = workersDefs + getWorkerDef(worker);
174             ind++;
175         }
176
177         String JavaDoc contentFile = "\n"
178             +
179             "# -----------------------" + "\n"
180             +
181             "# List the workers name" + "\n"
182             +
183             "# -----------------------" + "\n"
184             +
185             "worker.list=loadbalancer,jkstatus" + "\n"
186             +
187             workersDefs + "\n"
188             +
189             "# -----------------------" + "\n"
190             +
191             "# Load Balancer worker" + "\n"
192             +
193             "# -----------------------" + "\n"
194             +
195             "worker.loadbalancer.type=lb" + "\n"
196             +
197             "worker.loadbalancer.balanced_workers=" + workersNameList + "\n"
198             +
199             "worker.loadbalancer.sticky_session=" + stickySession + "\n"
200             +
201             "# -----------------------" + "\n"
202             +
203             "# jkstatus worker" + "\n"
204             +
205             "# -----------------------" + "\n"
206             +
207             "worker.jkstatus.type=status" + "\n";
208
209         echo.setMessage(contentFile);
210         addTask(echo);
211     }
212
213     /**
214      * creation of the tomcat-jk file
215      */

216     private void flushTomcatFile() {
217         JEcho echo = new JEcho();
218         echo.setDestDir(fileTomcat);
219
220         String JavaDoc contentFile = "\n"
221             +
222             "LoadModule jk_module modules/mod_jk.so" + "\n"
223             +
224             "# Location of the worker file" + "\n"
225             +
226             "JkWorkersFile /etc/apache2/workers.properties" + "\n"
227             +
228             "# Location of the log file" + "\n"
229             +
230             "JkLogFile /var/log/apache2/mod_jk.log" + "\n"
231             +
232             "# Log level : debug, info, error or emerg" + "\n"
233             +
234             "JkLogLevel emerg" + "\n"
235             +
236             "# Shared Memory Filename ( Only for Unix platform ) required by loadbalancer" + "\n"
237             +
238             "JkShmFile /var/log/apache2/jk.shm" + "\n"
239             +
240             "# Assign specific URL to Tomcat workers" + "\n"
241             +
242             "JkMount /sampleCluster2 loadbalancer" + "\n"
243             +
244             "JkMount /sampleCluster2/* loadbalancer" + "\n"
245             +
246             "# A mount point to the status worker" + "\n"
247             +
248             "JkMount /jkmanager jkstatus" + "\n"
249             +
250             "JkMount /jkmanager/* jkstatus" + "\n"
251             +
252             "# Enable the Jk manager access only from localhost" + "\n"
253             +
254             "<Location /jkmanager/>" + "\n"
255             +
256             " JkMount jkstatus" + "\n"
257             +
258             " Order deny,allow" + "\n"
259             +
260             " Deny from all" + "\n"
261             +
262             " Allow from 127.0.0.1" + "\n"
263             +
264             "</Location>" + "\n";
265
266         echo.setMessage(contentFile);
267         addTask(echo);
268     }
269
270     /**
271      * Generation of the config files
272      */

273     public void flushFiles() {
274         flushWorkerFile();
275         flushTomcatFile();
276     }
277
278     /**
279      * Set sticky Session
280      * @param stickySession to set
281      **/

282     public void setStickySession(boolean stickySession) {
283         this.stickySession = stickySession;
284     }
285
286     /**
287      * Define an inner class for workers
288      * @author Benoit Pelletier
289      */

290     public class Worker {
291
292         /**
293          * port number
294          */

295         private String JavaDoc portNumber = null;
296
297         /**
298          * load balancing factor
299          */

300         private String JavaDoc lbFactor = null;
301
302         /**
303          * name
304          */

305         private String JavaDoc name = null;
306
307         /**
308          * get port number
309          * @return port number
310          */

311         public String JavaDoc getPortNumber() {
312             return portNumber;
313         }
314
315         /**
316          * set port number
317          * @param portNumber port number
318          */

319         public void setPortNumber(String JavaDoc portNumber) {
320             this.portNumber = portNumber;
321         }
322
323         /**
324          * get load balancing factor
325          * @return load balancing factor
326          */

327         public String JavaDoc getLbFactor() {
328             return lbFactor;
329         }
330
331         /**
332          * set load balancing factor
333          * @param lbFactor load balancing factor
334          */

335         public void setLbFactor(String JavaDoc lbFactor) {
336             this.lbFactor = lbFactor;
337         }
338
339         /**
340          * get name
341          * @return name
342          */

343         public String JavaDoc getName() {
344             return name;
345         }
346
347         /**
348          * set name
349          * @param name name to set
350          */

351         public void setName(String JavaDoc name) {
352             this.name = name;
353         }
354     }
355 }
Popular Tags