1 28 29 package com.idaremedia.antx.init; 30 31 import java.io.ByteArrayInputStream ; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.net.MalformedURLException ; 35 import java.net.URL ; 36 import java.util.Properties ; 37 import java.util.PropertyResourceBundle ; 38 39 import org.apache.tools.ant.AntClassLoader; 40 import org.apache.tools.ant.BuildException; 41 import org.apache.tools.ant.Project; 42 import org.apache.tools.ant.types.Path; 43 import org.apache.tools.ant.types.Reference; 44 import org.apache.tools.ant.util.ClasspathUtils; 45 46 import com.idaremedia.antx.AntX; 47 import com.idaremedia.antx.AntXFixture; 48 import com.idaremedia.antx.AssertableDataType; 49 import com.idaremedia.antx.FixtureComponent; 50 import com.idaremedia.antx.apis.AntLibFriendly; 51 import com.idaremedia.antx.apis.BuildError; 52 import com.idaremedia.antx.apis.ProblemHandler; 53 import com.idaremedia.antx.helpers.InputFileLoader; 54 import com.idaremedia.antx.helpers.Tk; 55 import com.idaremedia.antx.ownhelpers.CustomLoaderFactoryMethod; 56 import com.idaremedia.antx.ownhelpers.LocalTk; 57 import com.idaremedia.antx.parameters.CustomLoaderEnabled; 58 import com.idaremedia.antx.parameters.FlexExistenceEnabled; 59 60 84 85 public class UISMBundle extends AssertableDataType 86 implements FixtureComponent, CustomLoaderEnabled, FlexExistenceEnabled, 87 UISMSource, Cloneable , AntLibFriendly 88 { 89 93 public UISMBundle() 94 { 95 super(AntX.uism); 96 } 97 98 99 105 public UISMBundle(boolean mustExist) 106 { 107 super(AntX.uism); 108 m_failIfMissing = mustExist; 109 } 110 111 112 116 public void setId(String id) 117 { 118 m_Id= id; 119 } 120 121 122 128 public final String getId() 129 { 130 if (m_Id!=null) { 131 return m_Id; 132 } 133 if (isReference()) { 134 return getBundleRef().getId(); 135 } 136 return super.getId(); 137 } 138 139 140 147 public void setController(CustomLoaderFactoryMethod cLFactory) 148 { 149 m_cLFactory = cLFactory; 150 } 151 152 153 158 public void setMustExist(boolean mustExist) 159 { 160 if (isReference()) { 161 throw tooManyAttributes(); 162 } 163 m_failIfMissing = mustExist; 164 edited(); 165 } 166 167 168 173 public boolean getMustExist() 174 { 175 if (isReference()) { 176 return getBundleRef().getMustExist(); 177 } 178 return m_failIfMissing; 179 } 180 181 182 183 189 public synchronized Properties newProperties(ProblemHandler errH) 190 { 191 if (isReference()) { 192 return getBundleRef().newProperties(errH); 193 } 194 if (!triedLoadOnce()) { 195 tryLoadSource(errH); 196 } 197 try { 198 Properties p= new Properties (); 199 p.load(new ByteArrayInputStream (getBytes())); 200 return p; 201 } catch(IOException iox) { 202 throw new BuildError(iox.getMessage()); } 204 } 205 206 207 208 209 215 public synchronized PropertyResourceBundle newPropertyBundle(ProblemHandler errH) 216 { 217 if (isReference()) { 218 PropertyResourceBundle rb = getBundleRef().newPropertyBundle(errH); 219 220 if (!m_isInited) { m_isInited=true; 222 } 223 return rb; 224 } 225 226 if (!triedLoadOnce()) { 227 tryLoadSource(errH); 228 } 229 try { 230 return new PropertyResourceBundle (new ByteArrayInputStream (getBytes())); 231 } catch(IOException iox) { 232 throw new BuildError(iox.getMessage()); } 234 } 235 236 237 243 public Object clone() 244 { 245 if (isReference()) { 246 return getBundleRef().clone(); 247 } 248 try { 249 UISMBundle copy = (UISMBundle)super.clone(); 250 if (m_file!=null) { 251 copy.m_file = new File (m_file.getPath()); 252 } 253 if (m_URL!=null) { 254 copy.m_URL= new URL (m_URL.toString()); 255 } 256 if (m_bytes.length>0) { 257 byte[] newbytes = new byte[m_bytes.length]; 258 System.arraycopy(m_bytes,0,newbytes,0,m_bytes.length); 259 copy.setBytes(newbytes); 260 } 261 return copy; 262 } catch (MalformedURLException mfx) { 263 throw new BuildException(mfx); 264 } catch(CloneNotSupportedException clnx) { 265 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 266 } 267 } 268 269 273 279 public void setURL(String urlstr) 280 { 281 require_(urlstr!=null,"setURL- nonzro url"); 282 if (isReference()) { 283 throw tooManyAttributes(); 284 } 285 checkNewDatasource(); 286 m_URLstr= urlstr; 287 m_URL= null; 288 edited(UISMTk.BY_URL); 289 } 290 291 292 293 297 public final URL getURL() 298 { 299 if (isReference()) { 300 return getBundleRef().getURL(); 301 } 302 if (m_URL!=null) { 303 return m_URL; 304 } 305 if (m_URLstr!=null) { 306 try { 307 m_URL = new URL (m_URLstr); 308 } catch(MalformedURLException mfx) { 309 log(uistrs().get("task.uism.err.bad.url",m_URLstr), Project.MSG_ERR); 310 } 311 } 312 return m_URL; 313 } 314 315 316 324 protected final boolean copyURLStreamToSelf(URL url) 325 { 326 boolean copied=false; 327 try { 328 setBytes(new InputFileLoader().loadURL(url)); log("Copied UISM URL("+url+") to bundle",Project.MSG_DEBUG); 330 copied=true; 331 } catch(IOException iox) { 332 log(uistrs().get("task.uism.err.loading.url",url.toString()), 333 Project.MSG_ERR); 334 } 335 return copied; 336 } 337 338 339 343 protected final boolean handleThisURL(URL url) 344 { 345 File f = Tk.toFile(url); 346 if (f==null) { 347 return copyURLStreamToSelf(url); 348 } else if (f.canRead()) { 349 boolean OK = copyURLStreamToSelf(url); 350 if (OK) { 351 m_file = f; 352 } 353 return OK; 354 } else { 355 log(uistrs().get("task.uism.err.bad.file",f.getPath()), 356 Project.MSG_ERR); 357 } 358 return false; 359 } 360 361 362 367 protected final boolean handleURL() 368 { 369 URL url = getURL(); 370 371 return (url!=null) ? handleThisURL(url) : false; 372 } 373 374 378 384 public void setFile(String fp) 385 { 386 require_(fp!=null,"setFil- nonzro filpath"); 387 if (isReference()) { 388 throw tooManyAttributes(); 389 } 390 checkNewDatasource(); 391 m_file= getProject().resolveFile(fp); 392 edited(UISMTk.BY_FILE); 393 } 394 395 396 400 public final File getFile() 401 { 402 if (isReference()) { 403 return getBundleRef().getFile(); 404 } 405 return m_file; 406 } 407 408 409 414 protected final boolean handleFile() 415 { 416 File f = getFile(); 417 if (f!=null) { 418 if (f.canRead()) { 419 try { 420 URL furl = AntXFixture.fileUtils().getFileURL(f); 421 boolean OK = copyURLStreamToSelf(furl); 422 if (OK) { 423 m_URL = furl; 424 } 425 return OK; 426 } catch(MalformedURLException mfx) { 427 log(uistrs().get("task.uism.err.bad.file",f.getPath()), 428 Project.MSG_ERR); 429 } 430 } else { 431 log(uistrs().get("task.uism.err.bad.file",f.getPath()), 432 Project.MSG_ERR); 433 } 434 } 435 return false; 436 } 437 438 442 447 public Path createClassPath() 448 { 449 return getCLSpi(true).createClasspath(); 450 } 451 452 453 457 public final Path getClassPath() 458 { 459 if (isReference()) { 460 return getBundleRef().getClassPath(); 461 } 462 if (m_CLspi!=null) { 463 return m_CLspi.getClasspath(); 464 } 465 return null; 466 } 467 468 469 473 public void setClassPath(Path classpath) 474 { 475 require_(classpath!=null,"setClzpath- nonzro path"); 476 getCLSpi(false).setClasspath(classpath); 477 } 478 479 480 485 public void setClassPathRef(Reference r) 486 { 487 require_(r!=null,"setClzpathRef- nonzro ref"); 488 getCLSpi(false).setClasspathref(r); 489 } 490 491 492 500 public void setLoaderRef(Reference r) 501 { 502 require_(r!=null,"setLodrRef- nonzro ref"); 503 getCLSpi(false).setLoaderRef(r); 504 } 505 506 507 514 public String getLoaderRefId() 515 { 516 if (isReference()) { 517 return getBundleRef().getLoaderRefId(); 518 } 519 if (m_CLspi!=null) { 520 return m_CLspi.getClassLoadId(); 521 } 522 return null; 523 } 524 525 526 530 public void setResource(String rsrc) 531 { 532 require_(rsrc!=null,"setSysRsrc- nonzro rsrc"); 533 if (isReference()) { 534 throw tooManyAttributes(); 535 } 536 checkNewDatasource(); 537 m_resource = rsrc; 538 edited(UISMTk.BY_REZ); 539 } 540 541 542 546 public final String getResource() 547 { 548 if (isReference()) { 549 return getBundleRef().getResource(); 550 } 551 return m_resource; 552 } 553 554 555 559 protected final boolean handleThisResource(String rsrc) 560 { 561 boolean OK=false; 562 563 AntClassLoader AcL=null; 564 ClassLoader cL=null; 565 URL url; 566 Project P= getProject(); 567 568 if (m_cLFactory!=null) { 569 cL = m_cLFactory.getClassLoader(); 570 } 571 if (cL==null) { 572 if (P!=null && m_CLspi!=null) { 573 AcL = (AntClassLoader)m_CLspi.getClassLoader(); 574 cL = AcL; 575 } else { 576 cL = this.getClass().getClassLoader(); 577 } 578 } 579 if (cL==null) { 580 url = LocalTk.getSystemResource(rsrc, P); } else { 582 url = cL.getResource(rsrc); 583 } 584 585 if (url!=null) { 586 OK = handleThisURL(url); 587 if (OK) { 588 m_URL = url; 589 } 590 } else { 591 log(uistrs().get("task.uism.err.bad.resource",rsrc), 592 Project.MSG_ERR); 593 } 594 595 if (AcL!=null) { 596 if (m_CLspi.getClassLoadId()==null) { 597 AcL.cleanup(); 598 } 599 AcL=null; 600 } 601 return OK; 602 } 603 604 605 610 protected final boolean handleResource() 611 { 612 boolean OK=false; 613 String rsrc = getResource(); 614 615 if (rsrc!=null) { 616 OK = handleThisResource(rsrc); 617 } 618 619 return OK; 620 } 621 622 626 630 protected final UISMBundle getBundleRef() 631 { 632 return (UISMBundle)getCheckedRef(UISMBundle.class,"msgsbundle"); 633 } 634 635 636 640 protected final boolean triedLoadOnce() 641 { 642 return m_isInited; 643 } 644 645 646 652 private final ClasspathUtils.Delegate getCLSpi(boolean subel) 653 { 654 if (isReference()) { 655 if (subel) { 656 throw noChildrenAllowed(); 657 } 658 throw tooManyAttributes(); 659 } 660 if (m_CLspi==null) { 661 m_CLspi= ClasspathUtils.getDelegate(this); 662 edited(); 663 } 664 return m_CLspi; 665 } 666 667 668 677 private void tryLoadSource(ProblemHandler errH) throws BuildException 678 { 679 if (!m_isInited) { 680 if (!handleFile() && 681 !handleResource() && 682 !handleURL()) { 683 684 String defaultRsrc = null; 685 Project P= getProject(); 686 if (P!=null) { 687 defaultRsrc = P.getProperty(AntX.DEFAULT_UISTRS_BUNDLE_PROP); 688 } 689 if (defaultRsrc==null) { 690 defaultRsrc = System.getProperty(AntX.DEFAULT_UISTRS_BUNDLE_PROP); 691 } 692 if (defaultRsrc!=null) { 693 m_isInited = handleThisResource(defaultRsrc); 694 } 695 if (!m_isInited) { 696 String ermsg = uistrs().get("task.uism.err.invalid.setup"); 697 log(ermsg, Project.MSG_ERR); 698 if (errH!=null) { 699 errH.problem(ermsg, Project.MSG_ERR); 700 } 701 if (getMustExist()) { 702 throw new BuildException(ermsg); 703 } 704 } 705 } 706 m_isInited=true; 707 } 708 } 709 710 711 715 private void checkNewDatasource() 716 { 717 if (m_Nsources>0) { 718 throw UISMTk.tooManySources(); 719 } 720 m_Nsources++; 721 } 722 723 724 729 private byte[] getBytes() 730 { 731 return m_bytes; 732 } 733 734 735 739 protected final void setBytes(byte[] bytes) 740 { 741 require_(bytes!=null,"setBytes- nonzro byts"); 742 m_bytes = bytes; 743 } 744 745 746 private File m_file; 747 private String m_URLstr; 748 private URL m_URL; 749 private String m_resource; 750 private ClasspathUtils.Delegate m_CLspi; private boolean m_failIfMissing; private CustomLoaderFactoryMethod m_cLFactory; 754 private boolean m_isInited; 755 private byte[] m_bytes= new byte[0]; private int m_Nsources=0; private String m_Id; 758 } 759 760 761 | Popular Tags |