KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > ant > CarolTestTask


1 /**
2  * This library is developed inside the ObjectWeb Consortium,
3  * http://www.objectweb.org
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  *
20  * --------------------------------------------------------------------------
21  * $Id: CarolTestTask.java,v 1.9 2005/05/14 00:03:58 rhs Exp $
22  * --------------------------------------------------------------------------
23  */

24
25 package org.objectweb.carol.ant;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Properties JavaDoc;
38
39 import org.apache.tools.ant.BuildException;
40 import org.apache.tools.ant.Task;
41 import org.apache.tools.ant.taskdefs.Ant;
42 import org.apache.tools.ant.taskdefs.Property;
43
44 /**
45  * @author Vadim Nasardinov (vadimn@redhat.com)
46  */

47 public final class CarolTestTask extends Task {
48
49     private String JavaDoc antfile;
50
51     private String JavaDoc propDestination;
52
53     private String JavaDoc propSource;
54
55     private File JavaDoc propDestDir;
56
57     private File JavaDoc propSourceDir;
58
59     public void setAntfile(String JavaDoc antfile) {
60         this.antfile = antfile;
61     }
62
63     public void setPropDestination(String JavaDoc propDestination) {
64         this.propDestination = propDestination;
65     }
66
67     public void setPropSource(String JavaDoc propSource) {
68         this.propSource = propSource;
69     }
70
71     private void checkSettings() throws BuildException {
72         if (antfile == null) {
73             throw new BuildException("antfile is not set");
74         }
75         if (propDestination == null) {
76             throw new BuildException("propDestination is not set");
77         }
78         if (propSource == null) {
79             throw new BuildException("propSource is not set");
80         }
81     }
82
83     public void execute() throws BuildException {
84         checkSettings();
85
86         propDestDir = new File JavaDoc(getProject().getBaseDir(), propDestination);
87         assertIsDirectory("propDestination", propDestDir);
88
89         propSourceDir = new File JavaDoc(propSource);
90         assertIsDirectory("propSource", propSourceDir);
91
92         for (Iterator JavaDoc configs = Config.supportedConfigurations(); configs.hasNext();) {
93             Config config = (Config) configs.next();
94             Ant ant = newAnt();
95
96             setClientProperties(ant, config.getProto1(), 1);
97
98             if (config.getProto2() != null) {
99                 setClientProperties(ant, config.getProto2(), 2);
100             }
101             setServerProperties(ant, config);
102
103             System.out.println("executing " + config.toString());
104             ant.execute();
105         }
106     }
107
108     private void setClientProperties(Ant ant, CarolProtocol proto, int clientNum) throws BuildException {
109
110         if (clientNum != 1 && clientNum != 2) {
111             throw new IllegalStateException JavaDoc("can't happen");
112         }
113
114         Properties JavaDoc template = loadProperties(proto.getName() + ".properties");
115         Properties JavaDoc clientProps = new Properties JavaDoc();
116         clientProps.setProperty("carol.protocols", proto.getName());
117         clientProps.setProperty("carol.start.ns", "false");
118
119         for (Enumeration JavaDoc props = template.propertyNames(); props.hasMoreElements();) {
120             String JavaDoc propName = (String JavaDoc) props.nextElement();
121
122             if (propName.startsWith("carol.")) {
123                 clientProps.setProperty(propName, template.getProperty(propName));
124             } else {
125                 // XXX: get rid of this "alter" nonsense
126
setAntProperty(ant, propName + clientNum, alter(proto, propName, template.getProperty(propName)));
127             }
128         }
129
130         File JavaDoc clientPropsFile = new File JavaDoc(propDestDir, "client" + clientNum + ".properties");
131         saveProperties(clientPropsFile, clientProps);
132
133         setAntProperty(ant, "client.properties.file.name" + clientNum, clientPropsFile.getAbsolutePath());
134     }
135
136     private void setServerProperties(Ant ant, Config config) throws BuildException {
137
138         CarolProtocol proto1 = config.getProto1();
139         CarolProtocol proto2 = config.getProto2();
140
141         Properties JavaDoc template = loadProperties(proto1.getName() + ".properties");
142         Properties JavaDoc serverProps = new Properties JavaDoc();
143         serverProps.setProperty("carol.protocols", proto1.getName());
144
145         if (proto2 != null) {
146             Properties JavaDoc template2 = loadProperties(proto2.getName() + ".properties");
147             append(template, template2);
148             serverProps.setProperty("carol.protocols", proto1.getName() + "," + proto2.getName());
149         }
150
151         serverProps.setProperty("carol.start.ns", config.usesExternallyStartedNS());
152
153         for (Enumeration JavaDoc props = template.propertyNames(); props.hasMoreElements();) {
154             String JavaDoc propName = (String JavaDoc) props.nextElement();
155
156             if (propName.startsWith("carol.")) {
157                 serverProps.setProperty(propName, template.getProperty(propName));
158             }
159         }
160
161         File JavaDoc serverPropsFile = new File JavaDoc(propDestDir, "server.properties");
162         saveProperties(serverPropsFile, serverProps);
163
164         setAntProperty(ant, "test.name", config.toString());
165         setAntProperty(ant, "server.properties.file.name", serverPropsFile.getAbsolutePath());
166     }
167
168     private static void append(Properties JavaDoc p1, Properties JavaDoc p2) {
169         for (Enumeration JavaDoc props = p2.propertyNames(); props.hasMoreElements();) {
170             String JavaDoc propName = (String JavaDoc) props.nextElement();
171             p1.setProperty(propName, p2.getProperty(propName));
172         }
173     }
174
175     private static void saveProperties(File JavaDoc file, Properties JavaDoc props) throws BuildException {
176
177         try {
178             FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(file);
179             props.store(os, null);
180             os.close();
181         } catch (IOException JavaDoc ex) {
182             throw new BuildException("couldn't create " + file, ex);
183         }
184     }
185
186     private Ant newAnt() {
187         Ant ant = new Ant();
188         ant.setProject(getProject());
189         ant.setOwningTarget(getOwningTarget());
190         ant.setAntfile(antfile);
191         return ant;
192     }
193
194     private static void setAntProperty(Ant ant, String JavaDoc name, String JavaDoc value) {
195         Property antProp = ant.createProperty();
196         antProp.setName(name);
197         antProp.setValue(value);
198     }
199
200     private static void assertIsDirectory(String JavaDoc propName, File JavaDoc dir) {
201         if (!dir.exists()) {
202             System.err.println("dir: " + dir);
203             throw new BuildException(propName + " " + dir + " does not exist");
204         }
205         if (!dir.isDirectory()) {
206             throw new BuildException(propName + " " + dir + " is not a directory");
207         }
208     }
209
210     private static String JavaDoc alter(CarolProtocol proto, String JavaDoc name, String JavaDoc value) {
211         if (!proto.useSunStubs()) {
212             return value;
213         }
214         if (!name.startsWith("stub.jar.name")) {
215             return value;
216         }
217         String JavaDoc suffix = proto == CarolProtocol.JRMP11 ? "1.1" : "1.2";
218         return value + suffix + ".jar";
219     }
220
221     private Properties JavaDoc loadProperties(String JavaDoc filename) throws BuildException {
222         File JavaDoc propFile = new File JavaDoc(propSourceDir, filename);
223         InputStream JavaDoc is;
224         try {
225             is = new FileInputStream JavaDoc(propFile);
226         } catch (FileNotFoundException JavaDoc ex) {
227             throw new BuildException(filename + " not found", ex);
228         }
229
230         Properties JavaDoc props = new Properties JavaDoc();
231
232         try {
233             props.load(is);
234         } catch (IOException JavaDoc ex) {
235             throw new BuildException("couldn't load " + filename, ex);
236         } finally {
237             try {
238                 is.close();
239             } catch (IOException JavaDoc ex) {
240                 ;
241             }
242         }
243         return props;
244     }
245
246     private static class Config {
247
248         private final CarolProtocol proto1;
249
250         private final CarolProtocol proto2;
251
252         private final boolean usesExternallyStartedNS;
253
254         private static final List JavaDoc configurations;
255
256         static {
257             configurations = new LinkedList JavaDoc();
258             CarolProtocol[] protos = new CarolProtocol[] {CarolProtocol.IIOP, CarolProtocol.JEREMIE,
259                     CarolProtocol.JRMP11, CarolProtocol.JRMP12};
260
261             boolean[] nsValues = new boolean[] {true, false};
262
263             for (int nsIdx = 0; nsIdx < nsValues.length; nsIdx++) {
264                 final boolean usesExternal = nsValues[nsIdx];
265
266                 for (int ii = 0; ii < protos.length; ii++) {
267                     configurations.add(new Config(protos[ii], null, usesExternal));
268                 }
269
270                 for (int ii = 0; ii < protos.length - 1; ii++) {
271                     final CarolProtocol proto1 = protos[ii];
272
273                     for (int jj = ii + 1; jj < protos.length; jj++) {
274                         final CarolProtocol proto2 = protos[jj];
275                         configurations.add(new Config(proto1, proto2, usesExternal));
276                     }
277                 }
278             }
279         }
280
281         Config(CarolProtocol proto1, CarolProtocol proto2, boolean usesExternallyStartedNS) {
282             if (proto1 == null) {
283                 throw new NullPointerException JavaDoc("proto1");
284             }
285
286             this.proto1 = proto1;
287             this.proto2 = proto2;
288             this.usesExternallyStartedNS = usesExternallyStartedNS;
289         }
290
291         public CarolProtocol getProto1() {
292             return proto1;
293         }
294
295         public CarolProtocol getProto2() {
296             return proto2;
297         }
298
299         public String JavaDoc usesExternallyStartedNS() {
300             return Boolean.valueOf(usesExternallyStartedNS).toString();
301         }
302
303         public static Iterator JavaDoc allConfigurations() {
304             return configurations.iterator();
305         }
306
307         public static Iterator JavaDoc supportedConfigurations() {
308             List JavaDoc configs = new LinkedList JavaDoc();
309             CarolProtocol[] protos = new CarolProtocol[] {
310                 CarolProtocol.IIOP, CarolProtocol.JEREMIE,
311                 CarolProtocol.IRMI11, CarolProtocol.IRMI12,
312                 CarolProtocol.JRMP11, CarolProtocol.JRMP12,
313             };
314
315             for (int ii = 0; ii < protos.length; ii++) {
316                 configs.add(new Config(protos[ii], null, true));
317                 configs.add(new Config(protos[ii], null, false));
318             }
319
320             configs.add(new Config(CarolProtocol.IIOP, CarolProtocol.JEREMIE, true));
321             configs.add(new Config(CarolProtocol.IIOP, CarolProtocol.JEREMIE, false));
322             configs.add(new Config(CarolProtocol.IIOP, CarolProtocol.JRMP11, true));
323             configs.add(new Config(CarolProtocol.IIOP, CarolProtocol.JRMP11, false));
324             configs.add(new Config(CarolProtocol.IIOP, CarolProtocol.JRMP12, true));
325             configs.add(new Config(CarolProtocol.IIOP, CarolProtocol.JRMP12, false));
326             configs.add(new Config(CarolProtocol.IRMI11, CarolProtocol.IIOP, true));
327             configs.add(new Config(CarolProtocol.IRMI11, CarolProtocol.IIOP, false));
328             configs.add(new Config(CarolProtocol.IRMI12, CarolProtocol.IIOP, true));
329             configs.add(new Config(CarolProtocol.IRMI12, CarolProtocol.IIOP, false));
330             configs.add(new Config(CarolProtocol.JRMP11, CarolProtocol.IIOP, true));
331             configs.add(new Config(CarolProtocol.JRMP11, CarolProtocol.IIOP, false));
332             configs.add(new Config(CarolProtocol.JRMP12, CarolProtocol.IIOP, true));
333             configs.add(new Config(CarolProtocol.JRMP12, CarolProtocol.IIOP, false));
334
335             return configs.iterator();
336         }
337
338         public String JavaDoc toString() {
339             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(proto1.getNameVersion());
340             if (proto2 != null) {
341                 sb.append('.').append(proto2.getNameVersion());
342             }
343             if (!usesExternallyStartedNS) {
344                 sb.append('.').append("nons");
345             }
346             return sb.toString();
347         }
348     }
349 }
350
Popular Tags