Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HeuristicLabWeb.PluginHost/HLWebPluginHost/PluginLib/AssemblyResourceProvider.cs @ 5532

Last change on this file since 5532 was 4604, checked in by dkahn, 14 years ago

#1198 Imported new Plugin Host solution for the new MVC2 based web application

File size: 4.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Reflection;
6using System.IO;
7using System.Web.Hosting;
8using System.Web.Caching;
9using System.Collections;
10using System.Diagnostics;
11
12namespace HLWebPluginHost.PluginLib {
13
14  /// <summary>
15  /// The VirtualPathProvider class provides developers a way to intercept the calls MVC makes to
16  /// retrieve files from the filesystem and provide these files. In our case we look at the plugins
17  /// directory to find out if it provides the requested ressource.
18  /// In this case the data is pulled out of the plugin and returned if it was a file.
19  /// In Fact this class provides parts of the routing and dispatching mechanism known from other mvc frameworks.
20  /// </summary>
21  public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider {
22
23    /// <summary>
24    /// FileExists() is called by the framework to check for the existence of a physical file.
25    /// We want to override the method so we can check for the “~/Plugins/” string in our path.
26    /// If we find it, then we use reflection to determine if we have an embedded view in the targeted assembly that matches the requested file name. 
27    /// An example of a parameter that would be passed into this method would be “~/Plugins/FzySqrPlugin.dll/FzySqrPlugin.Views.HelloWorld.Index.aspx”. 
28    /// Given this path for a view, this code would load the FzySqrPlugin.dll and look for a resource with named FzySqrPlugin.Views.HelloWorld.Index.aspx.
29    /// If the”~/Plugins/” string is not found, then the super class’s FileExist() method is called and the normal behavior takes place. 
30    /// </summary>
31    /// <param name="virtualPath"></param>
32    /// <returns></returns>
33    public override bool FileExists(string virtualPath) {
34
35      if (IsAppResourcePath(virtualPath)) {
36        string path = VirtualPathUtility.ToAppRelative(virtualPath);
37        string[] parts = path.Split('/');
38        string assemblyName = parts[2];
39        string resourceName = parts[3];
40
41        assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
42        byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
43        Assembly assembly = Assembly.Load(assemblyBytes);
44
45        if (assembly != null) {
46          string[] resourceList = assembly.GetManifestResourceNames();
47          bool found = Array.Exists(resourceList, delegate(string r) { return r.Equals(resourceName); });
48
49          return found;
50        }
51        return false;
52      } else
53        return base.FileExists(virtualPath);
54    }
55
56    /// <summary>
57    /// If we have a plugin path, then return our custom AssemblyResourceVirtualFile type,
58    /// otherwise provide the standard behavior.
59    /// </summary>
60    /// <param name="virtualPath"></param>
61    /// <returns></returns>
62    public override VirtualFile GetFile(string virtualPath) {
63
64      if (IsAppResourcePath(virtualPath)) {
65        return new AssemblyResourceVirtualFile(virtualPath);
66      } else {
67        return base.GetFile(virtualPath);
68      }
69    }
70
71    /// <summary>
72    /// If we have a plugin path, return null (for now), otherwise invoke the super class’s method.
73    /// </summary>
74    /// <param name="virtualPath"></param>
75    /// <param name="virtualPathDependencies"></param>
76    /// <param name="utcStart"></param>
77    /// <returns></returns>
78    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
79
80      if (IsAppResourcePath(virtualPath)) {
81        return null;
82      }
83      return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
84    }
85
86    /// <summary>
87    /// check a virtual path and decide whether the resource is provided by the plugin or the plugin host.
88    /// To do this, we use the convention that all paths destined for plugin modules will begin with “~/Plugins/”.
89    /// In other words, the path ~/Plugins/MyPlugin.dll/SomeResource.aspx indicates that the resource it represents
90    /// is not a physical file and needs to be loaded from the MyPlugin.dll.
91    /// </summary>
92    /// <param name="virtualPath"></param>
93    /// <returns>True if a ressource at a given path is an app ressource</returns>
94    private bool IsAppResourcePath(string virtualPath) {
95      String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
96      return checkPath.StartsWith("~/Plugins/", StringComparison.InvariantCultureIgnoreCase);
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.