1 13 14 package org.ejbca.core.ejb.authorization; 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 20 import javax.ejb.CreateException ; 21 import javax.ejb.EJBException ; 22 import javax.ejb.RemoveException ; 23 24 import org.ejbca.core.ejb.BaseEntityBean; 25 import org.ejbca.core.ejb.ServiceLocator; 26 import org.ejbca.core.model.authorization.AccessRule; 27 import org.ejbca.core.model.authorization.AdminEntity; 28 import org.ejbca.core.model.authorization.AdminGroup; 29 30 31 102 public abstract class AdminGroupDataBean extends BaseEntityBean { 103 104 108 public abstract Integer getPrimKey(); 109 public abstract void setPrimKey(Integer primKey); 110 111 115 public abstract String getAdminGroupName(); 116 117 120 public abstract void setAdminGroupName(String admingroupname); 121 122 126 public abstract int getCaId(); 127 128 131 public abstract void setCaId(int caid); 132 133 149 public abstract Collection getAdminEntities(); 150 public abstract void setAdminEntities(Collection adminentities); 151 152 169 public abstract Collection getAccessRules(); 170 public abstract void setAccessRules(Collection accessrules); 171 172 176 public void addAccessRules(Collection accessrules) { 177 Iterator iter = accessrules.iterator(); 178 while (iter.hasNext()) { 179 AccessRule accessrule = (AccessRule) iter.next(); 180 try { 181 AccessRulesDataLocal data = createAccessRule(accessrule); 182 Iterator i = getAccessRules().iterator(); 183 while (i.hasNext()) { 184 AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next(); 185 if (ar.getAccessRuleObject().getAccessRule().equals(accessrule.getAccessRule())) { 186 getAccessRules().remove(ar); 187 try { 188 ar.remove(); 189 } catch (RemoveException e) { 190 error("Error adding AccessRules: ", e); 191 throw new EJBException (e); 192 } 193 break; 194 } 195 } 196 getAccessRules().add(data); 197 } catch (Exception e) { 198 error("Error adding AccessRules: ", e); 199 } 200 } 201 } 203 207 public void removeAccessRules(Collection accessrules) { 208 Iterator iter = accessrules.iterator(); 209 while (iter.hasNext()) { 210 String accessrule = (String ) iter.next(); 211 212 Iterator i = getAccessRules().iterator(); 213 while (i.hasNext()) { 214 AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next(); 215 if (ar.getAccessRuleObject().getAccessRule().equals(accessrule)) { 216 getAccessRules().remove(ar); 217 try { 218 ar.remove(); 219 } catch (RemoveException e) { 220 error("Error removing AccessRules: ", e); 221 throw new EJBException (e); 222 } 223 break; 224 } 225 } 226 } 227 } 229 235 public int getNumberOfAccessRules() { 236 return getAccessRules().size(); 237 } 239 243 public Collection getAccessRuleObjects() { 244 final Collection rules = getAccessRules(); 245 ArrayList objects = new ArrayList (rules.size()); 246 Iterator i = rules.iterator(); 247 while (i.hasNext()) { 248 AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next(); 249 objects.add(ar.getAccessRuleObject()); 250 } 251 return objects; 252 } 253 254 258 public void addAdminEntities(Collection adminentities) { 259 Iterator iter = adminentities.iterator(); 260 while (iter.hasNext()) { 261 AdminEntity adminentity = (AdminEntity) iter.next(); 262 try { 263 AdminEntityDataLocal data = createAdminEntity(adminentity); 264 AdminEntityPK datapk = createAdminEntityPK(getAdminGroupName(), getCaId(), adminentity.getMatchWith(), adminentity.getMatchType(), adminentity.getMatchValue()); 265 266 Iterator i = getAdminEntities().iterator(); 267 while (i.hasNext()) { 268 AdminEntityDataLocal ue = (AdminEntityDataLocal) i.next(); 269 AdminEntityPK uepk = createAdminEntityPK(getAdminGroupName(), getCaId(), ue.getMatchWith() 271 , ue.getMatchType(), ue.getMatchValue()); 272 if (uepk.equals(datapk)) { 273 getAdminEntities().remove(ue); 274 try { 275 ue.remove(); 276 } catch (RemoveException e) { 277 error("Error adding AdminEntities: ", e); 278 throw new EJBException (e); 279 } 280 break; 281 } 282 } 283 getAdminEntities().add(data); 284 } catch (Exception e) { 285 error("Error adding AdminEntities: ", e); 286 } 287 } 288 } 290 291 295 public void removeAdminEntities(Collection adminentities) { 296 Iterator iter = adminentities.iterator(); 297 298 while (iter.hasNext()) { 299 AdminEntity adminentity = (AdminEntity) iter.next(); 300 AdminEntityPK datapk = createAdminEntityPK(getAdminGroupName(), getCaId(), adminentity.getMatchWith(), adminentity.getMatchType(), adminentity.getMatchValue()); 301 302 Iterator i = getAdminEntities().iterator(); 303 while (i.hasNext()) { 304 AdminEntityDataLocal ue = (AdminEntityDataLocal) i.next(); 305 AdminEntityPK uepk = createAdminEntityPK(getAdminGroupName(), getCaId(), ue.getMatchWith(), ue.getMatchType(), ue.getMatchValue()); 307 if (uepk.equals(datapk)) { 308 getAdminEntities().remove(ue); 309 try { 310 ue.remove(); 311 } catch (RemoveException e) { 312 error("Error removing AdminEntities: ", e); 313 throw new EJBException (e); 314 } 315 break; 316 } 317 } 318 } 319 } 321 private AdminEntityPK createAdminEntityPK(String name, int id, int with, int type, String value){ 323 AdminEntityPK pk = new AdminEntityPK(name, id, with, type, value); 324 return pk; 325 } 326 327 328 334 public int getNumberOfAdminEntities() { 335 return getAdminEntities().size(); 336 } 338 342 public Collection getAdminEntityObjects() { 343 ArrayList returnval = new ArrayList (); 344 Iterator i = getAdminEntities().iterator(); 345 while (i.hasNext()) { 346 AdminEntityDataLocal ae = (AdminEntityDataLocal) i.next(); 347 returnval.add(ae.getAdminEntity(getCaId())); 348 } 349 return returnval; 350 } 352 356 public AdminGroup getAdminGroup() { 357 ArrayList accessrules = new ArrayList (); 358 ArrayList adminentities = new ArrayList (); 359 360 Iterator i = null; 361 i = getAdminEntities().iterator(); 362 while (i.hasNext()) { 363 AdminEntityDataLocal ae = (AdminEntityDataLocal) i.next(); 364 adminentities.add(ae.getAdminEntity(getCaId())); 365 } 366 367 i = getAccessRules().iterator(); 368 while (i.hasNext()) { 369 AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next(); 370 accessrules.add(ar.getAccessRuleObject()); 371 } 372 373 return new AdminGroup(getPrimKey().intValue(), getAdminGroupName(), getCaId(), accessrules, adminentities); 374 } 376 380 public AdminGroup getAdminGroupNames() { 381 return new AdminGroup(getPrimKey().intValue(), getAdminGroupName(), getCaId(), null, null); 382 } 387 393 public Integer ejbCreate(Integer pk, String admingroupname, int caid) throws CreateException { 394 setPrimKey(pk); 395 setAdminGroupName(admingroupname); 396 setCaId(caid); 397 debug("Created admingroup : " + admingroupname); 398 return pk; 399 } 400 401 public void ejbPostCreate(Integer pk, String admingroupname, int caid) { 402 } 404 405 private AdminEntityDataLocal createAdminEntity(AdminEntity adminentity) throws CreateException { 407 AdminEntityDataLocalHome home = (AdminEntityDataLocalHome) ServiceLocator.getInstance().getLocalHome(AdminEntityDataLocalHome.COMP_NAME); 408 AdminEntityDataLocal returnval = home.create(getAdminGroupName(), getCaId(), adminentity.getMatchWith(), 409 adminentity.getMatchType(), adminentity.getMatchValue()); 410 return returnval; 411 } 412 413 private AccessRulesDataLocal createAccessRule(AccessRule accessrule) throws CreateException { 414 AccessRulesDataLocalHome home = (AccessRulesDataLocalHome) ServiceLocator.getInstance().getLocalHome(AccessRulesDataLocalHome.COMP_NAME); 415 AccessRulesDataLocal returnval = home.create(getAdminGroupName(), getCaId(), accessrule.getAccessRule(), 416 accessrule.getRule(), accessrule.isRecursive()); 417 return returnval; 418 } 419 } 420 | Popular Tags |