1 package org.apache.turbine.services.security.torque; 2 3 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.PrintWriter ; 21 22 import java.sql.Connection ; 23 24 import java.util.Date ; 25 import java.util.Hashtable ; 26 27 import javax.servlet.http.HttpSessionBindingEvent ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.torque.om.Persistent; 32 33 import org.apache.turbine.om.security.User; 34 import org.apache.turbine.services.security.TurbineSecurity; 35 import org.apache.turbine.util.ObjectUtils; 36 import org.apache.turbine.util.security.TurbineSecurityException; 37 38 52 53 public class TorqueUser 54 extends TorqueObject 55 implements User, 56 Persistent 57 { 58 private static Log log = LogFactory.getLog(TorqueUser.class); 59 60 61 private Date lastAccessDate = null; 62 63 64 private Hashtable permStorage = null; 65 66 67 private Hashtable tempStorage = null; 68 69 73 public TorqueUser() 74 { 75 super(); 76 setCreateDate(new Date ()); 77 tempStorage = new Hashtable (10); 78 setHasLoggedIn(Boolean.FALSE); 79 } 80 81 88 public TorqueUser(Persistent obj) 89 { 90 super(obj); 91 92 95 tempStorage = new Hashtable (10); 96 setHasLoggedIn(Boolean.FALSE); 97 } 98 99 107 108 public Persistent getPersistentObj() 109 { 110 if (obj == null) 111 { 112 obj = UserPeerManager.newPersistentInstance(); 113 } 114 return obj; 115 } 116 117 125 public void save(String torqueName) 126 throws Exception 127 { 128 setObjectdata(ObjectUtils.serializeHashtable(getPermStorage())); 129 super.save(torqueName); 130 } 131 132 143 public void save(Connection con) 144 throws Exception 145 { 146 setObjectdata(ObjectUtils.serializeHashtable(getPermStorage())); 147 super.save(con); 148 } 149 150 156 public void save() 157 throws TurbineSecurityException 158 { 159 169 try 170 { 171 setObjectdata(ObjectUtils.serializeHashtable(getPermStorage())); 172 getPersistentObj().save(); 173 } 174 catch (Exception e) 175 { 176 throw new TurbineSecurityException("User object said " 177 + e.getMessage(), e); 178 } 179 } 180 181 186 public String getName() 187 { 188 return UserPeerManager.getName(getPersistentObj()); 189 } 190 191 196 public void setName(String name) 197 { 198 setUserName(name); 199 } 200 201 206 public int getId() 207 { 208 return UserPeerManager.getIdAsObj(getPersistentObj()).intValue(); 209 } 210 211 216 public Integer getIdAsObj() 217 { 218 return UserPeerManager.getIdAsObj(getPersistentObj()); 219 } 220 221 226 public void setId(int id) 227 { 228 UserPeerManager.setId(getPersistentObj(), id); 229 } 230 231 237 public String getUserName() 238 { 239 return getName(); 240 } 241 242 247 public void setUserName(String name) 248 { 249 UserPeerManager.setUserName(getPersistentObj(), name); 250 } 251 252 257 public String getPassword() 258 { 259 return UserPeerManager.getUserPassword(getPersistentObj()); 260 } 261 262 267 public void setPassword(String password) 268 { 269 UserPeerManager.setUserPassword(getPersistentObj(), password); 270 } 271 272 277 public String getFirstName() 278 { 279 return UserPeerManager.getUserFirstName(getPersistentObj()); 280 } 281 282 287 public void setFirstName(String firstName) 288 { 289 UserPeerManager.setUserFirstName(getPersistentObj(), firstName); 290 } 291 292 297 public String getLastName() 298 { 299 return UserPeerManager.getUserLastName(getPersistentObj()); 300 } 301 302 307 public void setLastName(String lastName) 308 { 309 UserPeerManager.setUserLastName(getPersistentObj(), lastName); 310 } 311 312 317 public String getEmail() 318 { 319 return UserPeerManager.getUserEmail(getPersistentObj()); 320 } 321 322 327 public void setEmail(String email) 328 { 329 UserPeerManager.setUserEmail(getPersistentObj(), email); 330 } 331 332 337 public String getConfirmed() 338 { 339 return UserPeerManager.getUserConfirmed(getPersistentObj()); 340 } 341 342 347 public void setConfirmed(String confirm) 348 { 349 UserPeerManager.setUserConfirmed(getPersistentObj(), confirm); 350 } 351 352 357 public java.util.Date getCreateDate() 358 { 359 return UserPeerManager.getUserCreateDate(getPersistentObj()); 360 } 361 362 367 public void setCreateDate(java.util.Date createDate) 368 { 369 UserPeerManager.setUserCreateDate(getPersistentObj(), createDate); 370 } 371 372 377 public java.util.Date getLastLogin() 378 { 379 return UserPeerManager.getUserLastLogin(getPersistentObj()); 380 } 381 382 387 public void setLastLogin(java.util.Date lastLogin) 388 { 389 UserPeerManager.setUserLastLogin(getPersistentObj(), lastLogin); 390 } 391 392 400 public byte [] getObjectdata() 401 { 402 return UserPeerManager.getUserObjectdata(getPersistentObj()); 403 } 404 405 410 public void setObjectdata(byte [] objectdata) 411 { 412 UserPeerManager.setUserObjectdata(getPersistentObj(), objectdata); 413 } 414 415 416 421 public int getAccessCounter() 422 { 423 try 424 { 425 return ((Integer ) getPerm(User.ACCESS_COUNTER)).intValue(); 426 } 427 catch (Exception e) 428 { 429 return 0; 430 } 431 } 432 433 438 public int getAccessCounterForSession() 439 { 440 try 441 { 442 return ((Integer ) getTemp(User.SESSION_ACCESS_COUNTER)).intValue(); 443 } 444 catch (Exception e) 445 { 446 return 0; 447 } 448 } 449 450 453 public void incrementAccessCounter() 454 { 455 setAccessCounter(getAccessCounter() + 1); 457 } 458 459 462 public void incrementAccessCounterForSession() 463 { 464 setAccessCounterForSession(getAccessCounterForSession() + 1); 465 } 466 467 472 public void setAccessCounter(int cnt) 473 { 474 setPerm(User.ACCESS_COUNTER, new Integer (cnt)); 475 } 476 477 483 public void setAccessCounterForSession(int cnt) 484 { 485 setTemp(User.SESSION_ACCESS_COUNTER, new Integer (cnt)); 486 } 487 488 496 public boolean isConfirmed() 497 { 498 String value = getConfirmed(); 499 return (value != null && value.equals(User.CONFIRM_DATA)); 500 } 501 502 507 public boolean hasLoggedIn() 508 { 509 Boolean loggedIn = getHasLoggedIn(); 510 return (loggedIn != null && loggedIn.booleanValue()); 511 } 512 513 519 public void setHasLoggedIn(Boolean value) 520 { 521 setTemp(User.HAS_LOGGED_IN, value); 522 } 523 524 530 public java.util.Date getLastAccessDate() 531 { 532 if (lastAccessDate == null) 533 { 534 setLastAccessDate(); 535 } 536 return lastAccessDate; 537 } 538 539 543 public void setLastAccessDate() 544 { 545 lastAccessDate = new java.util.Date (); 546 } 547 548 555 public Hashtable getPermStorage() 556 { 557 if (permStorage == null) 558 { 559 byte [] objectdata = getObjectdata(); 560 561 if (objectdata != null) 562 { 563 permStorage = (Hashtable ) ObjectUtils.deserialize(objectdata); 564 } 565 566 if (permStorage == null) 567 { 568 permStorage = new Hashtable (); 569 } 570 } 571 572 return permStorage; 573 } 574 575 581 public void setPermStorage(Hashtable permStorage) 582 { 583 if (permStorage != null) 584 { 585 this.permStorage = permStorage; 586 } 587 } 588 589 595 public Hashtable getTempStorage() 596 { 597 if (tempStorage == null) 598 { 599 tempStorage = new Hashtable (); 600 } 601 return tempStorage; 602 } 603 604 610 public void setTempStorage(Hashtable tempStorage) 611 { 612 if (tempStorage != null) 613 { 614 this.tempStorage = tempStorage; 615 } 616 } 617 618 624 public Object getPerm(String name) 625 { 626 return getPermStorage().get(name); 627 } 628 629 637 public Object getPerm(String name, Object def) 638 { 639 try 640 { 641 Object val = getPermStorage().get(name); 642 return (val == null ? def : val); 643 } 644 catch (Exception e) 645 { 646 return def; 647 } 648 } 649 650 659 public void setPerm(String name, Object value) 660 { 661 getPermStorage().put(name, (value == null) ? "" : value); 662 } 663 664 670 public Object getTemp(String name) 671 { 672 return getTempStorage().get(name); 673 } 674 675 683 public Object getTemp(String name, Object def) 684 { 685 Object val; 686 try 687 { 688 val = getTempStorage().get(name); 689 if (val == null) 690 { 691 val = def; 692 } 693 } 694 catch (Exception e) 695 { 696 val = def; 697 } 698 return val; 699 } 700 701 710 public void setTemp(String name, Object value) 711 { 712 getTempStorage().put(name, (value == null) ? "" : value); 713 } 714 715 721 public Object removeTemp(String name) 722 { 723 return getTempStorage().remove(name); 724 } 725 726 731 public void updateLastLogin() 732 throws Exception 733 { 734 setLastLogin(new java.util.Date ()); 735 } 736 737 743 public void valueBound(HttpSessionBindingEvent hsbe) 744 { 745 } 747 748 754 public void valueUnbound(HttpSessionBindingEvent hsbe) 755 { 756 try 757 { 758 if (hasLoggedIn()) 759 { 760 TurbineSecurity.saveOnSessionUnbind(this); 761 } 762 } 763 catch (Exception e) 764 { 765 767 ByteArrayOutputStream ostr = new ByteArrayOutputStream (); 771 e.printStackTrace(new PrintWriter (ostr, true)); 772 String stackTrace = ostr.toString(); 773 System.out.println(stackTrace); 774 } 775 } 776 777 784 private Boolean getHasLoggedIn() 785 { 786 return (Boolean ) getTemp(User.HAS_LOGGED_IN); 787 } 788 789 } 790 | Popular Tags |