activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r1214575 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src: main/csharp/Connection.cs test/csharp/BrokerToNMSExceptionsTest.cs
Date Thu, 15 Dec 2011 00:21:51 GMT
Author: tabish
Date: Thu Dec 15 00:21:51 2011
New Revision: 1214575

URL: http://svn.apache.org/viewvc?rev=1214575&view=rev
Log:
fix for: https://issues.apache.org/jira/browse/AMQNET-361
fix for: https://issues.apache.org/jira/browse/AMQNET-363

Added:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/BrokerToNMSExceptionsTest.cs
  (with props)
Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs?rev=1214575&r1=1214574&r2=1214575&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Connection.cs Thu Dec
15 00:21:51 2011
@@ -20,6 +20,8 @@ using System.Diagnostics;
 using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
+using System.Reflection;
+using System.Runtime.Remoting;
 using Apache.NMS.ActiveMQ.Commands;
 using Apache.NMS.ActiveMQ.Threads;
 using Apache.NMS.ActiveMQ.Transport;
@@ -687,11 +689,15 @@ namespace Apache.NMS.ActiveMQ
 				if(response is ExceptionResponse)
 				{
 					ExceptionResponse exceptionResponse = (ExceptionResponse) response;
-					BrokerError brokerError = exceptionResponse.Exception;
-					throw new BrokerException(brokerError);
+                    Exception exception = CreateExceptionFromResponse(exceptionResponse);
+					throw exception;
 				}
 				return response;
 			}
+            catch(NMSException)
+            {
+                throw;
+            }
 			catch(Exception ex)
 			{
 				throw NMSExceptionSupport.Create(ex);
@@ -793,10 +799,24 @@ namespace Apache.NMS.ActiveMQ
                                                 this.advisoryConsumer = new AdvisoryConsumer(this,
id);
                                             }
 										}
+                                        else
+                                        {
+                                            NMSException exception = CreateExceptionFromResponse(response
as ExceptionResponse);
+                                            if(exception is InvalidClientIDException)
+                                            {
+                                                // This is non-recoverable
+                                                throw exception;
+                                            }
+                                        }
 									}
 								}
-								catch
+                                catch(BrokerException)
+                                {
+                                    // We Swallow the generic version and throw ConnectionClosedException
+                                }
+								catch(NMSException)
 								{
+                                    throw;
 								}
 							}
 						}
@@ -1301,5 +1321,56 @@ namespace Apache.NMS.ActiveMQ
                 throw new ConnectionClosedException();
             }
         }
+
+        private NMSException CreateExceptionFromResponse(ExceptionResponse exceptionResponse)
+        {
+            if(String.IsNullOrEmpty(exceptionResponse.Exception.ExceptionClass))
+            {
+                return new BrokerException(exceptionResponse.Exception);
+            }
+
+            NMSException exception = null;
+            String name = exceptionResponse.Exception.ExceptionClass;
+            String message = exceptionResponse.Exception.Message;
+
+            // We only create instances of exceptions from the NMS API
+            Assembly nmsAssembly = Assembly.GetAssembly(typeof(NMSException));
+
+            // First try and see if its one we populated ourselves in which case
+            // it will have the correct namespace and exception name.
+            Type exceptionType = nmsAssembly.GetType(name, false, true);
+
+            // Exceptions from the broker don't have the same namespace, so we
+            // trim that and try using the NMS namespace to see if we can get an
+            // NMSException based version of the same type.  We have to convert
+            // the JMS preficed exceptions to NMS also.
+            if(exceptionType == null && !name.StartsWith("Apache.NMS") &&
name.Contains("."))
+            {
+                int pos = name.LastIndexOf(".");
+                name = name.Substring(pos + 1);
+                name = name.Replace("JMS", "NMS");
+                name = "Apache.NMS." + name;
+
+                exceptionType = nmsAssembly.GetType(name, false, true);
+            }
+
+            if(exceptionType != null)
+            {
+                object[] args = null;
+                if(!String.IsNullOrEmpty(message))
+                {
+                    args = new object[1];
+                    args[0] = message;
+                }
+
+                exception = Activator.CreateInstance(exceptionType, args) as NMSException;
+            }
+            else
+            {
+                exception = new BrokerException(exceptionResponse.Exception);
+            }
+
+            return exception;
+        }
 	}
 }

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/BrokerToNMSExceptionsTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/BrokerToNMSExceptionsTest.cs?rev=1214575&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/BrokerToNMSExceptionsTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/BrokerToNMSExceptionsTest.cs
Thu Dec 15 00:21:51 2011
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.
+ */
+
+using System;
+using Apache.NMS.Test;
+using Apache.NMS.Util;
+using NUnit.Framework;
+
+namespace Apache.NMS.ActiveMQ.Test
+{
+    [TestFixture]
+    public class BrokerToNMSExceptionsTest : NMSTestSupport
+    {
+        private readonly String connectionURI = "tcp://${activemqhost}:61616";
+
+        [SetUp]
+        public override void SetUp()
+        {
+            base.SetUp();
+        }
+
+        [TearDown]
+        public override void TearDown()
+        {
+            base.TearDown();
+        }
+
+        [Test]
+        public void InvalidSelectorExceptionTest()
+        {
+            using(IConnection connection = CreateConnection())
+            using(ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge))
+            {
+                ITemporaryQueue queue = session.CreateTemporaryQueue();
+
+                try
+                {
+                    session.CreateConsumer(queue, "3+5");
+                    Assert.Fail("Should throw an InvalidSelectorException");
+                }
+                catch(InvalidSelectorException)
+                {
+                }
+            }
+        }
+
+        [Test]
+        public void InvalidClientIdExceptionTest()
+        {
+            Uri uri = URISupport.CreateCompatibleUri(NMSTestSupport.ReplaceEnvVar(connectionURI));
+            ConnectionFactory factory = new ConnectionFactory(uri);
+            Assert.IsNotNull(factory);
+            using(IConnection connection = factory.CreateConnection())
+            {
+                connection.ClientId = "FOO";
+                connection.Start();
+
+                try
+                {
+                    IConnection connection2 = factory.CreateConnection();
+                    connection2.ClientId = "FOO";
+                    connection2.Start();
+                    Assert.Fail("Should throw an InvalidSelectorException");
+                }
+                catch(InvalidClientIDException)
+                {
+                }
+            }
+        }
+    }
+}
+

Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/BrokerToNMSExceptionsTest.cs
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message