Framework and FoundationsAvalon Framework is the central piece to the entire Avalon project. If you understand the contracts and constructs defined in the framework, you can understand anything that uses it. Remember the principles and patterns we have already discussed so far. In this section, we will expound on how the Role concept works practically, the lifecycle of components, and how the interfaces work. Defining the Service InterfaceIn Avalon, all components implements a Service, so we need to define the Service Interface and the associated semantic contract. Creating the Service InterfaceBelow you will find an example interface, followed by some best practices along with their reasoning. package org.apache.avalon.bizserver.docrepository; /** Interface to store and retrieve arbitrary Documents in a persisted store. * * The DocumentRepository is responsible to store Documents, create a reference * identity and be able to retrieve the same document at any point later in * time using the document identity returned in the <code>storeDocument()</code> * method call. */ public interface DocumentRepository { /** Retrieves a Document from the persisted document store. * * @return Document of the previously stored Document or null if not * found. * @throw SecurityException if the Principal is not authorized to * retrieve that document. */ Document retrieveDocument(Principal requestor, int refId) throws SecurityException; /** Store a Document into the persisted store. * * @return A Unique Document Identity, to be used later to retrieve the * same document. * @throw SecurityException if the Principal is not allowed to store * documents in the persisted store. */ int storeDocument( Principal requestor, Document doc ) throws SecurityException; } Best Practices
Overview of Framework InterfacesThe entire Avalon Framework can be divided into 5 main categories (as is the API): Activity, Service, Configuration, Context, Logger. Each of those categories represents a unique concern area. It is common for a component to implement several interfaces to identify all the concern areas that the component is concerned about. This will allow the component's container to manage each component in a consistent manner. Lifecycle for Avalon InterfacesWhen a framework implements several interfaces to separate the concerns of the component, there is potential for confusion over the order of method calls. Avalon Framework realizes this, so we developed the contract for lifecycle ordering of events. If your component does not implement the associated Interface, then simply skip to the next that will be called. The lifecycle order makes it easy to setup and prepare the component for use, in each of the lifecycle methods. The Lifecycle of a component is split into three phases: Initialization, Active Service, and Destruction. Because these phases are sequential, we will discuss them in that order. The Active Service phase is outside the concern of the container, and basically consists of other components calling the methods in the Service interface. In addition, the act of Construction and Finalization is implicit due to the Java language, so they will be skipped. The steps will list the method name, and the required interface. Within each phase, there will be a number of stages identified by method names. Those stages are executed if your component extends the associated interface specified in parenthesis. InitializationThis list of stages occurs in this specific order, and occurs only once during the life of the component.
DestructionThis list of stages occurs in the order specified, and occurs only once during the life of the component.
Avalon Framework ContractsIn this section, we will cover all the sections alphabetically. Activity
This group of interfaces refers to contracts for the life cycle of
the component. Each method allows the generic
Disposable
The
The contract surrounding this interface is that the
Initializable
The
The contract surrounding this interface is that the
Startable
The
The contract surrounding this interface is that the
Configuration
This group of interfaces describes the concern area of configuration.
If there are any problems, such as required
Configurable
Components that modify their exact behavior based on configurations
must implement this interface to obtain an instance of the
The contract surrounding this interface is that the
Configuration
The
There is a contract that says that if a
To simplify the code, the method calls
You will notice that you may not get parent
Context
The concept of the
Context
The
There is no set contract with the
Contextualizable
A component that wishes to receive the container's
The contract surrounding this interface is that the
LoggerEvery system needs the ability to log events. Avalon Merlin uses a subsystem called Avalon Logging which is capable of logger subsystem plug-ins. Currently, it supports Apache Log4J and the original Avalon LogKit. Expect more systems to follow soon. Also, it is fairly easy to create a new Avalon Logging Plug-In, in case you need to access a custom logging system. Logging is often done by statically addressing some factory method in the Logging subsystem. Avalon always tries to apply Inversion of Control, which means that the component should not try to figure out how to obtain a Logger instance, it should be given one by the container. The container can then be configured with the most suitable Logging system for the application, without any changes to the components. LogEnabled
Every component that needs a Logger instance implements this
interface. The interface has one method named
The contract surrounding this method is that it is called only once during the component's lifecycle before any other initialization step. Logger
The
ServiceThis is the core of Avalon Framework. Any interface defined in this concern area will throw ServiceException. Serviceable
A component that uses other components (what we call a
Dependency
) needs to implement this interface. The
interface has only one method
The contract surrounding this interface is that the
ServiceManager
The
|