1. Design Goals and Primary Roles
The com.aspectran.daemon.activity package provides the dedicated Activity implementation and virtualization adapters that actually execute translet invocations initiated programmatically (translate) from DaemonService.
The core architectural objectives of this package are:
- Execution Environment Concretization for Programmatic Invocation: Inherits from the abstract
CoreActivityto provideDaemonActivity, capable of fully driving the translet pipeline in a background daemon environment without physical network sockets or console I/O. - In-Memory Request/Response Simulation: Mounts adapters that completely simulate requests and responses in memory using
Map<String, Object>(attributes/parameters) andOutputStringWriterinstead of external HTTP streams. - Complete Session Lifecycle Management in Non-Web Environments: Accesses the virtual session (
access()) viaDefaultSessionAdapterwhileDaemonActivityexecutes, and guarantees an elaborate session agent lifecycle by flushing and committing session state (complete()) at completion time. - Seamless Bridge Between Engine and Caller: Converts virtual request methods (
MethodType), translet names (requestName), and arguments supplied by the caller into standard preparation (prepare()) and execution (perform()) phases ofCoreActivity.
2. Key Classes In-Depth Analysis
classDiagram
class CoreActivity {
<<abstract>>
#adapt()
#saveCurrentActivity()
#removeCurrentActivity()
}
class DaemonActivity {
-DaemonService daemonService
-String requestName
-MethodType requestMethod
-Map~String, Object~ attributeMap
-ParameterMap parameterMap
+getMode() Mode
+prepare()
#adapt()
#saveCurrentActivity()
#removeCurrentActivity()
}
class DaemonRequestAdapter {
-ParameterMap parameterMap
-Map~String, Object~ attributeMap
}
class DaemonResponseAdapter {
-Writer outputWriter
}
CoreActivity <|-- DaemonActivity
DaemonActivity ..> DaemonRequestAdapter : instantiates
DaemonActivity ..> DaemonResponseAdapter : instantiates
DaemonActivity (Concrete Implementation Class)
The final concrete Activity class tailored for the daemon runtime environment. It is instantiated per request whenever DaemonService.translate() is invoked.
- Core Responsibilities and Attributes:
daemonService: Holds the parent daemon service instance to retrieve the context, session manager, FlashMap manager, and Locale resolver.getMode(): ReturnsMode.DAEMON, ensuring daemon-specific branch logic operates correctly.requestName/requestMethod: Stores the target translet name and virtual HTTP method (GET,POST, etc.), binding the targetTransletRulebased on these during theprepare()phase.attributeMap/parameterMap: Holds request attributes and parameters passed programmatically.
- Deep Dive into Core Methods:
prepare():public void prepare() throws TransletNotFoundException, ActivityPrepareException { Assert.state(requestName != null, "requestName is not set"); Assert.state(requestMethod != null, "requestMethod is not set"); prepare(requestName, requestMethod); }Validates required parameters prior to invocation, then queries and binds the translet rule matching the name and method from the core engine’s rule registry.
adapt(): The core of daemon virtualization adaptation.@Override protected void adapt() throws AdapterException { setSessionAdapter(daemonService.newSessionAdapter()); DaemonRequestAdapter requestAdapter = new DaemonRequestAdapter(getTranslet().getRequestMethod()); if (attributeMap != null) { requestAdapter.setAttributeMap(attributeMap); } if (parameterMap != null) { requestAdapter.setParameterMap(parameterMap); } setRequestAdapter(requestAdapter); Writer outputWriter = new OutputStringWriter(); DaemonResponseAdapter responseAdapter = new DaemonResponseAdapter(outputWriter); setResponseAdapter(responseAdapter); setFlashMapManager(daemonService.getFlashMapManager()); setLocaleResolver(daemonService.getLocaleResolver()); super.adapt(); }- Binds a virtual session adapter via
daemonService.newSessionAdapter(). - Injects caller-supplied attributes and parameters into
DaemonRequestAdapterand registers it as the standard request adapter. - Creates a
DaemonResponseAdapterwrappingOutputStringWriterand sets it as the response adapter. Template rendering or string conversion results accumulate safely in this Writer buffer. - Connects the internationalization resolver (
LocaleResolver) and flash map manager registered in the service.
- Binds a virtual session adapter via
saveCurrentActivity()/removeCurrentActivity():@Override protected void saveCurrentActivity() { super.saveCurrentActivity(); if (isOriginalActivity() && hasSessionAdapter() && getSessionAdapter() instanceof DefaultSessionAdapter sessionAdapter) { sessionAdapter.getSessionAgent().access(); } } @Override protected void removeCurrentActivity() { if (isOriginalActivity() && hasSessionAdapter() && getSessionAdapter() instanceof DefaultSessionAdapter sessionAdapter) { sessionAdapter.getSessionAgent().complete(); } super.removeCurrentActivity(); }When entering the root activity (
isOriginalActivity), invokessessionAdapter.getSessionAgent().access()to update the session’s last access timestamp. Upon activity completion, callscomplete()to flush and synchronize modified session data in a thread-safe manner.
3. Interactions with Other Packages
com.aspectran.daemon.service:DefaultDaemonServicecreates aDaemonActivityfor everytranslate()request, injects parameters, and drives the execution pipeline.com.aspectran.daemon.adapter:DaemonActivityinternally instantiatesDaemonRequestAdapterandDaemonResponseAdapterto process requests and responses in memory.com.aspectran.core.activity:DaemonActivityinherits fromCoreActivity, fully reusing its built-in AOP advice chains (BEFORE,AFTER,THROWN,FINALLY), action dispatching (dispatchContent), and response transformation logic without modification.
4. Package Summary and Architectural Significance
The com.aspectran.daemon.activity package and DaemonActivity demonstrate the pinnacle of abstraction and isolation achieved by Aspectran’s adapter architecture:
- Total Physical Environment Abstraction: Even in a pure in-memory Java runtime without sockets or streams,
DaemonRequestAdapterandDaemonResponseAdaptercompletely virtualize the request-response cycle. - Zero Modification to the Core Engine:
CoreActivityhas no need to know whether it was invoked from the web or a daemon; it simply interacts with adapter interfaces to read parameters and record outputs. - Sophisticated Session Lifecycle Synchronization: Through the
saveCurrentActivityandremoveCurrentActivityhooks, access (access) and completion (complete) of session agents are strictly governed even in non-web environments, ensuring robust state maintenance.