using System; using System.Collections.Generic; using System.Linq; using System.Text; using com.itac.mes.imsapi.client.dotnet; using com.itac.mes.imsapi.domain.container; using System.Reflection; using System.Runtime.Remoting.Proxies; using System.Runtime.Remoting.Messaging; using com.itac.mes.tools; using System.Diagnostics; using MesTools.ImsApi; using System.Collections.Specialized; using System.Globalization; namespace com.itac.mes.imsapi { /// /// Proxy for calling IMSAPI Functions /// all paramaters are logged automatically the same way as in the imsinterfaces /// public class ImsApiProxy : RealProxy { private IIMSApiDotNet delegateObject; public static bool parameterLogging = true; private ObjectEvaluator fieldEvaluator = new ObjectEvaluator(); private Dictionary methods = new Dictionary(); public ImsApiProxy(IIMSApiDotNet delegateObject) : base(typeof(IIMSApiDotNet)) { this.delegateObject = delegateObject; if (delegateObject != null) { MethodInfo[] apiMethodArray = typeof(IIMSApiDotNet).GetMethods(); foreach (MethodInfo method in apiMethodArray) { if (methods.ContainsKey(method.Name)) { continue; } methods.Add(method.Name, method); LogHandler.log(Constants.LOGGER, TraceEventType.Verbose, "calling method " + method.Name); } } } /// /// calling the functions /// /// /// public override IMessage Invoke(IMessage message) { // start der Methode DateTime dtStart = DateTime.Now; IMethodCallMessage methodMsg = (IMethodCallMessage)message; MethodInfo methodInfo = methods[methodMsg.MethodName]; StringBuilder paramStr = new StringBuilder(); // call api function on delegate object object result = methodInfo.Invoke(delegateObject, methodMsg.Args); Dictionary uploadValues = new Dictionary(); if (parameterLogging) { for (int i = 0; i < methodInfo.GetParameters().Length; i++) { ParameterInfo methodParameter = methodInfo.GetParameters()[i]; if (!ObjectEvaluator.CLASSES_IN_ONE_LINE) { paramStr.Append(" "); } // corresponding Key-Values? if (methodParameter.Name.EndsWith("Keys")) { // keep for evaluation of return values String baseName = methodParameter.Name.Substring(0, methodParameter.Name.Length - "Keys".Length); uploadValues.Add(baseName, (String[])methodMsg.Args[i]); // are there some more parameters if (methodInfo.GetParameters().Length > (i + 1)) { if (methodInfo.GetParameters()[i + 1].Name.StartsWith(baseName) && methodInfo.GetParameters()[i + 1].Name.EndsWith("Values")) { StringBuilder stb = new StringBuilder(); MesTools.ImsApi.ImsApiTools.printKeyValues(stb, baseName, (String[])methodMsg.Args[i], (String[])methodMsg.Args[i + 1]); paramStr.Append(stb.ToString()); paramStr.Append('\n'); i += 1; continue; } } } fieldEvaluator.evaluateObject(methodInfo.GetParameters()[i].Name, methodMsg.Args[i], paramStr); if (i < (methodInfo.GetParameters().Length - 1)) { if (ObjectEvaluator.CLASSES_IN_ONE_LINE) { paramStr.Append(", "); } else { paramStr.Append("\n"); } } } } StringBuilder returnValues = new StringBuilder(); // calculate execution time DateTime dtEnd = DateTime.Now; long elapsedTicks = dtEnd.Ticks - dtStart.Ticks; TimeSpan elapsedSpan = new TimeSpan(elapsedTicks); returnValues.Append(methodInfo.Name).Append("() completed in ").Append(elapsedSpan.ToString()).Append(" millies\n").Append(paramStr); if (parameterLogging) { StringBuilder stbResult = new StringBuilder(); if (result != null) { HashSet inArgNames = new HashSet(); for (int j = 0; j < methodMsg.InArgs.Length; j++) { inArgNames.Add(methodMsg.GetInArgName(j)); } List orderOutNames = new List(); Dictionary outValues = new Dictionary(); // alle out-args mit betrachten... for (int j = 0; j < methodMsg.Args.Length; j++) { if (!inArgNames.Contains(methodMsg.GetArgName(j))) { Debug.WriteLine("found out arg " + methodMsg.GetArgName(j)); orderOutNames.Add(methodMsg.GetArgName(j)); object k = methodMsg.GetArg(j); outValues.Add(methodMsg.GetArgName(j), (string[])methodMsg.GetArg(j)); } } stbResult.Append(new ImsApiResultEvaluator().evaluate(result, orderOutNames, outValues, uploadValues)); returnValues.Append("\nreturn:\n" + stbResult.ToString()); } } LogHandler.log(Constants.LOGGER, TraceEventType.Information, returnValues.ToString()); return new ReturnMessage(result, methodMsg.Args, 0, methodMsg.LogicalCallContext, methodMsg); } } }