コード例 #1
0
ファイル: SessionStateInternal.cs プロジェクト: nickchal/pash
 internal object GetVariableValueFromProvider(VariablePath variablePath, out CmdletProviderContext context, out SessionStateScope scope, CommandOrigin origin)
 {
     scope = null;
     if (variablePath == null)
     {
         throw PSTraceSource.NewArgumentNullException("variablePath");
     }
     context = null;
     DriveScopeItemSearcher searcher = new DriveScopeItemSearcher(this, variablePath);
     object obj2 = null;
     if (searcher.MoveNext())
     {
         PSDriveInfo current = (PSDriveInfo)searcher.Current;
         if (current == null)
         {
             return obj2;
         }
         context = new CmdletProviderContext(this.ExecutionContext, origin);
         context.Drive = current;
         Collection<IContentReader> contentReader = null;
         try
         {
             contentReader = this.GetContentReader(new string[] { variablePath.QualifiedName }, context);
         }
         catch (ItemNotFoundException)
         {
             return obj2;
         }
         catch (System.Management.Automation.DriveNotFoundException)
         {
             return obj2;
         }
         catch (ProviderNotFoundException)
         {
             return obj2;
         }
         catch (NotImplementedException exception)
         {
             ProviderInfo provider = null;
             this.Globber.GetProviderPath(variablePath.QualifiedName, out provider);
             throw this.NewProviderInvocationException("ProviderCannotBeUsedAsVariable", SessionStateStrings.ProviderCannotBeUsedAsVariable, provider, variablePath.QualifiedName, exception, false);
         }
         catch (NotSupportedException exception2)
         {
             ProviderInfo info3 = null;
             this.Globber.GetProviderPath(variablePath.QualifiedName, out info3);
             throw this.NewProviderInvocationException("ProviderCannotBeUsedAsVariable", SessionStateStrings.ProviderCannotBeUsedAsVariable, info3, variablePath.QualifiedName, exception2, false);
         }
         if ((contentReader == null) || (contentReader.Count == 0))
         {
             return obj2;
         }
         if (contentReader.Count > 1)
         {
             foreach (IContentReader reader in contentReader)
             {
                 reader.Close();
             }
             PSArgumentException e = PSTraceSource.NewArgumentException("path", "SessionStateStrings", "VariablePathResolvedToMultiple", new object[] { variablePath.QualifiedName });
             ProviderInfo info4 = null;
             this.Globber.GetProviderPath(variablePath.QualifiedName, out info4);
             throw this.NewProviderInvocationException("ProviderVariableSyntaxInvalid", SessionStateStrings.ProviderVariableSyntaxInvalid, info4, variablePath.QualifiedName, e);
         }
         IContentReader reader2 = contentReader[0];
         try
         {
             IList list = reader2.Read(-1L);
             if (list == null)
             {
                 return obj2;
             }
             if (list.Count == 0)
             {
                 return null;
             }
             if (list.Count == 1)
             {
                 return list[0];
             }
             return list;
         }
         catch (Exception exception4)
         {
             ProviderInfo info5 = null;
             this.Globber.GetProviderPath(variablePath.QualifiedName, out info5);
             CommandProcessorBase.CheckForSevereException(exception4);
             ProviderInvocationException exception5 = new ProviderInvocationException("ProviderContentReadError", SessionStateStrings.ProviderContentReadError, info5, variablePath.QualifiedName, exception4);
             throw exception5;
         }
         finally
         {
             reader2.Close();
         }
     }
     return obj2;
 }
コード例 #2
0
        /// <summary>
        /// Looks up the specified variable and returns the context under which 
        /// the variable was found as well as the variable itself.
        /// </summary>
        /// 
        /// <param name="variablePath">
        /// The VariablePath helper for the variable.
        /// </param>
        /// 
        /// <param name="scope">
        /// The scope the variable was found in. Null if the variable wasn't found.
        /// </param>
        ///
        /// <param name="context">
        /// Returns the context under which the variable was found. The context will
        /// have the drive data already set. This will be null if the variable was
        /// not found.
        /// </param>
        /// 
        /// <param name="origin">
        /// The origin of the caller of this API
        /// </param>
        /// 
        /// <returns>
        /// The variable if it was found or null if it was not.
        /// </returns>
        /// 
        /// <remarks>
        /// The <paramref name="variablePath" /> is first parsed to see if it contains a drive
        /// specifier or special scope.  If a special scope is found ("LOCAL" or "GLOBAL") 
        /// then only that scope is searched for the variable. If any other drive specifier
        /// is found the lookup goes in the following order.
        ///     - current scope
        ///     - each consecutive parent scope until the variable is found.
        ///     - global scope
        /// </remarks>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="variablePath"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="variablePath"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="variablePath"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="variablePath"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
#pragma warning disable 0162
        internal object GetVariableValueFromProvider(
            VariablePath variablePath,
            out CmdletProviderContext context,
            out SessionStateScope scope,
            CommandOrigin origin)
        {
            scope = null;

            if (variablePath == null)
            {
                throw PSTraceSource.NewArgumentNullException("variablePath");
            }

            Dbg.Diagnostics.Assert(
                !variablePath.IsVariable,
                "This method can only be used to retrieve provider content");

            context = null;

            DriveScopeItemSearcher searcher =
                new DriveScopeItemSearcher(
                    this,
                    variablePath);

            object result = null;

            do // false loop
            {
                if (!searcher.MoveNext())
                {
                    break;
                }

                PSDriveInfo drive = ((IEnumerator<PSDriveInfo>)searcher).Current;

                if (drive == null)
                {
                    break;
                }

                // Create a new CmdletProviderContext and set the drive data

                context = new CmdletProviderContext(this.ExecutionContext, origin);

                context.Drive = drive;

#if true
                // PSVariable get/set is the get/set of content in the provider

                Collection<IContentReader> readers = null;

                try
                {
                    readers =
                        GetContentReader(new string[] { variablePath.QualifiedName }, context);
                }
                // If the item is not found we just return null like the normal
                // variable semantics.
                catch (ItemNotFoundException)
                {
                    break;
                }
                catch (DriveNotFoundException)
                {
                    break;
                }
                catch (ProviderNotFoundException)
                {
                    break;
                }
                catch (NotImplementedException notImplemented)
                {
                    // First get the provider for the path.

                    ProviderInfo providerInfo = null;
                    string unused =
                        this.Globber.GetProviderPath(variablePath.QualifiedName, out providerInfo);

                    throw NewProviderInvocationException(
                        "ProviderCannotBeUsedAsVariable",
                        SessionStateStrings.ProviderCannotBeUsedAsVariable,
                        providerInfo,
                        variablePath.QualifiedName,
                        notImplemented,
                        false);
                }
                catch (NotSupportedException notSupported)
                {
                    // First get the provider for the path.

                    ProviderInfo providerInfo = null;
                    string unused =
                        this.Globber.GetProviderPath(variablePath.QualifiedName, out providerInfo);

                    throw NewProviderInvocationException(
                        "ProviderCannotBeUsedAsVariable",
                        SessionStateStrings.ProviderCannotBeUsedAsVariable,
                        providerInfo,
                        variablePath.QualifiedName,
                        notSupported,
                        false);
                }

                if (readers == null || readers.Count == 0)
                {
                    // The drive was found but the path was wrong or something so return null.
                    // We don't want to continue searching if the provider didn't support content
                    // or the path wasn't found.
                    break;
                }

                if (readers.Count > 1)
                {
                    // Since more than one path was resolved, this is an error.

                    //Before throwing exception. Close the readers to avoid sharing violation.
                    foreach (IContentReader r in readers)
                    {
                        r.Close();
                    }

                    PSArgumentException argException =
                        PSTraceSource.NewArgumentException(
                            "path",
                            SessionStateStrings.VariablePathResolvedToMultiple,
                            variablePath.QualifiedName);

                    // First get the provider for the path.

                    ProviderInfo providerInfo = null;
                    string unused =
                        this.Globber.GetProviderPath(variablePath.QualifiedName, out providerInfo);

                    throw NewProviderInvocationException(
                        "ProviderVariableSyntaxInvalid",
                        SessionStateStrings.ProviderVariableSyntaxInvalid,
                        providerInfo,
                        variablePath.QualifiedName,
                        argException);
                }

                IContentReader reader = readers[0];

                try
                {
                    // Read all the content
                    IList resultList = reader.Read(-1);

                    if (resultList != null)
                    {
                        if (resultList.Count == 0)
                        {
                            result = null;
                        }
                        else if (resultList.Count == 1)
                        {
                            result = resultList[0];
                        }
                        else
                        {
                            result = resultList;
                        }
                    }
                }
                catch (Exception e) // Third-party callout, catch-all OK
                {
                    // First get the provider for the path.
                    ProviderInfo providerInfo = null;
                    string unused =
                        this.Globber.GetProviderPath(variablePath.QualifiedName, out providerInfo);

                    CommandProcessorBase.CheckForSevereException(e);

                    ProviderInvocationException providerException =
                        new ProviderInvocationException(
                            "ProviderContentReadError",
                            SessionStateStrings.ProviderContentReadError,
                            providerInfo,
                            variablePath.QualifiedName,
                            e);

                    throw providerException;
                }
                finally
                {
                    reader.Close();
                }

#else
                    try
                    {
                        GetItem(variablePath.LookupPath.ToString(), context);
                    }
                    catch (ItemNotFoundException)
                    {
                        break;
                    }

                    Collection<PSObject> items = context.GetAccumulatedObjects ();

                    if (items != null &&
                        items.Count > 0)
                    {
                        result = items[0];

                        if (!items[0].basObjectIsEmpty)
                        {
                            result = items[0].BaseObject;
                        }

                        try
                        {
                            DictionaryEntry entry = (DictionaryEntry)result;
                            result = entry.Value;
                        }
                            // Since DictionaryEntry is a value type we have to
                            // try the cast and catch the exception to determine
                            // if it is a DictionaryEntry type.
                        catch (InvalidCastException)
                        {
                        }
                    }

#endif
                break;
            } while (false);

            return result;
        } // GetVariableFromProvider