?? user.java
字號:
package push;import members.Members;import java.io.*;import java.util.Hashtable;import java.util.Vector;import java.util.Enumeration;import javax.servlet.http.*;/** * Class User is a used to store push information for users logged in, i.e * having an active session * * @version 1.0 * @since 1.0 * @see HttpSessionBindingListener */public final class User implements HttpSessionBindingListener { /* * vector of users who have currently a valid session (with push enabled WAP * device). Whenever the session becomes invalidated, the user object is * removed from the vector. */ private static final Vector usersOnline = new Vector(); private HttpSession session = null; private final String name; private final String address; private final Hashtable properties; /** * Creates a new <code>User</code> instance. * * @param name * a <code>String</code> value * @param address * a <code>String</code> value */ public User(String name, String address) { this.name = name; this.address = address; properties = new Hashtable(); } /** * Returns user name * * @return a <code>String</code> value */ public String getName() { return (String) session.getAttribute("name"); } /** * Returns date of birth * * @return a <code>String</code> value */ public String getDob() { return (String) session.getAttribute("dob"); } /** * Returns user selected push services * * @return a <code>String</code> value */ public String getPushServices() { return (String) session.getAttribute("pushservices"); } /** * Returns the user's (client) address * * @return a <code>String</code> value */ public String getAddress() { return address; } /** * Returns the user's (client) address. The address is of given type. * * @param addressType * a <code>String</code> value (IPv4 or PLMN) * @return a <code>String</code> value */ public String getAddress(String addressType) { if (addressType.equals("IPv4")) return address; else if (addressType.equals("PLMN")) return Members.getPhoneNumber(getName()); return null; } /** * Set a property with the user object * * @param name * a <code>String</code> value * @param value * a <code>String</code> value */ public void setProperty(String name, Object value) { properties.put(name, value); } /** * Finds property object by given property name * * @param name * a <code>String</code> value * @return an <code>Object</code> value */ public Object getProperty(String name) { return properties.get(name); } /** * Returns users currently logged in * * @return a <code>Vector</code> value */ public static Vector getUsersOnline() { return usersOnline; } /** * Finds user on the basis of given name * * @param name * a <code>String</code> value * @return an <code>User</code> value * @exception Exception * if an error occurs */ public static User findUser(String name) throws Exception { synchronized (usersOnline) { for (Enumeration e = usersOnline.elements(); e.hasMoreElements();) { User user = (User) e.nextElement(); if (user.getName().equals(name)) return user; } } return null; } /** * Notifies the User object that it is being bound to a session and * identifies the session. * * @param event * a <code>HttpSessionBindingEvent</code> value * @see HttpSessionBindingListener */ public void valueBound(HttpSessionBindingEvent event) { session = event.getSession(); synchronized (usersOnline) { usersOnline.add(this); } } /** * Notifies the User object that it is being unbound from a session and * identifies the session. * * @param event * a <code>HttpSessionBindingEvent</code> value * @see HttpSessionBindingListener */ public void valueUnbound(HttpSessionBindingEvent event) { synchronized (usersOnline) { usersOnline.remove(this); System.out .println("Session invalidated (max inactive time): user = " + name); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -