1 19 20 package org.netbeans.modules.j2ee.ddloaders.multiview; 21 22 import java.util.StringTokenizer ; 23 import javax.swing.JRadioButton ; 24 import org.netbeans.modules.j2ee.dd.api.common.RunAs; 25 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException; 26 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor; 27 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb; 28 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 29 import org.netbeans.modules.j2ee.dd.api.ejb.Method; 30 import org.netbeans.modules.j2ee.dd.api.ejb.MethodPermission; 31 import org.netbeans.modules.j2ee.dd.api.ejb.SecurityIdentity; 32 import org.netbeans.modules.j2ee.ddloaders.multiview.ui.SecurityForm; 33 import org.netbeans.modules.xml.multiview.ItemCheckBoxHelper; 34 import org.netbeans.modules.xml.multiview.ItemEditorHelper; 35 import org.netbeans.modules.xml.multiview.ItemOptionHelper; 36 import org.netbeans.modules.xml.multiview.XmlMultiViewDataSynchronizer; 37 import org.netbeans.modules.xml.multiview.ui.SectionNodeView; 38 39 45 public class EjbSecurityPanel extends SecurityForm { 46 private static String ALL_METHODS = "*"; 48 private MethodPermission methodPermission; 49 50 private EjbJar ejbJar; 51 52 private AssemblyDescriptor assemblyDesc; 53 54 55 public EjbSecurityPanel(SectionNodeView sectionNodeView, final Ejb ejb) { 56 super(sectionNodeView); 57 58 EjbJarMultiViewDataObject dataObject = (EjbJarMultiViewDataObject) sectionNodeView.getDataObject(); 59 this.ejbJar = dataObject.getEjbJar(); 60 61 this.assemblyDesc = ejbJar.getSingleAssemblyDescriptor(); 62 63 final XmlMultiViewDataSynchronizer synchronizer = dataObject.getModelSynchronizer(); 64 65 addRefreshable(new ItemOptionHelper(synchronizer, getSecurityIDButtonGroup()) { 66 public String getItemValue() { 67 SecurityIdentity securityIdentity = ejb.getSecurityIdentity(); 68 69 if (securityIdentity != null) { 70 if (securityIdentity.isUseCallerIdentity()) { 71 return USE_CALLER_ID; 72 } else if (securityIdentity.getRunAs() != null) { 73 return RUN_AS; 74 } 75 } 76 77 return NO_SECURITY_ID; 78 } 79 80 public void setItemValue(String value) { 81 updateSecurityIdentity(ejb); 82 83 updateVisualState(); 84 } 85 }); 86 87 addRefreshable(new ItemEditorHelper(getRunAsRoleNameTF(), 88 new TextItemEditorModel(synchronizer, true, true) { 89 protected String getValue() { 90 RunAs runAs = getRunAs(ejb); 91 92 if (runAs != null) { 93 return runAs.getRoleName(); 94 } else { 95 return getRunAsRoleNameTF().getText(); 96 } 97 } 98 99 protected void setValue(String value) { 100 RunAs runAs = getRunAs(ejb); 101 102 if (runAs != null) { 103 updateRunAs(runAs); 104 } 105 } 106 })); 107 108 addRefreshable(new ItemEditorHelper(getRunAsDescriptionTF(), 109 new TextItemEditorModel(synchronizer, true, true) { 110 protected String getValue() { 111 RunAs runAs = getRunAs(ejb); 112 113 if (runAs != null) { 114 return runAs.getDefaultDescription(); 115 } else { 116 return getRunAsDescriptionTF().getText(); 117 } 118 } 119 120 protected void setValue(String value) { 121 RunAs runAs = getRunAs(ejb); 122 123 if (runAs != null) { 124 updateRunAs(runAs); 125 } 126 } 127 })); 128 129 addRefreshable(new ItemOptionHelper(synchronizer, 130 getGlobalMethodPermissionButtonGroup()) { 131 public String getItemValue() { 132 MethodPermission permission = getGlobalMethodPermission(ejb); 133 134 if (permission != null) { 135 try { 136 if (permission.isUnchecked()) { 137 return ALL_METHOD_PERMISSION; 138 } else { 139 return SET_ROLE_METHOD_PERMISSION; 140 } 141 } catch (Exception ex) { 142 return SET_ROLE_METHOD_PERMISSION; 143 } 144 } 145 146 return NO_METHOD_PERMISSION; 147 } 148 149 public void setItemValue(String value) { 150 updateMethodPermission(assemblyDesc, ejb); 151 152 updateVisualState(); 153 } 154 }); 155 156 addRefreshable(new ItemEditorHelper(getSetRoleRoleNamesTF(), 157 new TextItemEditorModel(synchronizer, true, true) { 158 boolean endsWithComma = false; 159 160 protected String getValue() { 161 MethodPermission permission = getGlobalMethodPermission(ejb); 162 163 try { 164 if (permission != null && !permission.isUnchecked()) { 165 String roleNames = getCommaSeparatedString(permission.getRoleName()); 166 if (endsWithComma) 167 roleNames += ","; 169 return roleNames; 170 } else { 171 return getSetRoleRoleNamesTF().getText(); 172 } 173 } catch (VersionNotSupportedException ex) { 174 return ""; } 176 } 177 178 protected void setValue(String value) { 179 if (value != null && value.trim().endsWith(",")) { endsWithComma = true; 181 } else { 182 endsWithComma = false; 183 } 184 185 updateMethodPermission(assemblyDesc, ejb); 186 } 187 })); 188 189 updateVisualState(); 190 } 191 192 public void dataModelPropertyChange(Object source, String propertyName, 193 Object oldValue, Object newValue) { 194 scheduleRefreshView(); 195 } 196 197 private void updateSecurityIdentity(Ejb ejb) { 198 JRadioButton noSecurityIDRB = getNoSecurityIDRB(); 199 JRadioButton useCallerIDRB = getUseCallerIDRB(); 200 JRadioButton runAsRB = getRunAsRB(); 201 202 if (noSecurityIDRB.isSelected()) { 203 removeSecurityIdentity(ejb); 204 } else { 205 SecurityIdentity securityID = ejb.getSecurityIdentity(); 206 if (securityID == null) { 207 securityID = ejb.newSecurityIdentity(); 208 ejb.setSecurityIdentity(securityID); 209 } 210 211 if (runAsRB.isSelected()) { 212 RunAs runAs = securityID.getRunAs(); 213 214 if (runAs == null) { 215 runAs = securityID.newRunAs(); 216 securityID.setRunAs(runAs); 217 218 updateRunAs(runAs); 219 } 220 } else { 221 removeRunAs(securityID); 222 } 223 224 if (useCallerIDRB.isSelected()) { 225 securityID.setUseCallerIdentity(true); 226 } else { 227 securityID.setUseCallerIdentity(false); 228 } 229 } 230 } 231 232 private void removeSecurityIdentity(Ejb ejb) { 233 ejb.setSecurityIdentity(null); 234 } 235 236 private RunAs getRunAs(Ejb ejb) { 237 SecurityIdentity securityIdentity = ejb.getSecurityIdentity(); 238 if (securityIdentity != null) { 239 return securityIdentity.getRunAs(); 240 } 241 242 return null; 243 } 244 245 private void updateRunAs(RunAs runAs) { 246 String prevRoleName = runAs.getRoleName(); 247 String newRoleName = getRunAsRoleNameTF().getText(); 248 runAs.setRoleName(newRoleName); 249 runAs.setDescription(this.getRunAsDescriptionTF().getText()); 250 } 251 252 private void removeRunAs(SecurityIdentity securityIdentity) { 253 RunAs runAs = securityIdentity.getRunAs(); 254 255 if (runAs != null) { 256 securityIdentity.setRunAs(null); 257 } 258 } 259 260 private MethodPermission getGlobalMethodPermission(Ejb ejb) { 261 262 if (assemblyDesc == null) return null; 263 264 MethodPermission methodPermission = null; 265 266 MethodPermission[] permissions = assemblyDesc.getMethodPermission(); 267 String ejbName = ejb.getEjbName(); 268 269 for (int i = 0; i < permissions.length; i++) { 270 MethodPermission permission = permissions[i]; 271 Method method = permission.getMethod(0); 272 273 if (method != null) { 274 String methodEjbName = method.getEjbName(); 275 String methodName = method.getMethodName(); 276 277 if (methodEjbName != null && methodEjbName.equals(ejbName) && 278 methodName != null && methodName.equals(ALL_METHODS)) { 279 methodPermission = permission; 280 break; 281 } 282 } 283 } 284 285 return methodPermission; 286 } 287 288 private MethodPermission createGlobalMethodPermission(Ejb ejb) { 289 if (assemblyDesc == null) { 290 assemblyDesc = getAssemblyDesc(); 291 } 292 293 methodPermission = assemblyDesc.newMethodPermission(); 294 Method method = methodPermission.newMethod(); 295 method.setEjbName(ejb.getEjbName()); 296 method.setMethodName(ALL_METHODS); methodPermission.addMethod(method); 298 assemblyDesc.addMethodPermission(methodPermission); 299 300 return methodPermission; 301 } 302 303 private void removeGlobalMethodPermission() { 304 if (methodPermission != null) { 305 assemblyDesc.removeMethodPermission(methodPermission); 306 methodPermission = null; 307 } 308 } 309 310 private void updateMethodPermission(AssemblyDescriptor assemblyDesc, Ejb ejb) { 311 if (this.getNoPermissionRB().isSelected()) { 312 removeGlobalMethodPermission(); 313 } else { 314 MethodPermission permission = getGlobalMethodPermission(ejb); 315 if (permission == null) { 316 permission = createGlobalMethodPermission(ejb); 317 } 318 319 if (this.getAllMethodPermissionRB().isSelected()) { 320 permission.setRoleName(null); 321 322 try { 323 permission.setUnchecked(true); 324 } catch (VersionNotSupportedException ex) { 325 ex.printStackTrace(); 326 } 327 } else if (this.getSetRolePermissionRB().isSelected()) { 328 try { 329 permission.setUnchecked(false); 330 } catch (VersionNotSupportedException ex) { 331 ex.printStackTrace(); 332 } 333 334 String roleNames = getSetRoleRoleNamesTF().getText(); 335 StringTokenizer tokenizer = new StringTokenizer (roleNames, ","); permission.setRoleName(null); 337 338 while (tokenizer.hasMoreTokens()) { 339 String roleName = tokenizer.nextToken().trim(); 340 341 if (roleName.length() > 0) 342 permission.addRoleName(roleName); 343 } 344 } 345 } 346 } 347 348 private String getCommaSeparatedString(String [] values) { 349 String result = ""; 351 for (int i = 0; i < values.length; i++) { 352 if (i > 0) { 353 result += ", "; } 355 356 result += values[i]; 357 } 358 359 return result; 360 } 361 362 private AssemblyDescriptor getAssemblyDesc() { 363 AssemblyDescriptor assemblyDesc = ejbJar.getSingleAssemblyDescriptor(); 364 365 if (assemblyDesc == null) { 366 assemblyDesc = ejbJar.newAssemblyDescriptor(); 367 ejbJar.setAssemblyDescriptor(assemblyDesc); 368 } 369 370 return assemblyDesc; 371 } 372 } 373 | Popular Tags |