Refactoring in FileUploadHandlerStore

* Use synchronizedMap instead of lock object. This is possible since
  the handlers map is the only object covered by this lock.

* Register service handler in constructor instead of lazy creation.
  The constructor is only called from getInstance(), when this method
  is called, we know that the service handler is needed.
diff --git a/bundles/org.eclipse.rap.addons.fileupload/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore.java b/bundles/org.eclipse.rap.addons.fileupload/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore.java
index 3e1fe0a..53847a6 100644
--- a/bundles/org.eclipse.rap.addons.fileupload/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore.java
+++ b/bundles/org.eclipse.rap.addons.fileupload/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2012 EclipseSource and others.
+ * Copyright (c) 2011, 2014 EclipseSource and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -10,6 +10,9 @@
  ******************************************************************************/
 package org.eclipse.rap.addons.fileupload.internal;
 
+import static org.eclipse.rap.rwt.SingletonUtil.getUniqueInstance;
+
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -19,49 +22,28 @@
 
 public final class FileUploadHandlerStore {
 
-  private static final String ATTR_NAME = FileUploadHandlerStore.class.getName() + ".instance";
-
-  private static final Object LOCK = new Object();
-  private final Map< String, FileUploadHandler > handlers;
-  private final Object lock;
-  private boolean registered;
+  private final Map<String, FileUploadHandler> handlers;
 
   private FileUploadHandlerStore() {
-    handlers = new HashMap< String, FileUploadHandler >();
-    lock = new Object();
+    handlers = Collections.synchronizedMap( new HashMap<String, FileUploadHandler>() );
+    RWT.getServiceManager().registerServiceHandler( FileUploadServiceHandler.SERVICE_HANDLER_ID,
+                                                    new FileUploadServiceHandler() );
   }
 
   public static FileUploadHandlerStore getInstance() {
-    FileUploadHandlerStore result;
-    synchronized( LOCK ) {
-      result = ( FileUploadHandlerStore )RWT.getApplicationContext().getAttribute( ATTR_NAME );
-      if( result == null ) {
-        result = new FileUploadHandlerStore();
-        RWT.getApplicationContext().setAttribute( ATTR_NAME, result );
-      }
-    }
-    return result;
+    return getUniqueInstance( FileUploadHandlerStore.class, RWT.getApplicationContext() );
   }
 
   public void registerHandler( String token, FileUploadHandler fileUploadHandler ) {
-    ensureServiceHandler();
-    synchronized( lock ) {
-      handlers.put( token, fileUploadHandler );
-    }
+    handlers.put( token, fileUploadHandler );
   }
 
   public void deregisterHandler( String token ) {
-    synchronized( lock ) {
-      handlers.remove( token );
-    }
+    handlers.remove( token );
   }
 
   public FileUploadHandler getHandler( String token ) {
-    FileUploadHandler result;
-    synchronized( lock ) {
-      result = handlers.get( token );
-    }
-    return result;
+    return handlers.get( token );
   }
 
   public static String createToken() {
@@ -70,13 +52,4 @@
     return Integer.toHexString( random1 ) + Integer.toHexString( random2 );
   }
 
-  private void ensureServiceHandler() {
-    synchronized( lock ) {
-      if( !registered ) {
-        RWT.getServiceManager().registerServiceHandler( FileUploadServiceHandler.SERVICE_HANDLER_ID,
-                                                        new FileUploadServiceHandler() );
-        registered = true;
-      }
-    }
-  }
 }
diff --git a/tests/org.eclipse.rap.addons.fileupload.test/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore_Test.java b/tests/org.eclipse.rap.addons.fileupload.test/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore_Test.java
index 89aff17..1f6ad51 100644
--- a/tests/org.eclipse.rap.addons.fileupload.test/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore_Test.java
+++ b/tests/org.eclipse.rap.addons.fileupload.test/src/org/eclipse/rap/addons/fileupload/internal/FileUploadHandlerStore_Test.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2013 EclipseSource and others.
+ * Copyright (c) 2011, 2014 EclipseSource and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -10,11 +10,7 @@
  ******************************************************************************/
 package org.eclipse.rap.addons.fileupload.internal;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 import org.eclipse.rap.addons.fileupload.FileUploadHandler;
 import org.eclipse.rap.addons.fileupload.internal.FileUploadHandlerStore;
@@ -27,13 +23,13 @@
 
 public class FileUploadHandlerStore_Test {
 
-  private FileUploadHandlerStore itemStore;
+  private FileUploadHandlerStore handlerStore;
   private FileUploadHandler testHandler;
 
   @Before
   public void setUp() {
     Fixture.setUp();
-    itemStore = FileUploadHandlerStore.getInstance();
+    handlerStore = FileUploadHandlerStore.getInstance();
     testHandler = new FileUploadHandler( new TestFileUploadReceiver() );
   }
 
@@ -41,47 +37,68 @@
   public void tearDown() {
     testHandler.dispose();
     testHandler = null;
-    itemStore = null;
+    handlerStore = null;
     Fixture.tearDown();
   }
 
   @Test
-  public void testGetInstance() {
-    assertNotNull( itemStore );
-    assertSame( itemStore, FileUploadHandlerStore.getInstance() );
+  public void testGetInstance_returnsInstance() {
+    FileUploadHandlerStore instance = FileUploadHandlerStore.getInstance();
+
+    assertNotNull( instance );
+  }
+
+  @Test
+  public void testGetInstance_sameInstanceInDifferentUISession() {
+    Fixture.disposeOfServiceContext();
+    Fixture.createServiceContext();
+
+    assertSame( handlerStore, FileUploadHandlerStore.getInstance() );
+  }
+
+  @Test
+  public void testGetInstance_differentInstanceInOtherApplication() {
+    Fixture.disposeOfApplicationContext();
+    Fixture.createApplicationContext();
+    Fixture.createServiceContext();
+
+    FileUploadHandlerStore instance = FileUploadHandlerStore.getInstance();
+
+    assertNotNull( instance );
+    assertNotSame( handlerStore, instance );
   }
 
   @Test
   public void testGetNotExistingHandler() {
-    FileUploadHandler result = itemStore.getHandler( "testId" );
+    FileUploadHandler result = handlerStore.getHandler( "testId" );
 
     assertNull( result );
   }
 
   @Test
   public void testRegisterAndGetHandler() {
-    itemStore.registerHandler( "testId", testHandler );
+    handlerStore.registerHandler( "testId", testHandler );
 
-    FileUploadHandler result = itemStore.getHandler( "testId" );
+    FileUploadHandler result = handlerStore.getHandler( "testId" );
 
     assertSame( testHandler, result );
   }
 
   @Test
   public void testGetHandlerWithDifferentId() {
-    itemStore.registerHandler( "testId", testHandler );
+    handlerStore.registerHandler( "testId", testHandler );
 
-    FileUploadHandler result = itemStore.getHandler( "anotherId" );
+    FileUploadHandler result = handlerStore.getHandler( "anotherId" );
 
     assertNull( result );
   }
 
   @Test
   public void testDeregisterHandler() {
-    itemStore.registerHandler( "testId", testHandler );
+    handlerStore.registerHandler( "testId", testHandler );
 
-    itemStore.deregisterHandler( "testId" );
-    FileUploadHandler result = itemStore.getHandler( "testId" );
+    handlerStore.deregisterHandler( "testId" );
+    FileUploadHandler result = handlerStore.getHandler( "testId" );
 
     assertNull( result );
   }