[hornetq-commits] JBoss hornetq SVN: r10772 - in branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq: utils and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Jun 3 09:53:02 EDT 2011


Author: gaohoward
Date: 2011-06-03 09:53:02 -0400 (Fri, 03 Jun 2011)
New Revision: 10772

Added:
   branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/utils/ClassloadingUtil.java
Modified:
   branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java
Log:
HORNETQ-681


Modified: branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java
===================================================================
--- branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java	2011-06-03 08:14:56 UTC (rev 10771)
+++ branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java	2011-06-03 13:53:02 UTC (rev 10772)
@@ -14,8 +14,6 @@
 package org.hornetq.core.client.impl;
 
 import java.lang.ref.WeakReference;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -58,6 +56,7 @@
 import org.hornetq.spi.core.remoting.ConnectionLifeCycleListener;
 import org.hornetq.spi.core.remoting.Connector;
 import org.hornetq.spi.core.remoting.ConnectorFactory;
+import org.hornetq.utils.ClassloadingUtil;
 import org.hornetq.utils.ConcurrentHashSet;
 import org.hornetq.utils.ConfigurationHelper;
 import org.hornetq.utils.ExecutorFactory;
@@ -202,10 +201,10 @@
       closeExecutor = orderedExecutorFactory.getExecutor();
 
       this.interceptors = interceptors;
- 
+
    }
 
-   public void connect(int initialConnectAttempts, boolean failoverOnInitialConnection) throws HornetQException
+   public void connect(final int initialConnectAttempts, final boolean failoverOnInitialConnection) throws HornetQException
    {
       // Get the connection
       getConnectionWithRetry(initialConnectAttempts);
@@ -228,7 +227,7 @@
       return connectorConfig;
    }
 
-   public void setBackupConnector(TransportConfiguration live, TransportConfiguration backUp)
+   public void setBackupConnector(final TransportConfiguration live, final TransportConfiguration backUp)
    {
       if(live.equals(connectorConfig) && backUp != null)
       {
@@ -242,7 +241,7 @@
       {
          if (log.isDebugEnabled())
          {
-            log.debug("ClientSessionFactoryImpl received backup update for live/backup pair = " + live + " / " + backUp + " but it didn't belong to " + this.connectorConfig);
+            log.debug("ClientSessionFactoryImpl received backup update for live/backup pair = " + live + " / " + backUp + " but it didn't belong to " + connectorConfig);
          }
       }
    }
@@ -370,14 +369,14 @@
 
    // Must be synchronized to prevent it happening concurrently with failover which can lead to
    // inconsistencies
-   public void removeSession(final ClientSessionInternal session, boolean failingOver)
+   public void removeSession(final ClientSessionInternal session, final boolean failingOver)
    {
       synchronized (sessions)
       {
          sessions.remove(session);
       }
    }
-   
+
    public void connectionReadyForWrites(final Object connectionID, final boolean ready)
    {
    }
@@ -461,7 +460,7 @@
    {
       stopPingingAfterOne = true;
    }
-   
+
    public void resumePinging()
    {
       stopPingingAfterOne = false;
@@ -804,7 +803,7 @@
       throw new IllegalStateException("Oh my God it's full of stars!");
    }
 
-   private void callFailureListeners(final HornetQException me, final boolean afterReconnect, boolean failedOver)
+   private void callFailureListeners(final HornetQException me, final boolean afterReconnect, final boolean failedOver)
    {
       final List<SessionFailureListener> listenersClone = new ArrayList<SessionFailureListener>(listeners);
 
@@ -866,7 +865,7 @@
       {
          sessionsToFailover = new HashSet<ClientSessionInternal>(sessions);
       }
-      
+
       for (ClientSessionInternal session : sessionsToFailover)
       {
          session.handleFailover(connection);
@@ -902,7 +901,7 @@
                if (reconnectAttempts != 0)
                {
                   count++;
-                  
+
                   if (reconnectAttempts != -1 && count == reconnectAttempts)
                   {
                      log.warn("Tried " + reconnectAttempts + " times to connect. Now giving up on reconnecting it.");
@@ -1011,7 +1010,7 @@
                {
                   log.debug("Trying to connect at the main server using connector :" + connectorConfig);
                }
-               
+
                tc = connector.createConnection();
 
                if (tc == null)
@@ -1020,7 +1019,7 @@
                   {
                      log.debug("Main server is not up. Hopefully there's a backup configured now!");
                   }
-                  
+
                   try
                   {
                      connector.close();
@@ -1058,7 +1057,7 @@
                      {
                         log.debug("Backup is not active yet");
                      }
-                     
+
                      try
                      {
                         connector.close();
@@ -1072,12 +1071,12 @@
                   else
                   {
                      /*looks like the backup is now live, lets use that*/
-                     
+
                      if (log.isDebugEnabled())
                      {
                         log.debug("Connected to the backup at " + backupConfig);
                      }
-                     
+
                      connectorConfig = backupConfig;
 
                      backupConfig = null;
@@ -1171,6 +1170,7 @@
       return connection;
    }
 
+   @Override
    public void finalize() throws Throwable
    {
       if (!closed)
@@ -1188,24 +1188,7 @@
 
    private ConnectorFactory instantiateConnectorFactory(final String connectorFactoryClassName)
    {
-      return AccessController.doPrivileged(new PrivilegedAction<ConnectorFactory>()
-      {
-         public ConnectorFactory run()
-         {
-            ClassLoader loader = Thread.currentThread().getContextClassLoader();
-            try
-            {
-               Class<?> clazz = loader.loadClass(connectorFactoryClassName);
-               return (ConnectorFactory)clazz.newInstance();
-            }
-            catch (Exception e)
-            {
-               throw new IllegalArgumentException("Error instantiating connector factory \"" + connectorFactoryClassName +
-                                                           "\"",
-                                                  e);
-            }
-         }
-      });
+      return (ConnectorFactory)ClassloadingUtil.safeInitNewInstance(connectorFactoryClassName);
    }
 
    private void lockChannel1()
@@ -1262,7 +1245,7 @@
          if (type == PacketImpl.DISCONNECT)
          {
             final DisconnectMessage msg = (DisconnectMessage)packet;
-            
+
             closeExecutor.execute(new Runnable()
             {
                // Must be executed on new thread since cannot block the netty thread for a long time and fail can
@@ -1329,7 +1312,7 @@
          this.connectionID = connectionID;
       }
 
-      public void connectionFailed(final HornetQException me, boolean failedOver)
+      public void connectionFailed(final HornetQException me, final boolean failedOver)
       {
          handleConnectionFailure(connectionID, me);
       }
@@ -1374,7 +1357,7 @@
          first = false;
 
          long now = System.currentTimeMillis();
-         
+
          if (clientFailureCheckPeriod != -1 && connectionTTL != -1 && now >= lastCheck + connectionTTL )
          {
             if (!connection.checkDataReceived())
@@ -1405,7 +1388,7 @@
       }
 
       /**
-       * 
+       *
        */
       public void send()
       {

Added: branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/utils/ClassloadingUtil.java
===================================================================
--- branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/utils/ClassloadingUtil.java	                        (rev 0)
+++ branches/HORNETQ-681/hornetq-core/src/main/java/org/hornetq/utils/ClassloadingUtil.java	2011-06-03 13:53:02 UTC (rev 10772)
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2010 Red Hat, Inc.
+ * Red Hat licenses this file to you under the Apache License, version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package org.hornetq.utils;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * A ClassloadingUtil
+ *
+ * @author <a href="mailto:hgao at redhat.com">Howard Gao</a>
+ */
+public final class ClassloadingUtil
+{
+   public static Object safeInitNewInstance(final String className)
+   {
+      return AccessController.doPrivileged(new PrivilegedAction<Object>()
+      {
+         public Object run()
+         {
+            ClassLoader loader = ClassloadingUtil.class.getClassLoader();
+            if (loader == null)
+            {
+               loader = Thread.currentThread().getContextClassLoader();
+            }
+
+            try
+            {
+               Class<?> clazz = loader.loadClass(className);
+               return clazz.newInstance();
+            }
+            catch (Exception e)
+            {
+               throw new IllegalArgumentException("Error instantiating connector factory \"" + className + "\"", e);
+            }
+         }
+      });
+   }
+
+}



More information about the hornetq-commits mailing list