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.List ; 24 25 import org.apache.commons.configuration.Configuration; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.apache.turbine.om.security.Group; 31 import org.apache.turbine.om.security.Role; 32 import org.apache.turbine.om.security.User; 33 import org.apache.turbine.services.InitializationException; 34 import org.apache.turbine.services.security.TurbineSecurity; 35 import org.apache.turbine.services.security.torque.om.TurbineUserGroupRolePeer; 36 import org.apache.turbine.util.security.DataBackendException; 37 import org.apache.turbine.util.security.RoleSet; 38 39 import org.apache.torque.TorqueException; 40 import org.apache.torque.om.Persistent; 41 import org.apache.torque.util.BasePeer; 42 import org.apache.torque.util.Criteria; 43 44 52 53 public class RolePeerManager 54 implements RolePeerManagerConstants 55 { 56 57 private static Class rolePeerClass = null; 58 59 60 private static Class roleObject = null; 61 62 63 private static String tableName = null; 64 65 66 private static String nameColumn = null; 67 68 69 private static String idColumn = null; 70 71 72 private static PropertyDescriptor namePropDesc = null; 73 74 75 private static PropertyDescriptor idPropDesc = null; 76 77 78 static Log log = LogFactory.getLog(RolePeerManager.class); 79 80 89 90 public static void init(Configuration conf) 91 throws InitializationException 92 { 93 String rolePeerClassName = conf.getString(ROLE_PEER_CLASS_KEY, 94 ROLE_PEER_CLASS_DEFAULT); 95 96 String roleObjectName = null; 97 98 try 99 { 100 rolePeerClass = Class.forName(rolePeerClassName); 101 102 tableName = 103 (String ) rolePeerClass.getField("TABLE_NAME").get(null); 104 105 110 112 roleObject = getPersistenceClass(); 113 114 roleObjectName = conf.getString(ROLE_CLASS_KEY, 115 roleObject.getName()); 116 117 roleObject = Class.forName(roleObjectName); 119 120 124 125 nameColumn = (String ) rolePeerClass.getField( 126 conf.getString(ROLE_NAME_COLUMN_KEY, 127 ROLE_NAME_COLUMN_DEFAULT) 128 ).get(null); 129 130 idColumn = (String ) rolePeerClass.getField( 131 conf.getString(ROLE_ID_COLUMN_KEY, 132 ROLE_ID_COLUMN_DEFAULT) 133 ).get(null); 134 135 namePropDesc = new PropertyDescriptor ( 136 conf.getString(ROLE_NAME_PROPERTY_KEY, 137 ROLE_NAME_PROPERTY_DEFAULT), 138 roleObject); 139 140 idPropDesc = new PropertyDescriptor ( 141 conf.getString(ROLE_ID_PROPERTY_KEY, 142 ROLE_ID_PROPERTY_DEFAULT), 143 roleObject); 144 145 } 146 catch (Exception e) 147 { 148 if (rolePeerClassName == null || rolePeerClass == null) 149 { 150 throw new InitializationException( 151 "Could not find RolePeer class (" 152 + rolePeerClassName + ")", 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 (roleObject == null || roleObjectName == 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 "RolePeer " + rolePeerClassName 171 + " has no name column information!", e); 172 } 173 if (idColumn == null || idPropDesc == null) 174 { 175 throw new InitializationException( 176 "RolePeer " + rolePeerClassName 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 209 public static String getIdColumn() 210 { 211 return idColumn; 212 } 213 214 221 public static String getColumnName(String name) 222 { 223 StringBuffer sb = new StringBuffer (); 224 sb.append(getTableName()); 225 sb.append("."); 226 sb.append(name); 227 return sb.toString(); 228 } 229 230 238 239 public static Persistent newPersistentInstance() 240 { 241 Persistent obj = null; 242 243 if(roleObject == null) 244 { 245 return obj; 252 } 253 254 try 255 { 256 obj = (Persistent) roleObject.newInstance(); 257 } 258 catch (Exception e) 259 { 260 log.error("Could not instantiate a role object", e); 261 obj = null; 262 } 263 return obj; 264 } 265 266 277 public static RoleSet retrieveSet(Criteria criteria) 278 throws Exception 279 { 280 List results = doSelect(criteria); 281 RoleSet rs = new RoleSet(); 282 283 for(Iterator it = results.iterator(); it.hasNext(); ) 284 { 285 rs.add((Role) it.next()); 286 } 287 return rs; 288 } 289 290 300 public static RoleSet retrieveSet(User user, Group group) 301 throws Exception 302 { 303 Criteria criteria = new Criteria(); 304 305 criteria.add(UserPeerManager.getNameColumn(), 306 user.getName()); 307 308 criteria.add(TurbineUserGroupRolePeer.GROUP_ID, 309 ((Persistent) group).getPrimaryKey()); 310 311 criteria.addJoin(UserPeerManager.getIdColumn(), 312 TurbineUserGroupRolePeer.USER_ID); 313 314 criteria.addJoin(TurbineUserGroupRolePeer.ROLE_ID, getIdColumn()); 315 316 return retrieveSet(criteria); 317 } 318 319 329 public static boolean checkExists(Role role) 330 throws DataBackendException, Exception 331 { 332 Criteria criteria = new Criteria(); 333 334 criteria.addSelectColumn(getIdColumn()); 335 336 criteria.add(getNameColumn(), role.getName()); 337 338 List results = BasePeer.doSelect(criteria); 339 340 if (results.size() > 1) 341 { 342 throw new DataBackendException("Multiple roles named '" + 343 role.getName() + "' exist!"); 344 } 345 return (results.size() == 1); 346 } 347 348 358 359 369 370 public static Criteria buildCriteria(Role role) 371 { 372 Criteria crit; 373 374 try 375 { 376 Class [] clazz = new Class [] { roleObject }; 377 Object [] params = 378 new Object [] { ((TorqueRole) role).getPersistentObj() }; 379 380 crit = (Criteria) rolePeerClass 381 .getMethod("buildCriteria", clazz) 382 .invoke(null, params); 383 } 384 catch (Exception e) 385 { 386 crit = null; 387 } 388 389 return crit; 390 } 391 392 399 400 public static void doUpdate(Criteria criteria) 401 throws TorqueException 402 { 403 try 404 { 405 Class [] clazz = new Class [] { Criteria.class }; 406 Object [] params = new Object [] { criteria }; 407 408 rolePeerClass 409 .getMethod("doUpdate", clazz) 410 .invoke(null, params); 411 } 412 catch (Exception e) 413 { 414 throw new TorqueException("doUpdate failed", e); 415 } 416 } 417 418 425 426 public static void doInsert(Criteria criteria) 427 throws TorqueException 428 { 429 try 430 { 431 Class [] clazz = new Class [] { Criteria.class }; 432 Object [] params = new Object [] { criteria }; 433 434 rolePeerClass 435 .getMethod("doInsert", clazz) 436 .invoke(null, params); 437 } 438 catch (Exception e) 439 { 440 throw new TorqueException("doInsert failed", e); 441 } 442 } 443 444 453 public static List doSelect(Criteria criteria) 454 throws TorqueException 455 { 456 List list; 457 458 try 459 { 460 Class [] clazz = 461 new Class [] { Criteria.class }; 462 Object [] params = new Object [] { criteria }; 463 464 list = (List ) rolePeerClass 465 .getMethod("doSelect", clazz) 466 .invoke(null, params); 467 } 468 catch (Exception e) 469 { 470 throw new TorqueException("doSelect failed", e); 471 } 472 List newList = new ArrayList (list.size()); 473 474 for (Iterator it = list.iterator(); it.hasNext(); ) 478 { 479 Role r = getNewRole((Persistent) it.next()); 480 newList.add(r); 481 } 482 483 return newList; 484 } 485 486 493 public static void doDelete(Criteria criteria) 494 throws TorqueException 495 { 496 try 497 { 498 Class [] clazz = new Class [] { Criteria.class }; 499 Object [] params = new Object [] { criteria }; 500 501 rolePeerClass 502 .getMethod("doDelete", clazz) 503 .invoke(null, params); 504 } 505 catch (Exception e) 506 { 507 throw new TorqueException("doDelete failed", e); 508 } 509 } 510 511 518 public static void setRoleName(Persistent obj, String name) 519 { 520 if(obj == null) 521 { 522 return; 523 } 524 525 try 526 { 527 Object [] params = new Object [] { name }; 528 namePropDesc.getWriteMethod().invoke(obj, params); 529 } 530 catch (ClassCastException cce) 531 { 532 String msg = obj.getClass().getName() + " does not seem to be a Role Object!"; 533 log.error(msg); 534 throw new RuntimeException (msg); 535 } 536 catch (Exception e) 537 { 538 log.error(e, e); 539 } 540 } 541 542 549 public static String getRoleName(Persistent obj) 550 { 551 String name = null; 552 553 if(obj == null) 554 { 555 return null; 556 } 557 558 try 559 { 560 name = (String ) namePropDesc 561 .getReadMethod() 562 .invoke(obj, new Object [] {}); 563 } 564 catch (ClassCastException cce) 565 { 566 String msg = obj.getClass().getName() + " does not seem to be a Role Object!"; 567 log.error(msg); 568 throw new RuntimeException (msg); 569 } 570 catch (Exception e) 571 { 572 log.error(e, e); 573 } 574 return name; 575 } 576 577 583 public static void setId(Persistent obj, int id) 584 { 585 if(obj == null) 586 { 587 return; 588 } 589 590 try 591 { 592 Object [] params = new Object [] { Integer.TYPE }; 593 idPropDesc.getWriteMethod().invoke(obj, params); 594 } 595 catch (ClassCastException cce) 596 { 597 String msg = obj.getClass().getName() + " does not seem to be a Role Object!"; 598 log.error(msg); 599 throw new RuntimeException (msg); 600 } 601 catch (Exception e) 602 { 603 log.error(e, e); 604 } 605 } 606 607 614 public static Integer getIdAsObj(Persistent obj) 615 { 616 Integer id = null; 617 618 if(obj == null) 619 { 620 return new Integer (0); 621 } 622 623 try 624 { 625 id = (Integer ) idPropDesc 626 .getReadMethod() 627 .invoke(obj, new Object [] {}); 628 } 629 catch (ClassCastException cce) 630 { 631 String msg = obj.getClass().getName() + " does not seem to be a Role Object!"; 632 log.error(msg); 633 throw new RuntimeException (msg); 634 } 635 catch (Exception e) 636 { 637 log.error(e, e); 638 } 639 return id; 640 } 641 642 648 649 private static Class getPersistenceClass() 650 { 651 Class persistenceClass = null; 652 653 try 654 { 655 Object [] params = new Object [0]; 656 657 persistenceClass = (Class ) rolePeerClass 658 .getMethod("getOMClass", null) 659 .invoke(null, params); 660 } 661 catch (Exception e) 662 { 663 persistenceClass = null; 664 } 665 666 return persistenceClass; 667 } 668 669 670 681 682 public static Role getNewRole(Persistent p) 683 { 684 Role r = null; 685 try 686 { 687 Class roleWrapperClass = TurbineSecurity.getRoleClass(); 688 689 Class [] clazz = new Class [] { Persistent.class }; 690 Object [] params = new Object [] { p }; 691 692 r = (Role) roleWrapperClass 693 .getConstructor(clazz) 694 .newInstance(params); 695 } 696 catch (Exception e) 697 { 698 log.error("Could not instantiate a new role from supplied persistent: ", e); 699 } 700 701 return r; 702 } 703 } 704 705 | Popular Tags |