activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgo...@apache.org
Subject svn commit: r719149 - /activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs
Date Thu, 20 Nov 2008 01:24:11 GMT
Author: jgomes
Date: Wed Nov 19 17:24:10 2008
New Revision: 719149

URL: http://svn.apache.org/viewvc?rev=719149&view=rev
Log:
Refactor NMSConnectionFactory to search for the config file and provider assemblies in the
following folder order:

1. Current folder.
2. Current executing assembly location folder.
3. AppDomain.CurrentDomain.BaseDirectory folder.
4. AppDomain.CurrentDomain.RelativeSearchPath folder.

This will help support ASP.NET and running as a service.  This change is based on a patch
submitted by Daniel Ellis.  Thanks, Dan!

Modified:
    activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs

Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs?rev=719149&r1=719148&r2=719149&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/NMSConnectionFactory.cs Wed
Nov 19 17:24:10 2008
@@ -97,18 +97,25 @@
 		/// <returns></returns>
 		private static Type GetTypeForScheme(string scheme)
 		{
+			string[] paths = GetConfigSearchPaths();
 			string assemblyFileName;
 			string factoryClassName;
 			Type factoryType = null;
 
-			if(LookupConnectionFactoryInfo(scheme, out assemblyFileName, out factoryClassName))
+			if(LookupConnectionFactoryInfo(paths, scheme, out assemblyFileName, out factoryClassName))
 			{
-#if NETCF
-				string assemblyFolder = "";
-#else
-				string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
-#endif
-				Assembly assembly = Assembly.LoadFrom(Path.Combine(assemblyFolder, assemblyFileName));
+				Assembly assembly = null;
+
+				foreach(string path in paths)
+				{
+					string fullpath = Path.Combine(path, assemblyFileName);
+
+					if(File.Exists(fullpath))
+					{
+						assembly = Assembly.LoadFrom(fullpath);
+						break;
+					}
+				}
 
 				if(null != assembly)
 				{
@@ -140,48 +147,78 @@
 		///		jms://localhost:7222
 		///
 		/// </summary>
+		/// <param name="paths">Folder paths to look in.</param>
 		/// <param name="scheme"></param>
 		/// <param name="assemblyFileName"></param>
 		/// <param name="factoryClassName"></param>
 		/// <returns></returns>
-		private static bool LookupConnectionFactoryInfo(string scheme, out string assemblyFileName,
out string factoryClassName)
+		private static bool LookupConnectionFactoryInfo(string[] paths, string scheme, out string
assemblyFileName, out string factoryClassName)
 		{
-#if NETCF
-			string assemblyFolder = "";
-#else
-			string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
-#endif
-			string configFileName = Path.Combine(assemblyFolder, String.Format("nmsprovider-{0}.config",
scheme.ToLower()));
+			string configFileName = String.Format("nmsprovider-{0}.config", scheme.ToLower());
 			bool foundFactory = false;
 
 			assemblyFileName = String.Empty;
 			factoryClassName = String.Empty;
 
-			try
+			foreach(string path in paths)
 			{
-				if(File.Exists(configFileName))
+				string fullpath = Path.Combine(path, configFileName);
+
+				try
 				{
-					XmlDocument configDoc = new XmlDocument();
+					if(File.Exists(fullpath))
+					{
+						XmlDocument configDoc = new XmlDocument();
 
-					configDoc.Load(configFileName);
-					XmlElement providerNode = (XmlElement) configDoc.SelectSingleNode("/configuration/provider");
+						configDoc.Load(fullpath);
+						XmlElement providerNode = (XmlElement) configDoc.SelectSingleNode("/configuration/provider");
 
-					if(null != providerNode)
-					{
-						assemblyFileName = providerNode.GetAttribute("assembly");
-						factoryClassName = providerNode.GetAttribute("classFactory");
-						if(String.Empty != assemblyFileName && String.Empty != factoryClassName)
+						if(null != providerNode)
 						{
-							foundFactory = true;
+							assemblyFileName = providerNode.GetAttribute("assembly");
+							factoryClassName = providerNode.GetAttribute("classFactory");
+							if(String.Empty != assemblyFileName && String.Empty != factoryClassName)
+							{
+								foundFactory = true;
+								break;
+							}
 						}
 					}
 				}
+				catch
+				{
+				}
+			}
+
+			return foundFactory;
+		}
+
+		/// <summary>
+		/// Get an array of search paths to look for config files.
+		/// </summary>
+		private static string[] GetConfigSearchPaths()
+		{
+			ArrayList pathList = new ArrayList();
+
+			// Check the current folder first.
+			pathList.Add("");
+#if !NETCF
+			AppDomain currentDomain = AppDomain.CurrentDomain;
+
+			// Check the folder the assembly is located in.
+			pathList.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
+			if(null != currentDomain.BaseDirectory)
+			{
+				pathList.Add(currentDomain.BaseDirectory);
 			}
-			catch
+
+			if(null != currentDomain.RelativeSearchPath)
 			{
+				pathList.Add(currentDomain.RelativeSearchPath);
 			}
+#endif
 
-			return foundFactory;
+			return (string[]) pathList.ToArray(typeof(string));
 		}
 
 		/// <summary>



Mime
View raw message