1 package org.apache.turbine.services.security.torque; 2 3 18 19 import java.beans.PropertyDescriptor ; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.Enumeration ; 24 import java.util.List ; 25 import java.util.Vector ; 26 27 import org.apache.commons.configuration.Configuration; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import org.apache.turbine.om.security.Permission; 33 import org.apache.turbine.om.security.Role; 34 import org.apache.turbine.services.InitializationException; 35 import org.apache.turbine.services.security.TurbineSecurity; 36 import org.apache.turbine.services.security.torque.om.TurbineRolePermissionPeer; 37 import org.apache.turbine.util.security.DataBackendException; 38 import org.apache.turbine.util.security.PermissionSet; 39 40 import org.apache.torque.TorqueException; 41 import org.apache.torque.om.Persistent; 42 import org.apache.torque.util.BasePeer; 43 import org.apache.torque.util.Criteria; 44 45 54 55 public class PermissionPeerManager 56 implements PermissionPeerManagerConstants 57 { 58 59 private static Class persistentPeerClass = null; 60 61 62 private static Class permissionObject = null; 63 64 65 private static String tableName = null; 66 67 68 private static String nameColumn = null; 69 70 71 private static String idColumn = null; 72 73 74 private static PropertyDescriptor namePropDesc = null; 75 76 77 private static PropertyDescriptor idPropDesc = null; 78 79 80 static Log log = LogFactory.getLog(PermissionPeerManager.class); 81 82 90 91 public static void init(Configuration conf) 92 throws InitializationException 93 { 94 String persistentPeerClassName = 95 conf.getString(PERMISSION_PEER_CLASS_KEY, 96 PERMISSION_PEER_CLASS_DEFAULT); 97 98 String permissionObjectName = null; 99 100 try 101 { 102 persistentPeerClass = Class.forName(persistentPeerClassName); 103 104 tableName = 105 (String ) persistentPeerClass.getField("TABLE_NAME").get(null); 106 107 113 permissionObject = getPersistenceClass(); 114 115 permissionObjectName = conf.getString(PERMISSION_CLASS_KEY, 116 permissionObject.getName()); 117 118 permissionObject = Class.forName(permissionObjectName); 120 121 125 126 nameColumn = (String ) persistentPeerClass.getField( 127 conf.getString(PERMISSION_NAME_COLUMN_KEY, 128 PERMISSION_NAME_COLUMN_DEFAULT) 129 ).get(null); 130 131 idColumn = (String ) persistentPeerClass.getField( 132 conf.getString(PERMISSION_ID_COLUMN_KEY, 133 PERMISSION_ID_COLUMN_DEFAULT) 134 ).get(null); 135 136 namePropDesc = new PropertyDescriptor ( 137 conf.getString(PERMISSION_NAME_PROPERTY_KEY, 138 PERMISSION_NAME_PROPERTY_DEFAULT), 139 permissionObject); 140 141 idPropDesc = new PropertyDescriptor ( 142 conf.getString(PERMISSION_ID_PROPERTY_KEY, 143 PERMISSION_ID_PROPERTY_DEFAULT), 144 permissionObject); 145 } 146 catch (Exception e) 147 { 148 if (persistentPeerClassName == null || persistentPeerClass == null) 149 { 150 throw new InitializationException( 151 "Could not find PermissionPeer class (" 152 + persistentPeerClassName + ")", e); 153 } 154 if (tableName == null) 155 { 156 throw new InitializationException( 157 "Failed to get the table name from the Peer object", e); 158 } 159 160 if (permissionObject == null || permissionObjectName == null) 161 { 162 throw new InitializationException( 163 "Failed to get the object type from the Peer object", e); 164 } 165 166 167 if (nameColumn == null || namePropDesc == null) 168 { 169 throw new InitializationException( 170 "PermissionPeer " + persistentPeerClassName + 171 " has no name column information!", e); 172 } 173 if (idColumn == null || idPropDesc == null) 174 { 175 throw new InitializationException( 176 "PermissionPeer " + persistentPeerClassName + 177 " has no id column information!", e); 178 } 179 } 180 } 181 182 187 public static String getTableName() 188 { 189 return tableName; 190 } 191 192 198 public static String getNameColumn() 199 { 200 return nameColumn; 201 } 202 203 210 public static String getIdColumn() 211 { 212 return idColumn; 213 } 214 215 222 public static String getColumnName(String name) 223 { 224 StringBuffer sb = new StringBuffer (); 225 sb.append(getTableName()); 226 sb.append("."); 227 sb.append(name); 228 return sb.toString(); 229 } 230 231 239 240 public static Persistent newPersistentInstance() 241 { 242 Persistent obj = null; 243 244 if(permissionObject == null) 245 { 246 return obj; 253 } 254 255 try 256 { 257 obj = (Persistent) permissionObject.newInstance(); 258 } 259 catch (Exception e) 260 { 261 log.error("Could not instantiate a permission object", e); 262 obj = null; 263 } 264 return obj; 265 } 266 267 277 public static boolean checkExists(Permission permission) 278 throws DataBackendException, Exception 279 { 280 Criteria criteria = new Criteria(); 281 282 criteria.addSelectColumn(getIdColumn()); 283 284 criteria.add(getNameColumn(), permission.getName()); 285 286 List results = BasePeer.doSelect(criteria); 287 288 if (results.size() > 1) 289 { 290 throw new DataBackendException("Multiple permissions named '" + 291 permission.getName() + "' exist!"); 292 } 293 return (results.size() == 1); 294 } 295 296 303 public static PermissionSet retrieveSet(Criteria criteria) 304 throws Exception 305 { 306 List results = doSelect(criteria); 307 PermissionSet ps = new PermissionSet(); 308 309 for(Iterator it = results.iterator(); it.hasNext(); ) 310 { 311 ps.add((Permission) it.next()); 312 } 313 return ps; 314 } 315 316 323 public static PermissionSet retrieveSet(Role role) 324 throws Exception 325 { 326 Criteria criteria = new Criteria(); 327 criteria.add(TurbineRolePermissionPeer.ROLE_ID, 328 ((Persistent) role).getPrimaryKey()); 329 330 criteria.addJoin(TurbineRolePermissionPeer.PERMISSION_ID, 331 getIdColumn()); 332 333 return retrieveSet(criteria); 334 } 335 336 344 public static final Vector getDifference(Vector some, Vector all) 345 { 346 Vector clone = (Vector ) all.clone(); 347 for (Enumeration e = some.elements() ; e.hasMoreElements() ;) 348 { 349 Permission tmp = (Permission) e.nextElement(); 350 for (Enumeration f = clone.elements() ; f.hasMoreElements() ;) 351 { 352 Permission tmp2 = (Permission) f.nextElement(); 353 if (((Persistent) tmp).getPrimaryKey() == 354 ((Persistent) tmp2).getPrimaryKey()) 355 { 356 clone.removeElement(tmp2); 357 break; 358 } 359 } 360 } 361 return clone; 362 } 363 364 374 375 389 390 public static Criteria buildCriteria(Permission permission) 391 { 392 Criteria crit; 393 394 try 395 { 396 Class [] clazz = new Class [] { permissionObject }; 397 Object [] params = 398 new Object [] { ((TorquePermission) permission).getPersistentObj() }; 399 400 crit = (Criteria) persistentPeerClass 401 .getMethod("buildCriteria", clazz) 402 .invoke(null, params); 403 } 404 catch (Exception e) 405 { 406 crit = null; 407 } 408 409 return crit; 410 } 411 412 419 420 public static void doUpdate(Criteria criteria) 421 throws TorqueException 422 { 423 try 424 { 425 Class [] clazz = new Class [] { Criteria.class }; 426 Object [] params = new Object [] { criteria }; 427 428 persistentPeerClass 429 .getMethod("doUpdate", clazz) 430 .invoke(null, params); 431 } 432 catch (Exception e) 433 { 434 throw new TorqueException("doUpdate failed", e); 435 } 436 } 437 438 445 446 public static void doInsert(Criteria criteria) 447 throws TorqueException 448 { 449 try 450 { 451 Class [] clazz = new Class [] { Criteria.class }; 452 Object [] params = new Object [] { criteria }; 453 454 persistentPeerClass 455 .getMethod("doInsert", clazz) 456 .invoke(null, params); 457 } 458 catch (Exception e) 459 { 460 throw new TorqueException("doInsert failed", e); 461 } 462 } 463 464 473 public static List doSelect(Criteria criteria) 474 throws TorqueException 475 { 476 List list; 477 478 try 479 { 480 Class [] clazz = 481 new Class [] { Criteria.class }; 482 Object [] params = new Object [] { criteria }; 483 484 list = (List ) persistentPeerClass 485 .getMethod("doSelect", clazz) 486 .invoke(null, params); 487 } 488 catch (Exception e) 489 { 490 throw new TorqueException("doSelect failed", e); 491 } 492 493 List newList = new ArrayList (list.size()); 494 495 for (Iterator it = list.iterator(); it.hasNext(); ) 499 { 500 Permission p = getNewPermission((Persistent) it.next()); 501 newList.add(p); 502 } 503 504 return newList; 505 } 506 507 514 public static void doDelete(Criteria criteria) 515 throws TorqueException 516 { 517 try 518 { 519 Class [] clazz = new Class [] { Criteria.class }; 520 Object [] params = new Object [] { criteria }; 521 522 persistentPeerClass 523 .getMethod("doDelete", clazz) 524 .invoke(null, params); 525 } 526 catch (Exception e) 527 { 528 throw new TorqueException("doDelete failed", e); 529 } 530 } 531 532 538 public static void setPermissionName(Persistent obj, String name) 539 { 540 if(obj == null) 541 { 542 return; 543 } 544 545 try 546 { 547 Object [] params = new Object [] { name }; 548 namePropDesc.getWriteMethod().invoke(obj, params); 549 } 550 catch (ClassCastException cce) 551 { 552 String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; 553 log.error(msg); 554 throw new RuntimeException (msg); 555 } 556 catch (Exception e) 557 { 558 log.error(e, e); 559 } 560 } 561 562 569 public static String getPermissionName(Persistent obj) 570 { 571 String name = null; 572 573 if(obj == null) 574 { 575 return null; 576 } 577 578 try 579 { 580 name = (String ) namePropDesc 581 .getReadMethod() 582 .invoke(obj, new Object [] {}); 583 } 584 catch (ClassCastException cce) 585 { 586 String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; 587 log.error(msg); 588 throw new RuntimeException (msg); 589 } 590 catch (Exception e) 591 { 592 log.error(e, e); 593 } 594 return name; 595 } 596 597 603 public static void setId(Persistent obj, int id) 604 { 605 if(obj == null) 606 { 607 return; 608 } 609 610 try 611 { 612 Object [] params = new Object [] { Integer.TYPE }; 613 idPropDesc.getWriteMethod().invoke(obj, params); 614 } 615 catch (ClassCastException cce) 616 { 617 String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; 618 log.error(msg); 619 throw new RuntimeException (msg); 620 } 621 catch (Exception e) 622 { 623 log.error(e, e); 624 } 625 } 626 627 634 public static Integer getIdAsObj(Persistent obj) 635 { 636 Integer id = null; 637 638 if(obj == null) 639 { 640 return new Integer (0); 641 } 642 643 try 644 { 645 id = (Integer ) idPropDesc 646 .getReadMethod() 647 .invoke(obj, new Object [] {}); 648 } 649 catch (ClassCastException cce) 650 { 651 String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; 652 log.error(msg); 653 throw new RuntimeException (msg); 654 } 655 catch (Exception e) 656 { 657 log.error(e, e); 658 } 659 return id; 660 } 661 662 669 670 private static Class getPersistenceClass() 671 { 672 Class persistenceClass = null; 673 674 try 675 { 676 Object [] params = new Object [0]; 677 678 persistenceClass = (Class ) persistentPeerClass 679 .getMethod("getOMClass", null) 680 .invoke(null, params); 681 } 682 catch (Exception e) 683 { 684 persistenceClass = null; 685 } 686 687 return persistenceClass; 688 } 689 690 701 702 public static Permission getNewPermission(Persistent p) 703 { 704 Permission perm = null; 705 try 706 { 707 Class permissionWrapperClass = TurbineSecurity.getPermissionClass(); 708 709 Class [] clazz = new Class [] { Persistent.class }; 710 Object [] params = new Object [] { p }; 711 712 perm = (Permission) permissionWrapperClass 713 .getConstructor(clazz) 714 .newInstance(params); 715 } 716 catch (Exception e) 717 { 718 log.error("Could not instantiate a new permission from supplied persistent: ", e); 719 } 720 721 return perm; 722 } 723 } 724 725 | Popular Tags |