ymikulski    2002/08/13 05:58:47

  Added:       csframework/src/cs/Context ContextException.cs
                        DefaultContext.cs IContext.cs IContextualizable.cs
                        IRecontextualizable.cs IResolvable.cs
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Context/ContextException.cs
  
  Index: ContextException.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Context
  {
        /// <summary>
        /// The Exception signalling a badly formed IContext.
        /// </summary>
        /// <remarks>
        ///     This can be thrown by IContext object when a entry is not
        /// found. It can also be thrown manually in Contextualize()
        /// when Component detects a malformed context value.
        /// </remarks> 
        public class ContextException: Exception
        {
                /// <summary>
                /// Constructs a new <code>ContextException</code> instance.
                /// </summary>
                public ContextException(): this(null)
                {
                }
  
                /// <summary>
                /// Constructs a new <code>ContextException</code> instance.
                /// </summary>
                /// <param name="message">The Detail message for this 
exception.</param>
                public ContextException(string message): this(message, null)
                {
                }
  
                /// <summary>
                /// Constructs a new <code>ContextException</code> instance.
                /// </summary>
                /// <param name="message">The Detail message for this 
exception.</param>
                /// <param name="inner">The Root cause of the exception.</param>
                public ContextException(string message, Exception inner): base 
(message, inner)
                {
                }
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Context/DefaultContext.cs
  
  Index: DefaultContext.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  using System.Collections;
  
  
  namespace Apache.Avalon.Context
  {
        /// <summary>
        /// Default implementation of IContext.
        /// </summary>
        public class DefaultContext: IContext, IEnumerable
        {
                private IDictionary components;
                private IContext parent;
                private bool readOnly;
                
                /// <summary>
                /// Creates a context with specified data and parent.
                /// </summary>
                /// <param name="data">The Context data.</param>
                /// <param name="context">The Parent context (may be null).</param>
                public DefaultContext(IDictionary data, IContext context)
                {
                        parent = context;
                        components = data;
                }
  
                /// <summary>
                /// Creates a context with specified data.
                /// </summary>
                /// <param name="data">The Context data.</param>
                public DefaultContext(IDictionary data): this (data, null )
                {
                }
  
                /// <summary>
                /// Creates a context with specified parent.
                /// </summary>
                /// <param name="parent">The Parent context (may be null).</param>
                public DefaultContext(IContext parent): this (new Hashtable(), parent)
                {
                }
  
                /// <summary>
                /// Creates a context with no parent.
                /// </summary>
                public DefaultContext(): this ((IContext) null)
                {
                }
  
                /// <summary>
                /// Gets the context data.
                /// </summary>
                /// <value>The Context data.</value>
                protected IDictionary Components 
                {
                        get
                        {
                                return components;
                        }
                }
  
                /// <summary>
                /// Gets the parent context if any.
                /// </summary>
                /// <value>The Parent context.</value>
                protected IContext Parent
                {
                        get
                        {
                                return parent;
                        }
                }
  
                /// <summary>
                /// Gets a value indicating whether the context is read-only.
                /// </summary>
                /// <value>True if the context is read-only; otherwise, false.</value>
                public bool IsReadOnly 
                {
                        get 
                        {
                                return readOnly;
                        }
                }
  
                /// <summary>
                /// Retrieves an item from the context.
                /// </summary>
                /// <value>a context item</value>
                public object this [object key] 
                {
  
                        get 
                        {
                                object component =  Components[key];
                                object result = null;
  
                                if (component != null)
                                {
                                        if (component is IResolvable) 
                                        {
                                                result = ((IResolvable) 
component).Resolve(this);
                                        }
                                        else 
                                        {
                                                result = component;
                                        }
                                }
  
                                // If there is no one, check the parent.
                                if (Parent == null)
                                {
                                        throw new 
ContextException(string.Format("Unable to locate {0}.", key));
                                }
                                else
                                {
                                        result = Parent[key];
                                }
  
                                return result;
                        }       
  
                        set 
                        {
                                CheckReadOnly();
  
                                Components[key] = value;
                        }
                }
  
                /// <summary>
                /// Makes the context be read-only.
                /// </summary>
                public void MakeReadOnly()
                {
                        readOnly = true;
                }
                
                /// <summary>
                /// Utility method to check if context is read only and if true throw 
an exception.
                /// </summary>
                /// <exception cref="ContextException">If the context is read 
only.</exception>
                protected void CheckReadOnly()
                {
                        if (IsReadOnly) 
                        {
                                throw new ContextException("Context is read only and 
can not be modified");
                        }
                }
  
                /// <summary>
                /// Returns an <see cref="IDictionaryEnumerator"/> that can iterate 
through the context.
                /// </summary>
                /// <returns>An <see cref="IDictionaryEnumerator"/> for the 
context.</returns>
                public IEnumerator GetEnumerator()
                {
                        return components.GetEnumerator();
                }
  
        }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/csframework/src/cs/Context/IContext.cs
  
  Index: IContext.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Context
  {
        /// <summary>
        /// The context is the interface through which the Component
        /// and it's Container communicate.
        /// 
        /// Each Container-Component relationship will also involve defining
        /// a contract between two entities. This contract will specify the
        /// services, settings and information that is supplied by the
        /// Container to the Component.
        ///
        /// This relationship should be documented in a well known place.
        /// It is sometimes convenient to derive from IContext to provide
        /// a particular style of IContext for your Component-Container
        /// relationship. The documentation for required entries in context
        /// can then be defined there.
        /// </summary>
        public interface IContext
        {
                /// <summary>
                /// Retrieves an object from IContext.
                /// </summary>
                /// <value>The Object</value>
                /// <exception cref="ContextException">
                /// If object not found. Note that this means that
                /// either Component is asking for invalid entry or
                /// the Container is not living up to contract.
                /// </exception>
                object this[object index] 
                {
                        get; 
                }
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Context/IContextualizable.cs
  
  Index: IContextualizable.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Context
  {
        /// <summary>
        /// This inteface should be implemented by components that need
        /// a IContext to work. IContext contains runtime generated object
        /// provided by the Container to this Component.
        /// </summary>
        public interface IContextualizable
        {
                /// <summary>
                /// Pass the IContext to the component. 
                /// This method is called after the 
<c>ILogEnabled.EnableLogging(ILogger logger)</c>
                /// (if present) method and before any other method.
                /// </summary>
                /// <param name="context">The IContext.</param>
                /// <exception cref="ContextException">If the context is 
invalid.</exception>
                void Contextualize(IContext context);
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Context/IRecontextualizable.cs
  
  Index: IRecontextualizable.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Context
  {
        /// <summary>
        /// Extends IContextualizable to allow recontextualizing.
        /// This allows a component to re-receive it's context if 
        /// container environment has changed.
        /// </summary>
        public interface IRecontextualizable: IContextualizable
        {
                /// <summary>
                /// Pass the new IContext to the component. 
                /// This method is usually called when component is suspended via use 
of
                /// <c>ISuspendable.Suspend()</c> method.
                /// </summary>
                /// <param name="context">The IContext.</param>
                /// <exception cref="ContextException">If the context is 
invalid.</exception>
                void Recontextualize(IContext context);
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Context/IResolvable.cs
  
  Index: IResolvable.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Context
  {
        /// <summary>
        /// This interface is used to indicate objects that need to be
        /// resolved in some particular context.
        /// </summary>
        public interface IResolvable
        {
                /// <summary>
                /// Resolves a object to a value.
                /// </summary>
                /// <param name="context">The IContext with respect which to 
resolve.</param> 
                /// <returns>The Resolved object.</returns> 
                /// <exception cref="ContextException"></exception>
                object Resolve(IContext context);
        }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to