1 24 25 package org.objectweb.carol.ant; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.util.Enumeration ; 34 import java.util.Iterator ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 import java.util.Properties ; 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 47 public final class CarolTestTask extends Task { 48 49 private String antfile; 50 51 private String propDestination; 52 53 private String propSource; 54 55 private File propDestDir; 56 57 private File propSourceDir; 58 59 public void setAntfile(String antfile) { 60 this.antfile = antfile; 61 } 62 63 public void setPropDestination(String propDestination) { 64 this.propDestination = propDestination; 65 } 66 67 public void setPropSource(String 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 (getProject().getBaseDir(), propDestination); 87 assertIsDirectory("propDestination", propDestDir); 88 89 propSourceDir = new File (propSource); 90 assertIsDirectory("propSource", propSourceDir); 91 92 for (Iterator 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 ("can't happen"); 112 } 113 114 Properties template = loadProperties(proto.getName() + ".properties"); 115 Properties clientProps = new Properties (); 116 clientProps.setProperty("carol.protocols", proto.getName()); 117 clientProps.setProperty("carol.start.ns", "false"); 118 119 for (Enumeration props = template.propertyNames(); props.hasMoreElements();) { 120 String propName = (String ) props.nextElement(); 121 122 if (propName.startsWith("carol.")) { 123 clientProps.setProperty(propName, template.getProperty(propName)); 124 } else { 125 setAntProperty(ant, propName + clientNum, alter(proto, propName, template.getProperty(propName))); 127 } 128 } 129 130 File clientPropsFile = new File (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 template = loadProperties(proto1.getName() + ".properties"); 142 Properties serverProps = new Properties (); 143 serverProps.setProperty("carol.protocols", proto1.getName()); 144 145 if (proto2 != null) { 146 Properties 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 props = template.propertyNames(); props.hasMoreElements();) { 154 String propName = (String ) props.nextElement(); 155 156 if (propName.startsWith("carol.")) { 157 serverProps.setProperty(propName, template.getProperty(propName)); 158 } 159 } 160 161 File serverPropsFile = new File (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 p1, Properties p2) { 169 for (Enumeration props = p2.propertyNames(); props.hasMoreElements();) { 170 String propName = (String ) props.nextElement(); 171 p1.setProperty(propName, p2.getProperty(propName)); 172 } 173 } 174 175 private static void saveProperties(File file, Properties props) throws BuildException { 176 177 try { 178 FileOutputStream os = new FileOutputStream (file); 179 props.store(os, null); 180 os.close(); 181 } catch (IOException 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 name, String value) { 195 Property antProp = ant.createProperty(); 196 antProp.setName(name); 197 antProp.setValue(value); 198 } 199 200 private static void assertIsDirectory(String propName, File 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 alter(CarolProtocol proto, String name, String value) { 211 if (!proto.useSunStubs()) { 212 return value; 213 } 214 if (!name.startsWith("stub.jar.name")) { 215 return value; 216 } 217 String suffix = proto == CarolProtocol.JRMP11 ? "1.1" : "1.2"; 218 return value + suffix + ".jar"; 219 } 220 221 private Properties loadProperties(String filename) throws BuildException { 222 File propFile = new File (propSourceDir, filename); 223 InputStream is; 224 try { 225 is = new FileInputStream (propFile); 226 } catch (FileNotFoundException ex) { 227 throw new BuildException(filename + " not found", ex); 228 } 229 230 Properties props = new Properties (); 231 232 try { 233 props.load(is); 234 } catch (IOException ex) { 235 throw new BuildException("couldn't load " + filename, ex); 236 } finally { 237 try { 238 is.close(); 239 } catch (IOException 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 configurations; 255 256 static { 257 configurations = new LinkedList (); 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 ("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 usesExternallyStartedNS() { 300 return Boolean.valueOf(usesExternallyStartedNS).toString(); 301 } 302 303 public static Iterator allConfigurations() { 304 return configurations.iterator(); 305 } 306 307 public static Iterator supportedConfigurations() { 308 List configs = new LinkedList (); 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 toString() { 339 StringBuffer sb = new StringBuffer (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 |