JavaScript is disabled on your browser.
javolution.context

Class AbstractContext

  • Direct Known Subclasses:
    ConcurrentContext, FormatContext, LocalContext, LogContext, SecurityContext


    @Realtime
    @Parallelizable(comment="Sequential configuration, parallel use")
    public abstract class AbstractContext
    extends Object 

    The parent class for all contexts. Contexts allow for cross cutting concerns (performance, logging, security, ...) to be addressed at run-time through OSGi published services without polluting the application code ( Separation of Concerns).

    Context configuration is performed in a try, finally block statement and impacts only the current thread (although inherited by inner ConcurrentContext threads).

     AnyContext ctx = AnyContext.enter(); // Enters a context scope. 
     try { 
     ctx.configure(...); // Local configuration (optional).
     ... // Current thread executes using the configured context.
     } finally {
     ctx.exit(); 
     }

    Version:
    6.0, July 21, 2013
    Author:
    Jean-Marie Dautelle
    • Constructor Summary

      Constructors
      Modifier Constructor and Description
      protected AbstractContext ()
      Default constructor.
    • Method Summary

      Methods
      Modifier and Type Method and Description
      static AbstractContext current ()
      Returns the current context for the current thread or null if this thread has no context (default).
      protected static <T extends AbstractContext>
      T
      current (Class<T> type)
      Returns the current context of specified type or null if none.
      static <T extends AbstractContext>
      T
      enter (Class<T> custom)
      Enters the scope of a custom context.
      protected AbstractContext enterInner ()
      Enters the scope of an inner context which becomes the current context; the previous current context becomes the outer of this context.
      void exit ()
      Exits the scope of this context; the outer of this context becomes the current context.
      protected AbstractContext getOuter ()
      Returns the outer context of this context or null if this context has no outer context.
      static void inherit (AbstractContext ctx)
      Inherits the specified context which becomes the context of the current thread.
      protected abstract AbstractContext inner ()
      Returns a new inner instance of this context inheriting the properties of this context.
    • Constructor Detail

      • AbstractContext

        protected AbstractContext()
        Default constructor.
    • Method Detail

      • current

        public static AbstractContext current()
        Returns the current context for the current thread or null if this thread has no context (default).
      • current

        protected static <T extends AbstractContext> T current(Class<T> type)
        Returns the current context of specified type or null if none.
      • enter

        public static <T extends AbstractContext> T enter(Class<T> custom)

        Enters the scope of a custom context. This method raises a SecurityException if the permission to enter contexts of the specified class is not granted. For example, the following disallow entering any custom context.

         SecurityContext ctx = SecurityContext.enter(); 
         try {
         ctx.revoke(new SecurityContext.Permission(AbstractContext.class, "enter"));
         ... // Cannot enter any custom context.
         } finally {
         ctx.exit(); // Back to previous security settings. 
         }

        Parameters:
        custom - the custom context to enter.
        Throws:
        IllegalArgumentException - if the specified class default constructor cannot be instantiated.
        SecurityException - if SecurityContext.Permission(custom, "enter") is not granted.
        See Also:
        SecurityContext.Permission
      • inherit

        public static void inherit(AbstractContext ctx)
        Inherits the specified context which becomes the context of the current thread. This method is particularly useful when creating new threads to make them inherits from the context stack of the parent thread.
         //Spawns a new thread inheriting the context of the current thread.
         MyThread myThread = new MyThread();
         myThread.inherited = AbstractContext.current(); 
         myThread.start(); 
         ...
         class MyThread extends Thread {
         AbstractContext inherited;
         public void run() {
         AbstractContext.inherit(inherited); // Sets current context. 
         ...
         }
         }

      • enterInner

        protected AbstractContext enterInner()
        Enters the scope of an inner context which becomes the current context; the previous current context becomes the outer of this context.
        Returns:
        the inner context entered.
      • exit

        public void exit()
        Exits the scope of this context; the outer of this context becomes the current context.
        Throws:
        IllegalStateException - if this context is not the current context.
      • getOuter

        protected AbstractContext getOuter()
        Returns the outer context of this context or null if this context has no outer context.
      • inner

        protected abstract AbstractContext inner()
        Returns a new inner instance of this context inheriting the properties of this context. The new instance can be configured independently from its parent.

Copyright © 2005-2013 Javolution. All Rights Reserved.

AltStyle によって変換されたページ (->オリジナル) /