KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > JOnASClusterConfigTask


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: JOnASClusterConfigTask.java,v 1.1 2005/06/07 08:21:27 pelletib Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.ant;
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 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.Task;
37 import org.apache.tools.ant.taskdefs.Copy;
38 import org.apache.tools.ant.types.FileSet;
39
40 import org.objectweb.jonas.ant.cluster.Common;
41 import org.objectweb.jonas.ant.cluster.ClusterTasks;
42 import org.objectweb.jonas.ant.cluster.EjbLevel;
43 import org.objectweb.jonas.ant.cluster.WebLevel;
44
45 import org.objectweb.jonas.ant.jonasbase.BaseTaskItf;
46 import org.objectweb.jonas.ant.jonasbase.Tasks;
47
48 /**
49  * Class used to create a JOnAS Cluster Configuration
50  * @author Benoit Pelletier
51  */

52 public class JOnASClusterConfigTask extends Task {
53
54     /**
55      * Archictecture of the JOnAS's instances : contain both Web level and Ejb level
56      */

57     private static final String JavaDoc ARCH_BOTH_WEB_EJB = "bothWebEjb";
58
59     /**
60      * Archictecture of the JOnAS's instances : separate Web level and Ejb level
61      */

62     private static final String JavaDoc ARCH_DIFF_WEB_EJB = "diffWebEjb";
63
64     /**
65      * Source directory (JOnAS root)
66      */

67     private File JavaDoc jonasRoot = null;
68
69     /**
70      * Destination directory prefix (used to create the jonasBase(s))
71      */

72     private String JavaDoc destDirPrefix = null;
73
74     /**
75      * Cluster architecture : share instance for web/ejb or separate them
76      */

77     private String JavaDoc arch = null;
78
79     /**
80      * number of JOnAS's instance for web level
81      */

82     private int webInstNb = -1;
83
84     /**
85      * number of JOnAS's instance for ejb level
86      */

87     private int ejbInstNb = -1;
88
89     /**
90      * Update only JONAS_BASE without erasing it
91      */

92     private boolean onlyUpdate = false;
93
94     /**
95      * List of tasks to do
96      */

97     private List JavaDoc tasks = null;
98
99
100     /**
101      * Constructor
102      */

103     public JOnASClusterConfigTask() {
104         tasks = new ArrayList JavaDoc();
105     }
106
107     /**
108      * Run this task
109      * @see org.apache.tools.ant.Task#execute()
110      */

111     public void execute() {
112         if (jonasRoot == null) {
113             throw new BuildException("jonasRoot element is not set");
114         }
115
116         if (destDirPrefix == null) {
117             throw new BuildException("destDirPrefix element is not set");
118         }
119
120         if (jonasRoot.getPath().equals(destDirPrefix)) {
121             throw new BuildException("jonasRoot and destDirPrefix is the same path !");
122         }
123
124         // First, JONAS_BASE creation
125
for (int i = 1; i <= webInstNb + ejbInstNb; i++) {
126
127             String JavaDoc destDir = ClusterTasks.getDestDir(destDirPrefix, i);
128             File JavaDoc destDirFile = new File JavaDoc(destDir);
129             File JavaDoc jprops = new File JavaDoc(destDir + File.separator + "conf" + File.separator + "jonas.properties");
130
131             if (onlyUpdate) {
132                 if (jprops.exists()) {
133                     log("Only updating JONAS_BASE in the directory '" + destDir + "' from source directory '" + jonasRoot + "'",
134                         Project.MSG_INFO);
135                     JOnASAntTool.updateJonasBase(this, jonasRoot, destDirFile);
136
137                     return;
138                 } else {
139                     throw new BuildException("JOnAS base directory '" + destDir + "' doesn't exists. Cannot update.");
140                 }
141             }
142
143             log("Creating JONAS_BASE in the directory '" + destDir + "' from source directory '" + jonasRoot + "'",
144                     Project.MSG_INFO);
145             Copy copy = new Copy();
146             JOnASAntTool.configure(this, copy);
147             copy.setTodir(destDirFile);
148             FileSet fileSet = new FileSet();
149             fileSet.setDir(new File JavaDoc(new File JavaDoc(jonasRoot, "templates"), "conf"));
150             copy.addFileset(fileSet);
151             copy.setOverwrite(true);
152             copy.execute();
153         }
154
155         // 2nd, executes the tasks
156
for (Iterator JavaDoc it = tasks.iterator(); it.hasNext();) {
157             BaseTaskItf task = (BaseTaskItf) it.next();
158             task.setJonasRoot(jonasRoot);
159             JOnASAntTool.configure(this, (Task) task);
160             String JavaDoc info = task.getLogInfo();
161             if (info != null) {
162                 log(info, Project.MSG_INFO);
163             }
164
165             task.execute();
166         }
167
168         // Then update JonasBase
169
for (int i = 1; i <= webInstNb + ejbInstNb; i++) {
170             String JavaDoc destDir = ClusterTasks.getDestDir(destDirPrefix, i);
171             File JavaDoc destDirFile = new File JavaDoc(destDir);
172             JOnASAntTool.updateJonasBase(this, jonasRoot, destDirFile);
173         }
174     }
175
176
177     /**
178      * Add tasks for configured object
179      * @param subTasks some tasks to do on files
180      */

181     public void addTasks(Tasks subTasks) {
182         if (subTasks != null) {
183             for (Iterator JavaDoc it = subTasks.getTasks().iterator(); it.hasNext();) {
184                 tasks.add(it.next());
185             }
186         }
187     }
188
189     /**
190      * Set the destination directory prefix
191      * @param destDirPrefix the destination directory prefix
192      */

193     public void setDestDirPrefix(String JavaDoc destDirPrefix) {
194         this.destDirPrefix = destDirPrefix;
195     }
196
197     /**
198      * Set the source directory for the replacement
199      * @param jonasRoot the source directory
200      */

201     public void setJonasRoot(File JavaDoc jonasRoot) {
202         this.jonasRoot = jonasRoot;
203     }
204
205     /**
206      * Set architecture
207      * @param arch the architecture
208      */

209     public void setArch(String JavaDoc arch) {
210         this.arch = arch;
211     }
212
213     /**
214      * Set the web instances number
215      * @param webInstNb number of web instances
216      */

217     public void setWebInstNb(int webInstNb) {
218         this.webInstNb = webInstNb;
219     }
220
221     /**
222      * Set the ejb instances number
223      * @param ejbInstNb number of ejb instances
224      */

225     public void setEjbInstNb(int ejbInstNb) {
226         this.ejbInstNb = ejbInstNb;
227     }
228
229     /**
230      * Add tasks for the common instances (both web & ejb )
231      * @param common common tasks to do
232      */

233     public void addConfiguredCommon(Common common) {
234         common.setRootTask(this);
235         common.setDestDirPrefix(destDirPrefix);
236         common.setDestDirSuffixIndFirst(1);
237         common.setDestDirSuffixIndLast(webInstNb + ejbInstNb);
238         common.setArch(arch);
239         common.generatesTasks();
240         addTasks(common);
241     }
242
243     /**
244      * Add tasks for the web level instances
245      * If choosen architecture is both web and ejb within the same JOnAS,
246      * the tasks are added to all instances
247      * @param webLevel tasks to do on files
248      */

249     public void addConfiguredWebLevel(WebLevel webLevel) {
250         webLevel.setRootTask(this);
251         webLevel.setDestDirPrefix(destDirPrefix);
252         webLevel.setArch(arch);
253
254         if (arch.compareTo(ARCH_BOTH_WEB_EJB) == 0) {
255             webLevel.setDestDirSuffixIndFirst(1);
256             webLevel.setDestDirSuffixIndLast(webInstNb + ejbInstNb);
257         } else {
258             webLevel.setDestDirSuffixIndFirst(1);
259             webLevel.setDestDirSuffixIndLast(webInstNb);
260         }
261         webLevel.generatesTasks();
262         addTasks(webLevel);
263     }
264
265     /**
266      * Add tasks for the ejb level instances
267      * If choosen architecture is both web and ejb within the same JOnAS,
268      * the tasks are added to all instances
269      * @param ejbLevel tasks to do on files
270      */

271     public void addConfiguredEjbLevel(EjbLevel ejbLevel) {
272         ejbLevel.setRootTask(this);
273         ejbLevel.setDestDirPrefix(destDirPrefix);
274         ejbLevel.setArch(arch);
275
276         if (arch.compareTo(ARCH_BOTH_WEB_EJB) == 0) {
277             ejbLevel.setDestDirSuffixIndFirst(1);
278             ejbLevel.setDestDirSuffixIndLast(webInstNb + ejbInstNb);
279         } else {
280             ejbLevel.setDestDirSuffixIndFirst(webInstNb + 1);
281             ejbLevel.setDestDirSuffixIndLast(webInstNb + ejbInstNb);
282         }
283         ejbLevel.generatesTasks();
284         addTasks(ejbLevel);
285     }
286
287     /**
288      * Set if this is only an update or a new JONAS_BASE
289      * @param onlyUpdate If true update, else create and then update
290      */

291     public void setUpdate(boolean onlyUpdate) {
292         this.onlyUpdate = onlyUpdate;
293     }
294
295 }
Popular Tags