KickJava   Java API By Example, From Geeks To Geeks.

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


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: Director.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 director Load Balancer for Apache
42  * @author Benoit Pelletier
43  */

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

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

54     private static final String JavaDoc DIRECTOR_FILE = "enhydra_director.conf";
55
56     /**
57      * Configuration file
58      */

59     private File JavaDoc configurationFile = null;
60
61     /**
62      * Application Server list
63      */

64     private List JavaDoc appServerList = null;
65
66     /**
67      * sticky session
68      */

69     private boolean stickySession = false;
70
71     /**
72      * Default constructor
73      */

74     public Director() {
75         super();
76         appServerList = new ArrayList JavaDoc();
77     }
78
79     /**
80      * Creation of the Director file
81      * @param destDir destination directory
82      */

83     public void createFile(String JavaDoc destDir) {
84         JMkdir mkdir = new JMkdir();
85         mkdir.setDestDir(new File JavaDoc(destDir));
86         addTask(mkdir);
87
88         JTouch touchWorker = new JTouch();
89         configurationFile = new File JavaDoc(destDir + "/" + DIRECTOR_FILE);
90         touchWorker.setDestDir(configurationFile);
91         addTask(touchWorker);
92
93    }
94
95     /**
96      * Add an application server
97      * @param portNumber port number
98      * @param lbFactor load balancing factor
99      */

100     public void addAppServer(String JavaDoc portNumber, String JavaDoc lbFactor) {
101         AppServer appServer = new AppServer();
102         appServer.setPortNumber(portNumber);
103         appServer.setLbFactor(lbFactor);
104         appServerList.add(appServer);
105     }
106
107     /**
108      * get app server definition
109      * @param appServer application server to define
110      * @return definition of the application server
111      */

112     private String JavaDoc getAppServerDef(AppServer appServer) {
113         String JavaDoc appServerDef = "\n"
114             + " <AppServer host= \"localhost\" port=\""
115             + appServer.getPortNumber()
116             + "\" weight=\""
117             + appServer.getLbFactor()
118             + "\" />";
119         return appServerDef;
120     }
121
122     /**
123      * creation of the AppServer file
124      */

125     private void flushAppServerFile() {
126         JEcho echo = new JEcho();
127         echo.setDestDir(configurationFile);
128         String JavaDoc appServersDefs = "";
129         int ind = 1;
130         for (Iterator JavaDoc it = this.appServerList.iterator(); it.hasNext();) {
131             AppServer appServer = (AppServer) it.next();
132             appServer.setName("appServer" + ind);
133             appServersDefs = appServersDefs + getAppServerDef(appServer);
134             ind++;
135         }
136
137         String JavaDoc contentFile = "\n"
138             + "<?xml version=\"1.0\"?>" + "\n"
139             + "<!DOCTYPE EnhydraDirectorConfig SYSTEM \"EnhydraDirectorConfig.dtd\">" + "\n"
140             + "<EnhydraDirectorConfig>" + "\n"
141             + " <Application prefix=\"/sampleCluster2/\">"
142             + appServersDefs + "\n"
143             + " </Application>" + "\n"
144             + " <Status prefix=\"/status\">" + "\n"
145             + " <Restrict server=\"127.0.0.1\" />" + "\n"
146             + " <Restrict client=\"127.0.0.1\" />" + "\n"
147             + " </Status>" + "\n"
148             + "</EnhydraDirectorConfig>";
149
150         echo.setMessage(contentFile);
151         addTask(echo);
152     }
153
154     /**
155      * Generation of the config files
156      */

157     public void flushFile() {
158         flushAppServerFile();
159     }
160
161     /**
162      * Set sticky Session
163      * @param stickySession to set
164      **/

165     public void setStickySession(boolean stickySession) {
166         this.stickySession = stickySession;
167     }
168
169     /**
170      * Define an inner class for application servers
171      * @author Benoit Pelletier
172      */

173     public class AppServer {
174
175         /**
176          * port number
177          */

178         private String JavaDoc portNumber = null;
179
180         /**
181          * load balancing factor
182          */

183         private String JavaDoc lbFactor = null;
184
185         /**
186          * name
187          */

188         private String JavaDoc name = null;
189
190         /**
191          * get port number
192          * @return port number
193          */

194         public String JavaDoc getPortNumber() {
195             return portNumber;
196         }
197
198         /**
199          * set port number
200          * @param portNumber port number
201          */

202         public void setPortNumber(String JavaDoc portNumber) {
203             this.portNumber = portNumber;
204         }
205
206         /**
207          * get load balancing factor
208          * @return load balancing factor
209          */

210         public String JavaDoc getLbFactor() {
211             return lbFactor;
212         }
213
214         /**
215          * set load balancing factor
216          * @param lbFactor load balancing factor
217          */

218         public void setLbFactor(String JavaDoc lbFactor) {
219             this.lbFactor = lbFactor;
220         }
221
222         /**
223          * get name
224          * @return name
225          */

226         public String JavaDoc getName() {
227             return name;
228         }
229
230         /**
231          * set name
232          * @param name name to set
233          */

234         public void setName(String JavaDoc name) {
235             this.name = name;
236         }
237     }
238 }
Popular Tags