001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
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: $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.ant.cluster;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.util.Iterator;
030:
031: import javax.xml.parsers.DocumentBuilder;
032: import javax.xml.parsers.DocumentBuilderFactory;
033: import javax.xml.parsers.ParserConfigurationException;
034:
035: import org.apache.tools.ant.BuildException;
036:
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041: import org.w3c.dom.Text;
042: import org.xml.sax.SAXException;
043:
044: import org.objectweb.jonas.ant.jonasbase.BaseTaskItf;
045: import org.objectweb.jonas.ant.jonasbase.Carol;
046: import org.objectweb.jonas.ant.jonasbase.XMLSerializerTask;
047:
048: /**
049: * Configures the cluster daemon
050: * @author Benoit Pelletier
051: */
052: public class ClusterDaemon extends ClusterTasks {
053:
054: /**
055: * Info for the logger
056: */
057: private static final String INFO = "[ClusterDaemon] ";
058:
059: /**
060: * Name of the configuration file
061: */
062: private static final String CLUSTER_DAEMON_CONF_FILE = "clusterd.xml";
063:
064: /**
065: * Domain.xml structure
066: */
067: private Document clusterDaemonDoc = null;
068:
069: /**
070: * Flag indicating if the document has been loaded
071: */
072: private boolean clusterDaemonDocLoaded = false;
073:
074: /**
075: * name
076: */
077: private String name = null;
078:
079: /**
080: * domain name
081: */
082: private String domainName = null;
083:
084: /**
085: * directory
086: */
087: private String cdDir = null;
088:
089: /**
090: * protocol
091: */
092: private String protocol = null;
093:
094: /**
095: * port number
096: */
097: private String port = null;
098:
099: /**
100: * jdk
101: */
102: private String jdk = null;
103:
104: /**
105: * interaction mode
106: */
107: private String interactionMode = null;
108:
109: /**
110: * autoboot
111: */
112: private String autoBoot = null;
113:
114: /**
115: * xprm
116: */
117: private String xprm = null;
118:
119: /**
120: * nodes nb
121: */
122: private int instNb = 0;
123:
124: /**
125: * JONAS_ROOT
126: */
127: private String jonasRoot = null;
128:
129: /**
130: * nodes name
131: */
132: private String clusterNodesName = null;
133:
134: /**
135: * Default constructor
136: */
137: public ClusterDaemon() {
138: super ();
139: }
140:
141: /**
142: * load the clusterd.xml file in a DOM structure
143: */
144: private void loadClusterDaemonXmlDoc() {
145:
146: if (!clusterDaemonDocLoaded) {
147:
148: // Load the orignal configuration file
149: DocumentBuilderFactory factory = DocumentBuilderFactory
150: .newInstance();
151: DocumentBuilder docBuilder = null;
152: try {
153: docBuilder = factory.newDocumentBuilder();
154: } catch (ParserConfigurationException e) {
155: throw new BuildException(INFO
156: + "Exception during loadClusterDaemonXmlDoc", e);
157: }
158: try {
159: clusterDaemonDoc = docBuilder.parse(jonasRoot
160: + File.separator + "conf" + File.separator
161: + CLUSTER_DAEMON_CONF_FILE);
162: } catch (SAXException e) {
163: throw new BuildException(INFO
164: + "Error during parsing of the file "
165: + CLUSTER_DAEMON_CONF_FILE, e);
166: } catch (IOException e) {
167: throw new BuildException(INFO
168: + "Error during parsing of the file "
169: + CLUSTER_DAEMON_CONF_FILE, e);
170: }
171:
172: Element root = clusterDaemonDoc.getDocumentElement();
173:
174: // Remove the default servers list
175: NodeList serverNodeL = root.getElementsByTagName("server");
176: for (int i = 0; i < serverNodeL.getLength(); i++) {
177: Node n = serverNodeL.item(i);
178: root.removeChild(n);
179: }
180:
181: // Prepare the serialization
182: XMLSerializerTask xmlSerTask = new XMLSerializerTask();
183: xmlSerTask.setXmlDoc(clusterDaemonDoc);
184: xmlSerTask.setXmlFileName(CLUSTER_DAEMON_CONF_FILE);
185:
186: addTask(xmlSerTask);
187:
188: clusterDaemonDocLoaded = true;
189: }
190: }
191:
192: /**
193: * Add the servers definition in the clusterd.xml
194: */
195: public void addServersDefinition() {
196:
197: // Load the orignal configuration file
198: loadClusterDaemonXmlDoc();
199:
200: Element root = clusterDaemonDoc.getDocumentElement();
201:
202: for (int i = 1; i <= instNb; i++) {
203:
204: // Element server
205: Node s = clusterDaemonDoc.createElement("server");
206: root.appendChild(s);
207:
208: // Element name
209: Node sn = clusterDaemonDoc.createElement("name");
210: s.appendChild(sn);
211:
212: Text tsn = clusterDaemonDoc.createTextNode(clusterNodesName
213: + i);
214: sn.appendChild(tsn);
215:
216: // Element description
217: Node sd = clusterDaemonDoc.createElement("description");
218: s.appendChild(sd);
219:
220: Text tsd = clusterDaemonDoc.createTextNode(clusterNodesName
221: + i);
222: sd.appendChild(tsd);
223:
224: // Element java-home
225: Node sjdk = clusterDaemonDoc.createElement("java-home");
226: s.appendChild(sjdk);
227:
228: Text tsjdk = clusterDaemonDoc.createTextNode(jdk);
229: sjdk.appendChild(tsjdk);
230:
231: // Element jonas-root
232: Node sjr = clusterDaemonDoc.createElement("jonas-root");
233: s.appendChild(sjr);
234:
235: Text tsjr = clusterDaemonDoc.createTextNode(jonasRoot);
236: sjr.appendChild(tsjr);
237:
238: // Element jonas-base
239: Node sjb = clusterDaemonDoc.createElement("jonas-base");
240: s.appendChild(sjb);
241: String jb = getDestDirPrefix() + i;
242:
243: if (File.separatorChar == '/') {
244: jb = jb.replace('\\', File.separatorChar);
245: } else {
246: jb = jb.replace('/', File.separatorChar);
247: }
248:
249: Text tsjb = clusterDaemonDoc
250: .createTextNode(getDestDirPrefix() + i);
251: sjb.appendChild(tsjb);
252:
253: // Element xprm
254: Node sxprm = clusterDaemonDoc.createElement("xprm");
255: s.appendChild(sxprm);
256:
257: Text tsxprm = clusterDaemonDoc.createTextNode(xprm);
258: sxprm.appendChild(tsxprm);
259:
260: // Element auto-boot
261: Node sab = clusterDaemonDoc.createElement("auto-boot");
262: s.appendChild(sab);
263:
264: Text tsab = clusterDaemonDoc.createTextNode(autoBoot);
265: sab.appendChild(tsab);
266: }
267:
268: }
269:
270: /**
271: * update the name
272: */
273: public void updateName() {
274:
275: // Load the orignal configuration file
276: loadClusterDaemonXmlDoc();
277:
278: Element root = clusterDaemonDoc.getDocumentElement();
279:
280: // update the name
281: NodeList nameNodeL = root.getElementsByTagName("name");
282: nameNodeL.item(0).getFirstChild().setNodeValue(name);
283:
284: }
285:
286: /**
287: * update the domain name
288: */
289: public void updateDomainName() {
290:
291: // Load the orignal configuration file
292: loadClusterDaemonXmlDoc();
293:
294: Element root = clusterDaemonDoc.getDocumentElement();
295:
296: // update the domain name
297: NodeList nameNodeL = root.getElementsByTagName("domain-name");
298: nameNodeL.item(0).getFirstChild().setNodeValue(domainName);
299:
300: }
301:
302: /**
303: * update the interaction mode
304: */
305: public void updateInteractionMode() {
306:
307: // Load the orignal configuration file
308: loadClusterDaemonXmlDoc();
309:
310: Element root = clusterDaemonDoc.getDocumentElement();
311:
312: // update the domain name
313: NodeList nameNodeL = root
314: .getElementsByTagName("jonas-interaction-mode");
315: nameNodeL.item(0).getFirstChild().setNodeValue(interactionMode);
316:
317: }
318:
319: /**
320: * update the carol.propertier
321: */
322: public void updateCarol() {
323:
324: Carol carol = new Carol();
325: carol.setDefaultPort(port);
326: carol.setProtocols(protocol);
327:
328: addTasks(carol);
329:
330: }
331:
332: /**
333: * Set nodes number
334: * @param instNb nodes nb
335: */
336: public void setInstNb(int instNb) {
337: this .instNb = instNb;
338: }
339:
340: /**
341: * cluster daemon directory
342: * @param cdDir directory
343: */
344: public void setCdDir(String cdDir) {
345: this .cdDir = cdDir;
346: if (File.separatorChar == '/') {
347: this .cdDir = this .cdDir.replace('\\', File.separatorChar);
348: } else {
349: this .cdDir = this .cdDir.replace('/', File.separatorChar);
350: }
351: }
352:
353: /**
354: * Set the name
355: * @param name name
356: */
357: public void setName(String name) {
358: this .name = name;
359: }
360:
361: /**
362: * Set the domain name
363: * @param domainName domain name
364: */
365: public void setDomainName(String domainName) {
366: this .domainName = domainName;
367: }
368:
369: /**
370: * interaction mode
371: * @param interactionMode loosely/tighly coupled
372: */
373: public void setInteractionMode(String interactionMode) {
374: this .interactionMode = interactionMode;
375: }
376:
377: /**
378: * jdk to use
379: * @param jdk jdk
380: */
381: public void setJdk(String jdk) {
382:
383: this .jdk = jdk;
384:
385: if (File.separatorChar == '/') {
386: this .jdk = this .jdk.replace('\\', File.separatorChar);
387: } else {
388: this .jdk = this .jdk.replace('/', File.separatorChar);
389: }
390: }
391:
392: /**
393: * port number
394: * @param port port number
395: */
396: public void setPort(String port) {
397: this .port = port;
398: }
399:
400: /**
401: * set the protocol
402: * @param protocol protocol
403: */
404: public void setProtocol(String protocol) {
405: this .protocol = protocol;
406: }
407:
408: /**
409: * Set JONAS_ROOT
410: * @param jonasRoot JONAS_ROOT
411: */
412: public void setJonasRoot(String jonasRoot) {
413: this .jonasRoot = jonasRoot;
414:
415: if (File.separatorChar == '/') {
416: this .jonasRoot = this .jonasRoot.replace('\\',
417: File.separatorChar);
418: } else {
419: this .jonasRoot = this .jonasRoot.replace('/',
420: File.separatorChar);
421: }
422:
423: }
424:
425: /**
426: * set the name prefix for the cluster nodes
427: * @param clusterNodesName prefix of the nodes names in the cluster
428: */
429: public void setClusterNodesName(String clusterNodesName) {
430: this .clusterNodesName = clusterNodesName;
431: }
432:
433: /**
434: * Set auto boot
435: * @param autoBoot true/false
436: */
437: public void setAutoBoot(String autoBoot) {
438: this .autoBoot = autoBoot;
439: }
440:
441: /**
442: * Set xprm
443: * @param xprm xprm
444: */
445: public void setXprm(String xprm) {
446: this .xprm = xprm;
447: }
448:
449: /**
450: * Generates the script tasks
451: */
452: public void generatesTasks() {
453:
454: updateDomainName();
455: updateInteractionMode();
456: addServersDefinition();
457: updateCarol();
458:
459: // set destDir for each task
460: for (Iterator it = this .getTasks().iterator(); it.hasNext();) {
461: BaseTaskItf task = (BaseTaskItf) it.next();
462: task.setDestDir(new File(cdDir));
463: }
464: }
465:
466: }
|