KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > lifecycle > AbstractResourceProvider


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.util.lifecycle;
88
89 import java.util.HashMap JavaDoc;
90 import java.util.Iterator JavaDoc;
91 import java.util.Map JavaDoc;
92 import org.apache.avalon.framework.component.Component;
93 import org.apache.avalon.framework.component.ComponentManager;
94 import org.apache.avalon.framework.component.DefaultComponentManager;
95 import org.apache.avalon.framework.configuration.Configuration;
96 import org.apache.avalon.framework.context.Context;
97 import org.apache.avalon.framework.context.DefaultContext;
98 import org.apache.avalon.framework.parameters.Parameters;
99 import org.apache.avalon.framework.service.DefaultServiceManager;
100 import org.apache.avalon.framework.service.ServiceManager;
101 import org.codehaus.loom.components.util.factory.ComponentFactory;
102 import org.codehaus.loom.components.util.info.ComponentInfo;
103 import org.codehaus.loom.components.util.metadata.ComponentTemplate;
104 import org.codehaus.loom.components.util.metadata.DependencyDirective;
105 import org.codehaus.spice.alchemist.configuration.ConfigurationAlchemist;
106 import org.codehaus.spice.salt.i18n.ResourceManager;
107 import org.codehaus.spice.salt.i18n.Resources;
108 import org.codehaus.dna.AbstractLogEnabled;
109
110 /**
111  * This is a base object via which the {@link org.codehaus.loom.components.util.lifecycle.LifecycleHelper}
112  * aquires resources for each component. This base implementation will aquire
113  * components and make sure that all required components are present. It will
114  * also make sure that the types of values returned from context are valid.
115  *
116  * <p>Note that this class assumes that the dependency graph has been validated
117  * (presumably via {@link org.codehaus.loom.components.util.verifier.AssemblyVerifier}</p>
118  *
119  * @author Peter Donald
120  * @version $Revision: 1.4 $ $Date: 2004/08/17 23:14:32 $
121  */

122 public abstract class AbstractResourceProvider
123     extends AbstractLogEnabled
124     implements ResourceProvider
125 {
126     private final static Resources REZ =
127         ResourceManager.getPackageResources( AbstractResourceProvider.class );
128
129     private final ComponentFactory m_factory;
130
131     /**
132      * Create an abstract provider which the specified ComponentFactory.
133      *
134      * @param factory the ComponentFactory
135      */

136     protected AbstractResourceProvider( final ComponentFactory factory )
137     {
138         if( null == factory )
139         {
140             throw new NullPointerException JavaDoc( "factory" );
141         }
142         m_factory = factory;
143     }
144
145     /**
146      * Utility method via which the provider aquires services to place in
147      * ServiceManager for a particular component.
148      *
149      * <p>Must be implemented in subclass.</p>
150      *
151      * @param name the name of service
152      * @param entry the entry for component aquiring service
153      * @return the service object for specified name
154      */

155     protected abstract Object JavaDoc getService( String JavaDoc name, Object JavaDoc entry );
156
157     /**
158      * Utility method via which the provider aquires a context value to place in
159      * Context for a particular component.
160      *
161      * <p>Must be implemented in subclass.</p>
162      *
163      * @param name the name of service
164      * @param entry the entry for component aquiring service
165      * @return the context value for specified name
166      */

167     protected abstract Object JavaDoc getContextValue( String JavaDoc name, Object JavaDoc entry );
168
169     /**
170      * Create a component for a particular entry. This implementation uses the
171      * associated ComponentFactory to create instance of component.
172      *
173      * @param entry the entry
174      * @return the newly created component
175      * @throws java.lang.Exception if unable to create component
176      */

177     public Object JavaDoc createObject( final Object JavaDoc entry )
178         throws Exception JavaDoc
179     {
180         final ComponentTemplate component = getMetaData( entry );
181         final String JavaDoc implementationKey = component.getImplementationKey();
182         return m_factory.createComponent( implementationKey );
183     }
184
185     /**
186      * Create a Parameters object by Creating configuration object and
187      * converting that.
188      *
189      * @param entry the entry
190      * @return a new Parameters object for component
191      * @throws java.lang.Exception if unable to create resource
192      */

193     public Parameters createParameters( final Object JavaDoc entry )
194         throws Exception JavaDoc
195     {
196         final ComponentTemplate component = getMetaData( entry );
197         final Parameters parameters = component.getParameters();
198         if( null == parameters )
199         {
200             final String JavaDoc message =
201                 REZ.format( "resource.missing-parameters.error",
202                             component.getName() );
203             throw new Exception JavaDoc( message );
204         }
205         parameters.makeReadOnly();
206         return parameters;
207     }
208
209     /**
210      * Create a new Configuration object for component.
211      *
212      * @param entry the entry
213      * @return a new Configuration object for component
214      * @throws java.lang.Exception if unable to create resource
215      */

216     public Configuration createConfiguration( final Object JavaDoc entry )
217         throws Exception JavaDoc
218     {
219         final ComponentTemplate component = getMetaData( entry );
220         final Configuration configuration =
221             ConfigurationAlchemist.toAvalonConfiguration(
222                 component.getConfiguration() );
223         if( null == configuration )
224         {
225             final String JavaDoc message =
226                 REZ.format( "resource.missing-configuration.error",
227                             component.getName() );
228             throw new Exception JavaDoc( message );
229         }
230         return configuration;
231     }
232
233     /**
234      * Create a Context object that contains values specified in map. The
235      * default implementation creates a basic Context object but different
236      * containers may choose to overide this to provide their own subclass of
237      * context.
238      *
239      * @param contextData the data to place in context
240      * @return the Context object
241      */

242     protected Context createContextImpl( final Map JavaDoc contextData )
243     {
244         final DefaultContext context = new DefaultContext( contextData );
245         context.makeReadOnly();
246         return context;
247     }
248
249     /**
250      * Return the ComponentTemplate for specified component entry. This
251      * implementation assumes that entry is instance of ComponentTemplate but
252      * subclasses should overide this method if this assumption does not hold
253      * true.
254      *
255      * @param entry the component entry
256      * @return the ComponentTemplate
257      */

258     protected ComponentTemplate getMetaData( final Object JavaDoc entry )
259     {
260         return (ComponentTemplate)entry;
261     }
262
263     /**
264      * Create a context object for specified component.
265      *
266      * @param componentEntry the entry representing component
267      * @return the created Context
268      * @throws java.lang.Exception if unable to create context or entrys in
269      * context
270      */

271     public final Context createContext( final Object JavaDoc componentEntry )
272         throws Exception JavaDoc
273     {
274         return createContextImpl( new HashMap JavaDoc() );
275     }
276
277     /**
278      * Create a new ComponentManager for component.
279      *
280      * @param entry the entry
281      * @return a new ComponentManager for component
282      * @throws java.lang.Exception if unable to create resource
283      */

284     public final ComponentManager createComponentManager( final Object JavaDoc entry )
285         throws Exception JavaDoc
286     {
287         final Map JavaDoc services = createServiceMap( entry );
288
289         final DefaultComponentManager componentManager = new DefaultComponentManager();
290
291         final Iterator JavaDoc keys = services.keySet().iterator();
292         while( keys.hasNext() )
293         {
294             final String JavaDoc key = (String JavaDoc)keys.next();
295             final Object JavaDoc service = services.get( key );
296             if( !Component.class.isInstance( service ) )
297             {
298                 final String JavaDoc message =
299                     REZ.format( "resource.service-not-a-component.error",
300                                 key,
301                                 service.getClass().getName() );
302                 throw new Exception JavaDoc( message );
303             }
304             componentManager.put( key, (Component)service );
305         }
306
307         componentManager.makeReadOnly();
308         return componentManager;
309     }
310
311     /**
312      * Create a new ServiceManager for component.
313      *
314      * @param entry the entry
315      * @return a new ServiceManager for component
316      * @throws Exception if unable to create resource
317      */

318     public final ServiceManager createServiceManager( final Object JavaDoc entry )
319         throws Exception JavaDoc
320     {
321         final Map JavaDoc services = createServiceMap( entry );
322
323         final DefaultServiceManager serviceManager = new DefaultServiceManager();
324
325         final Iterator JavaDoc keys = services.keySet().iterator();
326         while( keys.hasNext() )
327         {
328             final String JavaDoc key = (String JavaDoc)keys.next();
329             final Object JavaDoc service = services.get( key );
330             serviceManager.put( key, service );
331         }
332
333         serviceManager.makeReadOnly();
334         return serviceManager;
335     }
336
337     /**
338      * Accessor for component factory for sub-classes.
339      *
340      * @return the componentFactory associated with ResourceProvider.
341      */

342     protected final ComponentFactory getComponentFactory()
343     {
344         return m_factory;
345     }
346
347     /**
348      * Create a Map of services for specified component. The map maps key name
349      * to service provider.
350      *
351      * @param entry the component entry creating map for
352      * @return the map
353      * @throws java.lang.Exception if error aquiring a service to place in map
354      */

355     private Map JavaDoc createServiceMap( final Object JavaDoc entry )
356         throws Exception JavaDoc
357     {
358         final ComponentTemplate component = getMetaData( entry );
359         final String JavaDoc impl = component.getImplementationKey();
360         final ComponentInfo info = m_factory.createInfo( impl );
361         final DependencyDirective[] dependencies = component.getDependencies();
362
363         final HashMap JavaDoc services = new HashMap JavaDoc();
364
365         for( int i = 0; i < dependencies.length; i++ )
366         {
367             final DependencyDirective dependency = dependencies[ i ];
368             final String JavaDoc key = dependency.getKey();
369             final String JavaDoc providerName = dependency.getProviderName();
370             final boolean optional = info.getDependency( key ).isOptional();
371
372             final Object JavaDoc service =
373                 getService( providerName, entry );
374             if( null == service )
375             {
376                 final String JavaDoc message =
377                     REZ.format( "resource.missing-dependency.error",
378                                 optional ? "1" : "2",
379                                 key,
380                                 component.getName() );
381                 if( !optional )
382                 {
383                     throw new Exception JavaDoc( message );
384                 }
385                 else
386                 {
387                     getLogger().warn( message );
388                     continue;
389                 }
390             }
391
392             services.put( key, service );
393         }
394
395         return services;
396     }
397 }
398
Popular Tags