001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004-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: * --------------------------------------------------------------------------
022: * $Id: JmsRa.java 7937 2006-01-25 17:01:31Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ant.jonasbase;
025:
026: import java.io.File;
027: import java.util.Date;
028:
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Task;
031: import org.apache.tools.ant.taskdefs.Copy;
032: import org.apache.tools.ant.taskdefs.Java;
033: import org.apache.tools.ant.taskdefs.Expand;
034: import org.apache.tools.ant.types.FileSet;
035: import org.apache.tools.ant.types.selectors.FilenameSelector;
036:
037: import org.objectweb.jonas.ant.JOnASAntTool;
038:
039: /**
040: * Allow to create/adapt the JMS resource adaptor
041: * @author Benoit Pelletier
042: */
043: public class JmsRa extends JTask implements BaseTaskItf {
044:
045: /**
046: * Info for the logger
047: */
048: private static final String INFO = "[JmsRa] ";
049:
050: /**
051: * Name of the rar
052: */
053: private static final String JONAS_JORAMRA = "joram_for_jonas_ra.rar";
054:
055: /**
056: * RAConfig classname
057: */
058: private static final String RACONFIG_CLASS = "org.objectweb.jonas_rar.raconfig.RAConfig";
059:
060: /**
061: * Default port number
062: */
063: private static final String DEFAULT_PORT = "16010";
064:
065: /**
066: * Port number of the server
067: */
068: private String serverPort = DEFAULT_PORT;
069:
070: /**
071: * Set the port number
072: * @param serverPort the port nb
073: */
074: public void setServerPort(String serverPort) {
075: this .serverPort = serverPort;
076: }
077:
078: /**
079: * Execute this task
080: */
081: public void execute() {
082:
083: // Build new temp file for making a resource adaptor
084: File tmpFile = new File(System.getProperty("java.io.tmpdir"),
085: String.valueOf(new Date().getTime()));
086:
087: // Copy the RAR file from JONAS_ROOT to JONAS_BASE
088: String srcRarDir = getJonasRoot().getPath() + File.separator
089: + "rars" + File.separator + "autoload";
090: String dstRarDir = getDestDir().getPath() + File.separator
091: + "rars" + File.separator + "autoload";
092: String dstRarFileName = dstRarDir + File.separator
093: + JONAS_JORAMRA;
094:
095: Copy copy = new Copy();
096: JOnASAntTool.configure(this , copy);
097: copy.setTodir(new File(dstRarDir));
098: FilenameSelector fns1 = new FilenameSelector();
099: fns1.setName(JONAS_JORAMRA);
100: FileSet fs1 = new FileSet();
101: fs1.setDir(new File(srcRarDir));
102: fs1.addFilename(fns1);
103: copy.addFileset(fs1);
104: copy.setOverwrite(true);
105: try {
106: copy.execute();
107: } catch (Exception rae) {
108: rae.printStackTrace();
109: JOnASAntTool.deleteAllFiles(tmpFile);
110: throw new BuildException(INFO + "Cannot copy "
111: + JONAS_JORAMRA + " file : ", rae);
112: }
113:
114: // Extract the jonas-ra.xml
115: Expand jarTask = new Expand();
116: JOnASAntTool.configure(this , (Task) jarTask);
117: jarTask.setDest(tmpFile);
118: jarTask.setSrc(new File(dstRarFileName));
119: try {
120: jarTask.execute();
121: } catch (Exception rae) {
122: rae.printStackTrace();
123: JOnASAntTool.deleteAllFiles(tmpFile);
124: throw new BuildException(
125: INFO
126: + "Cannot extract jonas-ra.xml with jar command : ",
127: rae);
128: }
129:
130: // update the jonas-ra.xml
131: Replace replaceTask = new Replace();
132: JOnASAntTool.configure(this , (Task) replaceTask);
133: replaceTask.setFile(new File(tmpFile.getPath() + File.separator
134: + "META-INF/jonas-ra.xml"));
135:
136: String token = "<jonas-config-property-name>ServerPort</jonas-config-property-name>"
137: + "\n"
138: + " <jonas-config-property-value></jonas-config-property-value>";
139: String value = "<jonas-config-property-name>ServerPort</jonas-config-property-name>"
140: + "\n"
141: + " <jonas-config-property-value>"
142: + serverPort + "</jonas-config-property-value>";
143:
144: replaceTask.setToken(token);
145: replaceTask.setValue(value);
146: try {
147: replaceTask.execute();
148: } catch (Exception rae) {
149: rae.printStackTrace();
150: JOnASAntTool.deleteAllFiles(tmpFile);
151: throw new BuildException(
152: INFO
153: + "Cannot replace port number in the jonas-ra.xml file : ",
154: rae);
155: }
156:
157: // update the archive
158: Java raConfigTask = getBootstraptask("");
159: JOnASAntTool.configure(this , (Task) raConfigTask);
160: raConfigTask.clearArgs();
161: raConfigTask.createArg().setValue(RACONFIG_CLASS);
162: raConfigTask.createArg().setValue("-u");
163: raConfigTask.createArg().setValue(
164: tmpFile.getPath() + File.separator
165: + "META-INF/jonas-ra.xml");
166: raConfigTask.createArg().setValue(dstRarFileName);
167:
168: try {
169: raConfigTask.execute();
170: } catch (Exception rae) {
171: rae.printStackTrace();
172: throw new BuildException(INFO
173: + "Cannot make a resource adaptor on RAConfig: ",
174: rae);
175: } finally {
176: JOnASAntTool.deleteAllFiles(tmpFile);
177: }
178:
179: log(INFO + "Setting Port number to :" + serverPort
180: + " in the rar '" + dstRarFileName + "'.");
181: }
182: }
|