Differences between revisions 1 and 2
Revision 1 as of 2004-01-10 19:43:04
Size: 4684
Editor: cache4-outside-b
Comment: fixed formatting -farra
Revision 2 as of 2005-03-22 05:40:07
Size: 4451
Editor: anonymous
Comment: missing edit-log entry for this revision
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
{{{ public void init(final ServletConfig pServletConfig) throws ServletException }}}
{{{ { }}}
{{{ ... }}}
{{{
final DefaultContext lContext = }}}
{{{
new DefaultContext(initializeMap(),FortressConfig.createDefaultConfig()); }}}
{{{
lContext.makeReadOnly(); }}}
{{{ final FortressConfig lConfig = new FortressConfig(lContext); }}}
{{{ ... }}}
{{{ } }}}
{{{ private Map initializeMap() }}}
{{{ { }}}
{{{ Map lMap = new
Hashtable(); }}}
{{{
lMap.put(CACHE_KEY, getServletConfig.getInitParameter("enable-cache")); }}}
{{{ return lMap; }}}
{{{ } }}}
{{{
private static String CACHE_KEY = "app.cache"; }}}
 }}}
   public void init(final ServletConfig pServletConfig) throws ServletException
    
{          ...
final DefaultContext lContext =
new DefaultContext(initializeMap(),FortressConfig.createDefaultConfig());
lContext.makeReadOnly();
        final FortressConfig lConfig = new FortressConfig(lContext);
        ...
    
}
    private Map initializeMap()
    
{          Map lMap = new Hashtable();
lMap.put(CACHE_KEY, getServletConfig.getInitParameter("enable-cache"));
        return lMap;
    
}   private static String CACHE_KEY = "app.cache";
}}}
Line 32: Line 32:
{{{ public static final String ROLE = MimeTypeService.class.getName(); }}}
{{{ /** Request a mime type for a file name. */ }}}
{{{ public String get(final String pFilename); }}}
    public static final String ROLE = MimeTypeService.class.getName();
    /** Request a mime type for a file name. */
    public String get(final String pFilename);
Line 43: Line 43:
{{{ private final ServletContext m_ServletContext; }}}
{{{
public ServletMimeTypeService(final ServletContext pServletContext) }}}
{{{ { }}}
{{{ m_ServletContext = pServletContext; }}}
{{{ } }}}
{{{
public String get(final String pFilename) }}}
{{{ { }}}
{{{
return m_ServletContext.getMimeType(pFilename); }}}
{{{ } }}}
    private final ServletContext m_ServletContext;
public ServletMimeTypeService(final ServletContext pServletContext)
    {
        m_ServletContext = pServletContext;
    
}   public String get(final String pFilename)
    
{   return m_ServletContext.getMimeType(pFilename);
    
} 
Line 58: Line 58:
{{{ public void init(final ServletConfig pServletConfig) throws ServletException }}}
{{{ { }}}
{{{ ... }}}
{{{
final ServletContext lServletContext = pServletConfig.getServletContext(); }}}
{{{
final DefaultServiceManager lServiceManager = new DefaultServiceManager(); }}}
{{{
lServiceManager.put(MimeTypeService.ROLE, new ServletMimeTypeService(lServletContext); }}}
{{{ final FortressConfig lConfig = new FortressConfig(); }}}
{{{ lConfig.setServiceManager(lServiceManager); }}}
{{{ ... }}}
{{{ } }}}
    public void init(final ServletConfig pServletConfig) throws ServletException
    
{          ...
final ServletContext lServletContext = pServletConfig.getServletContext();
final DefaultServiceManager lServiceManager = new DefaultServiceManager();
lServiceManager.put(MimeTypeService.ROLE, new ServletMimeTypeService(lServletContext);
        final FortressConfig lConfig = new FortressConfig();
        lConfig.setServiceManager(lServiceManager);
        ...
    
} 

Fortress comes with examples showing the initialization of a Fortress application done by initializing the root container of the application with an instance of class [http://avalon.apache.org/api/org/apache/avalon/fortress/util/FortressConfig.html FortressConfig]. The elements of the [http://avalon.apache.org/api/org/apache/avalon/fortress/util/FortressConfig.html FortressConfig] are fixed and each example currently assumes, that they are enough for starting the component and service logic. This is normally true for standalone applications, but if the root container is embedded into an environment like a servlet container or into a plug-in, this might not be enough.

The [http://avalon.apache.org/api/org/apache/avalon/fortress/util/FortressConfig.html FortressConfig] class creates internally two [http://avalon.apache.org/api/org/apache/avalon/framework/context/Context.html Context] objects, one for the configuration of the root container and one for the default (system) configuration, that is set as the parent context of the first one. Contexts are hierarchical. If a key is not found in the current context, it is requested from its parent. Therefore we have to create our own Context object, if we want to support additional configuration parameters from the embedding application.

[http://avalon.apache.org/api/org/apache/avalon/fortress/util/FortressConfig.html FortressConfig] has an additional constructor to define the parent context on our own. It is used to set our own context as parent of the Fortress root container's context. The easiest solution is to create an instance of [http://avalon.apache.org/api/org/apache/avalon/framework/context/DefaultContext.html DefaultContext] and initialize it with a populated map. Do not forget to set the parent of your own context. This should still be the default configuration created with [http://avalon.apache.org/api/org/apache/avalon/fortress/util/FortressConfig.html#createDefaultConfig() FortressConfig.createDefaultContext].

See example code used in your servlets init method:

   public void init(final ServletConfig pServletConfig) throws ServletException 
    { 
        ... 
        final DefaultContext lContext =  
            new DefaultContext(initializeMap(),FortressConfig.createDefaultConfig()); 
        lContext.makeReadOnly(); 
        final FortressConfig lConfig = new FortressConfig(lContext); 
        ... 
    }
    private Map initializeMap() 
    { 
        Map lMap = new Hashtable();
        lMap.put(CACHE_KEY, getServletConfig.getInitParameter("enable-cache")); 
        return lMap; 
    } 
    private static String CACHE_KEY = "app.cache"; 

You might add also additional services provided by the servlet context istelf i.e. a servive for mime types. Assuming you have an interface for this:

public interface MimeTypeService 
{
    public static final String ROLE = MimeTypeService.class.getName(); 
    /** Request a mime type for a file name. */ 
    public String get(final String pFilename); 
}

and an implementation for a servlet environment:

public class ServletMimeTypeService implements MimeTypeService
{
    private final ServletContext m_ServletContext; 
    public ServletMimeTypeService(final ServletContext pServletContext) 
    { 
        m_ServletContext = pServletContext; 
    } 
    public String get(final String pFilename)  
    { 
        return m_ServletContext.getMimeType(pFilename); 
    } 
}

you can make this service available for your Fortress container by using an own [http://avalon.apache.org/api/org/apache/avalon/framework/service/DefaultServiceManager.html DefaultServiceManager] and put this into the [http://avalon.apache.org/api/org/apache/avalon/fortress/util/FortressConfig.html FortressConfig] during the servlet init:

    public void init(final ServletConfig pServletConfig) throws ServletException 
    { 
        ... 
        final ServletContext lServletContext = pServletConfig.getServletContext(); 
        final DefaultServiceManager lServiceManager = new DefaultServiceManager(); 
        lServiceManager.put(MimeTypeService.ROLE, new ServletMimeTypeService(lServletContext); 
        final FortressConfig lConfig = new FortressConfig(); 
        lConfig.setServiceManager(lServiceManager); 
        ...
    } 

Naturally you can combine this with your own context described above.

AvalonFortressFAQ/ExtendingTheFortressConfiguration (last edited 2009-09-20 23:16:43 by localhost)