001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 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: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: Discovery.java 9301 2006-08-02 07:40:51Z pelletib $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.ant.jonasbase;
026:
027: import java.io.File;
028: import java.io.IOException;
029:
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.parsers.ParserConfigurationException;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038: import org.w3c.dom.Text;
039: import org.xml.sax.SAXException;
040:
041: import org.apache.tools.ant.BuildException;
042:
043: import org.objectweb.jonas.ant.JOnASBaseTask;
044:
045: /**
046: * Defines properties for carol.properties
047: * @author Florent Benoit
048: */
049: public class Discovery extends Tasks {
050:
051: /**
052: * Info for the logger
053: */
054: private static final String INFO = "[Discovery] ";
055:
056: /**
057: * Default mcast addr
058: */
059: private static final String DEFAULT_DISCOVERY_MCASTADDR = "224.224.224.224";
060:
061: /**
062: * Default mcast port
063: */
064: private static final String DEFAULT_DISCOVERY_MCASTPORT = "9080";
065:
066: /**
067: * Default greeting port
068: */
069: private static final String DEFAULT_DISCOVERY_GREETING_PORT = "9899";
070:
071: /**
072: * Default source port
073: */
074: private static final String DEFAULT_DISCOVERY_SOURCE_PORT = "9888";
075:
076: /**
077: * Discovery mcast addr property
078: */
079: private static final String DISCOVERY_MCASTADDR_PROPERTY = "jonas.service.discovery.multicast.address";
080:
081: /**
082: * Discovery mcast port property
083: */
084: private static final String DISCOVERY_MCASTPORT_PROPERTY = "jonas.service.discovery.multicast.port";
085:
086: /**
087: * Discovery master property
088: */
089: private static final String DISCOVERY_MASTER_PROPERTY = "jonas.service.discovery.master";
090:
091: /**
092: * Discovery greeting port property
093: */
094: private static final String DISCOVERY_GREETING_PORT_PROPERTY = "jonas.service.discovery.greeting.port";
095:
096: /**
097: * Discovery greeting timeout property
098: */
099: private static final String DISCOVERY_GREETING_TIMEOUT_PROPERTY = "jonas.service.discovery.greeting.timeout";
100:
101: /**
102: * Discovery source port property
103: */
104: private static final String DISCOVERY_SOURCE_PORT_PROPERTY = "jonas.service.discovery.source.port";
105:
106: /**
107: * domain management configuration file
108: */
109: private static final String DOMAIN_MNGT_CONF_FILE = "domain.xml";
110:
111: /**
112: * Domain.xml structure
113: */
114: private Document domainDoc = null;
115:
116: /**
117: * Flag indicating if the domain document has been loaded
118: */
119: private boolean domainDocLoaded = false;
120:
121: /**
122: * directory JONAS_ROOT
123: */
124: private String jonasRoot = null;
125:
126: /**
127: * Default constructor
128: */
129: public Discovery() {
130: super ();
131: }
132:
133: /**
134: * Set the source port for the discovery service
135: * @param portNumber port number (-1 means to disable to master node)
136: */
137: public void setSourcePort(String portNumber) {
138:
139: int pn = new Integer(portNumber).intValue();
140: if (pn != -1) {
141: enableMaster();
142:
143: // Token to replace
144: String token = DISCOVERY_SOURCE_PORT_PROPERTY + "="
145: + DEFAULT_DISCOVERY_SOURCE_PORT;
146: String value = DISCOVERY_SOURCE_PORT_PROPERTY + "="
147: + portNumber;
148: JReplace propertyReplace = new JReplace();
149: propertyReplace.setLogInfo(INFO
150: + "Setting source port for discovery");
151: propertyReplace
152: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
153: propertyReplace.setToken(token);
154: propertyReplace.setValue(value);
155: addTask(propertyReplace);
156: }
157: }
158:
159: /**
160: * Enable greeting part of discovery service
161: */
162: private void enableMaster() {
163: // 1st Token to replace
164: String token1 = "#" + DISCOVERY_MASTER_PROPERTY;
165: String value1 = DISCOVERY_MASTER_PROPERTY;
166: JReplace propertyReplace1 = new JReplace();
167: propertyReplace1.setLogInfo(INFO
168: + "Enable master node for discovery");
169: propertyReplace1
170: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
171: propertyReplace1.setToken(token1);
172: propertyReplace1.setValue(value1);
173: addTask(propertyReplace1);
174:
175: //2nd Token to replace
176: String token2 = "#" + DISCOVERY_SOURCE_PORT_PROPERTY;
177: String value2 = DISCOVERY_SOURCE_PORT_PROPERTY;
178: JReplace propertyReplace2 = new JReplace();
179: propertyReplace2.setLogInfo(INFO
180: + "Enable greeting timeout for discovery");
181: propertyReplace2
182: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
183: propertyReplace2.setToken(token2);
184: propertyReplace2.setValue(value2);
185: addTask(propertyReplace2);
186: }
187:
188: /**
189: * Enable greeting part of discovery service
190: */
191: private void enableGreeting() {
192: // 1st Token to replace
193: String token1 = "#" + DISCOVERY_GREETING_PORT_PROPERTY;
194: String value1 = DISCOVERY_GREETING_PORT_PROPERTY;
195: JReplace propertyReplace1 = new JReplace();
196: propertyReplace1.setLogInfo(INFO
197: + "Enable greeting port for discovery");
198: propertyReplace1
199: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
200: propertyReplace1.setToken(token1);
201: propertyReplace1.setValue(value1);
202: addTask(propertyReplace1);
203:
204: //2nd Token to replace
205: String token2 = "#" + DISCOVERY_GREETING_TIMEOUT_PROPERTY;
206: String value2 = DISCOVERY_GREETING_TIMEOUT_PROPERTY;
207: JReplace propertyReplace2 = new JReplace();
208: propertyReplace2.setLogInfo(INFO
209: + "Enable greeting timeout for discovery");
210: propertyReplace2
211: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
212: propertyReplace2.setToken(token2);
213: propertyReplace2.setValue(value2);
214: addTask(propertyReplace2);
215: }
216:
217: /**
218: * Set the port for the discovery service
219: * @param portNumber port number
220: */
221: public void setGreetingPort(String portNumber) {
222: enableGreeting();
223: // Token to replace
224: String token = DISCOVERY_GREETING_PORT_PROPERTY + "="
225: + DEFAULT_DISCOVERY_GREETING_PORT;
226: String value = DISCOVERY_GREETING_PORT_PROPERTY + "="
227: + portNumber;
228: JReplace propertyReplace = new JReplace();
229: propertyReplace.setLogInfo(INFO
230: + "Setting greeting port for discovery");
231: propertyReplace
232: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
233: propertyReplace.setToken(token);
234: propertyReplace.setValue(value);
235: addTask(propertyReplace);
236: }
237:
238: /**
239: * Set mcastAddr
240: * @param mcastAddr multicast address
241: */
242: public void setMcastAddr(String mcastAddr) {
243:
244: // Token to replace
245: String token = DISCOVERY_MCASTADDR_PROPERTY + "="
246: + DEFAULT_DISCOVERY_MCASTADDR;
247: String value = DISCOVERY_MCASTADDR_PROPERTY + "=" + mcastAddr;
248: JReplace propertyReplace = new JReplace();
249: propertyReplace.setLogInfo(INFO
250: + "Setting mcastaddr for discovery");
251: propertyReplace
252: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
253: propertyReplace.setToken(token);
254: propertyReplace.setValue(value);
255: addTask(propertyReplace);
256:
257: }
258:
259: /**
260: * Set mcastPort
261: * @param mcastPort multicast port
262: */
263: public void setMcastPort(String mcastPort) {
264:
265: // Token to replace
266: String token = DISCOVERY_MCASTPORT_PROPERTY + "="
267: + DEFAULT_DISCOVERY_MCASTPORT;
268: String value = DISCOVERY_MCASTPORT_PROPERTY + "=" + mcastPort;
269: JReplace propertyReplace = new JReplace();
270: propertyReplace.setLogInfo(INFO
271: + "Setting mcastport for discovery");
272: propertyReplace
273: .setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
274: propertyReplace.setToken(token);
275: propertyReplace.setValue(value);
276: addTask(propertyReplace);
277:
278: }
279:
280: /**
281: * load the domain.xml file in a DOM structure
282: */
283: private void loadDomainXmlDoc() {
284:
285: if (!domainDocLoaded) {
286:
287: // Load the orignal configuration file
288: DocumentBuilderFactory factory = DocumentBuilderFactory
289: .newInstance();
290: DocumentBuilder docBuilder = null;
291: try {
292: docBuilder = factory.newDocumentBuilder();
293: } catch (ParserConfigurationException e) {
294: throw new BuildException(INFO
295: + "Exception during setDomainName", e);
296: }
297: try {
298: domainDoc = docBuilder.parse(jonasRoot + File.separator
299: + "conf" + File.separator
300: + DOMAIN_MNGT_CONF_FILE);
301: } catch (SAXException e) {
302: throw new BuildException(INFO
303: + "Error during parsing of the file "
304: + DOMAIN_MNGT_CONF_FILE, e);
305: } catch (IOException e) {
306: throw new BuildException(INFO
307: + "Error during parsing of the file "
308: + DOMAIN_MNGT_CONF_FILE, e);
309: }
310:
311: Element root = domainDoc.getDocumentElement();
312:
313: // Remove the default servers list
314: NodeList serverNodeL = root.getElementsByTagName("server");
315: for (int i = 0; i < serverNodeL.getLength(); i++) {
316: Node n = serverNodeL.item(i);
317: root.removeChild(n);
318: }
319:
320: // Prepare the serialization
321: XMLSerializerTask xmlSerTask = new XMLSerializerTask();
322: xmlSerTask.setXmlDoc(domainDoc);
323: xmlSerTask.setXmlFileName(DOMAIN_MNGT_CONF_FILE);
324:
325: addTask(xmlSerTask);
326:
327: domainDocLoaded = true;
328: }
329:
330: }
331:
332: /**
333: * Set the domain name
334: * @param domainName domain name
335: */
336: public void setDomainName(String domainName) {
337:
338: // Load the orignal configuration file
339: loadDomainXmlDoc();
340:
341: Element root = domainDoc.getDocumentElement();
342:
343: // update the domain name
344: NodeList nameNodeL = root.getElementsByTagName("name");
345: nameNodeL.item(0).getFirstChild().setNodeValue(domainName);
346:
347: }
348:
349: /**
350: * Set the domain description
351: * @param domainDesc domain description
352: */
353: public void setDomainDesc(String domainDesc) {
354:
355: // Load the orignal configuration file
356: loadDomainXmlDoc();
357:
358: Element root = domainDoc.getDocumentElement();
359:
360: // update the domain name
361: NodeList nameNodeL = root.getElementsByTagName("description");
362: nameNodeL.item(0).getFirstChild().setNodeValue(domainDesc);
363:
364: }
365:
366: /**
367: * Add a cluster in the domain
368: * @param clusterName cluster name
369: * @param clusterDesc cluster desc
370: * @param nodesName prefix of the nodes names within the cluster
371: * @param nodesNb number of nodes within the cluster
372: * @param protocol protocol
373: * @param portRange protocol port range
374: */
375: public void setDomainCluster(String clusterName,
376: String clusterDesc, String nodesName, int nodesNb,
377: String protocol, String[] portRange, String cdName,
378: String cdUrl) {
379:
380: // Load the orignal configuration file
381: loadDomainXmlDoc();
382:
383: Element root = domainDoc.getDocumentElement();
384:
385: // creation of the cluster-daemon node
386: Node dnc = domainDoc.createElement("cluster-daemon");
387: root.appendChild(dnc);
388:
389: // Element name
390: Node dncn = domainDoc.createElement("name");
391: dnc.appendChild(dncn);
392:
393: Text dtncn = domainDoc.createTextNode(cdName);
394: dncn.appendChild(dtncn);
395:
396: // Element description
397: Node dncd = domainDoc.createElement("description");
398: dnc.appendChild(dncd);
399:
400: Text dtncd = domainDoc.createTextNode("");
401: dncd.appendChild(dtncd);
402:
403: // Element location
404: Node dncl = domainDoc.createElement("location");
405: dnc.appendChild(dncl);
406:
407: // Element url
408: Node dncu = domainDoc.createElement("url");
409: dncl.appendChild(dncu);
410:
411: Text dtncu = domainDoc.createTextNode(cdUrl);
412: dncu.appendChild(dtncu);
413:
414: // creation of the cluster node
415: Node cnc = domainDoc.createElement("cluster");
416: root.appendChild(cnc);
417:
418: Node cncn = domainDoc.createElement("name");
419: cnc.appendChild(cncn);
420:
421: Text ctncn = domainDoc.createTextNode(clusterName);
422: cncn.appendChild(ctncn);
423:
424: Node cncd = domainDoc.createElement("description");
425: cnc.appendChild(cncd);
426:
427: Text ctncd = domainDoc.createTextNode(clusterDesc);
428: cncd.appendChild(ctncd);
429:
430: for (int i = 1; i <= nodesNb; i++) {
431:
432: // Element server
433: Node cncs = domainDoc.createElement("server");
434: cnc.appendChild(cncs);
435:
436: // Element name
437: Node cncsn = domainDoc.createElement("name");
438: cncs.appendChild(cncsn);
439:
440: Text ctncsn = domainDoc.createTextNode(nodesName + i);
441: cncsn.appendChild(ctncsn);
442:
443: // Element location
444: Node cncsl = domainDoc.createElement("location");
445: cncs.appendChild(cncsl);
446:
447: // Element url
448: Node cncsu = domainDoc.createElement("url");
449: cncsl.appendChild(cncsu);
450:
451: String url = "service:jmx:rmi://localhost/jndi/cmi://localhost:"
452: + portRange[i - 1] + "/cmiconnector_node" + i;
453:
454: Text ctncsu = domainDoc.createTextNode(url);
455: cncsu.appendChild(ctncsu);
456:
457: // Element cluster-daemon
458: Node cncscd = domainDoc.createElement("cluster-daemon");
459: cncs.appendChild(cncscd);
460:
461: Text ctncscd = domainDoc.createTextNode(cdName);
462: cncscd.appendChild(ctncscd);
463:
464: }
465: }
466:
467: /**
468: * Set the JONAS_ROOT dir
469: * @param jonasRoot directory
470: */
471: public void setJonasRoot(String jonasRoot) {
472: this.jonasRoot = jonasRoot;
473: }
474: }
|