1 19 20 package org.netbeans.modules.j2ee.sun.share.configbean; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Collection ; 26 import java.util.ResourceBundle ; 27 import java.text.MessageFormat ; 28 29 import java.beans.PropertyChangeSupport ; 30 import java.beans.PropertyChangeListener ; 31 import java.beans.VetoableChangeSupport ; 32 import java.beans.VetoableChangeListener ; 33 34 import javax.enterprise.deploy.spi.DConfigBean ; 35 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 36 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 37 import javax.enterprise.deploy.model.DDBean ; 38 39 import org.netbeans.modules.j2ee.sun.share.Constants; 40 41 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean; 42 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp; 43 import org.netbeans.modules.j2ee.sun.dd.api.web.Cache; 44 import org.netbeans.modules.j2ee.sun.dd.api.web.CacheHelper; 45 import org.netbeans.modules.j2ee.sun.dd.api.web.CacheMapping; 46 import org.netbeans.modules.j2ee.sun.dd.api.web.DefaultHelper; 47 import org.netbeans.modules.j2ee.sun.dd.api.web.WebProperty; 48 49 50 109 public class WebAppCache { 110 111 113 private static final ResourceBundle bundle = ResourceBundle.getBundle( 114 "org.netbeans.modules.j2ee.sun.share.configbean.Bundle"); 116 private static final String SunWebFileName = "sun-web.xml"; 118 119 private WebAppRoot webAppRoot; 120 121 122 private String cacheMaxEntries; 123 124 125 private String timeoutInSeconds; 126 127 128 private String cacheEnabled; 129 130 131 private List properties; 132 133 134 private DefaultHelper defaultHelper; 135 136 137 private List cacheHelpers; 138 139 140 private List cacheMappings; 141 142 143 public WebAppCache() { 144 } 145 146 150 156 protected void init(WebAppRoot parent) { 157 webAppRoot = parent; 158 159 } 161 162 public Base getParent() { 163 return webAppRoot; 164 } 165 166 169 174 private Boolean isValid = null; 175 176 protected List validationFieldList = new ArrayList (); 177 178 public static final String FIELD_CACHE_MAX_ENTRIES=":max-entries"; 179 public static final String FIELD_CACHE_TIMEOUT=":timeout"; 180 181 protected void updateValidationFieldList() { 182 validationFieldList.add(FIELD_CACHE_MAX_ENTRIES); 183 validationFieldList.add(FIELD_CACHE_TIMEOUT); 184 } 185 186 public void validationStateChanged(Boolean newState) { 187 isValid = newState; 188 } 189 190 195 public boolean isValid() { 196 if(isValid == null) { 197 boolean tempValid = validateFields(true); 198 isValid = Boolean.valueOf(tempValid); 199 } 200 201 return isValid.booleanValue(); 202 } 203 204 209 public boolean validateFields(boolean shortCircuit) { 210 ErrorMessageDB messageDB = webAppRoot.getMessageDB(); 211 boolean result = true; 212 213 messageDB.clearErrors(); 214 for(Iterator iter = validationFieldList.iterator(); iter.hasNext() && (result || !shortCircuit); ) { 215 boolean fieldResult = validateField((String ) iter.next()); 216 result = result && fieldResult; 217 } 218 219 return result; 220 } 221 222 229 public boolean validateField(String fieldId) { 230 ValidationError error = null; 231 232 if(fieldId.equals(FIELD_CACHE_MAX_ENTRIES)) { 233 String absoluteFieldXpath = getAbsoluteXpath(fieldId); 234 235 if(Utils.notEmpty(cacheMaxEntries)) { 236 try { 237 int value = Integer.parseInt(cacheMaxEntries); 238 if(value < 0) { 239 Object [] args = new Object [2]; 240 args[0] = cacheMaxEntries; 241 args[1] = "0"; String message = MessageFormat.format(bundle.getString("ERR_NumberTooLow"), args); error = ValidationError.getValidationError(ValidationError.PARTITION_CACHE_GENERAL, absoluteFieldXpath, message); 244 } 245 } catch(NumberFormatException ex) { 246 Object [] args = new Object [1]; 247 args[0] = cacheMaxEntries; String message = MessageFormat.format(bundle.getString("ERR_NumberInvalid"), args); error = ValidationError.getValidationError(ValidationError.PARTITION_CACHE_GENERAL, absoluteFieldXpath, message); 250 } 251 } 252 253 if(error == null) { 254 error = ValidationError.getValidationErrorMask(ValidationError.PARTITION_CACHE_GENERAL, absoluteFieldXpath); 255 } 256 } else if(fieldId.equals(FIELD_CACHE_TIMEOUT)) { 257 String absoluteFieldXpath = getAbsoluteXpath(fieldId); 258 259 if(Utils.notEmpty(timeoutInSeconds)) { 260 try { 261 int value = Integer.parseInt(timeoutInSeconds); 262 if(value < -1) { 263 Object [] args = new Object [2]; 264 args[0] = timeoutInSeconds; 265 args[1] = "-1"; String message = MessageFormat.format(bundle.getString("ERR_NumberTooLow"), args); error = ValidationError.getValidationError(ValidationError.PARTITION_CACHE_GENERAL, absoluteFieldXpath, message); 268 } 269 } catch(NumberFormatException ex) { 270 Object [] args = new Object [1]; 271 args[0] = timeoutInSeconds; String message = MessageFormat.format(bundle.getString("ERR_NumberInvalid"), args); error = ValidationError.getValidationError(ValidationError.PARTITION_CACHE_GENERAL, absoluteFieldXpath, message); 274 } 275 } 276 277 if(error == null) { 278 error = ValidationError.getValidationErrorMask(ValidationError.PARTITION_CACHE_GENERAL, absoluteFieldXpath); 279 } 280 } 281 282 if(error != null) { 283 webAppRoot.getMessageDB().updateError(error); 284 } 285 286 return (error == null || !Utils.notEmpty(error.getMessage())); 288 } 289 290 protected String getAbsoluteXpath(String field) { 291 StringBuffer buf = new StringBuffer (field.length() + 20); 292 buf.append("/sun-web-app/cache/"); buf.append(field); 294 return buf.toString(); 295 } 296 297 299 300 304 public String getCacheMaxEntries() { 305 return this.cacheMaxEntries; 306 } 307 308 314 public void setCacheMaxEntries(String newCacheMaxEntries) throws java.beans.PropertyVetoException { 315 if(newCacheMaxEntries == null || newCacheMaxEntries.length() == 0) { newCacheMaxEntries = null; 317 } 318 319 String oldCacheMaxEntries = cacheMaxEntries; 320 getVCS().fireVetoableChange("cacheMaxEntries", oldCacheMaxEntries, newCacheMaxEntries); cacheMaxEntries = newCacheMaxEntries; 322 getPCS().firePropertyChange("cacheMaxEntries", oldCacheMaxEntries, cacheMaxEntries); } 324 325 329 public String getTimeoutInSeconds() { 330 return timeoutInSeconds; 331 } 332 333 339 public void setTimeoutInSeconds(String newTimeoutInSeconds) throws java.beans.PropertyVetoException { 340 if(newTimeoutInSeconds == null || newTimeoutInSeconds.length() == 0) { newTimeoutInSeconds = null; 342 } 343 344 String oldTimeoutInSeconds = timeoutInSeconds; 345 getVCS().fireVetoableChange("timeoutInSeconds", oldTimeoutInSeconds, newTimeoutInSeconds); timeoutInSeconds = newTimeoutInSeconds; 347 getPCS().firePropertyChange("timeoutInSeconds", oldTimeoutInSeconds, timeoutInSeconds); } 349 350 354 public String getCacheEnabled() { 355 return cacheEnabled; 356 } 357 358 364 public void setCacheEnabled(String newCacheEnabled) throws java.beans.PropertyVetoException { 365 String oldClassLoader = cacheEnabled; 366 getVCS().fireVetoableChange("cacheEnabled", new Boolean (oldClassLoader), new Boolean (newCacheEnabled)); cacheEnabled = newCacheEnabled; 368 getPCS().firePropertyChange("cacheEnabled", new Boolean (oldClassLoader), new Boolean (cacheEnabled)); } 370 371 375 public List getProperties() { 376 return properties; 377 } 378 379 public WebProperty getProperty(int index) { 380 return (WebProperty) properties.get(index); 381 } 382 383 389 public void setProperties(List newProperties) throws java.beans.PropertyVetoException { 390 List oldProperties = properties; 391 getVCS().fireVetoableChange("properties", oldProperties, newProperties); properties = newProperties; 393 getPCS().firePropertyChange("properties", oldProperties, properties); } 395 396 public void addProperty(WebProperty newProperty) throws java.beans.PropertyVetoException { 397 getVCS().fireVetoableChange("property", null, newProperty); if(properties == null) { 399 properties = new ArrayList (); 400 } 401 properties.add(newProperty); 402 getPCS().firePropertyChange("property", null, newProperty ); } 404 405 public void removeProperty(WebProperty oldProperty) throws java.beans.PropertyVetoException { 406 getVCS().fireVetoableChange("property", oldProperty, null); properties.remove(oldProperty); 408 getPCS().firePropertyChange("property", oldProperty, null ); } 410 411 414 public DefaultHelper getDefaultHelper() { 415 return this.defaultHelper; 416 } 417 418 423 public void setDefaultHelper(DefaultHelper newDefaultHelper) throws java.beans.PropertyVetoException { 424 if(newDefaultHelper == null) { 425 newDefaultHelper = webAppRoot.getConfig().getStorageFactory().createDefaultHelper(); 426 } 427 428 DefaultHelper oldDefaultHelper = this.defaultHelper; 429 getVCS().fireVetoableChange("defaultHelper", oldDefaultHelper, newDefaultHelper); this.defaultHelper = newDefaultHelper; 431 getPCS().firePropertyChange("defaultHelper", oldDefaultHelper, defaultHelper); } 433 434 438 public List getCacheHelpers() { 439 return cacheHelpers; 440 } 441 442 public CacheHelper getCacheHelper(int index) { 443 return (CacheHelper) cacheHelpers.get(index); 444 } 445 446 452 public void setCacheHelpers(List newCacheHelpers) throws java.beans.PropertyVetoException { 453 List oldCacheHelpers = cacheHelpers; 454 getVCS().fireVetoableChange("cacheHelpers", oldCacheHelpers, newCacheHelpers); cacheHelpers = newCacheHelpers; 456 getPCS().firePropertyChange("cacheHelpers", oldCacheHelpers, cacheHelpers); } 458 459 public void addProperty(CacheHelper newCacheHelper) throws java.beans.PropertyVetoException { 460 getVCS().fireVetoableChange("cacheHelper", null, newCacheHelper); if(cacheHelpers == null) { 462 cacheHelpers = new ArrayList (); 463 } 464 cacheHelpers.add(newCacheHelper); 465 getPCS().firePropertyChange("cacheHelper", null, newCacheHelper); } 467 468 public void removeProperty(CacheHelper oldCacheHelper) throws java.beans.PropertyVetoException { 469 getVCS().fireVetoableChange("cacheHelper", oldCacheHelper, null); cacheHelpers.remove(oldCacheHelper); 471 getPCS().firePropertyChange("cacheHelper", oldCacheHelper, null ); } 473 474 478 public List getCacheMappings() { 479 return cacheMappings; 480 } 481 482 public CacheMapping getCacheMapping(int index) { 483 return (CacheMapping) cacheMappings.get(index); 484 } 485 486 492 public void setCacheMappings(List newCacheMappings) throws java.beans.PropertyVetoException { 493 List oldCacheMappings = cacheMappings; 494 getVCS().fireVetoableChange("cacheMappings", oldCacheMappings, newCacheMappings); cacheMappings = newCacheMappings; 496 getPCS().firePropertyChange("cacheMappings", oldCacheMappings, cacheMappings); } 498 499 public void addCacheMapping(CacheMapping newCacheMapping) throws java.beans.PropertyVetoException { 500 getVCS().fireVetoableChange("cacheMapping", null, newCacheMapping); if(cacheMappings == null) { 502 cacheMappings = new ArrayList (); 503 } 504 cacheMappings.add(newCacheMapping); 505 getPCS().firePropertyChange("cacheMapping", null, newCacheMapping); } 507 508 public void removeCacheMapping(CacheMapping oldCacheMapping) throws java.beans.PropertyVetoException { 509 getVCS().fireVetoableChange("cacheMapping", oldCacheMapping, null); cacheMappings.remove(oldCacheMapping); 511 getPCS().firePropertyChange("cacheMapping", oldCacheMapping, null ); } 513 514 515 519 Collection getSnippets() { 520 Collection snippets = new ArrayList (); 521 Snippet snipOne = new Snippet() { 522 523 public org.netbeans.modules.schema2beans.BaseBean getCmpDDSnippet() { 524 return null; 525 } 526 527 public CommonDDBean getDDSnippet() { 528 Cache cache = webAppRoot.getConfig().getStorageFactory().createCache_NoDefaults(); 529 String version = webAppRoot.getAppServerVersion().getWebAppVersionAsString(); 530 531 if(cacheMaxEntries != null) { 533 cache.setMaxEntries(cacheMaxEntries); 534 } 535 536 if(timeoutInSeconds != null) { 537 cache.setTimeoutInSeconds(timeoutInSeconds); 538 } 539 540 if(cacheEnabled != null) { 541 cache.setEnabled(cacheEnabled); 542 } 543 544 CacheHelper [] helpers = (CacheHelper []) 546 Utils.listToArray(getCacheHelpers(), CacheHelper.class, version); 547 if(helpers != null) { 548 cache.setCacheHelper(helpers); 549 } 550 551 DefaultHelper dh = getDefaultHelper(); 553 if(dh.sizeWebProperty() > 0) { 554 cache.setDefaultHelper((DefaultHelper) dh.cloneVersion(version)); 555 } 556 557 WebProperty [] webProps = (WebProperty []) 559 Utils.listToArray(getProperties(), WebProperty.class, version); 560 if(webProps != null) { 561 cache.setWebProperty(webProps); 562 } 563 564 CacheMapping [] mappings = (CacheMapping []) 566 Utils.listToArray(getCacheMappings(), CacheMapping.class, version); 567 if(mappings != null) { 568 cache.setCacheMapping(mappings); 569 } 570 571 return cache; 572 } 573 574 public boolean hasDDSnippet() { 575 if(getCacheMaxEntries() != null) { 576 return true; 577 } 578 579 if(getTimeoutInSeconds() != null) { 580 return true; 581 } 582 583 if(getCacheEnabled() != null) { 584 return true; 585 } 586 587 List cacheHelpers = getCacheHelpers(); 588 if(cacheHelpers != null && cacheHelpers.size() > 0) { 589 return true; 590 } 591 592 DefaultHelper dh = getDefaultHelper(); 593 if(dh.sizeWebProperty() > 0) { 594 return true; 595 } 596 597 List properties = getProperties(); 598 if(properties != null && properties.size() > 0) { 599 return true; 600 } 601 602 List cacheMappings = getCacheMappings(); 603 if(cacheMappings != null && cacheMappings.size() > 0) { 604 return true; 605 } 606 607 return false; 608 } 609 610 public String getPropertyName() { 611 return SunWebApp.CACHE; 612 } 613 614 618 public String getFileName() { 619 return SunWebFileName; } 621 622 public CommonDDBean mergeIntoRootDD(CommonDDBean ddRoot) { 623 Cache cache = (Cache) getDDSnippet(); 624 625 if(ddRoot instanceof SunWebApp) { 626 SunWebApp swa = (SunWebApp) ddRoot; 627 swa.setCache(cache); 628 } 629 630 return cache; 631 } 632 633 public CommonDDBean mergeIntoRovingDD(CommonDDBean ddParent) { 634 return mergeIntoRootDD(ddParent); 639 } 640 641 }; 642 643 snippets.add(snipOne); 644 return snippets; 645 } 646 647 private class CacheFinder implements ConfigFinder { 648 public Object find(Object obj) { 649 Cache result = null; 650 651 if(obj instanceof SunWebApp) { 652 SunWebApp swa = (SunWebApp) obj; 653 result = swa.getCache(); 654 } 655 656 return result; 657 } 658 } 659 660 boolean loadFromPlanFile(SunONEDeploymentConfiguration config) { 661 String uriText = webAppRoot.getUriText(); 662 663 Cache beanGraph = (Cache) config.getBeans( 664 uriText, SunWebFileName, webAppRoot.getParser(), new CacheFinder()); 665 666 clearProperties(); 667 668 if(null != beanGraph) { 669 cacheMaxEntries = beanGraph.getMaxEntries(); 670 timeoutInSeconds = beanGraph.getTimeoutInSeconds(); 671 cacheEnabled = beanGraph.getEnabled(); 672 673 cacheHelpers = Utils.arrayToList(beanGraph.getCacheHelper()); 674 675 DefaultHelper dh = beanGraph.getDefaultHelper(); 676 if(dh != null && dh.sizeWebProperty() > 0) { 677 defaultHelper = (DefaultHelper) dh.clone(); 678 } 679 680 properties = Utils.arrayToList(beanGraph.getWebProperty()); 681 cacheMappings = Utils.arrayToList(beanGraph.getCacheMapping()); 682 } else { 683 setDefaultProperties(); 684 } 685 686 return (beanGraph != null); 687 } 688 689 protected void clearProperties() { 690 cacheMaxEntries = null; 691 timeoutInSeconds = null; 692 cacheEnabled = null; 693 cacheHelpers = null; 694 defaultHelper = webAppRoot.getConfig().getStorageFactory().createDefaultHelper(); 695 properties = null; 696 cacheMappings = null; 697 } 698 699 protected void setDefaultProperties() { 700 } 702 703 710 713 protected java.beans.PropertyChangeSupport getPCS() { 714 return webAppRoot.getPCS(); 715 } 716 717 720 protected java.beans.VetoableChangeSupport getVCS() { 721 return webAppRoot.getVCS(); 722 } 723 } 724 | Popular Tags |