1 28 29 package com.idaremedia.antx.solo; 30 31 import java.net.URL ; 32 import java.util.List ; 33 import java.util.Properties ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 38 import com.idaremedia.apis.Buildstrs; 39 import com.idaremedia.apis.EmptyBuildstrs; 40 41 import com.idaremedia.antx.AntX; 42 import com.idaremedia.antx.AssertableTask; 43 import com.idaremedia.antx.apis.AntLibFriendly; 44 import com.idaremedia.antx.helpers.InputFileLoader; 45 import com.idaremedia.antx.helpers.Tk; 46 47 48 69 70 public class VendorInfoTask extends AssertableTask 71 implements AntLibFriendly 72 { 73 75 private static final String ANTX_BI= AntX.ANTX_PACKAGE_PREFIX+".BuildInfo"; 76 77 78 81 private static final Properties DEFAULT_KNOWN_MAPPINGS; 82 static { 83 Properties list = null; 84 URL url = VendorInfoTask.class.getResource("vendors.properties"); 85 if (url!=null) { 86 try { 87 list = InputFileLoader.loadProperties(url,null); 88 } catch(Exception iox) {} 89 } 90 if (list==null) { 91 System.err.println(AntX.uistrs().get("vendorinfo.noini")); 92 list = new Properties (); 93 list.setProperty("none",EmptyBuildstrs.class.getName()); 94 list.setProperty("antxtras",ANTX_BI); 95 list.setProperty("antx",ANTX_BI); 96 } 97 DEFAULT_KNOWN_MAPPINGS= list; 98 list= null; 99 } 100 101 102 103 106 public VendorInfoTask() 107 { 108 super(AntX.nopackage); 109 } 110 111 112 116 public VendorInfoTask(String iam) 117 { 118 super(iam); 119 } 120 121 122 129 protected final void setDefaultName(String defaultName) 130 { 131 require_(defaultName!=null,"setDfltName- nonzro name"); 132 m_defaultVendorName = defaultName; 133 } 134 135 139 144 public void setName(String name) 145 { 146 require_(name!=null,"setName- nonzro vendor name"); 147 if (m_infoClass!=null) { 148 String error = uistrs().get("task.one.or.other.attr", 149 "infoclass","name"); 150 log(error, Project.MSG_ERR); 151 throw new BuildException(error,getLocation()); 152 } 153 m_vendorName = name; 154 } 155 156 157 161 public String getName() 162 { 163 return m_vendorName; 164 } 165 166 167 174 public void setInfoClass(Class infoClass) 175 { 176 require_(infoClass!=null,"setInfoClaz- nonzro claz"); 177 String error=null; 178 if (!Buildstrs.class.isAssignableFrom(infoClass)) { 179 error = uistrs().get("task.bad.custimpl.class1",infoClass.getName(), 180 "Buildstrs"); 181 } 182 if (m_vendorName!=null) { 183 error = uistrs().get("task.one.or.other.attr","infoclass","name"); 184 } 185 if (error!=null) { 186 log(error, Project.MSG_ERR); 187 throw new BuildException(error,getLocation()); 188 } 189 m_infoClass = infoClass; 190 } 191 192 193 198 public Class getInfoClass() 199 { 200 return m_infoClass; 201 } 202 203 204 210 public void setMustExist(boolean must) 211 { 212 m_mustExist = must; 213 } 214 215 216 221 public boolean getMustExist() 222 { 223 return m_mustExist; 224 } 225 226 227 233 public void setPrefix(String prefix) 234 { 235 require_(prefix!=null,"setPrefix- nonzro prefix"); 236 m_customPrefix = prefix; 237 } 238 239 240 245 public String getPrefix() 246 { 247 return m_customPrefix; 248 } 249 250 251 256 public String getFinalPrefix() 257 { 258 if (m_customPrefix!=null) { 259 String prefix = m_customPrefix; 260 if (!prefix.endsWith(".")) { 261 prefix += "."; 262 } 263 return prefix; 264 } 265 if (m_vendorName!=null) { 266 return m_vendorName+".build."; 267 } 268 return "vendor.build."; 269 } 270 271 272 278 public void setFields(String fieldnames) 279 { 280 require_(fieldnames!=null,"setFields- nonzro list"); 281 m_fieldsList = Tk.lowercaseFrom(fieldnames); 282 } 283 284 285 289 public String getFieldsList() 290 { 291 return m_fieldsList; 292 } 293 294 298 public static final String LABEL="label"; 299 public static final String VERSION="version"; 300 public static final String LONGVERSION="longversion"; 301 public static final String DATE="date"; 302 public static final String LONGDATE="longdate"; 303 public static final String PLATFORM="platform"; 304 public static final String HOST="host"; 305 306 307 312 protected Properties getKnownVendors() 313 { 314 return m_knownVendors; 315 } 316 317 318 326 private void determineBuildInfoClass() 327 { 328 if (m_infoClass==null) { 329 if (m_vendorName==null && m_infoClass==null) { 330 m_vendorName = m_defaultVendorName; 331 } 332 333 if (m_vendorName!=null) { 334 String classname = getKnownVendors().getProperty(m_vendorName); 335 if (classname==null) { 336 String error = uistrs().get("vendorinfo.nomatch.name", 337 m_vendorName); 338 if (getMustExist()) { 339 log(error, Project.MSG_ERR); 340 throw new BuildException(error,getLocation()); 341 } 342 log(error, Project.MSG_WARN); 343 m_infoClass = EmptyBuildstrs.class; 344 } else { 345 try { 346 m_infoClass = Class.forName(classname); 347 } catch(Exception anyX) { 348 String error = uistrs().get("vendorinfo.cant.makeinfo", 349 classname); 350 if (getMustExist()) { 351 log(error, Project.MSG_ERR); 352 throw new BuildException(error,getLocation()); 353 } 354 log(error, Project.MSG_WARN); 355 m_infoClass = EmptyBuildstrs.class; 356 } 357 } 358 } } 360 } 361 362 363 371 private Buildstrs getBuildInfo() 372 { 373 if (m_buildinfo==null) { 374 determineBuildInfoClass(); 375 try { 376 m_buildinfo = (Buildstrs)m_infoClass.newInstance(); 377 } catch(Exception anyX) { 378 String error = uistrs().get("vendorinfo.cant.makeinfo", 379 m_infoClass.getName()); 380 if (getMustExist()) { 381 log(error, Project.MSG_ERR); 382 throw new BuildException(error,anyX,getLocation()); 383 } 384 log(error, Project.MSG_WARN); 385 m_buildinfo = EmptyBuildstrs.INSTANCE; 386 } 387 } 388 return m_buildinfo; 389 } 390 391 392 395 private void setInfoProperty(String name, String value) 396 { 397 name = getFinalPrefix()+name; 398 checkIfProperty_(name,true); 399 getProject().setNewProperty(name,value); 400 } 401 402 403 406 private void putAllProperties(Buildstrs info) 407 { 408 setInfoProperty(LABEL,info.getDisplayName()); 409 setInfoProperty(VERSION,info.getVersion()); 410 setInfoProperty(LONGVERSION,info.getBuildVersion()); 411 setInfoProperty(DATE,info.getAbbrDate()); 412 setInfoProperty(LONGDATE,info.getLongDate()); 413 setInfoProperty(PLATFORM,info.getOS()); 414 setInfoProperty(HOST,info.getHostID()); 415 } 416 417 418 422 private String getFieldValue(Buildstrs info, String f) 423 { 424 String value = null; 425 switch (f.charAt(0)) { 426 case 'v': { 427 if (VERSION.equals(f)) { 428 value = info.getVersion(); 429 } break; 430 } 431 case 'd': { 432 if (DATE.equals(f)) { 433 value = info.getAbbrDate(); 434 } break; 435 } 436 case 'l': { 437 if (LABEL.equals(f)) { 438 value = info.getDisplayName(); 439 } else if (LONGVERSION.equals(f)) { 440 value = info.getBuildVersion(); 441 } else if (LONGDATE.equals(f)) { 442 value = info.getLongDate(); 443 } break; 444 } 445 case 'p': { 446 if (PLATFORM.equals(f)) { 447 value = info.getOS(); 448 } break; 449 } 450 case 'h': { 451 if (HOST.equals(f)) { 452 value = info.getHostID(); 453 } break; 454 } 455 } 456 return value; 457 } 458 459 460 463 private void putTheseProperties(Buildstrs info, String filterlist) 464 { 465 List l= Tk.splitList(filterlist); 466 if (!l.isEmpty()) { 467 for (int i=0,N=l.size();i<N;i++) { 468 String f = l.get(i).toString(); 469 String value = getFieldValue(info,f); 470 if (value!=null) { 471 setInfoProperty(f,value); 472 } else { 473 String warning = uistrs().get("vendorinfo.bad.field",f); 474 log(warning,Project.MSG_WARN); 475 } 476 } 477 } 478 l=null; 479 } 480 481 482 486 public void execute() 487 { 488 verifyCanExecute_("exec"); 489 490 Buildstrs info = getBuildInfo(); 491 492 if (m_fieldsList==null) { 493 putAllProperties(info); 494 } else { 495 putTheseProperties(info,m_fieldsList); 496 } 497 } 498 499 500 private boolean m_mustExist=true; 501 private String m_vendorName; 502 private Class m_infoClass; 503 private String m_customPrefix, m_fieldsList; 504 private Buildstrs m_buildinfo; 505 private String m_defaultVendorName="antxtras"; 506 private Properties m_knownVendors= new Properties (DEFAULT_KNOWN_MAPPINGS); 507 } 508 509 510 | Popular Tags |