001: package desktop;
002:
003: import javax.servlet.http.Cookie;
004: import javax.servlet.http.HttpServletRequest;
005: import java.util.prefs.Preferences;
006:
007: public class PreferenceHandler {
008:
009: private static PreferenceHandler handler;
010: private static String COOKIE_NAME = "PreferencesCookie";
011: private Preferences pref;
012: private static String PANE_SUFFIX = "desktoppanes";
013: private static String ITEM_SUFFIX = "desktopitems";
014: private static Integer userID = 0;
015: private static String NEXT_FREE_USER_ID = "nextFreeUserID";
016: private static Preferences systemPref;
017:
018: private PreferenceHandler() {
019: systemPref = Preferences.systemRoot();
020: System.out.println(systemPref.getInt(NEXT_FREE_USER_ID, 0));
021: System.out.println();
022: //pref= Preferences.userRoot().node("temp");
023: //systemPref = Preferences.systemNodeForPackage(this.getClass());
024: /*
025: File configFile = new File("desktop.xml");
026:
027: if(configFile.exists()){
028: try{
029: System.out.println(configFile.getAbsolutePath());
030: Preferences.importPreferences(new FileInputStream(configFile));
031: String[] keys = systemPref.keys();
032:
033: userID = systemPref.getInt(NEXT_FREE_USER_ID, 0);
034: System.out.println("UserID: "+userID);
035:
036: }catch(Exception ex){ex.printStackTrace();}
037: }
038:
039: //pref = Preferences.userRoot().node("temp");
040:
041: */
042: }
043:
044: public boolean returningUser() {
045: HttpServletRequest request = org.wings.session.SessionManager
046: .getSession().getServletRequest();
047: boolean isReturning = false;
048:
049: if (request.getUserPrincipal() != null
050: && request.getUserPrincipal().getName() != null) {
051: String userName = request.getUserPrincipal().getName();
052: pref = Preferences.userRoot().node(userName);
053:
054: } else {
055: Cookie[] cookies = request.getCookies();
056: for (int i = 0; i < cookies.length; i++) {
057: if (cookies[i].getName().equals(COOKIE_NAME)) {
058: pref = Preferences.userRoot().node(
059: cookies[i].getValue());
060:
061: isReturning = true;
062: break;
063: }
064: }
065:
066: if (!isReturning) {
067: pref = Preferences.userRoot().node(userID.toString());
068: Cookie cookie = new Cookie(COOKIE_NAME,
069: ((Integer) systemPref.getInt(NEXT_FREE_USER_ID,
070: 0)).toString());
071:
072: cookie.setMaxAge(1000000000);
073: org.wings.session.SessionManager.getSession()
074: .getServletResponse().addCookie(cookie);
075: userID++;
076: systemPref.putInt(NEXT_FREE_USER_ID, userID);
077:
078: try {
079: systemPref.flush();
080: //OutputStream os = new BufferedOutputStream(new FileOutputStream("desktop.xml"));
081: //systemPref.exportNode(os);
082: } catch (Exception ex) {
083: ex.printStackTrace();
084: }
085:
086: }
087: }
088:
089: return isReturning;
090: }
091:
092: public static PreferenceHandler getPreferenceHandler() {
093: if (handler == null)
094: handler = new PreferenceHandler();
095:
096: return handler;
097: }
098:
099: public Preferences getUserRootPreference() {
100: return pref;
101: }
102:
103: public Preferences getPanePreferences() {
104: return pref.node(PANE_SUFFIX);
105: }
106:
107: public Preferences getItemPreferences() {
108: return pref.node(ITEM_SUFFIX);
109: }
110:
111: }
|