1 19 20 21 package org.netbeans.modules.j2ee.sun.share.configbean; 22 23 import java.util.Collection ; 24 import java.util.ArrayList ; 25 import java.text.MessageFormat ; 26 27 import org.netbeans.modules.schema2beans.BaseBean; 28 29 import javax.enterprise.deploy.spi.DConfigBean ; 30 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 31 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 32 import javax.enterprise.deploy.model.DDBean ; 33 import javax.enterprise.deploy.model.XpathEvent ; 34 35 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean; 36 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp; 37 import org.netbeans.modules.j2ee.sun.dd.api.common.DefaultResourcePrincipal; 38 39 40 44 public class ResourceRef extends Base { 45 46 48 public static final String RES_REF_NAME = "resRefName"; 50 51 private DDBean resRefNameDD; 52 53 54 private String jndiName; 55 56 57 private String principalName; 58 59 60 private String principalPassword; 61 62 63 public ResourceRef() { 64 setDescriptorElement(bundle.getString("BDN_ResourceRef")); } 66 67 71 protected void init(DDBean dDBean, Base parent) throws ConfigurationException { 72 super.init(dDBean, parent); 73 76 resRefNameDD = getNameDD("res-ref-name"); 77 78 updateNamedBeanCache(SunWebApp.RESOURCE_REF); 79 80 loadFromPlanFile(getConfig()); 81 } 82 83 protected String getComponentName() { 84 return getResRefName(); 85 } 86 87 90 91 public static final String FIELD_JNDI_NAME="jndi-name"; 93 94 protected void updateValidationFieldList() { 95 super.updateValidationFieldList(); 96 validationFieldList.add(FIELD_JNDI_NAME); 97 } 98 99 public boolean validateField(String fieldId) { 100 ValidationError error = null; 101 boolean result = true; 102 103 if(fieldId.equals(FIELD_JNDI_NAME)) { 104 J2EEBaseVersion moduleVersion = getJ2EEModuleVersion(); 109 if(moduleVersion.compareSpecification(J2EEVersion.JAVAEE_5_0) < 0) { 110 String absoluteFieldXpath = getAbsoluteXpath(fieldId); 111 if(!Utils.notEmpty(jndiName)) { 112 Object [] args = new Object [1]; 113 args[0] = FIELD_JNDI_NAME; 114 String message = MessageFormat.format(bundle.getString("ERR_SpecifiedFieldIsEmpty"), args); error = ValidationError.getValidationError(absoluteFieldXpath, message); 116 } else { 117 error = ValidationError.getValidationErrorMask(absoluteFieldXpath); 118 } 119 } 120 } 121 122 if(error != null) { 123 getMessageDB().updateError(error); 124 } 125 126 return (error == null || !Utils.notEmpty(error.getMessage())); 128 } 129 130 133 public String getHelpId() { 134 return "AS_CFG_ResourceRef"; 135 } 136 137 142 public void notifyDDChange(XpathEvent xpathEvent) { 143 super.notifyDDChange(xpathEvent); 144 145 if(resRefNameDD == xpathEvent.getBean()) { 146 getPCS().firePropertyChange(RES_REF_NAME, "", getResRefName()); 148 getPCS().firePropertyChange(DISPLAY_NAME, "", getDisplayName()); 149 150 updateNamedBeanCache(SunWebApp.RESOURCE_REF); 151 } 152 } 153 154 158 public String getResRefName() { 159 return cleanDDBeanText(resRefNameDD); 160 } 161 162 166 public String getJndiName() { 167 return this.jndiName; 168 } 169 170 176 public void setJndiName(String jndiName) throws java.beans.PropertyVetoException { 177 String oldJndiName = this.jndiName; 178 getVCS().fireVetoableChange("jndiName", oldJndiName, jndiName); 179 this.jndiName = jndiName; 180 getPCS().firePropertyChange("jndiName", oldJndiName, jndiName); 181 } 182 183 187 public String getPrincipalName() { 188 return this.principalName; 189 } 190 191 197 public void setPrincipalName(String principalName) throws java.beans.PropertyVetoException { 198 String oldPrincipalName = this.principalName; 199 getVCS().fireVetoableChange("principalName", oldPrincipalName, principalName); 200 this.principalName = principalName; 201 getPCS().firePropertyChange("principalName", oldPrincipalName, principalName); 202 } 203 204 208 public String getPrincipalPassword() { 209 return this.principalPassword; 210 } 211 212 218 public void setPrincipalPassword(String principalPassword) throws java.beans.PropertyVetoException { 219 String oldPrincipalPassword = this.principalPassword; 220 getVCS().fireVetoableChange("principalPassword", oldPrincipalPassword, principalPassword); 221 this.principalPassword = principalPassword; 222 getPCS().firePropertyChange("principalPassword", oldPrincipalPassword, principalPassword); 223 } 224 225 237 Collection getSnippets() { 238 Collection snippets = new ArrayList (); 239 Snippet snipOne = new DefaultSnippet() { 240 241 public CommonDDBean getDDSnippet() { 242 org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef ref = 243 getConfig().getStorageFactory().createResourceRef(); 244 245 String resRefName = getResRefName(); 247 if(resRefName != null) { 248 ref.setResRefName(resRefName); 249 } 250 251 if(jndiName != null && jndiName.length() > 0) { 252 ref.setJndiName(jndiName); 253 } 254 255 boolean hasPrincipalName = (principalName != null && principalName.length() > 0); 256 boolean hasPrincipalPassword = (principalPassword != null && principalPassword.length() > 0); 257 258 if(hasPrincipalName || hasPrincipalPassword) { 259 DefaultResourcePrincipal drp = ref.getDefaultResourcePrincipal(); 260 if(drp == null) { 261 drp = ref.newDefaultResourcePrincipal(); 262 } 263 264 if(hasPrincipalName) { 265 drp.setName(principalName); 266 } 267 268 if(hasPrincipalPassword) { 269 drp.setPassword(principalPassword); 270 } 271 272 ref.setDefaultResourcePrincipal(drp); 273 } 274 275 return ref; 276 } 277 278 public boolean hasDDSnippet() { 279 if(jndiName != null && jndiName.length() > 0) { 280 return true; 281 } 282 283 if(principalName != null && principalName.length() > 0) { 284 return true; 285 } 286 287 if(principalPassword != null && principalPassword.length() > 0) { 288 return true; 289 } 290 291 return false; 292 } 293 294 295 public String getPropertyName() { 296 return SunWebApp.RESOURCE_REF; 297 } 298 299 }; 300 301 snippets.add(snipOne); 302 return snippets; 303 } 304 305 332 333 private class ResourceRefFinder extends NameBasedFinder { 334 public ResourceRefFinder(String beanName) { 335 super(org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef.RES_REF_NAME, 336 beanName, org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef.class); 337 } 338 } 339 340 boolean loadFromPlanFile(SunONEDeploymentConfiguration config) { 341 String uriText = getUriText(); 342 343 org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef beanGraph = 344 (org.netbeans.modules.j2ee.sun.dd.api.common.ResourceRef) config.getBeans(uriText, 345 constructFileName(), getParser(), new ResourceRefFinder(getResRefName())); 346 347 clearProperties(); 348 349 if(beanGraph != null) { 350 jndiName = beanGraph.getJndiName(); 351 352 DefaultResourcePrincipal drp = beanGraph.getDefaultResourcePrincipal(); 353 if(drp != null) { 354 principalName = drp.getName(); 355 principalPassword = drp.getPassword(); 356 } 357 } else { 358 setDefaultProperties(); 359 } 360 361 return (beanGraph != null); 362 } 363 364 protected void clearProperties() { 365 jndiName = null; 366 principalName = null; 367 principalPassword = null; 368 } 369 370 protected void setDefaultProperties() { 371 if(requiresJndiName()) { 372 jndiName = getResRefName(); 373 getConfig().getMasterDCBRoot().setDirty(); 374 } 375 } 376 } 377 | Popular Tags |