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: Director.java 7936 2006-01-25 17:00:15Z sauthieg $
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 director Load Balancer for Apache
039: * @author Benoit Pelletier
040: */
041: public class Director extends Tasks {
042:
043: /**
044: * Info for the logger
045: */
046: private static final String INFO = "[Director] ";
047:
048: /**
049: * Name of the director conf file
050: */
051: private static final String DIRECTOR_FILE = "enhydra_director.conf";
052:
053: /**
054: * Configuration file
055: */
056: private File configurationFile = null;
057:
058: /**
059: * Application Server list
060: */
061: private List appServerList = null;
062:
063: /**
064: * sticky session
065: */
066: private boolean stickySession = false;
067:
068: /**
069: * Default constructor
070: */
071: public Director() {
072: super ();
073: appServerList = new ArrayList();
074: }
075:
076: /**
077: * Creation of the Director file
078: * @param destDir destination directory
079: */
080: public void createFile(String destDir) {
081: JMkdir mkdir = new JMkdir();
082: mkdir.setDestDir(new File(destDir));
083: addTask(mkdir);
084:
085: JTouch touchWorker = new JTouch();
086: configurationFile = new File(destDir + "/" + DIRECTOR_FILE);
087: touchWorker.setDestDir(configurationFile);
088: addTask(touchWorker);
089:
090: }
091:
092: /**
093: * Add an application server
094: * @param portNumber port number
095: * @param lbFactor load balancing factor
096: */
097: public void addAppServer(String portNumber, String lbFactor) {
098: AppServer appServer = new AppServer();
099: appServer.setPortNumber(portNumber);
100: appServer.setLbFactor(lbFactor);
101: appServerList.add(appServer);
102: }
103:
104: /**
105: * get app server definition
106: * @param appServer application server to define
107: * @return definition of the application server
108: */
109: private String getAppServerDef(AppServer appServer) {
110: String appServerDef = "\n"
111: + " <AppServer host= \"localhost\" port=\""
112: + appServer.getPortNumber() + "\" weight=\""
113: + appServer.getLbFactor() + "\" />";
114: return appServerDef;
115: }
116:
117: /**
118: * creation of the AppServer file
119: */
120: private void flushAppServerFile() {
121: JEcho echo = new JEcho();
122: echo.setDestDir(configurationFile);
123: String appServersDefs = "";
124: int ind = 1;
125: for (Iterator it = this .appServerList.iterator(); it.hasNext();) {
126: AppServer appServer = (AppServer) it.next();
127: appServer.setName("appServer" + ind);
128: appServersDefs = appServersDefs
129: + getAppServerDef(appServer);
130: ind++;
131: }
132:
133: String contentFile = "\n"
134: + "<?xml version=\"1.0\"?>"
135: + "\n"
136: + "<!DOCTYPE EnhydraDirectorConfig SYSTEM \"EnhydraDirectorConfig.dtd\">"
137: + "\n" + "<EnhydraDirectorConfig>" + "\n"
138: + " <Application prefix=\"/sampleCluster2/\">"
139: + appServersDefs + "\n" + " </Application>" + "\n"
140: + " <Status prefix=\"/status\">" + "\n"
141: + " <Restrict server=\"127.0.0.1\" />" + "\n"
142: + " <Restrict client=\"127.0.0.1\" />" + "\n"
143: + " </Status>" + "\n" + "</EnhydraDirectorConfig>";
144:
145: echo.setMessage(contentFile);
146: echo.setLogInfo(INFO + "Flushing AppServer Configuration in '"
147: + configurationFile + "'");
148: addTask(echo);
149: }
150:
151: /**
152: * Generation of the config files
153: */
154: public void flushFile() {
155: flushAppServerFile();
156: }
157:
158: /**
159: * Set sticky Session
160: * @param stickySession to set
161: **/
162: public void setStickySession(boolean stickySession) {
163: this .stickySession = stickySession;
164: }
165:
166: /**
167: * Define an inner class for application servers
168: * @author Benoit Pelletier
169: */
170: public class AppServer {
171:
172: /**
173: * port number
174: */
175: private String portNumber = null;
176:
177: /**
178: * load balancing factor
179: */
180: private String lbFactor = null;
181:
182: /**
183: * name
184: */
185: private String name = null;
186:
187: /**
188: * get port number
189: * @return port number
190: */
191: public String getPortNumber() {
192: return portNumber;
193: }
194:
195: /**
196: * set port number
197: * @param portNumber port number
198: */
199: public void setPortNumber(String portNumber) {
200: this .portNumber = portNumber;
201: }
202:
203: /**
204: * get load balancing factor
205: * @return load balancing factor
206: */
207: public String getLbFactor() {
208: return lbFactor;
209: }
210:
211: /**
212: * set load balancing factor
213: * @param lbFactor load balancing factor
214: */
215: public void setLbFactor(String lbFactor) {
216: this .lbFactor = lbFactor;
217: }
218:
219: /**
220: * get name
221: * @return name
222: */
223: public String getName() {
224: return name;
225: }
226:
227: /**
228: * set name
229: * @param name name to set
230: */
231: public void setName(String name) {
232: this.name = name;
233: }
234: }
235: }
|