Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Store authenticated hive sites in the runtime for better performance

  • Loading branch information...
commit 54721d24a8014ba16828d09f3f9151cb77050ac9 1 parent 235f792
@kmees kmees authored
Showing with 68 additions and 27 deletions.
  1. +68 −27 src/IronSharePoint/IronRuntime.cs
View
95 src/IronSharePoint/IronRuntime.cs
@@ -10,6 +10,7 @@
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
+using System.Linq;
namespace IronSharePoint
{
@@ -25,6 +26,7 @@ public class IronRuntime : IDisposable
private IronConsole.IronConsole _console;
private bool _isInitialized;
private ScriptRuntime _scriptRuntime;
+ private IList<Guid> _authenticatedSites;
private IronRuntime(Guid hiveId)
{
@@ -33,6 +35,7 @@ private IronRuntime(Guid hiveId)
DynamicTypeRegistry = new Dictionary<string, Object>();
DynamicFunctionRegistry = new Dictionary<string, Object>();
Engines = new Dictionary<string, IronEngine>();
+ _authenticatedSites = new List<Guid>();
}
internal static Dictionary<Guid, IronRuntime> LivingRuntimes
@@ -187,54 +190,92 @@ private void PrivilegedInitialize(string rubyRoot)
public static IronRuntime GetDefaultIronRuntime(SPSite targetSite)
{
const string RuntimeKey = "IronSP_Runtime";
- IronRuntime runtime = null;
- if (HttpContext.Current != null && HttpContext.Current.Items.Contains(RuntimeKey))
+ using (new SPMonitoredScope("Retrieving IronRuntime"))
{
- runtime = HttpContext.Current.Items[RuntimeKey] as IronRuntime;
- }
+ var runtime = TryGetFromAuthenticated(targetSite);
+ runtime = TryGetFromHttpContext(RuntimeKey, runtime);
- if (runtime == null)
- {
- using (new SPMonitoredScope("Retrieving IronRuntime"))
+ if (runtime == null)
{
- Guid hiveId = IronHiveRegistry.Local.GetHiveForSite(targetSite.ID);
- if (hiveId == Guid.Empty)
+ using (new SPMonitoredScope("Checking IronHiveRegistry"))
{
- throw new InvalidOperationException(
- String.Format("There is no IronHive mapping for the site with id {0}", targetSite.ID));
- }
+ Guid hiveId = IronHiveRegistry.Local.GetHiveForSite(targetSite.ID);
+ if (hiveId == Guid.Empty)
+ {
+ throw new InvalidOperationException(
+ String.Format("There is no IronHive mapping for the site with id {0}", targetSite.ID));
+ }
- if (!LivingRuntimes.ContainsKey(hiveId))
- {
- lock (_sync)
+ if (!LivingRuntimes.ContainsKey(hiveId))
{
- if (!LivingRuntimes.TryGetValue(hiveId, out runtime))
+ lock (_sync)
{
- using (new SPMonitoredScope("Creating IronRuntime"))
+ if (!LivingRuntimes.TryGetValue(hiveId, out runtime))
{
- runtime = new IronRuntime(hiveId);
- LivingRuntimes[hiveId] = runtime;
- runtime.Initialize();
+ using (new SPMonitoredScope("Creating IronRuntime"))
+ {
+ runtime = new IronRuntime(hiveId);
+ runtime.Authenticate(targetSite.ID);
+ LivingRuntimes[hiveId] = runtime;
+ runtime.Initialize();
+ }
}
}
}
+
+ runtime = LivingRuntimes[hiveId];
+ if (HttpContext.Current != null)
+ {
+ HttpContext.Current.Items[RuntimeKey] = runtime;
+ }
}
- runtime = LivingRuntimes[hiveId];
- if (HttpContext.Current != null)
- {
- HttpContext.Current.Items[RuntimeKey] = runtime;
- }
}
+
+
+ if (!runtime.IsInitialized)
+ {
+ ShowUnavailable();
+ }
+
+ return runtime;
}
+ }
- if (!runtime.IsInitialized)
+ private static IronRuntime TryGetFromHttpContext(string RuntimeKey, IronRuntime runtime)
+ {
+ if (HttpContext.Current != null)
{
- ShowUnavailable();
+ if (runtime != null)
+ {
+ HttpContext.Current.Items[RuntimeKey] = runtime;
+ }
+ else if (HttpContext.Current.Items.Contains(RuntimeKey))
+ {
+ runtime = HttpContext.Current.Items[RuntimeKey] as IronRuntime;
+ }
}
+ return runtime;
+ }
+ private static IronRuntime TryGetFromAuthenticated(SPSite targetSite)
+ {
+ IronRuntime runtime = LivingRuntimes.Values.FirstOrDefault(x => x.IsAuthenticated(targetSite.ID));
return runtime;
}
+ private void Authenticate(Guid targetSite)
+ {
+ if (!_authenticatedSites.Contains(targetSite))
+ {
+ _authenticatedSites.Add(targetSite);
+ }
+ }
+
+ private bool IsAuthenticated(Guid targetSite)
+ {
+ return _authenticatedSites.Contains(targetSite);
+ }
+
public IronEngine GetEngineByExtension(string extension)
{
return GetEngineByExtension(extension, true);

0 comments on commit 54721d2

Please sign in to comment.
Something went wrong with that request. Please try again.