private IResourceAction ReadAction(XPathNavigator actionNode) { IResourceAction resourceAction = new ResourceAction(); try { XmlResourceHelper.AddActionToResource(actionNode, resourceAction); } catch (Exception e) { Logger.LogError(e); throw new ApplicationException(Properties.Resources.COULD_NOT_READ_RESOURCES); } return resourceAction; }
/// <summary> /// This is the main method for adding actions to the resources. /// It is currently assumed that only a single action class and /// action property set class exists in the assembly. If the action /// property set does not exist then an empty one will be added /// </summary> /// <remarks> /// The action will not be added if an action of the same name already exists /// in the internal list /// </remarks> /// <param name="assemblypath">The fully specified path and filename of the assembly to be loaded</param> /// <returns>The resource action created from the action assembly, null if the Add was cancelled</returns> public IResourceAction Add(string assemblypath) { IResourceAction resourceAction = new ResourceAction(); bool actionClassFound = false; Logger.LogDebug("Trying to load assembly: " + assemblypath); try { Assembly actionAssembly = Assembly.LoadFrom(assemblypath); // Need to find the action class and the property class within the assembly // Note that we're just getting the first one we find of each Type[] types = actionAssembly.GetTypes(); //QUE??? if (OnBeforeAddResourceAction != null) { ResourceActionEventArgs args = new ResourceActionEventArgs(resourceAction); OnBeforeAddResourceAction(this, args); if (args.Cancel) { //We no longer want to add this action so can return straight away return null; } } actionClassFound = AddIAction3(types, actionAssembly, resourceAction as ResourceAction); if (!actionClassFound) actionClassFound = AddIAction(types, actionAssembly, resourceAction as ResourceAction); if (actionClassFound) { resourceAction.Assembly = actionAssembly.ManifestModule.Name; //check for duplicate action already existing if (DuplicateFoundInList(resourceAction) && OnDuplicateResourceAction != null) { ResourceActionEventArgs args = new ResourceActionEventArgs(resourceAction); do { OnDuplicateResourceAction(this, args); } while (!args.Cancel && DuplicateFoundInList(resourceAction)); if (args.Cancel) { //We no longer want to add this action so can return straight away return null; } } } } catch (ReflectionTypeLoadException ex) { foreach (Exception e in ex.LoaderExceptions) { Logger.LogError(string.Format(CultureInfo.InvariantCulture, "ResourceActions: error loading DLL {0}, {1}", assemblypath, e.GetType().ToString())); Logger.LogError(e); Console.WriteLine(e.Message + e.GetType().ToString()); } } catch (Exception e) { // File couldn't be found or couldn't be opened Logger.LogError("ResourceActions: error loading DLL " + assemblypath); Logger.LogError(e); throw new ApplicationException(Properties.Resources.COULD_NOT_LOAD_ACTION); } if (actionClassFound) { // Does the action already exist? If it does then don't add it. if (Contains(resourceAction)) { Logger.LogError(String.Format(CultureInfo.InvariantCulture, "ResourceActions: error loading DLL {0}, as action {1} already exists", assemblypath, resourceAction.Name)); throw new ApplicationException(Properties.Resources.COULD_NOT_LOAD_ACTION_ALREADY_EXISTS); } Add(resourceAction); return resourceAction; } Logger.LogError(String.Format(CultureInfo.InvariantCulture, "ResourceActions: error loading DLL {0}, no action or property set in the dll", assemblypath)); throw new ApplicationException(Properties.Resources.COULD_NOT_LOAD_ACTION); }
/// <summary> /// Populate a resource action given the class and property set and /// add it to the internal list of resource actions. If the action /// property set does not exist (passed in as null) then an empty one /// will be added. /// </summary> /// <remarks> /// The action will not be added to the list if an action of the same /// name is already present /// </remarks> /// <param name="actionClass">The type of the action to be added</param> /// <param name="actionPropertySet">The type of the action property set to be added</param> /// <returns>The populated IResourceAction</returns> public IResourceAction Add(Type actionClass, Type actionPropertySet) { IResourceAction resourceAction = new ResourceAction(); // Check the inputs are what they say they are if (actionClass.GetInterface(typeof(IAction3).FullName) == null) { Logger.LogError("ResourceActions:Add actionClass does not implement IAction3"); throw new ApplicationException(Properties.Resources.COULD_NOT_LOAD_ACTION); } if (actionPropertySet != null) { if (actionPropertySet.GetInterface(typeof(IActionPropertySet).FullName) == null) { Logger.LogError("ResourceActions:Add actionPropertySet does not implement IActionPropertySet"); throw new ApplicationException(Properties.Resources.COULD_NOT_LOAD_ACTION); } } LoadAction(actionClass.Assembly, resourceAction, actionClass); // Does the action already exist? If it does then don't add it if (Contains(resourceAction)) { Logger.LogError(String.Format(CultureInfo.InvariantCulture, "ResourceActions: error loading DLL {0}, as action {1} already exists", actionClass.Assembly, resourceAction.Name)); throw new ApplicationException(Properties.Resources.COULD_NOT_LOAD_ACTION_ALREADY_EXISTS); } LoadActionProperties(actionPropertySet.Assembly, resourceAction, actionPropertySet); Add(resourceAction); return resourceAction; }
private bool AddIAction(Type[] types, Assembly actionAssembly, ResourceAction resourceAction) { bool actionClassFound = false; bool actionPropertyFound = false; bool actionPropertySetLayoutFound = false; foreach (Type type in types) { if (type.GetInterface(typeof(IAction).FullName) != null) { actionClassFound = true; Logger.LogDebug("Loading action class: " + type.FullName); resourceAction.ActionClass = type.FullName; resourceAction.SetAction(actionAssembly.CreateInstance(type.FullName) as IAction); if (null != OnBeforeAddResourceAction) { ResourceActionEventArgs args = new ResourceActionEventArgs(resourceAction); OnBeforeAddResourceAction(this, args); if (args.Cancel) { //We no longer want to add this action so can return straight away return false; } } //check for duplicate action already existing if (DuplicateFoundInList(resourceAction) && OnDuplicateResourceAction != null) { ResourceActionEventArgs args = new ResourceActionEventArgs(resourceAction); do { OnDuplicateResourceAction(this, args); } while (!args.Cancel && DuplicateFoundInList(resourceAction)); if (args.Cancel) { //We no longer want to add this action so can return straight away return false; } } } if (type.GetInterface(typeof(IActionPropertySet).FullName) != null) { actionPropertyFound = true; LoadActionProperties(actionAssembly, resourceAction, type); } else { CreateActionProperties(resourceAction); } //If we have this interface, we can populate an extra data structure //and get some extra layout info if (type.GetInterface(typeof(IPropertySetLayout).FullName) != null) { actionPropertySetLayoutFound = true; LoadActionPropertyLayout(actionAssembly, resourceAction, type); } if (actionClassFound && actionPropertyFound && actionPropertySetLayoutFound) { return true; } } // The least requirement is for the action and the action properties to be present. return (actionClassFound && actionPropertyFound); }
private bool AddIAction3(Type[] types, Assembly assembly, ResourceAction resourceAction) { Type action2Type = null; Type actionInstallerType = null; Type customDataType = null; foreach(Type type in types) { if (type.GetInterface(typeof(IAction3).FullName) != null && action2Type == null) action2Type = type; if (typeof(ActionInstaller).IsAssignableFrom(type) && actionInstallerType == null) actionInstallerType = type; if (type.GetInterface(typeof(ICustomDataType).FullName) != null && customDataType == null) customDataType = type; } if (action2Type == null) return false; Logger.LogDebug("Loading action class: " + action2Type.FullName); resourceAction.ActionClass = action2Type.FullName; IAction3 action2 = assembly.CreateInstance(action2Type.FullName) as IAction3; resourceAction.SetAction(action2); if (customDataType != null) resourceAction.CustomDataTypeUI = assembly.CreateInstance(customDataType.FullName) as ICustomDataType; ResourceActionProperties properties = new ResourceActionProperties(); properties.SetActionProperties(action2.PropertySet); resourceAction.Properties = properties; PolicyType[] supportedPolicyTypes = null; RunAt[] execLocations = null; if (actionInstallerType != null) { ActionInstaller installer = assembly.CreateInstance(actionInstallerType.FullName) as ActionInstaller; if(installer.ExecuteLocations != null) { execLocations = new RunAt[installer.ExecuteLocations.Count]; installer.ExecuteLocations.CopyTo(execLocations, 0); } if (installer.SupportedPolicyTypes != null) { supportedPolicyTypes = new PolicyType[installer.SupportedPolicyTypes.Count]; installer.SupportedPolicyTypes.CopyTo(supportedPolicyTypes, 0); } resourceAction.ImpliesIncident = installer.ImpliesIncident; resourceAction.AllowTransparent = installer.AllowsTransparency; } //default to supporting all if (supportedPolicyTypes == null) supportedPolicyTypes = new PolicyType[] { PolicyType.ActiveContent, PolicyType.ClientEmail, PolicyType.ClientRemovableMedia, PolicyType.NetworkContentDiscovery, PolicyType.Mta, PolicyType.NetMon }; resourceAction.SupportedPolicySetting = supportedPolicyTypes; //default to supporting all if(execLocations == null) execLocations = new RunAt[] { RunAt.Both }; resourceAction.ExecutionTarget = execLocations; resourceAction.ExecutionTargetCapabilities = (RunAt[])execLocations.Clone(); return true; }