예제 #1
0
        private void CreateServiceApplicationProxy(BaristaServiceApplication serviceApp)
        {
            // get reference to the installed service proxy
            var serviceProxy = BaristaHelper.GetBaristaServiceProxy(SPFarm.Local);

            // create service app proxy
            var serviceAppProxy = new BaristaServiceApplicationProxy(
                ServiceAppName.Text + " Proxy",
                serviceProxy,
                serviceApp.Uri);

            serviceAppProxy.Update(true);

            // provision service app proxy
            serviceAppProxy.Provision();

            // start it if it isn't already started
            // ReSharper disable once RedundantCheckBeforeAssignment
            if (serviceAppProxy.Status != SPObjectStatus.Online)
            {
                serviceAppProxy.Status = SPObjectStatus.Online;
            }
            serviceAppProxy.Update(true);

            // add the proxy to the default group if selected
            if (!DefaultServiceApp.Checked)
            {
                return;
            }

            var defaultGroup = SPServiceApplicationProxyGroup.Default;

            defaultGroup.Add(serviceAppProxy);
            defaultGroup.Update(true);
        }
예제 #2
0
        private void SetupBaristaServiceApp()
        {
            // create a long running op..
            using (var op = new SPLongOperation(this))
            {
                op.Begin();

                try
                {
                    // get reference to the installed service
                    var service = BaristaHelper.GetBaristaService(SPFarm.Local);

                    // create the service application
                    var serviceApp = CreateServiceApplication(service);

                    // if the service instance isn't running, start it up
                    StartServiceInstances();

                    // create service app proxy
                    CreateServiceApplicationProxy(serviceApp);
                }
                catch (Exception e)
                {
                    throw new SPException("Error creating Barista service application.", e);
                }
            }
        }
        /// <summary>
        /// Given DataContract serialized BrewRequest/BrewResponse objects, deserialize and return a new context.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public static SPBaristaContext CreateContextFromXmlRequestResponse(string request, string response)
        {
            var brewRequest  = BaristaHelper.DeserializeXml <BrewRequest>(request);
            var brewResponse = BaristaHelper.DeserializeXml <BrewResponse>(response);

            return(new SPBaristaContext(brewRequest, brewResponse));
        }
예제 #4
0
        protected override void InternalProcessRecord()
        {
            #region validation stuff
            // ensure can hit farm
            var farm = SPFarm.Local;
            if (farm == null)
            {
                ThrowTerminatingError(new InvalidOperationException("SharePoint farm not found."), ErrorCategory.ResourceUnavailable, this);
                SkipProcessCurrentRecord();
                return;
            }

            // ensure can hit local server
            var server = SPServer.Local;
            if (server == null)
            {
                ThrowTerminatingError(new InvalidOperationException("SharePoint local server not found."), ErrorCategory.ResourceUnavailable, this);
                SkipProcessCurrentRecord();
                return;
            }

            // ensure can hit service application
            var service = BaristaHelper.GetBaristaService(farm);
            if (service == null)
            {
                ThrowTerminatingError(new InvalidOperationException("Barista Service not found (likely not installed)."), ErrorCategory.ResourceUnavailable, this);
                SkipProcessCurrentRecord();
                return;
            }

            // ensure can hit app pool
            var appPool = ApplicationPool.Read();
            if (appPool == null)
            {
                ThrowTerminatingError(new InvalidOperationException("Application pool not found."), ErrorCategory.ResourceUnavailable, this);
                SkipProcessCurrentRecord();
            }
            #endregion

            // verify a service app doesn't already exist
            var existingServiceApp = service.Applications.GetValue <BaristaServiceApplication>();
            if (existingServiceApp != null)
            {
                WriteError(new InvalidOperationException("Barista Service Application already exists."),
                           ErrorCategory.ResourceExists,
                           existingServiceApp);
                SkipProcessCurrentRecord();
            }

            // create & provision the service app
            if (!ShouldProcess(Name))
            {
                return;
            }

            var serviceApp = BaristaServiceApplication.Create(
                Name,
                service,
                appPool);

            // provision the service app
            serviceApp.Provision();

            //Check for and copy farm default property bag settings to the service app.
            if (farm.Properties.ContainsKey(BaristaHelper.BaristaTrustedLocationsPropertyBagKey))
            {
                serviceApp.Properties.Add(BaristaHelper.BaristaTrustedLocationsPropertyBagKey, farm.Properties[BaristaHelper.BaristaTrustedLocationsPropertyBagKey]);
                serviceApp.Update();
            }

            if (farm.Properties.ContainsKey(BaristaHelper.BaristaSearchIndexDefinitionsPropertyBagKey))
            {
                serviceApp.Properties.Add(BaristaHelper.BaristaSearchIndexDefinitionsPropertyBagKey, farm.Properties[BaristaHelper.BaristaSearchIndexDefinitionsPropertyBagKey]);
                serviceApp.Update();
            }

            if (farm.Properties.ContainsKey(BaristaHelper.BaristaPackageApprovalsPropertyBagKey))
            {
                serviceApp.Properties.Add(BaristaHelper.BaristaPackageApprovalsPropertyBagKey, farm.Properties[BaristaHelper.BaristaPackageApprovalsPropertyBagKey]);
                serviceApp.Update();
            }

            // pass service app back to PowerShell
            WriteObject(serviceApp);
        }
예제 #5
0
        protected override void InternalProcessRecord()
        {
            #region validation stuff
            // ensure can hit farm
            var farm = SPFarm.Local;
            if (farm == null)
            {
                ThrowTerminatingError(new InvalidOperationException("SharePoint farm not found."), ErrorCategory.ResourceUnavailable, this);
                SkipProcessCurrentRecord();
                return;
            }

            // ensure proxy installed
            var serviceProxy = BaristaHelper.GetBaristaServiceProxy(farm);
            if (serviceProxy == null)
            {
                ThrowTerminatingError(new InvalidOperationException("Barista Service Proxy not found (likely not installed)."), ErrorCategory.NotInstalled, this);
                SkipProcessCurrentRecord();
                return;
            }

            // ensure can hit service application
            var existingServiceAppProxy = serviceProxy.ApplicationProxies.GetValue <BaristaServiceApplicationProxy>();
            if (existingServiceAppProxy != null)
            {
                ThrowTerminatingError(new InvalidOperationException("Barista Service Application Proxy already exists."), ErrorCategory.ResourceExists, this);
                SkipProcessCurrentRecord();
                return;
            }
            #endregion

            Uri serviceApplicationAddress = null;
            switch (ParameterSetName)
            {
            case "Uri":
                serviceApplicationAddress = m_uri;
                break;

            case "ServiceApplication":
            {
                // make sure can get a refernce to service app
                var serviceApp = ServiceApplication.Read();
                if (serviceApp == null)
                {
                    WriteError(new InvalidOperationException("Service application not found."), ErrorCategory.ResourceExists, null);
                    SkipProcessCurrentRecord();
                    return;
                }

                // make sure can connect to service app
                var sharedServiceApp = serviceApp as ISharedServiceApplication;
                if (sharedServiceApp == null)
                {
                    WriteError(new InvalidOperationException("Service application not found."), ErrorCategory.ResourceExists, serviceApp);
                    SkipProcessCurrentRecord();
                    return;
                }

                serviceApplicationAddress = sharedServiceApp.Uri;
            }
            break;

            default:
                ThrowTerminatingError(new InvalidOperationException("Invalid parameter set."), ErrorCategory.InvalidArgument, this);
                break;
            }

            // create the service app proxy
            if ((serviceApplicationAddress == null) || !ShouldProcess(Name))
            {
                return;
            }

            var serviceAppProxy = new BaristaServiceApplicationProxy(
                Name,
                serviceProxy,
                serviceApplicationAddress);

            // provision the service app proxy
            serviceAppProxy.Provision();

            // pass service app proxy back to the PowerShell
            WriteObject(serviceAppProxy);
        }
        /// <summary>
        /// Returns a new instance of a script engine object with all runtime objects available.
        /// </summary>
        /// <returns></returns>
        public override IScriptEngine GetScriptEngine(WebBundleBase webBundle, out bool isNewScriptEngineInstance, out bool errorInInitialization)
        {
            isNewScriptEngineInstance = false;
            errorInInitialization     = false;

            //Based on the instancing mode, either retrieve the ScriptEngine from the desired store, or create a new ScriptEngine instance.
            var instanceSettings = SPBaristaContext.Current.Request.ParseInstanceSettings();

            ScriptEngine engine;

            switch (instanceSettings.InstanceMode)
            {
            case BaristaInstanceMode.PerCall:
                //Always create a new instance of the script engine.
                engine = new ScriptEngine();
                isNewScriptEngineInstance = true;
                break;

            case BaristaInstanceMode.Single:
                engine = BaristaSharePointGlobal.GetOrCreateScriptEngineInstanceFromRuntimeCache(instanceSettings.InstanceName, out isNewScriptEngineInstance);
                break;

            case BaristaInstanceMode.PerSession:
                engine = BaristaSharePointGlobal.GetOrCreateScriptEngineInstanceFromSession(instanceSettings.InstanceName, out isNewScriptEngineInstance);
                break;

            default:
                throw new NotImplementedException("The instance mode of " + instanceSettings.InstanceMode + " is currently not supported.");
            }

            if (SPBaristaContext.Current.Request.ShouldForceStrict())
            {
                engine.ForceStrictMode = true;
            }

            if (!isNewScriptEngineInstance)
            {
                return(engine);
            }

            var console = new FirebugConsole(engine)
            {
                Output = new SPBaristaConsoleOutput(engine)
            };

            //Register Bundles.
            var instance = new BaristaSharePointGlobal(engine);

            if (webBundle != null)
            {
                instance.Common.RegisterBundle(webBundle);
            }

            var binDirectory = "";

            if (HttpRuntime.AppDomainAppId != null)
            {
                binDirectory = HttpRuntime.BinDirectory;
            }

            instance.Common.RegisterBundle(new StringBundle());
            instance.Common.RegisterBundle(new SugarBundle());
            instance.Common.RegisterBundle(new SucraloseBundle());
            instance.Common.RegisterBundle(new LoDashBundle());
            instance.Common.RegisterBundle(new SPWebOptimizationBundle());
            instance.Common.RegisterBundle(new MomentBundle());
            instance.Common.RegisterBundle(new MustacheBundle());
            instance.Common.RegisterBundle(new LinqBundle());
            instance.Common.RegisterBundle(new JsonDataBundle());
            instance.Common.RegisterBundle(new SharePointBundle());
            instance.Common.RegisterBundle(new SharePointSearchBundle());
            instance.Common.RegisterBundle(new SharePointPublishingBundle());
            instance.Common.RegisterBundle(new SharePointContentMigrationBundle());
            instance.Common.RegisterBundle(new SharePointTaxonomyBundle());
            instance.Common.RegisterBundle(new SharePointWorkflowBundle());
            instance.Common.RegisterBundle(new SPActiveDirectoryBundle());
            instance.Common.RegisterBundle(new SPDocumentBundle());
            instance.Common.RegisterBundle(new DiagnosticsBundle());
            instance.Common.RegisterBundle(new iCalBundle());
            instance.Common.RegisterBundle(new SmtpBundle());
            instance.Common.RegisterBundle(new K2Bundle());
            instance.Common.RegisterBundle(new UtilityBundle());
            instance.Common.RegisterBundle(new UlsLogBundle());
            instance.Common.RegisterBundle(new DocumentStoreBundle());
            instance.Common.RegisterBundle(new SimpleInheritanceBundle());
            instance.Common.RegisterBundle(new SqlDataBundle());
            instance.Common.RegisterBundle(new StateMachineBundle());
            instance.Common.RegisterBundle(new DeferredBundle());
            instance.Common.RegisterBundle(new BaristaSearchIndexBundle());
            instance.Common.RegisterBundle(new WebAdministrationBundle());
            //instance.Common.RegisterBundle(new UnitTestingBundle());
            instance.Common.RegisterBundle(new WkHtmlToPdf.Library.WkHtmlToPdfBundle(binDirectory));

            //Let's do some DI
            var kernel = new StandardKernel();

            BaristaHelper.BindBaristaBundles(kernel, Path.Combine(binDirectory, "bundles"));

            //Let's get information about the approved packages
            var approvedPackages = new Dictionary <string, IList <ApprovedBundleInfo> >();

            try
            {
                var baristaServiceApplication = BaristaHelper.GetCurrentServiceApplication();
                if (baristaServiceApplication.Properties.ContainsKey("BaristaPackageApprovals"))
                {
                    var packageApprovals =
                        Convert.ToString(baristaServiceApplication.Properties["BaristaPackageApprovals"]);
                    if (!packageApprovals.IsNullOrWhiteSpace())
                    {
                        approvedPackages = GetApprovedPackages(JObject.Parse(packageApprovals));
                    }
                }
            }
            catch (Exception)
            {
                //Do Nothing...
            }

            foreach (var bundle in kernel.GetAll <IBundle>(m => IsApprovedPackage(m, approvedPackages)))
            {
                instance.Common.RegisterBundle(bundle);
            }

            //Global Types
            engine.SetGlobalValue("barista", instance);

            //engine.SetGlobalValue("file", new FileSystemInstance(engine));

            engine.SetGlobalValue("Guid", new GuidConstructor(engine));
            engine.SetGlobalValue("HashTable", new HashtableConstructor(engine));
            engine.SetGlobalValue("Uri", new UriConstructor(engine));
            engine.SetGlobalValue("Encoding", new EncodingConstructor(engine));

            engine.SetGlobalValue("NetworkCredential", new NetworkCredentialConstructor(engine));
            engine.SetGlobalValue("Base64EncodedByteArray", new Base64EncodedByteArrayConstructor(engine));

            engine.SetGlobalValue("console", console);


            //If we came from the Barista event receiver, set the appropriate context.
            if (
                SPBaristaContext.Current.Request != null &&
                SPBaristaContext.Current.Request.ExtendedProperties != null &&
                SPBaristaContext.Current.Request.ExtendedProperties.ContainsKey("SPItemEventProperties"))
            {
                var properties =
                    SPBaristaContext.Current.Request.ExtendedProperties["SPItemEventProperties"];

                var itemEventProperties = JsonConvert.DeserializeObject <BaristaItemEventProperties>(properties);
                engine.SetGlobalValue("CurrentItemEventProperties",
                                      new BaristaItemEventPropertiesInstance(engine.Object.InstancePrototype, itemEventProperties));
            }

            //Map Barista functions to global functions.
            engine.Execute(@"var help = function(obj) { return barista.help(obj); };
var require = function(name, assemblyName) { return barista.common.require(name, assemblyName); };
var listBundles = function() { return barista.common.listBundles(); };
var define = function() { return barista.common.define(arguments[0], arguments[1], arguments[2], arguments[3]); };
var include = function(scriptUrl) { return barista.include(scriptUrl); };");

            //Execute any instance initialization code.
            if (String.IsNullOrEmpty(instanceSettings.InstanceInitializationCode))
            {
                return(engine);
            }

            var initializationScriptSource =
                new StringScriptSource(SPBaristaContext.Current.Request.InstanceInitializationCode, SPBaristaContext.Current.Request.InstanceInitializationCodePath);

            try
            {
                engine.Execute(initializationScriptSource);
            }
            catch (JavaScriptException ex)
            {
                BaristaDiagnosticsService.Local.LogException(ex, BaristaDiagnosticCategory.JavaScriptException,
                                                             "A JavaScript exception was thrown while evaluating script: ");
                UpdateResponseWithJavaScriptExceptionDetails(engine, ex, SPBaristaContext.Current.Response);
                errorInInitialization = true;

                switch (instanceSettings.InstanceMode)
                {
                case BaristaInstanceMode.Single:
                    BaristaSharePointGlobal.RemoveScriptEngineInstanceFromRuntimeCache(
                        instanceSettings.InstanceName);
                    break;

                case BaristaInstanceMode.PerSession:
                    BaristaSharePointGlobal.RemoveScriptEngineInstanceFromRuntimeCache(
                        instanceSettings.InstanceName);
                    break;
                }
            }
            catch (Exception ex)
            {
                BaristaDiagnosticsService.Local.LogException(ex, BaristaDiagnosticCategory.Runtime,
                                                             "An internal error occured while evaluating script: ");
                errorInInitialization = true;
                switch (instanceSettings.InstanceMode)
                {
                case BaristaInstanceMode.Single:
                    BaristaSharePointGlobal.RemoveScriptEngineInstanceFromRuntimeCache(
                        instanceSettings.InstanceName);
                    break;

                case BaristaInstanceMode.PerSession:
                    BaristaSharePointGlobal.RemoveScriptEngineInstanceFromRuntimeCache(
                        instanceSettings.InstanceName);
                    break;
                }
                throw;
            }

            return(engine);
        }