1 19 20 21 package org.netbeans.modules.j2ee.sun.share.configbean; 22 23 import java.text.MessageFormat ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 27 import javax.enterprise.deploy.model.DDBean ; 28 import javax.enterprise.deploy.model.XpathEvent ; 29 30 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean; 31 import org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException; 32 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp; 33 import org.netbeans.modules.j2ee.sun.dd.api.web.Servlet; 34 import org.netbeans.modules.j2ee.sun.share.configbean.Base.DefaultSnippet; 35 36 37 48 public class ServletRef extends Base { 49 50 52 public static final String SERVLET_NAME = "servletName"; public static final String RUN_AS_ROLE_NAME = "runAsRoleName"; 55 56 57 private DDBean servletNameDD; 58 59 60 private String principalName; 61 62 63 private String className; 64 65 66 public ServletRef() { 67 setDescriptorElement(bundle.getString("BDN_Servlet")); } 69 70 75 protected void init(DDBean dDBean, Base parent) throws ConfigurationException { 76 super.init(dDBean, parent); 77 78 81 servletNameDD = getNameDD("servlet-name"); 82 83 updateNamedBeanCache(SunWebApp.SERVLET); 84 85 loadFromPlanFile(getConfig()); 86 } 87 88 protected String getComponentName() { 89 return getServletName(); 90 } 91 92 95 96 public static final String FIELD_PRINCIPAL_CLASS_NAME=":class-name"; 98 99 protected void updateValidationFieldList() { 100 super.updateValidationFieldList(); 101 validationFieldList.add(FIELD_PRINCIPAL_CLASS_NAME); 102 } 103 104 public boolean validateField(String fieldId) { 105 ValidationError error = null; 106 boolean result = true; 107 108 if(fieldId.equals(FIELD_PRINCIPAL_CLASS_NAME)) { 109 String absoluteFieldXpath = getAbsoluteXpath("principal-name/" + fieldId); 114 if(Utils.notEmpty(className) && !Utils.isJavaClass(className)) { 115 Object [] args = new Object [1]; 116 args[0] = FIELD_PRINCIPAL_CLASS_NAME; 117 String message = MessageFormat.format(bundle.getString("ERR_InvalidJavaClass"), args); error = ValidationError.getValidationError(absoluteFieldXpath, message); 119 } else { 120 error = ValidationError.getValidationErrorMask(absoluteFieldXpath); 121 } 122 } 123 124 if(error != null) { 125 getMessageDB().updateError(error); 126 } 127 128 return (error == null || !Utils.notEmpty(error.getMessage())); 130 } 131 132 135 public String getHelpId() { 136 return "AS_CFG_Servlet"; 137 } 138 139 143 public String getServletName() { 144 return cleanDDBeanText(servletNameDD); 145 } 146 147 public String getRunAsRoleName() { 148 String roleName = null; 149 150 DDBean [] beans = getDDBean().getChildBean("run-as/role-name"); if(beans.length > 0) { 152 roleName = beans[0].getText(); 154 } 155 156 return roleName; 157 } 158 159 164 public void notifyDDChange(XpathEvent xpathEvent) { 165 super.notifyDDChange(xpathEvent); 166 167 DDBean eventBean = xpathEvent.getBean(); 168 169 if(eventBean == servletNameDD) { 170 getPCS().firePropertyChange(SERVLET_NAME, GenericOldValue, getServletName()); 172 getPCS().firePropertyChange(DISPLAY_NAME, GenericOldValue, getDisplayName()); 173 174 updateNamedBeanCache(SunWebApp.SERVLET); 175 } else if(eventBean.getXpath().indexOf("run-as") != -1) { 176 getPCS().firePropertyChange(RUN_AS_ROLE_NAME, GenericOldValue, getRunAsRoleName()); 177 } 178 } 179 180 184 public String getPrincipalName() { 185 return principalName; 186 } 187 188 194 public void setPrincipalName(String newPrincipalName) throws java.beans.PropertyVetoException { 195 String oldPrincipalName = principalName; 196 getVCS().fireVetoableChange("principalName", oldPrincipalName, newPrincipalName); 197 principalName = newPrincipalName; 198 getPCS().firePropertyChange("principalName", oldPrincipalName, principalName); 199 } 200 201 205 public String getClassName() { 206 return className; 207 } 208 209 215 public void setClassName(String newClassName) throws java.beans.PropertyVetoException { 216 String oldClassName = className; 217 getVCS().fireVetoableChange("className", oldClassName, newClassName); 218 className = newClassName; 219 getPCS().firePropertyChange("className", oldClassName, className); 220 } 221 222 226 Collection getSnippets() { 227 Collection snippets = new ArrayList (); 228 Snippet snipOne = new DefaultSnippet() { 229 public CommonDDBean getDDSnippet() { 230 Servlet sg = getConfig().getStorageFactory().createServlet(); 231 232 String sn = getServletName(); 234 if(sn != null) { 235 sg.setServletName(sn); 236 } 237 238 if(Utils.notEmpty(principalName)) { 239 sg.setPrincipalName(principalName); 240 if(Utils.notEmpty(className)) { 241 try { 242 sg.setPrincipalNameClassName(className); 243 } catch (VersionNotSupportedException ex) { 244 } 246 } 247 } 248 249 return sg; 250 } 251 252 public boolean hasDDSnippet() { 253 if(principalName != null && principalName.length() > 0) { 255 return true; 256 } 257 258 return false; 259 } 260 261 270 271 278 public String getPropertyName() { 279 return SunWebApp.SERVLET; 280 } 281 }; 282 283 snippets.add(snipOne); 284 return snippets; 285 } 286 287 private class ServletRefFinder extends NameBasedFinder { 288 public ServletRefFinder(String beanName) { 289 super(Servlet.SERVLET_NAME, beanName, Servlet.class); 290 } 291 } 292 293 boolean loadFromPlanFile(SunONEDeploymentConfiguration config) { 294 String uriText = getUriText(); 295 296 Servlet beanGraph = (Servlet) config.getBeans(uriText, constructFileName(), 297 getParser(), new ServletRefFinder(getServletName())); 298 299 clearProperties(); 300 301 if(beanGraph != null) { 302 principalName = beanGraph.getPrincipalName(); 303 try { 304 className = beanGraph.getPrincipalNameClassName(); 305 } catch(VersionNotSupportedException ex) { 306 } 308 } else { 309 setDefaultProperties(); 310 } 311 312 return (beanGraph != null); 313 } 314 315 protected void clearProperties() { 316 principalName = null; 317 className = null; 318 } 319 320 protected void setDefaultProperties() { 321 } 322 } 323 | Popular Tags |