asternet/Asterisk.2013/Asterisk.NET/FastAGI/MappingStrategy.cs

130 lines
4.3 KiB
C#
Raw Normal View History

2015-01-03 15:37:29 +00:00
using System;
using System.Collections;
using System.Reflection;
2015-01-03 15:37:29 +00:00
using System.Resources;
2014-01-08 14:16:39 +00:00
namespace AsterNET.FastAGI
{
2015-01-03 15:37:29 +00:00
/// <summary>
/// A MappingStrategy that is configured via a resource bundle.<br />
/// The resource bundle contains the script part of the url as key and the fully
/// qualified class name of the corresponding AGIScript as value.<br />
/// Example:
/// <pre>
/// noopcommand = AsterNET.FastAGI.Command.NoopCommand
/// </pre>
/// NoopCommand must implement the AGIScript interface and have a default constructor with no parameters.<br />
/// </summary>
[Obsolete("This class has been depreciated in favour of MappingStrategies.ResourceMappingStrategy", false)]
2013-08-21 10:31:26 +00:00
public class MappingStrategy : IMappingStrategy
2015-01-03 15:37:29 +00:00
{
#if LOGGER
2015-01-03 15:37:29 +00:00
private readonly Logger logger = Logger.Instance();
#endif
2015-01-03 15:37:29 +00:00
private string resourceName;
private Hashtable mapping;
2015-01-03 15:37:29 +00:00
public MappingStrategy()
{
resourceName = Common.AGI_DEFAULT_RESOURCE_BUNDLE_NAME;
mapping = null;
}
2013-08-21 10:31:26 +00:00
public MappingStrategy(string resourceName)
2015-01-03 15:37:29 +00:00
{
this.resourceName = resourceName;
mapping = null;
}
2015-01-03 15:37:29 +00:00
public AGIScript DetermineScript(AGIRequest request)
{
AGIScript script = null;
if (mapping != null)
lock (mapping.SyncRoot)
{
if (mapping.Contains(request.Script))
script = (AGIScript) mapping[request.Script];
}
return script;
}
2015-01-03 15:37:29 +00:00
public string ResourceBundleName
{
set
{
if (value == null)
{
mapping = null;
resourceName = null;
}
else if (resourceName != value)
{
resourceName = value;
Load();
}
}
}
2015-01-03 15:37:29 +00:00
public void Load()
{
string scriptName;
string className;
AGIScript agiScript;
2015-01-03 15:37:29 +00:00
if (mapping == null)
mapping = new Hashtable();
lock (mapping)
{
mapping.Clear();
try
{
var rr = new ResourceReader(AppDomain.CurrentDomain.BaseDirectory + resourceName);
foreach (DictionaryEntry de in rr)
{
scriptName = (string) de.Key;
className = (string) de.Value;
agiScript = CreateAGIScriptInstance(className);
if (mapping.Contains(scriptName))
throw new AGIException(String.Format("Duplicate mapping name '{0}' in file {1}", scriptName,
resourceName));
mapping.Add(scriptName, agiScript);
#if LOGGER
2015-01-03 15:37:29 +00:00
logger.Info("Added mapping for '" + scriptName + "' to class " + agiScript.GetType().FullName);
#endif
2015-01-03 15:37:29 +00:00
}
}
catch (Exception ex)
{
#if LOGGER
2015-01-03 15:37:29 +00:00
logger.Error("Resource bundle '" + resourceName + "' is missing.");
#endif
2015-01-03 15:37:29 +00:00
throw ex;
}
}
}
private AGIScript CreateAGIScriptInstance(string className)
{
Type agiScriptClass;
ConstructorInfo constructor;
AGIScript agiScript;
2015-01-03 15:37:29 +00:00
try
{
agiScriptClass = Type.GetType(className);
constructor = agiScriptClass.GetConstructor(new Type[] {});
agiScript = (AGIScript) constructor.Invoke(new object[] {});
}
catch (Exception ex)
{
#if LOGGER
2015-01-03 15:37:29 +00:00
logger.Error("Unable to create AGIScript instance of type " + className, ex);
return null;
#else
throw new AGIException("Unable to create AGIScript instance of type " + className, ex);
#endif
2015-01-03 15:37:29 +00:00
}
return agiScript;
}
}
}