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.torque.TorqueException; 31 import org.apache.torque.om.Persistent; 32 import org.apache.torque.util.BasePeer; 33 import org.apache.torque.util.Criteria; 34 35 import org.apache.turbine.om.security.User; 36 import org.apache.turbine.services.InitializationException; 37 import org.apache.turbine.services.security.TurbineSecurity; 38 import org.apache.turbine.util.security.DataBackendException; 39 40 48 49 public class UserPeerManager 50 implements UserPeerManagerConstants 51 { 52 53 private static Class userPeerClass = null; 54 55 56 private static Class userObject = null; 57 58 59 private static String tableName = null; 60 61 62 private static String nameColumn = null; 63 64 65 private static String idColumn = null; 66 67 68 private static String passwordColumn = null; 69 70 71 private static String firstNameColumn = null; 72 73 74 private static String lastNameColumn = null; 75 76 77 private static String emailColumn = null; 78 79 80 private static String confirmColumn = null; 81 82 83 private static String createDateColumn = null; 84 85 86 private static String lastLoginColumn = null; 87 88 89 private static String objectdataColumn = null; 90 91 92 private static PropertyDescriptor namePropDesc = null; 93 94 95 private static PropertyDescriptor idPropDesc = null; 96 97 98 private static PropertyDescriptor passwordPropDesc = null; 99 100 101 private static PropertyDescriptor firstNamePropDesc = null; 102 103 104 private static PropertyDescriptor lastNamePropDesc = null; 105 106 107 private static PropertyDescriptor emailPropDesc = null; 108 109 110 private static PropertyDescriptor confirmPropDesc = null; 111 112 113 private static PropertyDescriptor createDatePropDesc = null; 114 115 116 private static PropertyDescriptor lastLoginPropDesc = null; 117 118 119 private static PropertyDescriptor objectdataPropDesc = null; 120 121 122 static Log log = LogFactory.getLog(UserPeerManager.class); 123 124 133 134 public static void init(Configuration conf) 135 throws InitializationException 136 { 137 String userPeerClassName = conf.getString(USER_PEER_CLASS_KEY, 138 USER_PEER_CLASS_DEFAULT); 139 String userObjectName = null; 140 141 try 142 { 143 userPeerClass = Class.forName(userPeerClassName); 144 145 tableName = 146 (String ) userPeerClass.getField("TABLE_NAME").get(null); 147 148 153 155 userObject = getPersistenceClass(); 156 157 userObjectName = conf.getString(USER_CLASS_KEY, 158 userObject.getName()); 159 160 userObject = Class.forName(userObjectName); 162 163 167 168 nameColumn = (String ) userPeerClass.getField( 169 conf.getString(USER_NAME_COLUMN_KEY, 170 USER_NAME_COLUMN_DEFAULT) 171 ).get(null); 172 173 idColumn = (String ) userPeerClass.getField( 174 conf.getString(USER_ID_COLUMN_KEY, 175 USER_ID_COLUMN_DEFAULT) 176 ).get(null); 177 178 passwordColumn = (String ) userPeerClass.getField( 179 conf.getString(USER_PASSWORD_COLUMN_KEY, 180 USER_PASSWORD_COLUMN_DEFAULT) 181 ).get(null); 182 183 firstNameColumn = (String ) userPeerClass.getField( 184 conf.getString(USER_FIRST_NAME_COLUMN_KEY, 185 USER_FIRST_NAME_COLUMN_DEFAULT) 186 ).get(null); 187 188 lastNameColumn = (String ) userPeerClass.getField( 189 conf.getString(USER_LAST_NAME_COLUMN_KEY, 190 USER_LAST_NAME_COLUMN_DEFAULT) 191 ).get(null); 192 193 emailColumn = (String ) userPeerClass.getField( 194 conf.getString(USER_EMAIL_COLUMN_KEY, 195 USER_EMAIL_COLUMN_DEFAULT) 196 ).get(null); 197 198 confirmColumn = (String ) userPeerClass.getField( 199 conf.getString(USER_CONFIRM_COLUMN_KEY, 200 USER_CONFIRM_COLUMN_DEFAULT) 201 ).get(null); 202 203 createDateColumn = (String ) userPeerClass.getField( 204 conf.getString(USER_CREATE_COLUMN_KEY, 205 USER_CREATE_COLUMN_DEFAULT) 206 ).get(null); 207 208 lastLoginColumn = (String ) userPeerClass.getField( 209 conf.getString(USER_LAST_LOGIN_COLUMN_KEY, 210 USER_LAST_LOGIN_COLUMN_DEFAULT) 211 ).get(null); 212 213 objectdataColumn = (String ) userPeerClass.getField( 214 conf.getString(USER_OBJECTDATA_COLUMN_KEY, 215 USER_OBJECTDATA_COLUMN_DEFAULT) 216 ).get(null); 217 218 namePropDesc = 219 new PropertyDescriptor (conf.getString( 220 USER_NAME_PROPERTY_KEY, 221 USER_NAME_PROPERTY_DEFAULT), 222 userObject); 223 224 idPropDesc = 225 new PropertyDescriptor (conf.getString( 226 USER_ID_PROPERTY_KEY, 227 USER_ID_PROPERTY_DEFAULT), 228 userObject); 229 230 passwordPropDesc = 231 new PropertyDescriptor (conf.getString( 232 USER_PASSWORD_PROPERTY_KEY, 233 USER_PASSWORD_PROPERTY_DEFAULT), 234 userObject); 235 236 firstNamePropDesc = 237 new PropertyDescriptor (conf.getString( 238 USER_FIRST_NAME_PROPERTY_KEY, 239 USER_FIRST_NAME_PROPERTY_DEFAULT), 240 userObject); 241 242 lastNamePropDesc = 243 new PropertyDescriptor (conf.getString( 244 USER_LAST_NAME_PROPERTY_KEY, 245 USER_LAST_NAME_PROPERTY_DEFAULT), 246 userObject); 247 248 emailPropDesc = 249 new PropertyDescriptor (conf.getString( 250 USER_EMAIL_PROPERTY_KEY, 251 USER_EMAIL_PROPERTY_DEFAULT), 252 userObject); 253 254 confirmPropDesc = 255 new PropertyDescriptor (conf.getString( 256 USER_CONFIRM_PROPERTY_KEY, 257 USER_CONFIRM_PROPERTY_DEFAULT), 258 userObject); 259 260 createDatePropDesc = 261 new PropertyDescriptor (conf.getString( 262 USER_CREATE_PROPERTY_KEY, 263 USER_CREATE_PROPERTY_DEFAULT), 264 userObject); 265 266 lastLoginPropDesc = 267 new PropertyDescriptor (conf.getString( 268 USER_LAST_LOGIN_PROPERTY_KEY, 269 USER_LAST_LOGIN_PROPERTY_DEFAULT), 270 userObject); 271 272 objectdataPropDesc = 273 new PropertyDescriptor (conf.getString( 274 USER_OBJECTDATA_PROPERTY_KEY, 275 USER_OBJECTDATA_PROPERTY_DEFAULT), 276 userObject); 277 } 278 catch (Exception e) 279 { 280 if (userPeerClassName == null || userPeerClass == null) 281 { 282 throw new InitializationException( 283 "Could not find UserPeer class (" 284 + userPeerClassName + ")", e); 285 } 286 if (tableName == null) 287 { 288 throw new InitializationException( 289 "Failed to get the table name from the Peer object", e); 290 } 291 292 if (userObject == null || userObjectName == null) 293 { 294 throw new InitializationException( 295 "Failed to get the object type from the Peer object", e); 296 } 297 298 299 if (nameColumn == null || namePropDesc == null) 300 { 301 throw new InitializationException( 302 "UserPeer " + userPeerClassName 303 + " has no name column information!", e); 304 } 305 if (idColumn == null || idPropDesc == null) 306 { 307 throw new InitializationException( 308 "UserPeer " + userPeerClassName 309 + " has no id column information!", e); 310 } 311 if (passwordColumn == null || passwordPropDesc == null) 312 { 313 throw new InitializationException( 314 "UserPeer " + userPeerClassName 315 + " has no password column information!", e); 316 } 317 if (firstNameColumn == null || firstNamePropDesc == null) 318 { 319 throw new InitializationException( 320 "UserPeer " + userPeerClassName 321 + " has no firstName column information!", e); 322 } 323 if (lastNameColumn == null || lastNamePropDesc == null) 324 { 325 throw new InitializationException( 326 "UserPeer " + userPeerClassName 327 + " has no lastName column information!", e); 328 } 329 if (emailColumn == null || emailPropDesc == null) 330 { 331 throw new InitializationException( 332 "UserPeer " + userPeerClassName 333 + " has no email column information!", e); 334 } 335 if (confirmColumn == null || confirmPropDesc == null) 336 { 337 throw new InitializationException( 338 "UserPeer " + userPeerClassName 339 + " has no confirm column information!", e); 340 } 341 if (createDateColumn == null || createDatePropDesc == null) 342 { 343 throw new InitializationException( 344 "UserPeer " + userPeerClassName 345 + " has no createDate column information!", e); 346 } 347 if (lastLoginColumn == null || lastLoginPropDesc == null) 348 { 349 throw new InitializationException( 350 "UserPeer " + userPeerClassName 351 + " has no lastLogin column information!", e); 352 } 353 if (objectdataColumn == null || objectdataPropDesc == null) 354 { 355 throw new InitializationException( 356 "UserPeer " + userPeerClassName 357 + " has no objectdata column information!", e); 358 } 359 } 360 } 361 362 367 public static String getTableName() 368 { 369 return tableName; 370 } 371 372 378 public static String getNameColumn() 379 { 380 return nameColumn; 381 } 382 383 389 public static String getIdColumn() 390 { 391 return idColumn; 392 } 393 394 400 public static String getPasswordColumn() 401 { 402 return passwordColumn; 403 } 404 405 411 public static String getFirstNameColumn() 412 { 413 return firstNameColumn; 414 } 415 416 422 public static String getLastNameColumn() 423 { 424 return lastNameColumn; 425 } 426 427 433 public static String getEmailColumn() 434 { 435 return emailColumn; 436 } 437 438 444 public static String getConfirmColumn() 445 { 446 return confirmColumn; 447 } 448 449 455 public static String getCreateDateColumn() 456 { 457 return createDateColumn; 458 } 459 460 466 public static String getLastLoginColumn() 467 { 468 return lastLoginColumn; 469 } 470 471 477 public static String getObjectdataColumn() 478 { 479 return objectdataColumn; 480 } 481 482 489 public static String getColumnName(String name) 490 { 491 StringBuffer sb = new StringBuffer (); 492 sb.append(getTableName()); 493 sb.append("."); 494 sb.append(name); 495 return sb.toString(); 496 } 497 498 506 public String getFullColumnName(String name) 507 { 508 return getColumnName(name); 509 } 510 511 512 520 521 public static Persistent newPersistentInstance() 522 { 523 Persistent obj = null; 524 525 if(userObject == null) 526 { 527 return obj; 534 } 535 536 try 537 { 538 obj = (Persistent) userObject.newInstance(); 539 } 540 catch (Exception e) 541 { 542 log.error("Could not instantiate a user object", e); 543 obj = null; 544 } 545 return obj; 546 } 547 548 558 public static boolean checkExists(User user) 559 throws DataBackendException, Exception 560 { 561 Criteria criteria = new Criteria(); 562 563 criteria.addSelectColumn(getIdColumn()); 564 565 criteria.add(getNameColumn(), user.getName()); 566 567 List results = BasePeer.doSelect(criteria); 568 569 if (results.size() > 1) 570 { 571 throw new DataBackendException("Multiple users named '" + 572 user.getName() + "' exist!"); 573 } 574 return (results.size() == 1); 575 } 576 577 583 public static List selectAllUsers() 584 throws Exception 585 { 586 Criteria criteria = new Criteria(); 587 criteria.addAscendingOrderByColumn(getLastNameColumn()); 588 criteria.addAscendingOrderByColumn(getFirstNameColumn()); 589 criteria.setIgnoreCase(true); 590 return doSelect(criteria); 591 } 592 593 599 public static List selectAllConfirmedUsers() 600 throws Exception 601 { 602 Criteria criteria = new Criteria(); 603 604 criteria.add (getConfirmColumn(), User.CONFIRM_DATA); 605 criteria.addAscendingOrderByColumn(getLastNameColumn()); 606 criteria.addAscendingOrderByColumn(getFirstNameColumn()); 607 criteria.setIgnoreCase(true); 608 return doSelect(criteria); 609 } 610 611 621 622 632 633 public static Criteria buildCriteria(User user) 634 { 635 Criteria crit; 636 637 try 638 { 639 Class [] clazz = new Class [] { userObject }; 640 Object [] params = 641 new Object [] { ((TorqueUser) user).getPersistentObj() }; 642 643 crit = (Criteria) userPeerClass 644 .getMethod("buildCriteria", clazz) 645 .invoke(null, params); 646 } 647 catch (Exception e) 648 { 649 crit = null; 650 } 651 652 return crit; 653 } 654 655 662 663 public static void doUpdate(Criteria criteria) 664 throws TorqueException 665 { 666 try 667 { 668 Class [] clazz = new Class [] { Criteria.class }; 669 Object [] params = new Object [] { criteria }; 670 671 userPeerClass 672 .getMethod("doUpdate", clazz) 673 .invoke(null, params); 674 } 675 catch (Exception e) 676 { 677 throw new TorqueException("doUpdate failed", e); 678 } 679 } 680 681 688 689 public static void doInsert(Criteria criteria) 690 throws TorqueException 691 { 692 try 693 { 694 Class [] clazz = new Class [] { Criteria.class }; 695 Object [] params = new Object [] { criteria }; 696 697 userPeerClass 698 .getMethod("doInsert", clazz) 699 .invoke(null, params); 700 } 701 catch (Exception e) 702 { 703 throw new TorqueException("doInsert failed", e); 704 } 705 } 706 707 716 public static List doSelect(Criteria criteria) 717 throws TorqueException 718 { 719 List list; 720 721 try 722 { 723 Class [] clazz = 724 new Class [] { Criteria.class }; 725 Object [] params = new Object [] { criteria }; 726 727 list = (List ) userPeerClass 728 .getMethod("doSelect", clazz) 729 .invoke(null, params); 730 } 731 catch (Exception e) 732 { 733 throw new TorqueException("doSelect failed", e); 734 } 735 List newList = new ArrayList (list.size()); 736 737 for (Iterator it = list.iterator(); it.hasNext(); ) 741 { 742 User u = getNewUser((Persistent) it.next()); 743 newList.add(u); 744 } 745 746 return newList; 747 } 748 749 756 public static void doDelete(Criteria criteria) 757 throws TorqueException 758 { 759 try 760 { 761 Class [] clazz = new Class [] { Criteria.class }; 762 Object [] params = new Object [] { criteria }; 763 764 userPeerClass 765 .getMethod("doDelete", clazz) 766 .invoke(null, params); 767 } 768 catch (Exception e) 769 { 770 throw new TorqueException("doDelete failed", e); 771 } 772 } 773 774 780 public static void setUserName(Persistent obj, String name) 781 { 782 if(obj == null) 783 { 784 return; 785 } 786 787 try 788 { 789 Object [] params = new Object [] { name }; 790 namePropDesc.getWriteMethod().invoke(obj, params); 791 } 792 catch (ClassCastException cce) 793 { 794 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 795 log.error(msg); 796 throw new RuntimeException (msg); 797 } 798 catch (Exception e) 799 { 800 log.error(e, e); 801 } 802 } 803 804 813 public static String getUserName(Persistent obj) 814 { 815 return getName(obj); 816 } 817 818 825 public static String getName(Persistent obj) 826 { 827 String name = null; 828 829 if(obj == null) 830 { 831 return null; 832 } 833 834 try 835 { 836 name = (String ) namePropDesc 837 .getReadMethod() 838 .invoke(obj, new Object [] {}); 839 } 840 catch (ClassCastException cce) 841 { 842 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 843 log.error(msg); 844 throw new RuntimeException (msg); 845 } 846 catch (Exception e) 847 { 848 log.error(e, e); 849 } 850 return name; 851 } 852 853 859 public static void setUserPassword(Persistent obj, String password) 860 { 861 if(obj == null) 862 { 863 return; 864 } 865 866 try 867 { 868 Object [] params = new Object [] { password }; 869 passwordPropDesc.getWriteMethod().invoke(obj, params); 870 } 871 catch (ClassCastException cce) 872 { 873 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 874 log.error(msg); 875 throw new RuntimeException (msg); 876 } 877 catch (Exception e) 878 { 879 log.error(e, e); 880 } 881 } 882 883 890 public static String getUserPassword(Persistent obj) 891 { 892 String password = null; 893 894 if(obj == null) 895 { 896 return null; 897 } 898 899 try 900 { 901 password = (String ) passwordPropDesc 902 .getReadMethod() 903 .invoke(obj, new Object [] {}); 904 } 905 catch (ClassCastException cce) 906 { 907 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 908 log.error(msg); 909 throw new RuntimeException (msg); 910 } 911 catch (Exception e) 912 { 913 log.error(e, e); 914 } 915 return password; 916 } 917 918 924 public static void setUserFirstName(Persistent obj, String firstName) 925 { 926 if(obj == null) 927 { 928 return; 929 } 930 931 try 932 { 933 Object [] params = new Object [] { firstName }; 934 firstNamePropDesc.getWriteMethod().invoke(obj, params); 935 } 936 catch (ClassCastException cce) 937 { 938 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 939 log.error(msg); 940 throw new RuntimeException (msg); 941 } 942 catch (Exception e) 943 { 944 log.error(e, e); 945 } 946 } 947 948 955 public static String getUserFirstName(Persistent obj) 956 { 957 String firstName = null; 958 959 if(obj == null) 960 { 961 return null; 962 } 963 964 try 965 { 966 firstName = (String ) firstNamePropDesc 967 .getReadMethod() 968 .invoke(obj, new Object [] {}); 969 } 970 catch (ClassCastException cce) 971 { 972 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 973 log.error(msg); 974 throw new RuntimeException (msg); 975 } 976 catch (Exception e) 977 { 978 log.error(e, e); 979 } 980 return firstName; 981 } 982 983 989 public static void setUserLastName(Persistent obj, String lastName) 990 { 991 if(obj == null) 992 { 993 return; 994 } 995 996 try 997 { 998 Object [] params = new Object [] { lastName }; 999 lastNamePropDesc.getWriteMethod().invoke(obj, params); 1000 } 1001 catch (ClassCastException cce) 1002 { 1003 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1004 log.error(msg); 1005 throw new RuntimeException (msg); 1006 } 1007 catch (Exception e) 1008 { 1009 log.error(e, e); 1010 } 1011 } 1012 1013 1020 public static String getUserLastName(Persistent obj) 1021 { 1022 String lastName = null; 1023 1024 if(obj == null) 1025 { 1026 return null; 1027 } 1028 1029 try 1030 { 1031 lastName = (String ) lastNamePropDesc 1032 .getReadMethod() 1033 .invoke(obj, new Object [] {}); 1034 } 1035 catch (ClassCastException cce) 1036 { 1037 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1038 log.error(msg); 1039 throw new RuntimeException (msg); 1040 } 1041 catch (Exception e) 1042 { 1043 log.error(e, e); 1044 } 1045 return lastName; 1046 } 1047 1048 1054 public static void setUserEmail(Persistent obj, String email) 1055 { 1056 if(obj == null) 1057 { 1058 return; 1059 } 1060 1061 try 1062 { 1063 Object [] params = new Object [] { email }; 1064 emailPropDesc.getWriteMethod().invoke(obj, params); 1065 } 1066 catch (ClassCastException cce) 1067 { 1068 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1069 log.error(msg); 1070 throw new RuntimeException (msg); 1071 } 1072 catch (Exception e) 1073 { 1074 log.error(e, e); 1075 } 1076 } 1077 1078 1085 public static String getUserEmail(Persistent obj) 1086 { 1087 String email = null; 1088 1089 if(obj == null) 1090 { 1091 return null; 1092 } 1093 1094 try 1095 { 1096 email = (String ) emailPropDesc 1097 .getReadMethod() 1098 .invoke(obj, new Object [] {}); 1099 } 1100 catch (ClassCastException cce) 1101 { 1102 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1103 log.error(msg); 1104 throw new RuntimeException (msg); 1105 } 1106 catch (Exception e) 1107 { 1108 log.error(e, e); 1109 } 1110 return email; 1111 } 1112 1113 1119 public static void setUserConfirmed(Persistent obj, String confirm) 1120 { 1121 if(obj == null) 1122 { 1123 return; 1124 } 1125 1126 try 1127 { 1128 Object [] params = new Object [] { confirm }; 1129 confirmPropDesc.getWriteMethod().invoke(obj, params); 1130 } 1131 catch (ClassCastException cce) 1132 { 1133 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1134 log.error(msg); 1135 throw new RuntimeException (msg); 1136 } 1137 catch (Exception e) 1138 { 1139 log.error(e, e); 1140 } 1141 } 1142 1143 1150 public static String getUserConfirmed(Persistent obj) 1151 { 1152 String confirm = null; 1153 1154 if(obj == null) 1155 { 1156 return null; 1157 } 1158 1159 try 1160 { 1161 confirm = (String ) confirmPropDesc 1162 .getReadMethod() 1163 .invoke(obj, new Object [] {}); 1164 } 1165 catch (ClassCastException cce) 1166 { 1167 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1168 log.error(msg); 1169 throw new RuntimeException (msg); 1170 } 1171 catch (Exception e) 1172 { 1173 log.error(e, e); 1174 } 1175 return confirm; 1176 } 1177 1178 1184 public static void setUserCreateDate(Persistent obj, java.util.Date createDate) 1185 { 1186 if(obj == null) 1187 { 1188 return; 1189 } 1190 1191 try 1192 { 1193 Object [] params = new Object [] { createDate }; 1194 createDatePropDesc.getWriteMethod().invoke(obj, params); 1195 } 1196 catch (ClassCastException cce) 1197 { 1198 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1199 log.error(msg); 1200 throw new RuntimeException (msg); 1201 } 1202 catch (Exception e) 1203 { 1204 log.error(e, e); 1205 } 1206 } 1207 1208 1215 public static java.util.Date getUserCreateDate(Persistent obj) 1216 { 1217 java.util.Date createDate = null; 1218 1219 if(obj == null) 1220 { 1221 return null; 1222 } 1223 1224 try 1225 { 1226 createDate = (java.util.Date ) createDatePropDesc 1227 .getReadMethod() 1228 .invoke(obj, new Object [] {}); 1229 } 1230 catch (ClassCastException cce) 1231 { 1232 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1233 log.error(msg); 1234 throw new RuntimeException (msg); 1235 } 1236 catch (Exception e) 1237 { 1238 log.error(e, e); 1239 } 1240 return createDate; 1241 } 1242 1243 1249 public static void setUserLastLogin(Persistent obj, java.util.Date lastLogin) 1250 { 1251 if(obj == null) 1252 { 1253 return; 1254 } 1255 1256 try 1257 { 1258 Object [] params = new Object [] { lastLogin }; 1259 lastLoginPropDesc.getWriteMethod().invoke(obj, params); 1260 } 1261 catch (ClassCastException cce) 1262 { 1263 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1264 log.error(msg); 1265 throw new RuntimeException (msg); 1266 } 1267 catch (Exception e) 1268 { 1269 log.error(e, e); 1270 } 1271 } 1272 1273 1280 public static java.util.Date getUserLastLogin(Persistent obj) 1281 { 1282 java.util.Date lastLogin = null; 1283 1284 if(obj == null) 1285 { 1286 return null; 1287 } 1288 1289 try 1290 { 1291 lastLogin = (java.util.Date ) lastLoginPropDesc 1292 .getReadMethod() 1293 .invoke(obj, new Object [] {}); 1294 } 1295 catch (ClassCastException cce) 1296 { 1297 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1298 log.error(msg); 1299 throw new RuntimeException (msg); 1300 } 1301 catch (Exception e) 1302 { 1303 log.error(e, e); 1304 } 1305 return lastLogin; 1306 } 1307 1308 1314 public static void setUserObjectdata(Persistent obj, byte [] objectdata) 1315 { 1316 if(obj == null) 1317 { 1318 return; 1319 } 1320 1321 try 1322 { 1323 Object [] params = new Object [] { objectdata }; 1324 objectdataPropDesc.getWriteMethod().invoke(obj, params); 1325 } 1326 catch (ClassCastException cce) 1327 { 1328 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1329 log.error(msg); 1330 throw new RuntimeException (msg); 1331 } 1332 catch (Exception e) 1333 { 1334 log.error(e, e); 1335 } 1336 } 1337 1338 1345 public static byte [] getUserObjectdata(Persistent obj) 1346 { 1347 byte [] objectdata = null; 1348 1349 if(obj == null) 1350 { 1351 return null; 1352 } 1353 1354 try 1355 { 1356 objectdata = (byte []) objectdataPropDesc 1357 .getReadMethod() 1358 .invoke(obj, new Object [] {}); 1359 } 1360 catch (ClassCastException cce) 1361 { 1362 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1363 log.error(msg); 1364 throw new RuntimeException (msg); 1365 } 1366 catch (Exception e) 1367 { 1368 log.error(e, e); 1369 } 1370 return objectdata; 1371 } 1372 1373 1379 public static void setId(Persistent obj, int id) 1380 { 1381 if(obj == null) 1382 { 1383 return; 1384 } 1385 1386 try 1387 { 1388 Object [] params = new Object [] { Integer.TYPE }; 1389 idPropDesc.getWriteMethod().invoke(obj, params); 1390 } 1391 catch (ClassCastException cce) 1392 { 1393 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1394 log.error(msg); 1395 throw new RuntimeException (msg); 1396 } 1397 catch (Exception e) 1398 { 1399 log.error(e, e); 1400 } 1401 } 1402 1403 1410 public static Integer getIdAsObj(Persistent obj) 1411 { 1412 Integer id = null; 1413 1414 if(obj == null) 1415 { 1416 return new Integer (0); 1417 } 1418 1419 try 1420 { 1421 id = (Integer ) idPropDesc 1422 .getReadMethod() 1423 .invoke(obj, new Object [] {}); 1424 } 1425 catch (ClassCastException cce) 1426 { 1427 String msg = obj.getClass().getName() + " does not seem to be an User Object!"; 1428 log.error(msg); 1429 throw new RuntimeException (msg); 1430 } 1431 catch (Exception e) 1432 { 1433 log.error(e, e); 1434 } 1435 return id; 1436 } 1437 1438 1445 1446 private static Class getPersistenceClass() 1447 { 1448 Class persistenceClass = null; 1449 1450 try 1451 { 1452 Object [] params = new Object [0]; 1453 1454 persistenceClass = (Class ) userPeerClass 1455 .getMethod("getOMClass", null) 1456 .invoke(null, params); 1457 } 1458 catch (Exception e) 1459 { 1460 persistenceClass = null; 1461 } 1462 1463 return persistenceClass; 1464 } 1465 1466 1477 1478 public static User getNewUser(Persistent p) 1479 { 1480 User u = null; 1481 try 1482 { 1483 Class userWrapperClass = TurbineSecurity.getUserClass(); 1484 1485 Class [] clazz = new Class [] { Persistent.class }; 1486 Object [] params = new Object [] { p }; 1487 1488 u = (User) userWrapperClass 1489 .getConstructor(clazz) 1490 .newInstance(params); 1491 } 1492 catch (Exception e) 1493 { 1494 log.error("Could not instantiate a new user from supplied persistent: ", e); 1495 } 1496 1497 return u; 1498 } 1499} 1500 1501 1502 | Popular Tags |