KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > application > DefaultBlockContext


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.application;
9
10 import java.io.File JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import org.apache.avalon.framework.context.ContextException;
13 import org.apache.avalon.framework.logger.Logger;
14 import org.apache.avalon.phoenix.BlockContext;
15 import org.apache.avalon.phoenix.interfaces.ApplicationContext;
16 import org.apache.avalon.phoenix.metadata.SarMetaData;
17
18 /**
19  * Context via which Blocks communicate with container.
20  *
21  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
22  */

23 final class DefaultBlockContext
24     implements BlockContext
25 {
26     private String JavaDoc m_name;
27     private ApplicationContext m_applicationContext;
28
29     protected DefaultBlockContext( final String JavaDoc name,
30                                    final ApplicationContext frame )
31     {
32         m_name = name;
33         m_applicationContext = frame;
34     }
35
36     public Object JavaDoc get( Object JavaDoc key )
37         throws ContextException
38     {
39         final SarMetaData metaData = m_applicationContext.getMetaData();
40         if( BlockContext.APP_NAME.equals( key ) )
41         {
42             return metaData.getName();
43         }
44         else if( BlockContext.APP_HOME_DIR.equals( key ) )
45         {
46             return metaData.getHomeDirectory();
47         }
48         else if( BlockContext.NAME.equals( key ) )
49         {
50             return m_name;
51         }
52         else
53         {
54             throw new ContextException( "Unknown key: " + key );
55         }
56     }
57
58     /**
59      * Base directory of .sar application.
60      *
61      * @return the base directory
62      */

63     public File JavaDoc getBaseDirectory()
64     {
65         return m_applicationContext.getMetaData().getHomeDirectory();
66     }
67
68     /**
69      * Retrieve name of block.
70      *
71      * @return the name of block
72      */

73     public String JavaDoc getName()
74     {
75         return m_name;
76     }
77
78     public void requestShutdown()
79     {
80         m_applicationContext.requestShutdown();
81     }
82
83     public InputStream JavaDoc getResourceAsStream( final String JavaDoc name )
84     {
85         return m_applicationContext.getResourceAsStream( name );
86     }
87
88     /**
89      * Retrieve logger coresponding to named category.
90      *
91      * @return the logger
92      */

93     public Logger getLogger( final String JavaDoc name )
94     {
95         return m_applicationContext.getLogger( getName() ).getChildLogger( name );
96     }
97 }
98
Popular Tags