Tomas Jelinek has uploaded a new change for review.

Change subject: userportal,webadmin: extract the ClientStorage interface
......................................................................

userportal,webadmin: extract the ClientStorage interface

This patch makes the ClientStorage to be an interface (e.g. simply mockable)
and bound to a ClientStorageImpl class in the BaseSystemModule.

Change-Id: If5eaa942181c9df77278e736ddd6ac6b08e83fc7
Signed-off-by: Tomas Jelinek <[email protected]>
---
M 
frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/gin/BaseSystemModule.java
M 
frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorage.java
A 
frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorageImpl.java
3 files changed, 108 insertions(+), 57 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/38/26238/1

diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/gin/BaseSystemModule.java
 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/gin/BaseSystemModule.java
index a37a081..dc22230 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/gin/BaseSystemModule.java
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/gin/BaseSystemModule.java
@@ -9,6 +9,7 @@
 import org.ovirt.engine.ui.common.system.ApplicationFocusManager;
 import org.ovirt.engine.ui.common.system.AsyncCallFailureHandler;
 import org.ovirt.engine.ui.common.system.ClientStorage;
+import org.ovirt.engine.ui.common.system.ClientStorageImpl;
 import org.ovirt.engine.ui.common.system.ErrorPopupManagerImpl;
 import org.ovirt.engine.ui.common.system.LockInteractionManager;
 import org.ovirt.engine.ui.common.uicommon.ClientAgentType;
@@ -47,7 +48,7 @@
         
bind(ErrorPopupManager.class).to(ErrorPopupManagerImpl.class).in(Singleton.class);
         bind(AsyncCallFailureHandler.class).asEagerSingleton();
         bind(ClientAgentType.class).in(Singleton.class);
-        bind(ClientStorage.class).in(Singleton.class);
+        
bind(ClientStorage.class).to(ClientStorageImpl.class).in(Singleton.class);
         bind(ApplicationFocusManager.class).asEagerSingleton();
         bind(LockInteractionManager.class).asEagerSingleton();
     }
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorage.java
 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorage.java
index fe65b14..0ff2ebe 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorage.java
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorage.java
@@ -1,10 +1,5 @@
 package org.ovirt.engine.ui.common.system;
 
-import java.util.Date;
-
-import com.google.gwt.storage.client.Storage;
-import com.google.gwt.user.client.Cookies;
-
 /**
  * Provides client-side key-value storage service.
  * <p>
@@ -18,82 +13,38 @@
  * <li>HTML5 session storage is transient and accessible only to one browser 
window/tab
  * </ul>
  */
-public class ClientStorage {
-
-    // Fifty years
-    private static final long PERSISTENT_COOKIE_EXPIRATION = 1000 * 60 * 60 * 
24 * 365 * 50;
-
-    private static final Storage localStorage = 
Storage.getLocalStorageIfSupported();
-    private static final Storage sessionStorage = 
Storage.getSessionStorageIfSupported();
+public interface ClientStorage {
 
     /**
      * Returns the value for the given key from local (persistent) storage, or 
{@code null} if there is no value for
      * such key.
      */
-    public String getLocalItem(String key) {
-        if (localStorage != null) {
-            return localStorage.getItem(key);
-        } else {
-            return Cookies.getCookie(key);
-        }
-    }
+    String getLocalItem(String key);
 
     /**
      * Sets the value for the given key using local (persistent) storage.
      */
-    public void setLocalItem(String key, String value) {
-        if (localStorage != null) {
-            localStorage.setItem(key, value);
-        } else {
-            // Emulate persistent storage using cookies which have predefined 
expiration date
-            Cookies.setCookie(key, value, new Date(new Date().getTime() + 
PERSISTENT_COOKIE_EXPIRATION));
-        }
-    }
+    void setLocalItem(String key, String value);
 
     /**
      * Removes the value associated with the given key from local (persistent) 
storage.
      */
-    public void removeLocalItem(String key) {
-        if (localStorage != null) {
-            localStorage.removeItem(key);
-        } else {
-            Cookies.removeCookie(key);
-        }
-    }
+    void removeLocalItem(String key);
 
     /**
      * Returns the value for the given key from session (transient) storage, 
or {@code null} if there is no value for
      * such key.
      */
-    public String getSessionItem(String key) {
-        if (sessionStorage != null) {
-            return sessionStorage.getItem(key);
-        } else {
-            return Cookies.getCookie(key);
-        }
-    }
+    String getSessionItem(String key);
 
     /**
      * Sets the value for the given key using session (transient) storage.
      */
-    public void setSessionItem(String key, String value) {
-        if (sessionStorage != null) {
-            sessionStorage.setItem(key, value);
-        } else {
-            // Emulate transient storage using cookies which expire when the 
browser session ends
-            Cookies.setCookie(key, value);
-        }
-    }
+    void setSessionItem(String key, String value);
 
     /**
      * Removes the value associated with the given key from session 
(transient) storage.
      */
-    public void removeSessionItem(String key) {
-        if (sessionStorage != null) {
-            sessionStorage.removeItem(key);
-        } else {
-            Cookies.removeCookie(key);
-        }
-    }
+    void removeSessionItem(String key);
 
 }
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorageImpl.java
 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorageImpl.java
new file mode 100644
index 0000000..a339d4a
--- /dev/null
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/system/ClientStorageImpl.java
@@ -0,0 +1,99 @@
+package org.ovirt.engine.ui.common.system;
+
+import java.util.Date;
+
+import com.google.gwt.storage.client.Storage;
+import com.google.gwt.user.client.Cookies;
+
+/**
+ * Provides client-side key-value storage service.
+ * <p>
+ * Uses HTML5 {@linkplain Storage Web Storage} when supported by the browser. 
Falls back to {@linkplain Cookies cookies}
+ * when HTML5 Web Storage is not available.
+ * <p>
+ * Some facts and limitations:
+ * <ul>
+ * <li>typically, there can be max. 20 cookies per domain, each holding max. 
4kB of data
+ * <li>HTML5 local storage is persistent and shared by all browser windows/tabs
+ * <li>HTML5 session storage is transient and accessible only to one browser 
window/tab
+ * </ul>
+ */
+public class ClientStorageImpl implements ClientStorage {
+
+    // Fifty years
+    private static final long PERSISTENT_COOKIE_EXPIRATION = 1000 * 60 * 60 * 
24 * 365 * 50;
+
+    private static final Storage localStorage = 
Storage.getLocalStorageIfSupported();
+    private static final Storage sessionStorage = 
Storage.getSessionStorageIfSupported();
+
+    /**
+     * Returns the value for the given key from local (persistent) storage, or 
{@code null} if there is no value for
+     * such key.
+     */
+    public String getLocalItem(String key) {
+        if (localStorage != null) {
+            return localStorage.getItem(key);
+        } else {
+            return Cookies.getCookie(key);
+        }
+    }
+
+    /**
+     * Sets the value for the given key using local (persistent) storage.
+     */
+    public void setLocalItem(String key, String value) {
+        if (localStorage != null) {
+            localStorage.setItem(key, value);
+        } else {
+            // Emulate persistent storage using cookies which have predefined 
expiration date
+            Cookies.setCookie(key, value, new Date(new Date().getTime() + 
PERSISTENT_COOKIE_EXPIRATION));
+        }
+    }
+
+    /**
+     * Removes the value associated with the given key from local (persistent) 
storage.
+     */
+    public void removeLocalItem(String key) {
+        if (localStorage != null) {
+            localStorage.removeItem(key);
+        } else {
+            Cookies.removeCookie(key);
+        }
+    }
+
+    /**
+     * Returns the value for the given key from session (transient) storage, 
or {@code null} if there is no value for
+     * such key.
+     */
+    public String getSessionItem(String key) {
+        if (sessionStorage != null) {
+            return sessionStorage.getItem(key);
+        } else {
+            return Cookies.getCookie(key);
+        }
+    }
+
+    /**
+     * Sets the value for the given key using session (transient) storage.
+     */
+    public void setSessionItem(String key, String value) {
+        if (sessionStorage != null) {
+            sessionStorage.setItem(key, value);
+        } else {
+            // Emulate transient storage using cookies which expire when the 
browser session ends
+            Cookies.setCookie(key, value);
+        }
+    }
+
+    /**
+     * Removes the value associated with the given key from session 
(transient) storage.
+     */
+    public void removeSessionItem(String key) {
+        if (sessionStorage != null) {
+            sessionStorage.removeItem(key);
+        } else {
+            Cookies.removeCookie(key);
+        }
+    }
+
+}


-- 
To view, visit http://gerrit.ovirt.org/26238
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If5eaa942181c9df77278e736ddd6ac6b08e83fc7
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Tomas Jelinek <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to