activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r1058235 - /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/TransportFactory.cs
Date Wed, 12 Jan 2011 17:07:08 GMT
Author: tabish
Date: Wed Jan 12 17:07:07 2011
New Revision: 1058235

URL: http://svn.apache.org/viewvc?rev=1058235&view=rev
Log:
https://issues.apache.org/jira/browse/AMQNET-306

Quick and dirty hack to allow manual additions of transports.  

Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/TransportFactory.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/TransportFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/TransportFactory.cs?rev=1058235&r1=1058234&r2=1058235&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/TransportFactory.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transport/TransportFactory.cs
Wed Jan 12 17:07:07 2011
@@ -16,6 +16,8 @@
  */
 
 using System;
+using System.Reflection;
+using System.Collections.Generic;
 using Apache.NMS.ActiveMQ.Transport.Discovery;
 using Apache.NMS.ActiveMQ.Transport.Failover;
 using Apache.NMS.ActiveMQ.Transport.Mock;
@@ -27,6 +29,9 @@ namespace Apache.NMS.ActiveMQ.Transport
 	{
 		public static event ExceptionListener OnException;
 
+        private static IDictionary<String, String> schemeToClassName = new Dictionary<String,
String>();
+        private static IDictionary<String, Type> schemeToTypeName = new Dictionary<String,
Type>();
+
 		public static void HandleException(Exception ex)
 		{
 			if(TransportFactory.OnException != null)
@@ -35,6 +40,36 @@ namespace Apache.NMS.ActiveMQ.Transport
 			}
 		}
 
+        public static void RegisterTransport(string scheme, string className)
+        {
+            if(String.IsNullOrEmpty(scheme))
+            {
+                throw new NMSException("Cannot register a transport with an empty scheme.");
+            }
+
+            if(String.IsNullOrEmpty(className))
+            {
+                throw new NMSException("Cannot register a transport with an empty className.");
+            }
+
+            TransportFactory.schemeToClassName[scheme.ToLower()] = className;
+        }
+
+        public static void RegisterTransport(string scheme, Type factoryType)
+        {
+            if(String.IsNullOrEmpty(scheme))
+            {
+                throw new NMSException("Cannot register a transport with an empty scheme.");
+            }
+
+            if(factoryType == null)
+            {
+                throw new NMSException("Cannot register a transport with an empty type name.");
+            }
+
+            TransportFactory.schemeToTypeName[scheme.ToLower()] = factoryType;
+        }
+
 		/// <summary>
 		/// Creates a normal transport. 
 		/// </summary>
@@ -95,6 +130,24 @@ namespace Apache.NMS.ActiveMQ.Transport
 					factory = new MockTransportFactory();
 					break;
 				default:
+
+                    // Types are easier lets check them first.
+                    if(TransportFactory.schemeToTypeName.ContainsKey(scheme))
+                    {
+                        Type objectType = schemeToTypeName[scheme];
+                        factory = CreateTransportByObjectType(objectType);
+                        break;
+                    }
+
+                    // Now we can look for Class names, may have to search lots of assemblies
+                    // for this one.
+                    if(TransportFactory.schemeToClassName.ContainsKey(scheme))
+                    {
+                        String className = schemeToClassName[scheme];
+                        factory = CreateTransportByClassName(className);
+                        break;
+                    }
+
 					throw new NMSConnectionException(String.Format("The transport {0} is not supported.",
scheme));
 				}
 			}
@@ -114,5 +167,52 @@ namespace Apache.NMS.ActiveMQ.Transport
 
 			return factory;
 		}
+
+        private static ITransportFactory CreateTransportByObjectType(Type objectType)
+        {
+            Tracer.Debug("Attempting to create a Transport by its Object Type: " + objectType.FullName);
+
+            try
+            {
+                return Activator.CreateInstance(objectType) as ITransportFactory;
+            }
+            catch
+            {
+                throw new NMSConnectionException("Could not create the transport with type
name: " + objectType);
+            }
+        }
+
+        private static ITransportFactory CreateTransportByClassName(String className)
+        {
+            Tracer.Debug("Attempting to create a Transport by its Class Name: " + className);
+
+            try
+            {
+                Assembly assembly = Assembly.GetExecutingAssembly();
+                Type type = assembly.GetType(className, false);
+
+                if(type == null)
+                {
+                    Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
+
+                    foreach(Assembly dll in loadedAssemblies)
+                    {
+                        Tracer.DebugFormat("Checking assembly {0} for class named {1}.",
dll.FullName, className);
+                        type = dll.GetType(className, false);
+
+                        if(type != null)
+                        {
+                            break;
+                        }
+                    }
+                }
+
+                return Activator.CreateInstance(type) as ITransportFactory;
+            }
+            catch
+            {
+                throw new NMSConnectionException("Could not create the transport with type
name: " + className);
+            }
+        }
 	}
 }



Mime
View raw message