changeset 43:1ee75426fb8d

8057958: com.oracle.dio.registry.RegistrationEventHandler::singleton is too visible Reviewed-by: bobv
author jld
date Wed, 10 Sep 2014 11:05:24 -0400
parents a8813dce24e3
children 306a4efd2ff7
files src/se/classes/com/oracle/dio/registry/RegistrationEventHandlerImpl.java src/share/classes/com/oracle/dio/registry/RegistrationEventHandler.java
diffstat 2 files changed, 18 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/registry/RegistrationEventHandlerImpl.java	Tue Sep 09 10:20:51 2014 -0400
+++ b/src/se/classes/com/oracle/dio/registry/RegistrationEventHandlerImpl.java	Wed Sep 10 11:05:24 2014 -0400
@@ -31,14 +31,10 @@
 class RegistrationEventHandlerImpl<T extends Device> extends RegistrationEventHandler<T> {
 
     static synchronized void registered(DeviceDescriptor dscr) {
-        if (null != singleton) {
-            singleton.notifyRegistered(dscr);
-        }
+        getHandler().notifyRegistered(dscr);
     }
 
     static synchronized void removed(DeviceDescriptor dscr) {
-        if (null != singleton) {
-            singleton.notifyRemoved(dscr);
-        }
+        getHandler().notifyRemoved(dscr);
     }
 }
--- a/src/share/classes/com/oracle/dio/registry/RegistrationEventHandler.java	Tue Sep 09 10:20:51 2014 -0400
+++ b/src/share/classes/com/oracle/dio/registry/RegistrationEventHandler.java	Wed Sep 10 11:05:24 2014 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -34,7 +34,7 @@
 
 public abstract class RegistrationEventHandler<T extends Device> {
 
-    protected static RegistrationEventHandler singleton;
+    private static RegistrationEventHandler singleton;
 
     protected Map<Class<T>, RegistrationListener> listeners;
 
@@ -43,19 +43,25 @@
     }
 
     public static synchronized <C extends Device> void addListener(RegistrationListener l, Class<C> type) {
-        //  if type is null?
+        getHandler().startListening(type, l);
+    }
+
+    protected static RegistrationEventHandler getHandler() {
+        // check null up front. Most invocations will never
+        // need to synchronize
         if (null == singleton) {
-            singleton = new RegistrationEventHandlerImpl();
+            synchronized (RegistrationEventHandler.class) {
+                // check again now that we've synched on the class
+                if (null == singleton) {
+                    singleton = new RegistrationEventHandlerImpl();
+                }
+            }
         }
-        singleton.startListening(type, l);
+        return singleton;
     }
 
     public static synchronized <C extends Device> void removeListener(RegistrationListener l, Class<C> type) {
-        //  if type is null?
-        if (null != singleton) {
-            // it is up to implementation to clean singleton
-            singleton.stopListening(type, l);
-        }
+        getHandler().stopListening(type, l);
     }
 
     protected void notifyRegistered(DeviceDescriptor dscr) {