1 26 27 package org.objectweb.jonas.ant.cluster; 28 29 import java.io.File ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 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 44 public class ModJk extends Tasks { 45 46 49 private static final String INFO = "[ModJk] "; 50 51 54 private static final String MOD_JK_WORKER_FILE = "workers.properties"; 55 56 59 private static final String MOD_JK_TOMCAT_FILE = "tomcat-jk.conf"; 60 61 64 private File fileWorker = null; 65 66 69 private File fileTomcat = null; 70 71 74 private List workersList = null; 75 76 79 private boolean stickySession = false; 80 81 84 public ModJk() { 85 super(); 86 workersList = new ArrayList (); 87 } 88 89 93 public void createFiles(String destDir) { 94 JMkdir mkdir = new JMkdir(); 95 mkdir.setDestDir(new File (destDir)); 96 addTask(mkdir); 97 98 JTouch touchWorker = new JTouch(); 99 fileWorker = new File (destDir + "/" + MOD_JK_WORKER_FILE); 100 touchWorker.setDestDir(fileWorker); 101 addTask(touchWorker); 102 103 JTouch touchTomcat = new JTouch(); 104 fileTomcat = new File (destDir + "/" + MOD_JK_TOMCAT_FILE); 105 touchTomcat.setDestDir(fileTomcat); 106 addTask(touchTomcat); 107 } 108 109 114 public void addWorker(String portNumber, String lbFactor) { 115 Worker worker = new Worker(); 116 worker.setPortNumber(portNumber); 117 worker.setLbFactor(lbFactor); 118 workersList.add(worker); 119 } 120 121 126 private String getWorkerDef(Worker worker) { 127 String 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 158 private void flushWorkerFile() { 159 JEcho echo = new JEcho(); 160 echo.setDestDir(fileWorker); 161 String workersNameList = ""; 162 String workersDefs = ""; 163 int ind = 1; 164 for (Iterator 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 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 216 private void flushTomcatFile() { 217 JEcho echo = new JEcho(); 218 echo.setDestDir(fileTomcat); 219 220 String 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 273 public void flushFiles() { 274 flushWorkerFile(); 275 flushTomcatFile(); 276 } 277 278 282 public void setStickySession(boolean stickySession) { 283 this.stickySession = stickySession; 284 } 285 286 290 public class Worker { 291 292 295 private String portNumber = null; 296 297 300 private String lbFactor = null; 301 302 305 private String name = null; 306 307 311 public String getPortNumber() { 312 return portNumber; 313 } 314 315 319 public void setPortNumber(String portNumber) { 320 this.portNumber = portNumber; 321 } 322 323 327 public String getLbFactor() { 328 return lbFactor; 329 } 330 331 335 public void setLbFactor(String lbFactor) { 336 this.lbFactor = lbFactor; 337 } 338 339 343 public String getName() { 344 return name; 345 } 346 347 351 public void setName(String name) { 352 this.name = name; 353 } 354 } 355 } | Popular Tags |