KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.excalibur.i18n.ResourceManager;
11 import org.apache.avalon.excalibur.i18n.Resources;
12 import org.apache.avalon.framework.component.ComponentManager;
13 import org.apache.avalon.framework.configuration.Configuration;
14 import org.apache.avalon.framework.configuration.ConfigurationException;
15 import org.apache.avalon.framework.context.Context;
16 import org.apache.avalon.framework.logger.AbstractLogEnabled;
17 import org.apache.avalon.framework.logger.Logger;
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.avalon.framework.service.ServiceManager;
20 import org.apache.avalon.phoenix.interfaces.ApplicationContext;
21 import org.apache.avalon.phoenix.metadata.BlockListenerMetaData;
22 import org.apache.excalibur.containerkit.lifecycle.ResourceProvider;
23
24 /**
25  * The accessor used to access resources for a particular
26  * Block or Listener.
27  *
28  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
29  * @version $Revision: 1.3 $ $Date: 2002/08/06 11:57:39 $
30  */

31 class ListenerResourceProvider
32     extends AbstractLogEnabled
33     implements ResourceProvider
34 {
35     private static final Resources REZ =
36         ResourceManager.getPackageResources( ListenerResourceProvider.class );
37
38     /**
39      * Context in which Blocks/Listeners operate.
40      */

41     private final ApplicationContext m_context;
42
43     public ListenerResourceProvider( final ApplicationContext context )
44     {
45         if( null == context )
46         {
47             throw new NullPointerException JavaDoc( "context" );
48         }
49
50         m_context = context;
51     }
52
53     /**
54      * Create Block for specified entry.
55      *
56      * @param entry the entry
57      * @return a new object
58      * @throws Exception
59      */

60     public Object JavaDoc createObject( final Object JavaDoc entry )
61         throws Exception JavaDoc
62     {
63         final BlockListenerMetaData metaData = getMetaData( entry );
64         final ClassLoader JavaDoc classLoader = m_context.getClassLoader();
65         final Class JavaDoc clazz =
66             classLoader.loadClass( metaData.getClassname() );
67         return clazz.newInstance();
68     }
69
70     /**
71      * Retrieve Logger for specified listener.
72      *
73      * @param entry the entry representing listener
74      * @return the new Logger object
75      * @throws Exception if an error occurs
76      */

77     public Logger createLogger( final Object JavaDoc entry )
78         throws Exception JavaDoc
79     {
80         final BlockListenerMetaData metaData = getMetaData( entry );
81         final String JavaDoc name = metaData.getName();
82         return m_context.getLogger( name );
83     }
84
85     public Context createContext( Object JavaDoc entry )
86         throws Exception JavaDoc
87     {
88         throw new UnsupportedOperationException JavaDoc();
89     }
90
91     public ComponentManager createComponentManager( final Object JavaDoc entry )
92         throws Exception JavaDoc
93     {
94         throw new UnsupportedOperationException JavaDoc();
95     }
96
97     public ServiceManager createServiceManager( final Object JavaDoc entry )
98         throws Exception JavaDoc
99     {
100         throw new UnsupportedOperationException JavaDoc();
101     }
102
103     /**
104      * Retrieve a configuration for specified component.
105      * If the configuration is missing then a exception
106      * is raised with an appropraite error message.
107      *
108      * @param entry the entry
109      * @return the Configuration object
110      * @throws ConfigurationException if an error occurs
111      */

112     public Configuration createConfiguration( final Object JavaDoc entry )
113         throws Exception JavaDoc
114     {
115         final BlockListenerMetaData metaData = getMetaData( entry );
116         final String JavaDoc name = metaData.getName();
117         try
118         {
119             return m_context.getConfiguration( name );
120         }
121         catch( final ConfigurationException ce )
122         {
123             //Note that this shouldn't ever happen once we
124
//create a Config validator
125
final String JavaDoc message =
126                 REZ.getString( "missing-listener-configuration",
127                                name );
128             throw new ConfigurationException( message, ce );
129         }
130     }
131
132     public Parameters createParameters( final Object JavaDoc entry )
133         throws Exception JavaDoc
134     {
135         final Configuration configuration = createConfiguration( entry );
136         final Parameters parameters =
137             Parameters.fromConfiguration( configuration );
138         parameters.makeReadOnly();
139         return parameters;
140     }
141
142     /**
143      * Get meta data for entry.
144      *
145      * @param entry the entry
146      * @return the metadata
147      */

148     private BlockListenerMetaData getMetaData( final Object JavaDoc entry )
149     {
150         return (BlockListenerMetaData)entry;
151     }
152 }
153
Popular Tags