1 17 18 package org.sape.carbon.services.security.management.rdbms; 19 20 import java.security.Principal ; 21 import java.security.acl.Group ; 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import org.sape.carbon.core.component.ComponentConfiguration; 33 import org.sape.carbon.core.component.lifecycle.Configurable; 34 import org.sape.carbon.core.config.InvalidConfigurationException; 35 import org.sape.carbon.core.exception.ExceptionUtility; 36 import org.sape.carbon.services.security.management.DefaultGroupImpl; 37 import org.sape.carbon.services.security.management.DefaultUserImpl; 38 import org.sape.carbon.services.security.management.DuplicateGroupException; 39 import org.sape.carbon.services.security.management.DuplicatePrincipalException; 40 import org.sape.carbon.services.security.management.LazyLoadGroup; 41 import org.sape.carbon.services.security.management.RuntimeSecurityManagementException; 42 import org.sape.carbon.services.security.management.UnknownGroupException; 43 import org.sape.carbon.services.security.management.UnknownPrincipalException; 44 import org.sape.carbon.services.security.management.UserManager; 45 import org.sape.carbon.services.sql.StatementFactoryException; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 61 public class RdbmsUserManagerImpl implements UserManager, Configurable { 62 63 66 private Log log = LogFactory.getLog(this.getClass()); 67 68 69 protected RdbmsUserManagerConfiguration config; 70 71 84 public Principal createUser(String username, Map userInfo) 85 throws DuplicatePrincipalException { 86 87 Principal resultUser = null; 88 Connection connection = null; 89 90 try { 91 connection = config.getConnectionFactory().getConnection(); 92 } catch (SQLException sqle) { 93 log.trace("Caught exception: " + sqle 94 + ExceptionUtility.captureStackTrace(sqle)); 95 96 throw new RuntimeSecurityManagementException( 97 this.getClass(), "Caught SQLException while opening connection", 98 sqle); 99 } 100 101 try { 102 resultUser = createUser(connection, username, userInfo); 103 } finally { 104 try { 105 if (connection != null) { 106 connection.close(); 107 } 108 } catch (SQLException sqle) { 109 log.info("Caught exception closing Connection: " + sqle 110 + ExceptionUtility.captureStackTrace(sqle)); 111 } 112 } 113 114 return resultUser; 115 } 116 117 139 protected Principal createUser( 140 Connection connection, String username, Map userInfo) 141 throws DuplicatePrincipalException { 142 143 if (log.isTraceEnabled()) { 144 log.trace("Adding user with name [" + username 145 + "] and additional info [" 146 + userInfo + "]"); 147 } 148 149 Principal currentUser = retreiveUser(connection, username); 150 151 if (currentUser != null) { 152 throw new DuplicatePrincipalException( 153 this.getClass(), currentUser); 154 } 155 156 try { 157 connection.setAutoCommit(false); 158 createUserEntry(connection, username); 159 currentUser = retreiveUser(connection, username); 160 createCredentialEntry(connection, currentUser, userInfo); 161 connection.commit(); 162 connection.setAutoCommit(true); 163 } catch (RuntimeSecurityManagementException rsme) { 164 try { 165 if (connection != null) { 166 connection.rollback(); 167 connection.setAutoCommit(true); 168 } 169 } catch (SQLException se) { 170 } 172 173 throw rsme; 174 } catch (SQLException se) { 175 try { 176 if (connection != null) { 177 connection.rollback(); 178 connection.setAutoCommit(true); 179 } 180 } catch (SQLException se2) { 181 } 183 184 throw new RuntimeSecurityManagementException( 185 this.getClass(), 186 "Caught SQLException while adding " + "new user named [" 187 + username + "]", se); 188 } 189 190 return currentUser; 191 } 192 193 205 protected void createUserEntry( 206 Connection connection, String username) { 207 PreparedStatement createUserStatement = null; 208 209 try { 210 createUserStatement = 211 config.getStatementFactory().createPreparedStatement( 212 config.getCreateUserQueryName(), connection); 213 createUserStatement.setString(1, username); 214 215 int resultCount = createUserStatement.executeUpdate(); 216 217 if (log.isInfoEnabled()) { 218 log.info("Added user with name [" + username + "]"); 219 } 220 } catch (StatementFactoryException sfe) { 221 log.trace("Caught exception: " + sfe 222 + ExceptionUtility.captureStackTrace(sfe)); 223 224 throw new RuntimeSecurityManagementException( 225 this.getClass(), 226 "Caught StatementFactoryException while adding " 227 + "new user.", sfe); 228 } catch (SQLException sqle) { 229 log.trace("Caught exception: " + sqle 230 + ExceptionUtility.captureStackTrace(sqle)); 231 232 throw new RuntimeSecurityManagementException( 233 this.getClass(), "Caught SQLException while adding " 234 + "new user.", 235 sqle); 236 } finally { 237 try { 238 if (createUserStatement != null) { 239 createUserStatement.close(); 240 } 241 } catch (SQLException sqle) { 242 } 244 } 245 } 246 247 259 protected void createCredentialEntry( 260 Connection connection, Principal currentUser, Map userInfo) { 261 PreparedStatement createCredentialStatement = null; 262 Object userPrimaryKey = null; 263 264 try { 265 userPrimaryKey = 266 retreivePrincipalPrimaryKey(connection, currentUser); 267 } catch (UnknownPrincipalException upe) { 268 throw new RuntimeSecurityManagementException( 269 this.getClass(), 270 "Caught UnknownPrincipalException while getting " 271 + "primary key for [" + currentUser.getName() + "]", upe); 272 } 273 274 try { 275 Object credential = 276 userInfo.get(this.config.getCreateUserBindParameters()[0]); 277 278 createCredentialStatement = 279 config.getStatementFactory().createPreparedStatement( 280 config.getCreateCredentialQueryName(), connection); 281 282 createCredentialStatement.setObject(1, userPrimaryKey); 283 createCredentialStatement.setObject(2, credential); 284 285 int resultCount = createCredentialStatement.executeUpdate(); 286 287 if (log.isInfoEnabled()) { 288 log.info("Added credential to user with name [" 289 + currentUser.getName() + "] and credential of type [" 290 + credential.getClass().getName() + "]"); 291 } 292 } catch (StatementFactoryException sfe) { 293 log.trace("Caught exception: " + sfe 294 + ExceptionUtility.captureStackTrace(sfe)); 295 296 throw new RuntimeSecurityManagementException( 297 this.getClass(), 298 "Caught StatementFactoryException while adding " 299 + "new user.", sfe); 300 } catch (SQLException sqle) { 301 log.trace("Caught exception: " + sqle 302 + ExceptionUtility.captureStackTrace(sqle)); 303 304 throw new RuntimeSecurityManagementException( 305 this.getClass(), "Caught SQLException while adding " 306 + "new user.", 307 sqle); 308 } finally { 309 try { 310 if (createCredentialStatement != null) { 311 createCredentialStatement.close(); 312 } 313 } catch (SQLException sqle) { 314 } 316 } 317 } 318 319 328 protected void removeCredential( 329 Connection connection, Object userPrimaryKey) { 330 PreparedStatement removeCredentialStatement = null; 331 332 try { 333 removeCredentialStatement = 334 config.getStatementFactory().createPreparedStatement( 335 config.getRemoveCredentialQueryName(), connection); 336 337 removeCredentialStatement.setObject(1, userPrimaryKey); 338 339 int resultCount = removeCredentialStatement.executeUpdate(); 340 341 if (log.isInfoEnabled()) { 342 log.info("Deleted credential to user with name [" 343 + userPrimaryKey + "]"); 344 } 345 } catch (StatementFactoryException sfe) { 346 log.trace("Caught exception: " + sfe 347 + ExceptionUtility.captureStackTrace(sfe)); 348 349 throw new RuntimeSecurityManagementException( 350 this.getClass(), 351 "Caught StatementFactoryException while deleting " 352 + "credential.", sfe); 353 } catch (SQLException sqle) { 354 log.trace("Caught exception: " + sqle 355 + ExceptionUtility.captureStackTrace(sqle)); 356 357 throw new RuntimeSecurityManagementException( 358 this.getClass(), "Caught SQLException while deleting " 359 + "credential.", 360 sqle); 361 } finally { 362 try { 363 if (removeCredentialStatement != null) { 364 removeCredentialStatement.close(); 365 } 366 } catch (SQLException sqle) { 367 } 369 } 370 } 371 372 398 public boolean authenticate(String username, Object credential) { 399 Connection connection = null; 400 boolean authenticated = false; 401 402 try { 403 connection = config.getConnectionFactory().getConnection(); 404 } catch (SQLException sqle) { 405 log.trace("Caught exception: " + sqle 406 + ExceptionUtility.captureStackTrace(sqle)); 407 408 throw new RuntimeSecurityManagementException( 409 this.getClass(), "Caught SQLException while opening " 410 + "connection", 411 sqle); 412 } 413 414 try { 415 authenticated = authenticate(connection, username, credential); 416 } finally { 417 try { 418 if (connection != null) { 419 connection.close(); 420 } 421 } catch (SQLException sqle) { 422 log.info("Caught exception closing Connection: " + sqle 423 + ExceptionUtility.captureStackTrace(sqle)); 424 } 425 } 426 427 return authenticated; 428 } 429 430 457 public boolean authenticate( 458 Connection connection, String username, Object credential) { 459 PreparedStatement authenticateUserStatement = null; 460 ResultSet authenticateUserResultSet = null; 461 boolean authenticated = false; 462 463 if (log.isTraceEnabled()) { 464 log.trace("Searching for user with principal name [" + username 465 + "]"); 466 } 467 468 try { 469 authenticateUserStatement = 470 config.getStatementFactory().createPreparedStatement( 471 config.getAuthenticateUserQueryName(), connection); 472 authenticateUserStatement.setString(1, username); 473 authenticateUserStatement.setObject(2, credential); 474 authenticateUserResultSet = 475 authenticateUserStatement.executeQuery(); 476 477 if (authenticateUserResultSet.next()) { 478 authenticated = true; 479 480 if (log.isTraceEnabled()) { 481 log.trace("User with principal name [" + username 482 + "] was successfully authenticated"); 483 } 484 } else { 485 authenticated = false; 486 487 if (log.isTraceEnabled()) { 488 log.trace("User with principal name [" + username 489 + "] failed authentication"); 490 } 491 } 492 } catch (StatementFactoryException sfe) { 493 log.trace("Caught exception: " + sfe 494 + ExceptionUtility.captureStackTrace(sfe)); 495 496 throw new RuntimeSecurityManagementException( 497 this.getClass(), 498 "Caught StatementFactoryException while adding " 499 + "new user.", sfe); 500 } catch (SQLException sqle) { 501 log.trace("Caught exception: " + sqle 502 + ExceptionUtility.captureStackTrace(sqle)); 503 504 throw new RuntimeSecurityManagementException( 505 this.getClass(), "Caught SQLException while authenticating " 506 + "user.", 507 sqle); 508 } finally { 509 try { 510 if (authenticateUserResultSet != null) { 511 authenticateUserResultSet.close(); 512 } 513 } catch (SQLException sqle) { 514 log.info("Caught exception closing ResultSet: " + sqle 515 + ExceptionUtility.captureStackTrace(sqle)); 516 } 517 518 try { 519 if (authenticateUserStatement != null) { 520 authenticateUserStatement.close(); 521 } 522 } catch (SQLException sqle) { 523 log.info("Caught exception closing Statement: " + sqle 524 + ExceptionUtility.captureStackTrace(sqle)); 525 } 526 } 527 528 return authenticated; 529 } 530 531 548 protected void removeAllRelationships( 549 Connection connection, Object principalPrimaryKey) 550 throws UnknownPrincipalException { 551 PreparedStatement removeAllRelationshipsStatement = null; 552 553 if (log.isTraceEnabled()) { 554 log.trace("Removing principal [" + principalPrimaryKey 555 + "] from all relationships"); 556 } 557 558 try { 559 removeAllRelationshipsStatement = 560 config.getStatementFactory().createPreparedStatement( 561 config.getRemoveAllRelationshipsQueryName(), connection); 562 removeAllRelationshipsStatement.setObject( 563 1, principalPrimaryKey); 564 removeAllRelationshipsStatement.setObject( 565 2, principalPrimaryKey); 566 567 int resultCount = 568 removeAllRelationshipsStatement.executeUpdate(); 569 570 if (log.isInfoEnabled()) { 571 log.info("Removed principal with name [" + principalPrimaryKey 572 + "] from [" + resultCount + "] relationships"); 573 } 574 } catch (StatementFactoryException sfe) { 575 log.trace("Caught exception: " + sfe 576 + ExceptionUtility.captureStackTrace(sfe)); 577 578 throw new RuntimeSecurityManagementException( 579 this.getClass(), 580 "Caught StatementFactoryException while removing " 581 + "user.", sfe); 582 } catch (SQLException sqle) { 583 log.trace("Caught exception: " + sqle 584 + ExceptionUtility.captureStackTrace(sqle)); 585 586 throw new RuntimeSecurityManagementException( 587 this.getClass(), "Caught SQLException while removing " 588 + "user.", 589 sqle); 590 } finally { 591 try { 592 if (removeAllRelationshipsStatement != null) { 593 removeAllRelationshipsStatement.close(); 594 } 595 } catch (SQLException sqle) { 596 log.info("Caught exception closing Statement: " + sqle 597 + ExceptionUtility.captureStackTrace(sqle)); 598 } 599 } 600 } 601 602 619 public void removeUser(Principal user) 620 throws UnknownPrincipalException { 621 622 Connection connection = null; 623 624 try { 625 connection = config.getConnectionFactory().getConnection(); 626 } catch (SQLException sqle) { 627 log.trace("Caught exception: " + sqle 628 + ExceptionUtility.captureStackTrace(sqle)); 629 630 throw new RuntimeSecurityManagementException( 631 this.getClass(), "Caught SQLException while opening " 632 + "connection", 633 sqle); 634 } 635 636 try { 637 removeUser(connection, user); 638 } finally { 639 try { 640 if (connection != null) { 641 connection.close(); 642 } 643 } catch (SQLException sqle) { 644 log.info("Caught exception closing Connection: " + sqle 645 + ExceptionUtility.captureStackTrace(sqle)); 646 } 647 } 648 } 649 650 668 protected void removeUser(Connection connection, Principal user) 669 throws UnknownPrincipalException { 670 671 Principal currentUser = retreiveUser(connection, user.getName()); 672 673 if (currentUser == null) { 674 throw new UnknownPrincipalException( 675 this.getClass(), user.getName()); 676 } 677 678 Object userPrimaryKey = 679 retreivePrincipalPrimaryKey(connection, user); 680 681 if (log.isTraceEnabled()) { 682 log.trace("Removing user with principal [" 683 + user.getName() 684 + "]"); 685 } 686 687 try { 688 connection.setAutoCommit(false); 689 690 removeAllRelationships(connection, userPrimaryKey); 691 removeCredential(connection, userPrimaryKey); 692 removeUserEntry(connection, userPrimaryKey); 693 694 connection.commit(); 695 connection.setAutoCommit(true); 696 697 if (log.isInfoEnabled()) { 698 log.info("Removed user with name [" + user.getName() + "]"); 699 } 700 } catch (RuntimeSecurityManagementException rsme) { 701 try { 702 if (connection != null) { 703 connection.rollback(); 704 connection.setAutoCommit(true); 705 } 706 } catch (SQLException se) { 707 } 709 710 throw rsme; 711 } catch (SQLException se) { 712 try { 713 if (connection != null) { 714 connection.rollback(); 715 connection.setAutoCommit(true); 716 } 717 } catch (SQLException se2) { 718 } 720 721 throw new RuntimeSecurityManagementException( 722 this.getClass(), 723 "Caught SQLException while deleting " + "user named [" 724 + user.getName() + "]", se); 725 } 726 } 727 728 743 protected void removeUserEntry( 744 Connection connection, Object userPrimaryKey) { 745 PreparedStatement removeUserStatement = null; 746 747 try { 748 removeUserStatement = 749 config.getStatementFactory().createPreparedStatement( 750 config.getRemoveUserQueryName(), connection); 751 removeUserStatement.setObject(1, userPrimaryKey); 752 753 int resultCount = removeUserStatement.executeUpdate(); 754 } catch (StatementFactoryException sfe) { 755 log.trace("Caught exception and rolled back transaction: " + sfe 756 + ExceptionUtility.captureStackTrace(sfe)); 757 758 throw new RuntimeSecurityManagementException( 759 this.getClass(), 760 "Caught StatementFactoryException while removing " 761 + "user.", sfe); 762 } catch (SQLException sqle) { 763 log.trace("Caught exception and rolled back transaction: " + sqle 764 + ExceptionUtility.captureStackTrace(sqle)); 765 766 throw new RuntimeSecurityManagementException( 767 this.getClass(), "Caught SQLException while removing " 768 + "user.", 769 sqle); 770 } finally { 771 try { 772 if (removeUserStatement != null) { 773 removeUserStatement.close(); 774 } 775 } catch (SQLException sqle) { 776 log.info("Caught exception closing Statement: " + sqle 777 + ExceptionUtility.captureStackTrace(sqle)); 778 } 779 } 780 } 781 782 793 public void updateCredential(Principal user, Object credential) 794 throws UnknownPrincipalException { 795 Connection connection = null; 796 797 try { 798 connection = config.getConnectionFactory().getConnection(); 799 } catch (SQLException sqle) { 800 log.trace("Caught exception: " + sqle 801 + ExceptionUtility.captureStackTrace(sqle)); 802 803 throw new RuntimeSecurityManagementException( 804 this.getClass(), "Caught SQLException while opening " 805 + "connection", 806 sqle); 807 } 808 809 try { 810 updateCredential(connection, user, credential); 811 } finally { 812 try { 813 if (connection != null) { 814 connection.close(); 815 } 816 } catch (SQLException sqle) { 817 log.info("Caught exception closing Connection: " + sqle 818 + ExceptionUtility.captureStackTrace(sqle)); 819 } 820 } 821 } 822 823 835 protected void updateCredential( 836 Connection connection, Principal user, Object credential) 837 throws UnknownPrincipalException { 838 PreparedStatement updateCredentialStatement = null; 839 840 if (log.isTraceEnabled()) { 841 log.trace("Updating user with principal [" + user 842 + "] and credential of type [" 843 + credential.getClass().getName() + "]"); 844 } 845 846 Object userPrimaryKey = 847 retreivePrincipalPrimaryKey(connection, user); 848 849 try { 850 updateCredentialStatement = 851 config.getStatementFactory().createPreparedStatement( 852 config.getUpdateCredentialQueryName(), connection); 853 updateCredentialStatement.setObject(1, credential); 854 updateCredentialStatement.setObject(2, userPrimaryKey); 855 856 int resultCount = updateCredentialStatement.executeUpdate(); 857 858 if (log.isDebugEnabled()) { 859 log.debug("Updated credential for user with name [" 860 + user.getName() + "]"); 861 } 862 } catch (StatementFactoryException sfe) { 863 log.trace("Caught exception: " + sfe 864 + ExceptionUtility.captureStackTrace(sfe)); 865 866 throw new RuntimeSecurityManagementException( 867 this.getClass(), 868 "Caught StatementFactoryException while updating " 869 + "user.", sfe); 870 } catch (SQLException sqle) { 871 log.trace("Caught exception: " + sqle 872 + ExceptionUtility.captureStackTrace(sqle)); 873 874 throw new RuntimeSecurityManagementException( 875 this.getClass(), "Caught SQLException while updating " 876 + "user.", 877 sqle); 878 } finally { 879 try { 880 updateCredentialStatement.close(); 881 } catch (SQLException sqle) { 882 log.info("Caught exception closing Statement: " + sqle 883 + ExceptionUtility.captureStackTrace(sqle)); 884 } 885 } 886 } 887 888 900 public Group createGroup(String groupName) 901 throws DuplicateGroupException { 902 Group result = null; 903 Connection connection = null; 904 905 try { 906 connection = config.getConnectionFactory().getConnection(); 907 } catch (SQLException sqle) { 908 log.trace("Caught exception: " + sqle 909 + ExceptionUtility.captureStackTrace(sqle)); 910 911 throw new RuntimeSecurityManagementException( 912 this.getClass(), "Caught SQLException while opening " 913 + "connection", 914 sqle); 915 } 916 917 try { 918 result = createGroup(connection, groupName); 919 } finally { 920 try { 921 if (connection != null) { 922 connection.close(); 923 } 924 } catch (SQLException sqle) { 925 log.info("Caught exception closing Connection: " + sqle 926 + ExceptionUtility.captureStackTrace(sqle)); 927 } 928 } 929 930 return result; 931 } 932 933 946 protected Group createGroup(Connection connection, String groupName) 947 throws DuplicateGroupException { 948 PreparedStatement createGroupStatement = null; 949 950 if (log.isTraceEnabled()) { 951 log.trace("Adding group [" + groupName + "]"); 952 } 953 954 Group currentGroup = retreiveGroup(connection, groupName); 955 956 if (currentGroup != null) { 957 throw new DuplicateGroupException( 958 this.getClass(), currentGroup); 959 } 960 961 try { 962 createGroupStatement = 963 config.getStatementFactory().createPreparedStatement( 964 config.getCreateGroupQueryName(), connection); 965 createGroupStatement.setString(1, groupName); 966 967 int resultCount = createGroupStatement.executeUpdate(); 968 969 if (log.isInfoEnabled()) { 970 log.info("Added group with name [" + groupName + "]"); 971 } 972 } catch (StatementFactoryException sfe) { 973 log.trace("Caught exception: " + sfe 974 + ExceptionUtility.captureStackTrace(sfe)); 975 976 throw new RuntimeSecurityManagementException( 977 this.getClass(), 978 "Caught StatementFactoryException while adding " 979 + "new group.", sfe); 980 } catch (SQLException sqle) { 981 log.trace("Caught exception: " + sqle 982 + ExceptionUtility.captureStackTrace(sqle)); 983 984 throw new RuntimeSecurityManagementException( 985 this.getClass(), "Caught SQLException while adding " 986 + "new group.", 987 sqle); 988 } finally { 989 try { 990 createGroupStatement.close(); 991 } catch (SQLException sqle) { 992 log.info("Caught exception closing Statement: " + sqle 993 + ExceptionUtility.captureStackTrace(sqle)); 994 } 995 } 996 997 Group newGroup = retreiveGroup(connection, groupName); 998 999 return newGroup; 1000 } 1001 1002 1012 public void removeGroup(Group group) throws UnknownGroupException { 1013 Connection connection = null; 1014 1015 try { 1016 connection = config.getConnectionFactory().getConnection(); 1017 } catch (SQLException sqle) { 1018 log.trace("Caught exception: " + sqle 1019 + ExceptionUtility.captureStackTrace(sqle)); 1020 1021 throw new RuntimeSecurityManagementException( 1022 this.getClass(), "Caught SQLException while opening " 1023 + "connection", 1024 sqle); 1025 } 1026 1027 try { 1028 removeGroup(connection, group); 1029 } finally { 1030 try { 1031 if (connection != null) { 1032 connection.close(); 1033 } 1034 } catch (SQLException sqle) { 1035 log.info("Caught exception closing Connection: " + sqle 1036 + ExceptionUtility.captureStackTrace(sqle)); 1037 } 1038 } 1039 } 1040 1041 1052 protected void removeGroup(Connection connection, Group group) 1053 throws UnknownGroupException { 1054 PreparedStatement removeGroupStatement = null; 1055 Object groupPrimaryKey; 1056 1057 Group currentGroup = retreiveGroup(connection, group.getName()); 1058 1059 if (currentGroup == null) { 1060 throw new UnknownGroupException( 1061 this.getClass(), group.getName()); 1062 } 1063 1064 try { 1065 groupPrimaryKey = 1066 retreivePrincipalPrimaryKey(connection, group); 1067 } catch (UnknownPrincipalException upe) { 1068 throw new UnknownGroupException( 1069 this.getClass(), group.getName(), 1070 upe); 1071 } 1072 1073 if (log.isTraceEnabled()) { 1074 log.trace("Removing group [" + group.getName() + "]"); 1075 } 1076 1077 try { 1078 connection.setAutoCommit(false); 1079 1080 removeAllRelationships(connection, groupPrimaryKey); 1081 1082 removeGroupStatement = 1083 config.getStatementFactory().createPreparedStatement( 1084 config.getRemoveGroupQueryName(), connection); 1085 removeGroupStatement.setObject(1, groupPrimaryKey); 1086 1087 int resultCount = removeGroupStatement.executeUpdate(); 1088 1089 connection.commit(); 1090 connection.setAutoCommit(true); 1091 1092 if (log.isInfoEnabled()) { 1093 log.info("Removed group with name [" + group.getName() + "]"); 1094 } 1095 } catch (UnknownPrincipalException upe) { 1096 try { 1097 if (connection != null) { 1098 connection.rollback(); 1099 connection.setAutoCommit(true); 1100 } 1101 } catch (SQLException sqle) { 1102 log.info("Caught exception rolling back transaction: " 1103 + sqle 1104 + ExceptionUtility.captureStackTrace(sqle)); 1105 } 1106 1107 throw new UnknownGroupException( 1110 this.getClass(), group.getName(), 1111 upe); 1112 } catch (StatementFactoryException sfe) { 1113 log.trace("Caught exception: " + sfe 1114 + ExceptionUtility.captureStackTrace(sfe)); 1115 1116 try { 1117 if (connection != null) { 1118 connection.rollback(); 1119 connection.setAutoCommit(true); 1120 } 1121 } catch (SQLException sqle) { 1122 log.info("Caught exception rolling back transaction: " 1123 + sqle 1124 + ExceptionUtility.captureStackTrace(sqle)); 1125 } 1126 1127 throw new RuntimeSecurityManagementException( 1128 this.getClass(), 1129 "Caught StatementFactoryException while removing " 1130 + "group.", sfe); 1131 } catch (SQLException sqle) { 1132 log.trace("Caught exception: " + sqle 1133 + ExceptionUtility.captureStackTrace(sqle)); 1134 1135 try { 1136 if (connection != null) { 1137 connection.rollback(); 1138 connection.setAutoCommit(true); 1139 } 1140 } catch (SQLException sqle2) { 1141 log.info("Caught exception rolling back transaction: " 1142 + sqle2 1143 + ExceptionUtility.captureStackTrace(sqle)); 1144 } 1145 1146 throw new RuntimeSecurityManagementException( 1147 this.getClass(), "Caught SQLException while removing " 1148 + "group.", 1149 sqle); 1150 } finally { 1151 try { 1152 if (removeGroupStatement != null) { 1153 removeGroupStatement.close(); 1154 } 1155 } catch (SQLException sqle) { 1156 log.info("Caught exception closing Statement: " + sqle 1157 + ExceptionUtility.captureStackTrace(sqle)); 1158 } 1159 } 1160 } 1161 1162 1173 public Principal retreiveUser(String username) { 1174 1175 Principal result = null; 1176 Connection connection = null; 1177 1178 try { 1179 connection = config.getConnectionFactory().getConnection(); 1180 } catch (SQLException sqle) { 1181 log.trace("Caught exception: " + sqle 1182 + ExceptionUtility.captureStackTrace(sqle)); 1183 1184 throw new RuntimeSecurityManagementException( 1185 this.getClass(), "Caught SQLException while opening " 1186 + "connection", 1187 sqle); 1188 } 1189 1190 try { 1191 result = retreiveUser(connection, username); 1192 } finally { 1193 try { 1194 if (connection != null) { 1195 connection.close(); 1196 } 1197 } catch (SQLException sqle) { 1198 log.debug("Caught exception closing Connection: " + sqle 1199 + ExceptionUtility.captureStackTrace(sqle)); 1200 } 1201 } 1202 1203 return result; 1204 } 1205 1206 1218 protected Principal retreiveUser( 1219 Connection connection, String username) { 1220 PreparedStatement retreiveUserStatement = null; 1221 ResultSet retreiveUserResultSet = null; 1222 Principal resultPrincipal; 1223 1224 if (log.isTraceEnabled()) { 1225 log.trace("Searching for user with principal name [" + username 1226 + "]"); 1227 } 1228 1229 try { 1230 retreiveUserStatement = 1231 config.getStatementFactory().createPreparedStatement( 1232 config.getRetreiveUserQueryName(), connection); 1233 retreiveUserStatement.setString(1, username); 1234 retreiveUserResultSet = retreiveUserStatement.executeQuery(); 1235 1236 if (retreiveUserResultSet.next()) { 1237 resultPrincipal = 1238 createUserObject(retreiveUserResultSet.getString(1)); 1239 } else { 1240 resultPrincipal = null; 1241 } 1242 } catch (StatementFactoryException sfe) { 1243 log.trace("Caught exception: " + sfe 1244 + ExceptionUtility.captureStackTrace(sfe)); 1245 1246 throw new RuntimeSecurityManagementException( 1247 this.getClass(), 1248 "Caught StatementFactoryException while adding " 1249 + "new user.", sfe); 1250 } catch (SQLException sqle) { 1251 log.trace("Caught exception: " + sqle 1252 + ExceptionUtility.captureStackTrace(sqle)); 1253 1254 throw new RuntimeSecurityManagementException( 1255 this.getClass(), "Caught SQLException while adding " 1256 + "new user.", 1257 sqle); 1258 } finally { 1259 try { 1260 if (retreiveUserResultSet != null) { 1261 retreiveUserResultSet.close(); 1262 } 1263 } catch (SQLException sqle) { 1264 log.info("Caught exception closing ResultSet: " + sqle 1265 + ExceptionUtility.captureStackTrace(sqle)); 1266 } 1267 1268 try { 1269 if (retreiveUserStatement != null) { 1270 retreiveUserStatement.close(); 1271 } 1272 } catch (SQLException sqle) { 1273 log.info("Caught exception closing Statement: " + sqle 1274 + ExceptionUtility.captureStackTrace(sqle)); 1275 } 1276 } 1277 1278 return resultPrincipal; 1279 } 1280 1281 1292 public Group retreiveGroup(String groupName) { 1293 1294 1295 Group result = null; 1296 Connection connection = null; 1297 1298 try { 1299 connection = config.getConnectionFactory().getConnection(); 1300 } catch (SQLException sqle) { 1301 log.trace("Caught exception: " + sqle 1302 + ExceptionUtility.captureStackTrace(sqle)); 1303 1304 throw new RuntimeSecurityManagementException( 1305 this.getClass(), "Caught SQLException while opening " 1306 + "connection", 1307 sqle); 1308 } 1309 1310 try { 1311 result = retreiveGroup(connection, groupName); 1312 } finally { 1313 try { 1314 if (connection != null) { 1315 connection.close(); 1316 } 1317 } catch (SQLException sqle) { 1318 log.info("Caught exception closing Connection: " + sqle 1319 + ExceptionUtility.captureStackTrace(sqle)); 1320 } 1321 } 1322 1323 return result; 1324 } 1325 1326 1338 protected Group retreiveGroup(Connection connection, String groupName) { 1339 1340 PreparedStatement retreiveGroupStatement = null; 1341 ResultSet retreiveGroupResultSet = null; 1342 Group resultGroup; 1343 1344 if (log.isTraceEnabled()) { 1345 log.trace("Searching for group with name [" + groupName + "]"); 1346 } 1347 1348 try { 1349 retreiveGroupStatement = 1350 config.getStatementFactory().createPreparedStatement( 1351 config.getRetreiveGroupQueryName(), connection); 1352 retreiveGroupStatement.setString(1, groupName); 1353 retreiveGroupResultSet = retreiveGroupStatement.executeQuery(); 1354 1355 if (retreiveGroupResultSet.next()) { 1356 String resultGroupName = 1357 retreiveGroupResultSet.getString(1); 1358 resultGroup = createGroupObject(resultGroupName, null); 1359 1360 if (resultGroup instanceof LazyLoadGroup) { 1361 retreiveMembers( 1362 connection, (LazyLoadGroup) resultGroup, null); 1363 } else { 1364 if (log.isDebugEnabled()) { 1366 log.debug("Group returned is not expected class [" 1367 + LazyLoadGroup.class.getName() 1368 + "] but is actually " + "instance of [" 1369 + resultGroup.getClass().getName() 1370 + "] and therefore members will not be loaded."); 1371 } 1372 } 1373 1374 if (log.isTraceEnabled()) { 1375 log.trace("Found group with name [" + groupName + "]"); 1376 } 1377 } else { 1378 resultGroup = null; 1379 1380 if (log.isTraceEnabled()) { 1381 log.trace( 1382 "Did not find group with name [" + groupName + "]"); 1383 } 1384 } 1385 } catch (StatementFactoryException sfe) { 1386 log.trace("Caught exception: " + sfe 1387 + ExceptionUtility.captureStackTrace(sfe)); 1388 1389 throw new RuntimeSecurityManagementException( 1390 this.getClass(), 1391 "Caught StatementFactoryException while finding " 1392 + "group.", sfe); 1393 } catch (SQLException sqle) { 1394 log.trace("Caught exception: " + sqle 1395 + ExceptionUtility.captureStackTrace(sqle)); 1396 1397 throw new RuntimeSecurityManagementException( 1398 this.getClass(), "Caught SQLException while finding " 1399 + "group.", 1400 sqle); 1401 } finally { 1402 try { 1403 if (retreiveGroupResultSet != null) { 1404 retreiveGroupResultSet.close(); 1405 } 1406 } catch (SQLException sqle) { 1407 log.info("Caught exception closing ResultSet: " + sqle 1408 + ExceptionUtility.captureStackTrace(sqle)); 1409 } 1410 1411 try { 1412 if (retreiveGroupStatement != null) { 1413 retreiveGroupStatement.close(); 1414 } 1415 } catch (SQLException sqle) { 1416 log.info("Caught exception closing Statement: " + sqle 1417 + ExceptionUtility.captureStackTrace(sqle)); 1418 } 1419 } 1420 1421 return resultGroup; 1422 } 1423 1424 1440 public Set retreiveGroups(Principal principal) 1441 throws UnknownPrincipalException { 1442 1443 Set result = null; 1444 Connection connection = null; 1445 1446 try { 1447 connection = config.getConnectionFactory().getConnection(); 1448 } catch (SQLException sqle) { 1449 log.trace("Caught exception: " + sqle 1450 + ExceptionUtility.captureStackTrace(sqle)); 1451 1452 throw new RuntimeSecurityManagementException( 1453 this.getClass(), "Caught SQLException while opening " 1454 + "connection", 1455 sqle); 1456 } 1457 1458 try { 1459 result = retreiveGroups(connection, principal); 1460 } finally { 1461 try { 1462 if (connection != null) { 1463 connection.close(); 1464 } 1465 } catch (SQLException sqle) { 1466 log.info("Caught exception closing Connection: " + sqle 1467 + ExceptionUtility.captureStackTrace(sqle)); 1468 } 1469 } 1470 1471 return result; 1472 } 1473 1474 1491 protected Set retreiveGroups( 1492 Connection connection, Principal principal) 1493 throws UnknownPrincipalException { 1494 1495 String queryname; 1496 Set membershipSet = new HashSet (); 1497 PreparedStatement retreiveMembershipStatement = null; 1498 ResultSet retreiveMembershipResultSet = null; 1499 1500 if (principal instanceof Group ) { 1501 Principal currentGroup = 1502 retreiveGroup(connection, principal.getName()); 1503 1504 if (currentGroup == null) { 1505 throw new UnknownPrincipalException( 1506 this.getClass(), principal); 1507 } 1508 1509 if (log.isTraceEnabled()) { 1510 log.trace("Get membership of group with name [" 1511 + principal.getName() + "]"); 1512 } 1513 1514 queryname = config.getRetreiveGroupsForGroupsQueryName(); 1515 } else { 1516 Principal currentUser = 1517 retreiveUser(connection, principal.getName()); 1518 1519 if (currentUser == null) { 1520 throw new UnknownPrincipalException( 1521 this.getClass(), principal); 1522 } 1523 1524 if (log.isTraceEnabled()) { 1525 log.trace("Get membership of user with name [" 1526 + principal.getName() + "]"); 1527 } 1528 1529 queryname = config.getRetreiveGroupsForUserQueryName(); 1530 } 1531 1532 try { 1533 retreiveMembershipStatement = 1534 config.getStatementFactory().createPreparedStatement( 1535 queryname, connection); 1536 retreiveMembershipStatement.setString(1, principal.getName()); 1537 retreiveMembershipResultSet = 1538 retreiveMembershipStatement.executeQuery(); 1539 1540 while (retreiveMembershipResultSet.next()) { 1541 Group containingGroup = 1542 retreiveGroup( 1543 connection, 1544 retreiveMembershipResultSet.getString(1)); 1545 1546 if (log.isTraceEnabled()) { 1547 log.trace("Adding membership group with name [" 1548 + containingGroup.getName() 1549 + "] to principals with name [" 1550 + principal.getName() + "]"); 1551 } 1552 1553 membershipSet.add(containingGroup); 1554 } 1555 } catch (StatementFactoryException sfe) { 1556 log.trace("Caught exception: " + sfe 1557 + ExceptionUtility.captureStackTrace(sfe)); 1558 1559 throw new RuntimeSecurityManagementException( 1560 this.getClass(), 1561 "Caught StatementFactoryException while getting groups " 1562 + "for principal [" + principal.getName() + "]", sfe); 1563 } catch (SQLException sqle) { 1564 log.trace("Caught exception: " + sqle 1565 + ExceptionUtility.captureStackTrace(sqle)); 1566 1567 throw new RuntimeSecurityManagementException( 1568 this.getClass(), 1569 "Caught SQLException while getting roups " 1570 + "for principal [" + principal.getName() + "]", sqle); 1571 } finally { 1572 try { 1573 if (retreiveMembershipResultSet != null) { 1574 retreiveMembershipResultSet.close(); 1575 } 1576 } catch (SQLException sqle) { 1577 log.info("Caught exception closing ResultSet: " + sqle 1578 + ExceptionUtility.captureStackTrace(sqle)); 1579 } 1580 1581 try { 1582 if (retreiveMembershipStatement != null) { 1583 retreiveMembershipStatement.close(); 1584 } 1585 } catch (SQLException sqle) { 1586 log.info("Caught exception closing Statement: " + sqle 1587 + ExceptionUtility.captureStackTrace(sqle)); 1588 } 1589 } 1590 1591 return membershipSet; 1592 } 1593 1594 1609 protected void retreiveMembers( 1610 Connection connection, LazyLoadGroup group, Map loadedGroups) { 1611 1612 if (loadedGroups == null) { 1613 loadedGroups = new HashMap (); 1614 1615 if (log.isTraceEnabled()) { 1616 log.trace("Retreiving members for top-level group named [" 1617 + group.getName() + "]"); 1618 } 1619 } 1620 1621 Set members = new HashSet (); 1622 1623 PreparedStatement retreiveMembersStatement = null; 1624 ResultSet retreiveMembersResultSet = null; 1625 1626 try { 1627 retreiveMembersStatement = 1628 config.getStatementFactory().createPreparedStatement( 1629 config.getRetreiveMemebersQueryName(), connection); 1630 1631 retreiveMembersStatement.setString(1, group.getName()); 1632 retreiveMembersResultSet = 1633 retreiveMembersStatement.executeQuery(); 1634 1635 while (retreiveMembersResultSet.next()) { 1636 members.add( 1637 retreiveMember(retreiveMembersResultSet, loadedGroups)); 1638 } 1639 1640 group.setMembers(members); 1641 1642 if (log.isTraceEnabled()) { 1643 log.trace("Added members to group named [" + group.getName() 1644 + "] with total of [" + members.size() 1645 + "] principals"); 1646 } 1647 } catch (StatementFactoryException sfe) { 1648 log.trace("Caught exception: " + sfe 1649 + ExceptionUtility.captureStackTrace(sfe)); 1650 1651 throw new RuntimeSecurityManagementException( 1652 this.getClass(), 1653 "Caught StatementFactoryException while adding " 1654 + "new user.", sfe); 1655 } catch (SQLException sqle) { 1656 log.trace("Caught exception: " + sqle 1657 + ExceptionUtility.captureStackTrace(sqle)); 1658 1659 throw new RuntimeSecurityManagementException( 1660 this.getClass(), "Caught SQLException while adding " 1661 + "new user.", 1662 sqle); 1663 } finally { 1664 try { 1665 retreiveMembersResultSet.close(); 1666 } catch (SQLException sqle) { 1667 log.info("Caught exception closing ResultSet: " + sqle 1668 + ExceptionUtility.captureStackTrace(sqle)); 1669 } 1670 1671 try { 1672 retreiveMembersStatement.close(); 1673 } catch (SQLException sqle) { 1674 log.info("Caught exception closing Statement: " + sqle 1675 + ExceptionUtility.captureStackTrace(sqle)); 1676 } 1677 } 1678 1679 Iterator membersIterator = members.iterator(); 1683 1684 while (membersIterator.hasNext()) { 1685 Object currentMember = membersIterator.next(); 1686 1687 if (currentMember instanceof LazyLoadGroup) { 1688 LazyLoadGroup currentGroup = (LazyLoadGroup) currentMember; 1689 1690 if (currentGroup.isLoaded()) { 1691 if (log.isTraceEnabled()) { 1692 log.trace("In group named [" + group.getName() 1693 + "] found loaded sub-group named [" 1694 + currentGroup.getName() + "]"); 1695 } 1696 } else { 1697 if (log.isTraceEnabled()) { 1698 log.trace("In group named [" + group.getName() 1699 + "] found UNLOADED sub-group named [" 1700 + currentGroup.getName() + "]"); 1701 } 1702 1703 retreiveMembers( 1704 connection, currentGroup, loadedGroups); 1705 } 1706 } 1707 } 1708 } 1709 1710 1719 protected Principal createUserObject(String username) { 1720 return new DefaultUserImpl(username); 1721 } 1722 1723 1733 protected Group createGroupObject(String groupName, Set members) { 1734 return new DefaultGroupImpl( 1735 groupName, members, config.getConfigurationName()); 1736 } 1737 1738 1764 protected Principal retreiveMember( 1765 ResultSet memberResultSet, Map loadedGroups) { 1766 1767 Principal resultPrincipal = null; 1768 1769 try { 1770 String principalName = memberResultSet.getString(1); 1771 PrincipalTypeEnum principalTypeEnum = 1772 PrincipalTypeEnum.getByOrdinal(memberResultSet.getInt(2)); 1773 1774 if (log.isTraceEnabled()) { 1775 log.trace("Instantiating new principal named [" 1776 + principalName 1777 + "] of enumeration type [" 1778 + principalTypeEnum 1779 + "]"); 1780 } 1781 1782 if (principalTypeEnum.equals(PrincipalTypeEnum.USER)) { 1783 resultPrincipal = createUserObject(principalName); 1784 } else if (principalTypeEnum.equals(PrincipalTypeEnum.GROUP)) { 1785 if (loadedGroups.containsKey(principalName)) { 1786 resultPrincipal = 1787 (Principal ) loadedGroups.get(principalName); 1788 } else { 1789 resultPrincipal = 1790 createGroupObject(principalName, null); 1791 1792 loadedGroups.put(principalName, resultPrincipal); 1793 } 1794 } 1795 } catch (PrincipalTypeEnum.PrincipalTypeEnumNotFoundException ptenfe) { 1796 log.trace("Caught exception: " + ptenfe 1797 + ExceptionUtility.captureStackTrace(ptenfe)); 1798 1799 throw new RuntimeSecurityManagementException( 1800 this.getClass(), "Invalid principal type encountered.", ptenfe); 1801 } catch (SQLException sqle) { 1802 log.trace("Caught exception: " + sqle 1803 + ExceptionUtility.captureStackTrace(sqle)); 1804 1805 throw new RuntimeSecurityManagementException( 1806 this.getClass(), "Caught SQLException while adding " 1807 + "new user.", 1808 sqle); 1809 } 1810 1811 return resultPrincipal; 1812 } 1813 1814 1829 public boolean addPrincipalToGroup(Principal principal, Group group) 1830 throws UnknownPrincipalException, UnknownGroupException { 1831 1832 boolean isPrincipalAdded = false; 1833 Connection connection = null; 1834 1835 try { 1836 connection = config.getConnectionFactory().getConnection(); 1837 } catch (SQLException sqle) { 1838 log.trace("Caught exception: " + sqle 1839 + ExceptionUtility.captureStackTrace(sqle)); 1840 1841 throw new RuntimeSecurityManagementException( 1842 this.getClass(), "Caught SQLException while opening connection", 1843 sqle); 1844 } 1845 1846 try { 1847 isPrincipalAdded = 1848 addPrincipalToGroup(connection, principal, group); 1849 } finally { 1850 try { 1851 if (connection != null) { 1852 connection.close(); 1853 } 1854 } catch (SQLException sqle) { 1855 log.info("Caught exception closing Connection: " + sqle 1856 + ExceptionUtility.captureStackTrace(sqle)); 1857 } 1858 } 1859 1860 return isPrincipalAdded; 1861 } 1862 1863 1879 protected boolean addPrincipalToGroup( 1880 Connection connection, Principal principal, Group group) 1881 throws UnknownPrincipalException, UnknownGroupException { 1882 PreparedStatement addPrincipalToGroupStatement = null; 1883 boolean result = false; 1884 1885 if (log.isTraceEnabled()) { 1886 log.trace("Adding principal [" + principal.getName() 1887 + "] to group [" + group.getName() + "]"); 1888 } 1889 1890 Object principalPrimaryKey = 1891 retreivePrincipalPrimaryKey(connection, principal); 1892 Object groupPrimaryKey = 1893 retreivePrincipalPrimaryKey(connection, group); 1894 1895 if (group.isMember(principal)) { 1896 if (log.isTraceEnabled()) { 1897 log.trace("User [" + principal 1898 + "] is already a member of group [" + group + "]"); 1899 } 1900 1901 result = false; 1902 } else { 1903 try { 1904 addPrincipalToGroupStatement = 1905 config.getStatementFactory().createPreparedStatement( 1906 config.getAddPrincipalToGroupQueryName(), 1907 connection); 1908 1909 addPrincipalToGroupStatement.setObject( 1910 1, principalPrimaryKey); 1911 addPrincipalToGroupStatement.setObject(2, groupPrimaryKey); 1912 1913 int resultCount = 1914 addPrincipalToGroupStatement.executeUpdate(); 1915 1916 if (resultCount > 0) { 1917 result = true; 1918 1919 if (log.isDebugEnabled()) { 1920 log.debug("Added principal [" + principal.getName() 1921 + "] to group [" + group.getName() + "]"); 1922 } 1923 } 1924 } catch (StatementFactoryException sfe) { 1925 log.trace("Caught exception: " + sfe 1926 + ExceptionUtility.captureStackTrace(sfe)); 1927 1928 throw new RuntimeSecurityManagementException( 1929 this.getClass(), 1930 "Caught StatementFactoryException while adding " 1931 + "new user.", sfe); 1932 } catch (SQLException sqle) { 1933 log.trace("Caught exception: " + sqle 1934 + ExceptionUtility.captureStackTrace(sqle)); 1935 1936 throw new RuntimeSecurityManagementException( 1937 this.getClass(), 1938 "Caught SQLException while adding " + "new user.", sqle); 1939 } finally { 1940 try { 1941 if (addPrincipalToGroupStatement != null) { 1942 addPrincipalToGroupStatement.close(); 1943 } 1944 } catch (SQLException sqle) { 1945 log.info("Caught exception closing Statement: " + sqle 1946 + ExceptionUtility.captureStackTrace(sqle)); 1947 } 1948 } 1949 } 1950 1951 return result; 1952 } 1953 1954 1970 public boolean removePrincipalFromGroup( 1971 Principal principal, Group group) 1972 throws UnknownPrincipalException, UnknownGroupException { 1973 Connection connection = null; 1974 boolean isRemoved = false; 1975 1976 try { 1977 connection = config.getConnectionFactory().getConnection(); 1978 } catch (SQLException sqle) { 1979 log.trace("Caught exception: " + sqle 1980 + ExceptionUtility.captureStackTrace(sqle)); 1981 1982 throw new RuntimeSecurityManagementException( 1983 this.getClass(), "Caught SQLException while opening connection", 1984 sqle); 1985 } 1986 1987 try { 1988 isRemoved = 1989 removePrincipalFromGroup(connection, principal, group); 1990 } finally { 1991 try { 1992 if (connection != null) { 1993 connection.close(); 1994 } 1995 } catch (SQLException sqle) { 1996 log.info("Caught exception closing Connection: " + sqle 1997 + ExceptionUtility.captureStackTrace(sqle)); 1998 } 1999 } 2000 2001 return isRemoved; 2002 } 2003 2004 2021 protected boolean removePrincipalFromGroup( 2022 Connection connection, Principal principal, Group group) 2023 throws UnknownPrincipalException, UnknownGroupException { 2024 PreparedStatement removePrincipalFromGroupStatement = null; 2025 boolean result = false; 2026 2027 if (log.isTraceEnabled()) { 2028 log.trace("Removing principal [" + principal.getName() 2029 + "] from group [" + group.getName() + "]"); 2030 } 2031 2032 Object principalPrimaryKey = 2033 retreivePrincipalPrimaryKey(connection, principal); 2034 Object groupPrimaryKey = 2035 retreivePrincipalPrimaryKey(connection, group); 2036 2037 if (!group.isMember(principal)) { 2038 result = false; 2039 2040 if (log.isTraceEnabled()) { 2041 log.trace("User [" + principal 2042 + "] is already not a member of group [" + group + "]"); 2043 } 2044 } else { 2045 try { 2046 removePrincipalFromGroupStatement = 2047 config.getStatementFactory().createPreparedStatement( 2048 config.getRemovePrincipalFromGroupQueryName(), 2049 connection); 2050 2051 removePrincipalFromGroupStatement.setObject( 2052 1, principalPrimaryKey); 2053 removePrincipalFromGroupStatement.setObject( 2054 2, groupPrimaryKey); 2055 2056 int resultCount = 2057 removePrincipalFromGroupStatement.executeUpdate(); 2058 2059 if (resultCount > 0) { 2060 result = true; 2061 2062 if (log.isDebugEnabled()) { 2063 log.debug("Removed principal [" + principal.getName() 2064 + "] from group [" + group.getName() + "]"); 2065 } 2066 } 2067 } catch (StatementFactoryException sfe) { 2068 log.trace("Caught exception: " + sfe 2069 + ExceptionUtility.captureStackTrace(sfe)); 2070 2071 throw new RuntimeSecurityManagementException( 2072 this.getClass(), 2073 "Caught StatementFactoryException while removing " 2074 + "user.", sfe); 2075 } catch (SQLException sqle) { 2076 log.trace("Caught exception: " + sqle 2077 + ExceptionUtility.captureStackTrace(sqle)); 2078 2079 throw new RuntimeSecurityManagementException( 2080 this.getClass(), "Caught SQLException while removing " 2081 + "user.", sqle); 2082 } finally { 2083 try { 2084 removePrincipalFromGroupStatement.close(); 2085 } catch (SQLException sqle) { 2086 log.info("Caught exception closing Statement: " + sqle 2087 + ExceptionUtility.captureStackTrace(sqle)); 2088 } 2089 } 2090 } 2091 2092 return result; 2093 } 2094 2095 2105 public Set retreiveAllUserNames() { 2106 2107 Connection connection = null; 2108 Set allUsers = null; 2109 2110 try { 2111 connection = config.getConnectionFactory().getConnection(); 2112 } catch (SQLException sqle) { 2113 log.trace("Caught exception: " + sqle 2114 + ExceptionUtility.captureStackTrace(sqle)); 2115 2116 throw new RuntimeSecurityManagementException( 2117 this.getClass(), "Caught SQLException while opening connection", 2118 sqle); 2119 } 2120 2121 try { 2122 allUsers = retreiveAllusernames(connection); 2123 } finally { 2124 try { 2125 if (connection != null) { 2126 connection.close(); 2127 } 2128 } catch (SQLException sqle) { 2129 log.info("Caught exception closing Connection: " + sqle 2130 + ExceptionUtility.captureStackTrace(sqle)); 2131 } 2132 } 2133 2134 return allUsers; 2135 } 2136 2137 2149 protected Set retreiveAllusernames(Connection connection) { 2150 Set allUsers = new HashSet (); 2151 PreparedStatement retreiveAllUsersStatement = null; 2152 ResultSet retreiveAllUsersResultSet = null; 2153 2154 if (log.isTraceEnabled()) { 2155 log.trace("Searching for all users."); 2156 } 2157 2158 try { 2159 retreiveAllUsersStatement = 2160 config.getStatementFactory().createPreparedStatement( 2161 config.getRetreiveAllUsersQuery(), connection); 2162 2163 retreiveAllUsersResultSet = 2164 retreiveAllUsersStatement.executeQuery(); 2165 2166 while (retreiveAllUsersResultSet.next()) { 2167 allUsers.add(retreiveAllUsersResultSet.getString(1)); 2168 } 2169 } catch (StatementFactoryException sfe) { 2170 log.trace("Caught exception: " + sfe 2171 + ExceptionUtility.captureStackTrace(sfe)); 2172 2173 throw new RuntimeSecurityManagementException( 2174 this.getClass(), 2175 "Caught StatementFactoryException while retreiving " 2176 + "users.", sfe); 2177 } catch (SQLException sqle) { 2178 log.trace("Caught exception: " + sqle 2179 + ExceptionUtility.captureStackTrace(sqle)); 2180 2181 throw new RuntimeSecurityManagementException( 2182 this.getClass(), 2183 "Caught SQLException while retreiving users.", 2184 sqle); 2185 } finally { 2186 try { 2187 if (retreiveAllUsersResultSet != null) { 2188 retreiveAllUsersResultSet.close(); 2189 } 2190 } catch (SQLException sqle) { 2191 log.info("Caught exception closing ResultSet: " + sqle 2192 + ExceptionUtility.captureStackTrace(sqle)); 2193 } 2194 2195 try { 2196 if (retreiveAllUsersStatement != null) { 2197 retreiveAllUsersStatement.close(); 2198 } 2199 } catch (SQLException sqle) { 2200 log.info("Caught exception closing Statement: " + sqle 2201 + ExceptionUtility.captureStackTrace(sqle)); 2202 } 2203 } 2204 2205 return allUsers; 2206 } 2207 2208 2218 public Set retreiveAllGroupNames() { 2219 2220 Connection connection = null; 2221 Set allGroups = null; 2222 2223 try { 2224 connection = config.getConnectionFactory().getConnection(); 2225 } catch (SQLException sqle) { 2226 log.trace("Caught exception: " + sqle 2227 + ExceptionUtility.captureStackTrace(sqle)); 2228 2229 throw new RuntimeSecurityManagementException( 2230 this.getClass(), "Caught SQLException while opening connection", 2231 sqle); 2232 } 2233 2234 try { 2235 allGroups = retreiveAllGroupNames(connection); 2236 } finally { 2237 try { 2238 if (connection != null) { 2239 connection.close(); 2240 } 2241 } catch (SQLException sqle) { 2242 log.info("Caught exception closing Connection: " + sqle 2243 + ExceptionUtility.captureStackTrace(sqle)); 2244 } 2245 } 2246 2247 return allGroups; 2248 } 2249 2250 2262 protected Set retreiveAllGroupNames(Connection connection) { 2263 Set allGroups = new HashSet (); 2264 PreparedStatement retreiveAllGroupsStatement = null; 2265 ResultSet retreiveAllGroupsResultSet = null; 2266 2267 if (log.isTraceEnabled()) { 2268 log.trace("Searching for all users."); 2269 } 2270 2271 try { 2272 retreiveAllGroupsStatement = 2273 config.getStatementFactory().createPreparedStatement( 2274 config.getRetreiveAllGroupsQuery(), connection); 2275 2276 retreiveAllGroupsResultSet = 2277 retreiveAllGroupsStatement.executeQuery(); 2278 2279 while (retreiveAllGroupsResultSet.next()) { 2280 allGroups.add(retreiveAllGroupsResultSet.getString(1)); 2281 } 2282 } catch (StatementFactoryException sfe) { 2283 log.trace("Caught exception: " + sfe 2284 + ExceptionUtility.captureStackTrace(sfe)); 2285 2286 throw new RuntimeSecurityManagementException( 2287 this.getClass(), 2288 "Caught StatementFactoryException while retreiving " 2289 + "groups.", sfe); 2290 } catch (SQLException sqle) { 2291 log.trace("Caught exception: " + sqle 2292 + ExceptionUtility.captureStackTrace(sqle)); 2293 2294 throw new RuntimeSecurityManagementException( 2295 this.getClass(), "Caught SQLException while retreiving groups.", 2296 sqle); 2297 } finally { 2298 try { 2299 if (retreiveAllGroupsResultSet != null) { 2300 retreiveAllGroupsResultSet.close(); 2301 } 2302 } catch (SQLException sqle) { 2303 log.info("Caught exception closing ResultSet: " + sqle 2304 + ExceptionUtility.captureStackTrace(sqle)); 2305 } 2306 2307 try { 2308 if (retreiveAllGroupsStatement != null) { 2309 retreiveAllGroupsStatement.close(); 2310 } 2311 } catch (SQLException sqle) { 2312 log.info("Caught exception closing Statement: " + sqle 2313 + ExceptionUtility.captureStackTrace(sqle)); 2314 } 2315 } 2316 2317 return allGroups; 2318 } 2319 2320 2342 protected Object retreivePrincipalPrimaryKey( 2343 Connection connection, Principal principal) 2344 throws UnknownPrincipalException, UnknownGroupException { 2345 Object primaryKey; 2346 2347 if (principal instanceof Group ) { 2348 primaryKey = 2349 retreiveGroupPrimaryKey(connection, (Group ) principal); 2350 } else { 2351 primaryKey = retreiveUserPrimaryKey(connection, principal); 2352 } 2353 2354 return primaryKey; 2355 } 2356 2357 2376 protected Object retreiveUserPrimaryKey( 2377 Connection connection, Principal principal) 2378 throws UnknownPrincipalException { 2379 PreparedStatement retreiveUserPrimaryKeyStatement = null; 2380 ResultSet retreiveUserPrimaryKeyResultSet = null; 2381 Object userPrimaryKey = null; 2382 2383 try { 2384 retreiveUserPrimaryKeyStatement = 2385 config.getStatementFactory().createPreparedStatement( 2386 config.getRetreiveUserPrimaryKeyQueryName(), connection); 2387 2388 retreiveUserPrimaryKeyStatement.setObject( 2389 1, principal.getName()); 2390 2391 retreiveUserPrimaryKeyResultSet = 2392 retreiveUserPrimaryKeyStatement.executeQuery(); 2393 2394 if (retreiveUserPrimaryKeyResultSet.next()) { 2395 userPrimaryKey = 2396 retreiveUserPrimaryKeyResultSet.getObject(1); 2397 } else { 2398 throw new UnknownPrincipalException( 2399 this.getClass(), principal.getName()); 2400 } 2401 2402 if (log.isTraceEnabled()) { 2403 log.trace("Retreived primary key for user with name [" 2404 + principal.getName() + "] of value [" 2405 + userPrimaryKey + "]"); 2406 } 2407 } catch (StatementFactoryException sfe) { 2408 log.trace("Caught exception: " + sfe 2409 + ExceptionUtility.captureStackTrace(sfe)); 2410 2411 throw new RuntimeSecurityManagementException( 2412 this.getClass(), 2413 "Caught StatementFactoryException while getting primary key " 2414 + "for user [" + principal.getName() + "]", sfe); 2415 } catch (SQLException sqle) { 2416 log.trace("Caught exception: " + sqle 2417 + ExceptionUtility.captureStackTrace(sqle)); 2418 2419 throw new RuntimeSecurityManagementException( 2420 this.getClass(), 2421 "Caught SQLException while getting primary key " 2422 + "for user [" + principal.getName() + "]", sqle); 2423 } finally { 2424 try { 2425 if (retreiveUserPrimaryKeyResultSet != null) { 2426 retreiveUserPrimaryKeyResultSet.close(); 2427 } 2428 } catch (SQLException sqle) { 2429 } 2431 2432 try { 2433 if (retreiveUserPrimaryKeyStatement != null) { 2434 retreiveUserPrimaryKeyStatement.close(); 2435 } 2436 } catch (SQLException sqle) { 2437 } 2439 } 2440 2441 return userPrimaryKey; 2442 } 2443 2444 2463 protected Object retreiveGroupPrimaryKey( 2464 Connection connection, Group group) throws UnknownGroupException { 2465 PreparedStatement retreiveGroupPrimaryKeyStatement = null; 2466 ResultSet retreiveGroupPrimaryKeyResultSet = null; 2467 Object groupPrimaryKey = null; 2468 2469 try { 2470 retreiveGroupPrimaryKeyStatement = 2471 config.getStatementFactory().createPreparedStatement( 2472 config.getRetreiveGroupPrimaryKeyQueryName(), 2473 connection); 2474 2475 retreiveGroupPrimaryKeyStatement.setObject(1, group.getName()); 2476 2477 retreiveGroupPrimaryKeyResultSet = 2478 retreiveGroupPrimaryKeyStatement.executeQuery(); 2479 2480 if (retreiveGroupPrimaryKeyResultSet.next()) { 2481 groupPrimaryKey = 2482 retreiveGroupPrimaryKeyResultSet.getObject(1); 2483 } else { 2484 throw new UnknownGroupException( 2485 this.getClass(), group.getName()); 2486 } 2487 2488 if (log.isTraceEnabled()) { 2489 log.trace("Retreived primary key for group with name [" 2490 + group.getName() + "] of value [" + groupPrimaryKey 2491 + "]"); 2492 } 2493 } catch (StatementFactoryException sfe) { 2494 log.trace("Caught exception: " + sfe 2495 + ExceptionUtility.captureStackTrace(sfe)); 2496 2497 throw new RuntimeSecurityManagementException( 2498 this.getClass(), 2499 "Caught StatementFactoryException while getting primary key " 2500 + "for group [" + group.getName() + "]", sfe); 2501 } catch (SQLException sqle) { 2502 log.trace("Caught exception: " + sqle 2503 + ExceptionUtility.captureStackTrace(sqle)); 2504 2505 throw new RuntimeSecurityManagementException( 2506 this.getClass(), 2507 "Caught SQLException while getting primary key " 2508 + "for group [" + group.getName() + "]", sqle); 2509 } finally { 2510 try { 2511 if (retreiveGroupPrimaryKeyResultSet != null) { 2512 retreiveGroupPrimaryKeyResultSet.close(); 2513 } 2514 } catch (SQLException sqle) { 2515 } 2517 2518 try { 2519 if (retreiveGroupPrimaryKeyStatement != null) { 2520 retreiveGroupPrimaryKeyStatement.close(); 2521 } 2522 } catch (SQLException sqle) { 2523 } 2525 } 2526 2527 return groupPrimaryKey; 2528 } 2529 2530 2539 public void configure(ComponentConfiguration configuration) { 2540 2541 try { 2542 this.config = (RdbmsUserManagerConfiguration) configuration; 2543 } catch (ClassCastException cce) { 2544 throw new InvalidConfigurationException( 2545 this.getClass(), configuration.getConfigurationName(), 2546 "ComponentConfiguration", 2547 "Specifed RdbmsUserManagerConfiguration does not implement " 2548 + "correct interface.", 2549 cce); 2550 } 2551 } 2552} 2553 | Popular Tags |