You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@river.apache.org by pe...@apache.org on 2013/04/17 16:46:56 UTC

svn commit: r1468943 [1/2] - in /river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini: mercury/MailboxImpl.java mercury/MailboxImplInit.java outrigger/OutriggerServerImpl.java reggie/RegistrarImpl.java

Author: peter_firmstone
Date: Wed Apr 17 14:46:56 2013
New Revision: 1468943

URL: http://svn.apache.org/r1468943
Log:
Removing instances of Thread.start() calls in object constructors and making fields final where possible.

Modified:
    river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImpl.java
    river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImplInit.java
    river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/outrigger/OutriggerServerImpl.java
    river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/reggie/RegistrarImpl.java

Modified: river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImpl.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImpl.java?rev=1468943&r1=1468942&r2=1468943&view=diff
==============================================================================
--- river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImpl.java (original)
+++ river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImpl.java Wed Apr 17 14:46:56 2013
@@ -80,6 +80,8 @@ import java.rmi.activation.ActivationSys
 import java.rmi.MarshalledObject;
 import java.rmi.NoSuchObjectException;
 import java.rmi.RemoteException;
+import java.security.AccessControlContext;
+import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.Arrays;
@@ -420,6 +422,7 @@ class MailboxImpl implements MailboxBack
     private Configuration config;
     private Throwable thrown;
     private boolean started = false;
+    private AccessControlContext context;
     
     ///////////////////////
     // Activation Methods
@@ -642,6 +645,7 @@ class MailboxImpl implements MailboxBack
                 expirer = init.expirer;
                 this.config = init.config;
                 this.loginContext = loginContext;
+                context = init.context;
                 
             } else {
                 activationID = activID;
@@ -673,6 +677,7 @@ class MailboxImpl implements MailboxBack
                 expirer = null;
                 this.config = null;
                 this.loginContext = loginContext;
+                context = null;
             }
             // Assign fields.
         }
@@ -1074,128 +1079,139 @@ class MailboxImpl implements MailboxBack
         concurrentObj.writeLock();
         if (started) return;
         started = true; // mutual exclusion
+        
         try {
             if (thrown != null) throw thrown;
-            if (persistent){
-                // Start snapshot thread belongs in start method
-    //	    snapshotter = new SnapshotThread();
-                snapshotter.start();
-            }
+            AccessController.doPrivileged(new PrivilegedExceptionAction(){
 
-            /*  ---  The following will go into start method --- */
+                @Override
+                public Object run() throws Exception {
+                    if (persistent){
+                        // Start snapshot thread belongs in start method
+            //	    snapshotter = new SnapshotThread();
+                        snapshotter.start();
+                    }
 
-            // Start threads
-    //	notifier = new Notifier(config);
-            notifier.start();
-    //	expirer = new ExpirationThread();
-            expirer.start();
-
-            // Export server instance and get its reference
-            serverStub = (MailboxBackEnd)exporter.export(this);
-            if (initLogger.isLoggable(Level.FINEST)) {
-                initLogger.log(Level.FINEST, "Service stub is: {0}", 
-                serverStub);	
-            }	
-
-            // Create the proxy that will be registered in the lookup service
-            mailboxProxy = 
-                MailboxProxy.create(serverStub, serviceID);
-            if (initLogger.isLoggable(Level.FINEST)) {
-                initLogger.log(Level.FINEST, "Service proxy is: {0}", 
-                mailboxProxy);
-            }		
-
-            // Create the admin proxy for this service
-            mailboxAdminProxy = 
-                MailboxAdminProxy.create(serverStub, serviceID);
-            if (initLogger.isLoggable(Level.FINEST)) {
-                initLogger.log(Level.FINEST, "Service admin proxy is: {0}", 
-                mailboxAdminProxy);		
-            }
+                    /*  ---  The following will go into start method --- */
 
-            // Create leaseFactory
-            leaseFactory = new LeaseFactory(serverStub, serviceID);
+                    // Start threads
+            //	notifier = new Notifier(config);
+                    notifier.start();
+            //	expirer = new ExpirationThread();
+                    expirer.start();
+
+                    // Export server instance and get its reference
+                    serverStub = (MailboxBackEnd)exporter.export(MailboxImpl.this);
+                    if (initLogger.isLoggable(Level.FINEST)) {
+                        initLogger.log(Level.FINEST, "Service stub is: {0}", 
+                        serverStub);	
+                    }	
+
+                    // Create the proxy that will be registered in the lookup service
+                    mailboxProxy = 
+                        MailboxProxy.create(serverStub, serviceID);
+                    if (initLogger.isLoggable(Level.FINEST)) {
+                        initLogger.log(Level.FINEST, "Service proxy is: {0}", 
+                        mailboxProxy);
+                    }		
+
+                    // Create the admin proxy for this service
+                    mailboxAdminProxy = 
+                        MailboxAdminProxy.create(serverStub, serviceID);
+                    if (initLogger.isLoggable(Level.FINEST)) {
+                        initLogger.log(Level.FINEST, "Service admin proxy is: {0}", 
+                        mailboxAdminProxy);		
+                    }
 
-            // Get shorthand reference to the discovery manager
-            try {
-                lookupDiscMgr  = 
-                    (DiscoveryManagement)Config.getNonNullEntry(config,
-                        MERCURY, "discoveryManager",
-                        DiscoveryManagement.class);
-                if(lookupDiscMgr instanceof DiscoveryGroupManagement) {
-                     // Verify proper initial state ---> NO_GROUPS
-                    String[] groups =
-                        ((DiscoveryGroupManagement)lookupDiscMgr).getGroups();
-                    if( (groups == DiscoveryGroupManagement.ALL_GROUPS) ||
-                        (groups.length != 0) )
-                    {
-                        throw new ConfigurationException(
-                            "discoveryManager entry must be configured " +
-                            " with no groups.");
-                    }//endif
-                } else {
-                   throw new ConfigurationException(
-                        "discoveryManager entry must implement " +
-                        "DiscoveryGroupManagement");
-                }
+                    // Create leaseFactory
+                    leaseFactory = new LeaseFactory(serverStub, serviceID);
 
-                if(lookupDiscMgr instanceof DiscoveryLocatorManagement) {
-                    LookupLocator[] locs =
-                            ((DiscoveryLocatorManagement)lookupDiscMgr).getLocators();
-                    if( (locs != null) && (locs.length != 0) ) {
-                        throw new ConfigurationException(
-                            "discoveryManager entry must be configured " +
-                            "with no locators");
-                    }//endif
-                } else {
-                    throw new ConfigurationException(
-                        "discoveryManager entry must implement " +
-                        "DiscoveryLocatorManagement");
-                }  
-
-                ((DiscoveryGroupManagement)lookupDiscMgr).setGroups(lookupGroups);
-                ((DiscoveryLocatorManagement)lookupDiscMgr).setLocators(lookupLocators);
-            } catch (NoSuchEntryException e) {
-                lookupDiscMgr  =
-                    new LookupDiscoveryManager(lookupGroups, lookupLocators,
-                        null, config);
-            }
-            if (initLogger.isLoggable(Level.FINEST)) {
-                initLogger.log(Level.FINEST, "Discovery manager is: {0}", 
-                lookupDiscMgr);
-            }		
-
-            ServiceID lookupID = new ServiceID(
-                serviceID.getMostSignificantBits(),
-                serviceID.getLeastSignificantBits());
+                    // Get shorthand reference to the discovery manager
+                    try {
+                        lookupDiscMgr  = 
+                            (DiscoveryManagement)Config.getNonNullEntry(config,
+                                MERCURY, "discoveryManager",
+                                DiscoveryManagement.class);
+                        if(lookupDiscMgr instanceof DiscoveryGroupManagement) {
+                             // Verify proper initial state ---> NO_GROUPS
+                            String[] groups =
+                                ((DiscoveryGroupManagement)lookupDiscMgr).getGroups();
+                            if( (groups == DiscoveryGroupManagement.ALL_GROUPS) ||
+                                (groups.length != 0) )
+                            {
+                                throw new ConfigurationException(
+                                    "discoveryManager entry must be configured " +
+                                    " with no groups.");
+                            }//endif
+                        } else {
+                           throw new ConfigurationException(
+                                "discoveryManager entry must implement " +
+                                "DiscoveryGroupManagement");
+                        }
 
-            if (initLogger.isLoggable(Level.FINEST)) {
-                initLogger.log(Level.FINEST, "Creating JoinManager.");
-            }
-            joiner = new JoinManager(
-                mailboxProxy,                // service object
-                lookupAttrs,               // service attributes
-                lookupID,                 // Service ID
-                lookupDiscMgr,             // DiscoveryManagement ref - default
-                null,                      // LeaseRenewalManager reference
-                config); 
+                        if(lookupDiscMgr instanceof DiscoveryLocatorManagement) {
+                            LookupLocator[] locs =
+                                    ((DiscoveryLocatorManagement)lookupDiscMgr).getLocators();
+                            if( (locs != null) && (locs.length != 0) ) {
+                                throw new ConfigurationException(
+                                    "discoveryManager entry must be configured " +
+                                    "with no locators");
+                            }//endif
+                        } else {
+                            throw new ConfigurationException(
+                                "discoveryManager entry must implement " +
+                                "DiscoveryLocatorManagement");
+                        }  
+
+                        ((DiscoveryGroupManagement)lookupDiscMgr).setGroups(lookupGroups);
+                        ((DiscoveryLocatorManagement)lookupDiscMgr).setLocators(lookupLocators);
+                    } catch (NoSuchEntryException e) {
+                        lookupDiscMgr  =
+                            new LookupDiscoveryManager(lookupGroups, lookupLocators,
+                                null, config);
+                    }
+                    if (initLogger.isLoggable(Level.FINEST)) {
+                        initLogger.log(Level.FINEST, "Discovery manager is: {0}", 
+                        lookupDiscMgr);
+                    }		
+
+                    ServiceID lookupID = new ServiceID(
+                        serviceID.getMostSignificantBits(),
+                        serviceID.getLeastSignificantBits());
 
-            if (operationsLogger.isLoggable(Level.FINER)) {
-                operationsLogger.exiting(mailboxSourceClass, 
-                    "doInit");
-            }
-            readyState.ready();
+                    if (initLogger.isLoggable(Level.FINEST)) {
+                        initLogger.log(Level.FINEST, "Creating JoinManager.");
+                    }
+                    joiner = new JoinManager(
+                        mailboxProxy,                // service object
+                        lookupAttrs,               // service attributes
+                        lookupID,                 // Service ID
+                        lookupDiscMgr,             // DiscoveryManagement ref - default
+                        null,                      // LeaseRenewalManager reference
+                        config); 
+
+                    if (operationsLogger.isLoggable(Level.FINER)) {
+                        operationsLogger.exiting(mailboxSourceClass, 
+                            "doInit");
+                    }
+                    readyState.ready();
 
-            if (startupLogger.isLoggable(Level.INFO)) {
-                startupLogger.log
-                       (Level.INFO, "Mercury started: {0}", this);
-            }
+                    if (startupLogger.isLoggable(Level.INFO)) {
+                        startupLogger.log
+                               (Level.INFO, "Mercury started: {0}", this);
+                    }
+                    return null;
+                }
+                
+            }, context);
+            
         } catch (Throwable t){
             cleanup();
 	    initFailed(t);
         } finally {
             config = null;
             thrown = null;
+            context = null;
             concurrentObj.writeUnlock();
         }
     }

Modified: river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImplInit.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImplInit.java?rev=1468943&r1=1468942&r2=1468943&view=diff
==============================================================================
--- river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImplInit.java (original)
+++ river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/mercury/MailboxImplInit.java Wed Apr 17 14:46:56 2013
@@ -32,6 +32,8 @@ import java.rmi.activation.ActivationExc
 import java.rmi.activation.ActivationGroup;
 import java.rmi.activation.ActivationID;
 import java.rmi.activation.ActivationSystem;
+import java.security.AccessControlContext;
+import java.security.AccessController;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -88,6 +90,7 @@ class MailboxImplInit {
     Thread notifier;
     Thread expirer;
     Configuration config;
+    AccessControlContext context;
 
     MailboxImplInit(Configuration config, 
                     boolean persistent, 
@@ -102,6 +105,7 @@ class MailboxImplInit {
         this.notifier = notifier;
         this.expirer = expirer;
         this.config = config;
+        context = AccessController.getContext();
         // Get activation specific configuration items, if activated
         if (activationID != null) {
             ProxyPreparer activationSystemPreparer = (ProxyPreparer) Config.getNonNullEntry(config, MailboxImpl.MERCURY, "activationSystemPreparer", ProxyPreparer.class, new BasicProxyPreparer());

Modified: river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/outrigger/OutriggerServerImpl.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/outrigger/OutriggerServerImpl.java?rev=1468943&r1=1468942&r2=1468943&view=diff
==============================================================================
--- river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/outrigger/OutriggerServerImpl.java (original)
+++ river/jtsk/skunk/qa_refactor/trunk/src/com/sun/jini/outrigger/OutriggerServerImpl.java Wed Apr 17 14:46:56 2013
@@ -77,6 +77,8 @@ import java.rmi.UnmarshalException;
 import java.rmi.activation.ActivationID;
 import java.rmi.activation.ActivationSystem;
 import java.rmi.activation.ActivationException;
+import java.security.AccessControlContext;
+import java.security.AccessController;
 import java.security.SecureRandom;
 import java.security.PrivilegedExceptionAction;
 import java.security.PrivilegedActionException;
@@ -491,6 +493,7 @@ public class OutriggerServerImpl 
     private Exception except;
     private boolean persistent;
     private long maxServerQueryTimeout;
+    private AccessControlContext context;
 
     /**
      * Create a new <code>OutriggerServerImpl</code> server (possibly a
@@ -602,6 +605,7 @@ public class OutriggerServerImpl 
             contentsQueryReaperThread = h.contentsQueryReaperThread;
             starter = h.starter;
             maxServerQueryTimeout = h.maxServerQueryTimeout;
+            context = h.context;
         } else {
             lifecycleLogger.log(Level.SEVERE, "Failed to construct Outrigger server", except == null ? thrown : except);
             this.loginContext = null;
@@ -633,6 +637,7 @@ public class OutriggerServerImpl 
             contentsQueryReaperThread = null;
             starter = null;
             maxServerQueryTimeout = 0;
+            context = null;
         }
     }
     
@@ -646,74 +651,83 @@ public class OutriggerServerImpl 
         if (thrown != null) throw (Error) thrown;
         if (except != null) throw except;
         try {
-            // This takes a while the first time, so let's get it going
-            txnMonitor.start();
-            starter.start();
-
-            // Get store from configuration
-            if (persistent) {
-                store = (Store)Config.getNonNullEntry(config,
-                                                      COMPONENT_NAME,
-                                                      "store", Store.class);
-                expirationOpQueue.start();
-            }
-            // If we have a store, recover the log
-            if (store != null) {
-                log = store.setupStore(this);
+            AccessController.doPrivileged(new PrivilegedExceptionAction(){
 
-                // Record this boot
-                //
-                log.bootOp(System.currentTimeMillis(), getSessionId());
-                recoverTxns();
-            } else if (activationID != null || persistent) {
-                /* else we don't have a store, if we need one complain
-                 * will be logged by constructor
-                 */
-                throw new ConfigurationException("Must provide for a " +
-                    "store for component " + COMPONENT_NAME + ", by providing " +
-                    "valid values for the store or " +
-                    PERSISTENCE_DIR_CONFIG_ENTRY + " entries if creating " +
-                    " a persistent space");
-            }
-
-            /* Now that we have recovered any store we have, create a 
-             * Uuid if there was not one in the store.
-             */
-            if (topUuid == null) {
-                topUuid = UuidFactory.generate();
-                if (log != null)
-                    log.uuidOp(topUuid);
-            }		
-
-            if (ourRemoteRef instanceof RemoteMethodControl) {
-                spaceProxy = new ConstrainableSpaceProxy2(ourRemoteRef, topUuid,
-                    maxServerQueryTimeout, null);
-                adminProxy = 
-                    new ConstrainableAdminProxy(ourRemoteRef, topUuid, null);
-                participantProxy =
-                    new ConstrainableParticipantProxy(ourRemoteRef, topUuid, null);
-            } else {
-                spaceProxy = new SpaceProxy2(ourRemoteRef, topUuid, 
-                                             maxServerQueryTimeout);
-                adminProxy = new AdminProxy(ourRemoteRef, topUuid);
-                participantProxy = new ParticipantProxy(ourRemoteRef, topUuid);
-            }
-
-            leaseFactory = new LeaseFactory(ourRemoteRef, topUuid);
-
-            /* Kick off independent threads. */
-
-            // start the JoinStateManager
-            joinStateManager.startManager(config, log, spaceProxy,
-                new ServiceID(topUuid.getMostSignificantBits(),
-                              topUuid.getLeastSignificantBits()),
-                attributesFor());
-            // Notifier uses TaskManager, which doesn't start threads until given tasks.
-            notifier = new Notifier(spaceProxy, recoveredListenerPreparer, config);
-            operationJournal.start();
-            templateReaperThread.start(); 
-            entryReaperThread.start(); 
-            contentsQueryReaperThread.start();
+                @Override
+                public Object run() throws Exception {
+                            // This takes a while the first time, so let's get it going
+                    txnMonitor.start();
+                    starter.start();
+
+                    // Get store from configuration
+                    if (persistent) {
+                        store = (Store)Config.getNonNullEntry(config,
+                                                              COMPONENT_NAME,
+                                                              "store", Store.class);
+                        expirationOpQueue.start();
+                    }
+                    // If we have a store, recover the log
+                    if (store != null) {
+                        log = store.setupStore(OutriggerServerImpl.this);
+
+                        // Record this boot
+                        //
+                        log.bootOp(System.currentTimeMillis(), getSessionId());
+                        recoverTxns();
+                    } else if (activationID != null || persistent) {
+                        /* else we don't have a store, if we need one complain
+                         * will be logged by constructor
+                         */
+                        throw new ConfigurationException("Must provide for a " +
+                            "store for component " + COMPONENT_NAME + ", by providing " +
+                            "valid values for the store or " +
+                            PERSISTENCE_DIR_CONFIG_ENTRY + " entries if creating " +
+                            " a persistent space");
+                    }
+
+                    /* Now that we have recovered any store we have, create a 
+                     * Uuid if there was not one in the store.
+                     */
+                    if (topUuid == null) {
+                        topUuid = UuidFactory.generate();
+                        if (log != null)
+                            log.uuidOp(topUuid);
+                    }		
+
+                    if (ourRemoteRef instanceof RemoteMethodControl) {
+                        spaceProxy = new ConstrainableSpaceProxy2(ourRemoteRef, topUuid,
+                            maxServerQueryTimeout, null);
+                        adminProxy = 
+                            new ConstrainableAdminProxy(ourRemoteRef, topUuid, null);
+                        participantProxy =
+                            new ConstrainableParticipantProxy(ourRemoteRef, topUuid, null);
+                    } else {
+                        spaceProxy = new SpaceProxy2(ourRemoteRef, topUuid, 
+                                                     maxServerQueryTimeout);
+                        adminProxy = new AdminProxy(ourRemoteRef, topUuid);
+                        participantProxy = new ParticipantProxy(ourRemoteRef, topUuid);
+                    }
+
+                    leaseFactory = new LeaseFactory(ourRemoteRef, topUuid);
+
+                    /* Kick off independent threads. */
+
+                    // start the JoinStateManager
+                    joinStateManager.startManager(config, log, spaceProxy,
+                        new ServiceID(topUuid.getMostSignificantBits(),
+                                      topUuid.getLeastSignificantBits()),
+                        attributesFor());
+                    // Notifier uses TaskManager, which doesn't start threads until given tasks.
+                    notifier = new Notifier(spaceProxy, recoveredListenerPreparer, config);
+                    operationJournal.start();
+                    templateReaperThread.start(); 
+                    entryReaperThread.start(); 
+                    contentsQueryReaperThread.start();
+                    return null;
+                }
+                
+            }, context);
+            
         } catch (Exception e) {
             // Clean up and rethrow.
             lifecycleLogger.log(Level.SEVERE, "Failed to start Outrigger server", e);
@@ -771,6 +785,7 @@ public class OutriggerServerImpl 
             starter = null;
             except = null;
             thrown = null;
+            context = null;
         }
     }
 
@@ -803,6 +818,7 @@ public class OutriggerServerImpl 
         ContentsQueryReaper contentsQueryReaperThread;
         Thread starter;
         long maxServerQueryTimeout;
+        AccessControlContext context;
     }
 
     /**
@@ -831,6 +847,7 @@ public class OutriggerServerImpl 
     	throws IOException, ConfigurationException, ActivationException
     {
         InitHolder h = new InitHolder();
+        h.context = AccessController.getContext();
         try {
             h.txnMonitor = new TxnMonitor(this, config);
             /* Get the activation related preparers we need */