KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > DefaultContainer


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.impl;
19
20 import org.apache.avalon.fortress.MetaInfoEntry;
21 import org.apache.avalon.fortress.impl.factory.ProxyManager;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.service.ServiceManager;
26
27 /**
28  * This is the default implementation of {@link org.apache.avalon.fortress.Container},
29  * adding configuration markup semantics to the {@link AbstractContainer}.
30  *
31  * @author <a HREF="mailto:dev@avalon.apache.org">The Avalon Team</a>
32  * @version CVS $Revision: 1.17 $ $Date: 2004/02/28 15:16:24 $
33  */

34 public class DefaultContainer
35     extends AbstractContainer
36     implements Configurable
37 {
38     /**
39      * <p>Process the configuration and set up the components and their
40      * mappings. At this point, all components are prepared and all mappings
41      * are made. However, nothing is initialized.</p>
42      *
43      * <p>The configuration format follows a specific convention:</p>
44      * <ul>
45      * <li>root configuration element may have any name.</li>
46      * <li>
47      * child configuration elements are named after short names
48      * managed by the <code>Container</code>'s
49      * <code>MetaInfoManager</code> or <code>RoleManager</code>
50      * </li>
51      * <li>
52      * alternatively <code>&lt;component&gt;</code> configuration elements
53      * may be used. These elements specify implementation class directly
54      * via the <code>class</code> attribute.
55      * </li>
56      * </ul>
57      *
58      * <p>Please note: each configuration element <strong>must</strong>
59      * have a unique id. If a configuration element does <strong>not</strong>
60      * have a unique id, it will not be treated as a component.
61      * This id is used later as a hint when there is more than one implementation
62      * of a role.</p>
63      *
64      * <p>The first component encourted which implements a specific role becomes
65      * the default implemenation of that role. This can be changed by the
66      * <code>default</code> attribute in this configuration.</p>
67      *
68      * <pre>
69      * &lt;my-config&gt;
70      * &lt;component id="default-connection"
71      * class="org.apache.avalon.excalibur.datasource.JdbcDataSourceComponent"&gt;
72      *
73      * &lt;!-- Component specific configuration --&gt;
74      *
75      * &lt;/component&gt;
76      *
77      * &lt;jdbc-data-source id="another-connection"&gt;
78      *
79      * &lt;!-- Component specific configuration --&gt;
80      *
81      * &lt;/jdbc-data-source&gt;
82      * &lt;/my-config&gt;
83      * </pre>
84      *
85      * @param config The configuration element to translate into the
86      * list of components this impl managers.
87      *
88      * @throws ConfigurationException if the configuration is not valid
89      */

90     public void configure( final Configuration config )
91         throws ConfigurationException
92     {
93         interpretProxy( config.getAttribute("proxy-type", "discover") );
94
95         final Configuration[] elements = config.getChildren();
96         for ( int i = 0; i < elements.length; i++ )
97         {
98             final Configuration element = elements[i];
99             final String JavaDoc hint = element.getAttribute( "id", null );
100             if ( null == hint )
101             {
102                 // Only components with an id attribute are treated as components.
103
getLogger().debug( "Ignoring configuration for component, " + element.getName()
104                     + ", because the id attribute is missing." );
105             }
106             else
107             {
108                 final String JavaDoc classname = getClassname( element );
109                 final int activation = getActivation( element );
110                 final ComponentHandlerMetaData metaData =
111                     new ComponentHandlerMetaData( hint, classname, element, activation );
112
113                 try
114                 {
115                     addComponent( metaData );
116                 }
117                 catch ( Exception JavaDoc e )
118                 {
119                     throw new ConfigurationException( "Could not add component", e );
120                 }
121             }
122         }
123     }
124
125     /**
126      * Interpret the ProxyManager type from the configuration element.
127      *
128      * @param proxyType
129      * @throws ConfigurationException
130      */

131     protected void interpretProxy( String JavaDoc proxyType ) throws ConfigurationException
132     {
133         int type = ProxyManager.DISCOVER;
134
135         if ( proxyType.equals("none") ) type = ProxyManager.NONE;
136         if ( proxyType.equals("bcel") ) type = ProxyManager.BCEL;
137         if ( proxyType.equals("java") || proxyType.equals("proxy") ) type = ProxyManager.PROXY;
138
139         if ( type == ProxyManager.DISCOVER && ! proxyType.equals("discover") )
140             throw new ConfigurationException("Proxy type '" + proxyType + "' not supported");
141
142         try
143         {
144             setProxyManager( new ProxyManager( type ) );
145         }
146         catch (Exception JavaDoc e)
147         {
148             throw new ConfigurationException("Could not create ProxyManager", e);
149         }
150     }
151
152     /**
153      * Retrieve the classname for component configuration.
154      *
155      * @param config the component configuration
156      * @return the class name
157      */

158     private String JavaDoc getClassname( final Configuration config )
159         throws ConfigurationException
160     {
161         final String JavaDoc className;
162
163         if ( "component".equals( config.getName() ) )
164         {
165             className = config.getAttribute( "class" );
166         }
167         else
168         {
169             final MetaInfoEntry roleEntry = m_metaManager.getMetaInfoForShortName( config.getName() );
170             if ( null == roleEntry )
171             {
172                 final String JavaDoc message = "No class found matching configuration name " +
173                     "[name: " + config.getName() + ", location: " + config.getLocation() + "]";
174                 throw new ConfigurationException( message );
175             }
176
177             className = roleEntry.getComponentClass().getName();
178         }
179
180         if ( getLogger().isDebugEnabled() )
181         {
182             getLogger().debug( "Configuration processed for: " + className );
183         }
184
185         return className;
186     }
187
188     /**
189      * Helper method to determine the activation policy for a given component
190      * handler configuration. Supported values for the "activation" attribute
191      * of the component are "background", "inline", or "lazy". The default
192      * activation is "background". For compatibility, "startup" is an alias
193      * for "background" and "request" is an alias for "lazy".
194      *
195      * @param component <code>Configuration</code>
196      *
197      * @return the activation policy, one of
198      * ComponentHandlerMetaData.ACTIVATION_BACKGROUND,
199      * ComponentHandlerMetaData.ACTIVATION_INLINE,
200      * ComponentHandlerMetaData.ACTIVATION_LAZY.
201      *
202      * @throws ConfigurationException if the handler specifies an unknown
203      * activation policy
204      */

205     private int getActivation( final Configuration component )
206         throws ConfigurationException
207     {
208         final String JavaDoc activation = component.getAttribute( "activation", "background" );
209         
210         if ( "background".equalsIgnoreCase( activation )
211             || "startup".equalsIgnoreCase( activation ) )
212         {
213             return ComponentHandlerMetaData.ACTIVATION_BACKGROUND;
214         }
215         else if ( "inline".equalsIgnoreCase( activation ) )
216         {
217             return ComponentHandlerMetaData.ACTIVATION_INLINE;
218         }
219         else if ( "lazy".equalsIgnoreCase( activation )
220             || "request".equalsIgnoreCase( activation ) )
221         {
222             return ComponentHandlerMetaData.ACTIVATION_LAZY;
223         }
224         else
225         {
226             // activation policy was unknown.
227
final String JavaDoc classname = component.getAttribute( "class", null );
228
229             final String JavaDoc message =
230                 "Unknown activation policy for class " + classname + ", \"" + activation + "\" "
231                 + "at " + component.getLocation();
232             throw new ConfigurationException( message );
233         }
234     }
235
236     /**
237      * Return the ServiceManager that exposes all the services in impl.
238      *
239      * @return the ServiceManager that exposes all the services in impl.
240      */

241     public ServiceManager getServiceManager()
242     {
243         return super.getServiceManager();
244     }
245 }
246
Popular Tags