1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Reflection;
|
---|
6 | using System.IO;
|
---|
7 | using System.Web.Hosting;
|
---|
8 | using System.Web.Caching;
|
---|
9 | using System.Collections;
|
---|
10 | using System.Diagnostics;
|
---|
11 |
|
---|
12 | namespace 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 | } |
---|