protected override void Render(HtmlTextWriter writer) { if (_exception != null) { if (SPContext.Current.Web.UserIsSiteAdmin && engine.IronRuntime.IronHive.Web.CurrentUser.IsSiteAdmin) { var eo = engine.ScriptEngine.GetService <ExceptionOperations>(); string error = eo.FormatException(_exception); IronRuntime.LogError(String.Format("Error executing script {0}: {1}", ScriptName, error), _exception); if (engine != null) { new IronLogger(engine.IronRuntime).Log(String.Format("Ruby Error: {0} at {1}", _exception.Message, error)); } writer.Write(error); } else { writer.Write("Error occured."); } } try { ctrl.RenderControl(writer); } catch (Exception ex) { IronDiagnosticsService.Local.WriteTrace(1, IronDiagnosticsService.Local[IronCategoryDiagnosticsId.Controls], TraceSeverity.Unexpected, String.Format("Error: {0}; Stack: {1}", ex.Message, ex.StackTrace)); writer.Write(ex.Message); } }
//private void OnPreRequest(object sender, EventArgs e) //{ // var application = sender as HttpApplication; // var context = application.Context; // if (SPContext.Current != null) // { // var runtime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site); // context.Items[IronHelper.GetPrefixedKey("Runtime")] = runtime; // } //} void Error(object sender, EventArgs e) { var application = sender as HttpApplication; if (SPContext.Current != null) { var runtime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site); var logger = new IronLogger(runtime); var exception = application.Server.GetLastError(); logger.Log(String.Format("Error: {0} at {1}!", exception.Message, exception.StackTrace), LogLevel.Fatal); //hack: ruby engine hard coded var engine = runtime.GetEngineByExtension(".rb"); if (engine != null) { var eo = engine.ScriptEngine.GetService <ExceptionOperations>(); string error = eo.FormatException(exception); logger.Log(String.Format("Ruby Error: {0} at {1}", exception.Message, error), LogLevel.Fatal); } } }
public static IronRuntime GetDefaultIronRuntime(SPSite targetSite) { const string RuntimeKey = "IronSP_Runtime"; using (new SPMonitoredScope("Retrieving IronRuntime")) { var runtime = TryGetFromAuthenticated(targetSite); runtime = TryGetFromHttpContext(RuntimeKey, runtime); if (runtime == null) { using (new SPMonitoredScope("Checking IronHiveRegistry")) { 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.TryGetValue(hiveId, out runtime)) { 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; } } } if (!runtime.IsInitialized) { ShowUnavailable(); } return(runtime); } }
public override void Execute(Guid targetInstanceId) { using (SPSite site = new SPSite(HiveId)) { var runtime = IronRuntime.GetDefaultIronRuntime(site); if (String.IsNullOrEmpty(Script)) { runtime.IronConsole.Execute(Script, ".rb", true); } } }
public void ProcessRequest(HttpContext context) { var response = new IronConsoleResult(); var jsonResponse = string.Empty; try { var site = SPContext.Current.Site; var web = SPContext.Current.Web; if (!web.CurrentUser.IsSiteAdmin) { context.Response.Write("Only Site Admins are allowed to use the Console"); return; } IronHiveRegistry.Local.EnsureTrustedHive(site.ID); //var ironRuntime = IronRuntime.GetIronRuntime(site, site.ID); var ironRuntime = IronRuntime.GetDefaultIronRuntime(site); var extension = HttpContext.Current.Request["ext"]; var expression = HttpContext.Current.Request["expression"]; if (expression == "_ = (kill);_.inspect") { ironRuntime.Dispose(); response.Output = "Runtime disposed."; } else if (expression == "_ = (sp_status);_.inspect") { } else { response = ironRuntime.IronConsole.Execute(expression, extension); } } catch (Exception ex) { response.Error = ex.Message; response.StackTrace = ex.StackTrace; } finally { if (response == null) { response = new IronConsoleResult(); response.Error = "Request timed out"; } jsonResponse = response.ToJson(); } context.Response.Write(jsonResponse); }
public override object EvaluateExpression(object target, System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { string value = String.Empty; string scriptName = entry.Expression; try { string functionName = null; if (scriptName.Contains("@")) { var tmp = scriptName.Split('@'); functionName = tmp[0].Trim(); scriptName = tmp[1].Trim(); } else { throw new ArgumentException("Invalid expression! Use <%$Iron:My.sayHello@my/functions.rb"); } engine = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site).GetEngineByExtension(Path.GetExtension(scriptName)); value = engine.InvokeDynamicFunction(functionName, scriptName, target, entry).ToString(); } catch (Exception ex) { IronRuntime.LogError("Error", ex); if (SPContext.Current.Web.UserIsSiteAdmin && engine.IronRuntime.IronHive.Web.CurrentUser.IsSiteAdmin) { var eo = engine.ScriptEngine.GetService <ExceptionOperations>(); string error = eo.FormatException(ex); IronRuntime.LogError(String.Format("Error executing script {0}: {1}", scriptName, error), ex); value = error; if (engine != null) { new IronLogger(engine.IronRuntime).Log(String.Format("Ruby Error: {0} at {1}", ex.Message, error)); } } else { value = "Error occured"; } } return(value); }
private static IronRuntime TryGetFromHttpContext(string RuntimeKey, IronRuntime runtime) { if (HttpContext.Current != null) { if (runtime != null) { HttpContext.Current.Items[RuntimeKey] = runtime; } else if (HttpContext.Current.Items.Contains(RuntimeKey)) { runtime = HttpContext.Current.Items[RuntimeKey] as IronRuntime; } } return(runtime); }
public object CreateDynamicInstance(string className, string scriptName, params object[] args) { if (!IronRuntime.DynamicTypeRegistry.ContainsKey(className)) { ScriptEngine.Execute("require '" + scriptName + "'", IronRuntime.ScriptRuntime.Globals); if (!IronRuntime.DynamicTypeRegistry.ContainsKey(className)) { throw new NullReferenceException( String.Format("The class {0} in script file {1} is not registered in the DynamicTypeRegistry", className, scriptName)); } } return(IronRuntime.CreateDynamicInstance(className, args)); }
protected override void OnInit(EventArgs e) { try { if (String.IsNullOrEmpty(ScriptName)) { Exception = new InvalidEnumArgumentException("Property ScriptName is empty!"); } else if (String.IsNullOrEmpty(ScriptClass)) { Exception = new InvalidEnumArgumentException("Property ScriptClass is empty!"); } if (Exception != null) { return; } //Guid hiveId = String.IsNullOrEmpty(ScriptHiveId) ? Guid.Empty : new Guid(ScriptHiveId); //IronRuntime ironRuntime = IronRuntime.GetIronRuntime(SPContext.Current.Site, hiveId); IronRuntime ironRuntime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site); engine = ironRuntime.GetEngineByExtension(Path.GetExtension(ScriptName)); if (engine != null) { ctrl = engine.CreateDynamicInstance(ScriptClass, ScriptName) as Control; var dynamicControl = ctrl as IIronControl; if (dynamicControl != null) { dynamicControl.WebPart = null; dynamicControl.Data = null; dynamicControl.Config = Config; } this.Controls.Add(ctrl); } } catch (Exception ex) { Exception = ex; } base.OnInit(e); }
protected override void Render(HtmlTextWriter writer) { if (Exception != null) { if (SPContext.Current.Web.UserIsSiteAdmin && engine.IronRuntime.IronHive.Web.CurrentUser.IsSiteAdmin) { var eo = engine.ScriptEngine.GetService <ExceptionOperations>(); string error = eo.FormatException(Exception); IronRuntime.LogError(String.Format("Error executing script {0}: {1}", ScriptName, error), Exception); if (engine != null) { new IronLogger(engine.IronRuntime).Log(String.Format("Ruby Error: {0} at {1}", Exception.Message, error)); } writer.Write(error); } else { writer.Write("Error occured."); } } else { try { ctrl.RenderControl(writer); } catch (Exception ex) { writer.Write(ex.Message); IronRuntime.LogError("Error", ex); } } }
private static IronRuntime TryGetFromAuthenticated(SPSite targetSite) { IronRuntime runtime = LivingRuntimes.Values.FirstOrDefault(x => x.IsAuthenticated(targetSite.ID)); return(runtime); }
protected override void OnInit(EventArgs e) { try { if (String.IsNullOrEmpty(ScriptName)) { _exception = new InvalidEnumArgumentException("Property ScripName is empty!"); } else if (String.IsNullOrEmpty(ScriptClass)) { _exception = new InvalidEnumArgumentException("Property ScripClass is empty!"); } if (_exception != null) { return; } Guid hiveId = String.IsNullOrEmpty(ScriptHiveId) ? Guid.Empty : new Guid(ScriptHiveId); //IronRuntime ironRuntime = IronRuntime.GetIronRuntime(SPContext.Current.Site, hiveId); IronRuntime ironRuntime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site); engine = ironRuntime.GetEngineByExtension(Path.GetExtension(ScriptName)); if (engine != null) { ctrl = engine.CreateDynamicInstance(ScriptClass, ScriptName) as Control; var dynamicControl = ctrl as IIronControl; if (dynamicControl != null) { dynamicControl.WebPart = null; dynamicControl.Data = null; dynamicControl.Config = Config; } if (Template != null) { Template.InstantiateIn(ctrl); } else { if (!String.IsNullOrEmpty(TemplatePath)) { var path = TemplatePath.Replace("~site", SPContext.Current.Site.ServerRelativeUrl) .Replace("~web", SPContext.Current.Web.ServerRelativeUrl) .Replace("~hiveSite", engine.IronRuntime.IronHive.Site.ServerRelativeUrl) .Replace("~hiveWeb", engine.IronRuntime.IronHive.Web.ServerRelativeUrl) .Replace("~hiveFolder", engine.IronRuntime.IronHive.Folder.ServerRelativeUrl); Template = this.LoadTemplate(path); Template.InstantiateIn(ctrl); } } this.Controls.Add(ctrl); } } catch (Exception ex) { IronDiagnosticsService.Local.WriteTrace(1, IronDiagnosticsService.Local[IronCategoryDiagnosticsId.Controls], TraceSeverity.Unexpected, String.Format("Error: {0}; Stack: {1}", ex.Message, ex.StackTrace)); _exception = ex; } base.OnInit(e); }
internal IronEngine(IronRuntime ironRuntime, ScriptEngine scriptEngine) { IronRuntime = ironRuntime; ScriptEngine = scriptEngine; }