ActivityContext: The Heart of Aspectran

1. Core Concept: The Central Hub of the Application

ActivityContext is the core container (IoC Container) and the most central interface of an Aspectran application. It acts as a Central Hub that oversees and manages all of the application’s configuration information, components (Beans), and runtime execution state. This is a concept similar to the ApplicationContext in the Spring Framework, and it is responsible for the entire lifecycle from the start to the end of the application.

ActivityContext is an interface, and within the framework, the com.aspectran.core.context.DefaultActivityContext class is mainly used as the core implementation of this interface.

2. Key Responsibilities and Roles

The roles of ActivityContext can be broadly divided as follows:

a. Component Lifecycle Management

  • Manages the creation, initialization, and destruction of all core components registered in the application.
  • The doInitialize() and doDestroy() methods of DefaultActivityContext serially call the lifecycle methods of all registered registries and renderers internally to maintain a consistent state.

b. Core Component Registry

  • Provides an access point to the registries that store and manage all the rules and components of the application. This allows all elements of the application to interact with each other through the ActivityContext.
  • BeanRegistry: Creates and manages instances of all bean objects defined by @Bean or XML configuration. It performs the practical role of a Dependency Injection (DI) container.
  • AspectRuleRegistry: Stores AspectRule, the core of aspects (AOP), and provides information on which advice should be applied to a specific joinpoint.
  • TransletRuleRegistry: Manages all the rules (TransletRule) of the core processing unit, the translet.
  • ScheduleRuleRegistry: Manages rules related to scheduling tasks (ScheduleRule).

c. Environment Access

  • Provides access to the Environment object, which represents the application’s execution environment.
  • This allows each component to behave differently depending on the currently active profile or the property values defined in .properties files. For example, you can configure it to use a local database in the dev profile and a production database in the prod profile.

d. Activity Management

  • Manages the lifecycle of the Activity object, which is a single unit of execution in the application. An Activity represents the processing of a single request, such as a web request, a scheduled task, or a shell command.
  • Thread Safety based on ThreadLocal: It uses a ThreadLocal variable called currentActivityHolder to independently manage the currently executing Activity for each thread. This is a very important feature that ensures that each request does not affect the state of other requests in a multi-threaded environment (e.g., a web application).
  • Through defaultActivity, it supports the ability to perform basic functions even in a non-request context where there is no ongoing Activity.

e. Resource Loading & i18n

  • Manages the ClassLoader that loads the application’s classes and resources, and the ApplicationAdapter that abstracts interaction with the external environment.
  • Provides an access point to MessageSource for internationalization (i18n), allowing the application to respond to various language environments.

3. Overall Operational Flow

  1. Bootstrapping: When an Aspectran application starts, ActivityContextBuilder parses configuration files (XML or Apon) and annotations to create a DefaultActivityContext instance.
  2. Registry Configuration: Based on the parsed configuration information, various registries such as BeanRegistry and AspectRuleRegistry are created and populated with rule information.
  3. Context Injection: The created registries and the Environment object are injected into DefaultActivityContext.
  4. Initialization: As the ActivityContext is initialized, it serially initializes all the components it manages (registries, singleton beans, renderers, etc.).
  5. Waiting for Requests: Once initialization is complete, the context is ready to handle external requests (HTTP requests, schedule executions, etc.).
  6. Activity Creation: When a request comes in, a new Activity object is created to handle that request.
  7. Thread Context Registration: The created Activity is registered in the ActivityContext as the “Current Activity” for the current thread.
  8. Request Processing: The Activity obtains the necessary beans through the ActivityContext, executes translets, and applies aspects to process the request.
  9. Thread Context Removal: When request processing is complete, the “CurrentActivity” for that thread is removed from the ActivityContext.
  10. Destruction: When the application shuts down, the destroy() method of ActivityContext is called to safely destroy all the components it managed.

4. Summary

ActivityContext is the central control unit of an Aspectran application and the starting point for everything. As a container that holds all core components and configuration information, it manages their lifecycle and provides a consistent method of access. In particular, it ensures stability in a multi-threaded environment by independently managing the Activity for each thread. Therefore, understanding ActivityContext is most crucial for understanding the entire architecture of the Aspectran framework.