KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > yaaficomponent > TurbineYaafiComponentService


1 package org.apache.turbine.services.yaaficomponent;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.File JavaDoc;
58 import java.util.ArrayList JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61
62 import org.apache.avalon.framework.context.DefaultContext;
63 import org.apache.avalon.framework.logger.Log4JLogger;
64 import org.apache.commons.logging.Log;
65 import org.apache.commons.logging.LogFactory;
66 import org.apache.fulcrum.BaseService;
67 import org.apache.fulcrum.InitializationException;
68 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
69 import org.apache.fulcrum.yaafi.framework.factory.ServiceManagerFactory;
70 import org.apache.turbine.Turbine;
71
72
73 /**
74  * An implementation of YaafiComponentService which loads all the
75  * components given in the TurbineResources.properties File.
76  * <p>
77  * For component which require the location of the application or
78  * context root, there are two ways to get it.
79  * <ol>
80  * <li>
81  * Implement the Contextualizable interface. The full path to the
82  * correct OS directory can be found under the ComponentAppRoot key.
83  * </li>
84  * <li>
85  * The system property "applicationRoot" is also set to the full path
86  * of the correct OS directory.
87  * </li>
88  * </ol>
89  * If you want to initialize a component by using the YaafiComponentService, you
90  * must activate Torque at initialization time by specifying
91  *
92  * services.YaafiComponentService.lookup = org.apache.torque.Torque
93  *
94  * in your TurbineResources.properties.
95  *
96  * @author <a HREF="mailto:epugh@upstate.com">Eric Pugh</a>
97  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
98  * @version $Id: TurbineYaafiComponentService.java,v 1.1 2004/11/12 10:26:14 epugh Exp $
99  */

100 public class TurbineYaafiComponentService
101         extends BaseService
102         implements YaafiComponentService
103 {
104     /** Logging */
105     private static Log log = LogFactory.getLog(TurbineYaafiComponentService.class);
106
107     /** YAFFI container */
108     private ServiceContainer container = null;
109
110     // -------------------------------------------------------------
111
// Service initialization
112
// -------------------------------------------------------------
113

114     /**
115      * Load all configured components and initialize them. This is a zero parameter variant which
116      * queries the Turbine Servlet for its config.
117      *
118      * @throws InitializationException Something went wrong in the init stage
119      */

120     public void init( Object JavaDoc data )
121         throws InitializationException
122     {
123         try
124         {
125             init();
126             setInit(true);
127         }
128         catch (Exception JavaDoc e)
129         {
130             log.error("Exception caught initialising service: ", e);
131             throw new InitializationException("init failed", e);
132         }
133     }
134
135     /**
136      * Shuts the Component Service down, calls dispose on the components that implement this
137      * interface
138      *
139      */

140     public void shutdown()
141     {
142         dispose();
143         setInit(false);
144     }
145
146     // -------------------------------------------------------------
147
// Avalon lifecycle interfaces
148
// -------------------------------------------------------------
149

150     /**
151      * Initializes the container
152      *
153      * @throws Exception generic exception
154      */

155     public void init() throws InitializationException
156     {
157         org.apache.commons.configuration.Configuration conf = getConfiguration();
158
159         // determine the home directory
160

161         String JavaDoc homePath = Turbine.getApplicationRoot();
162         File JavaDoc home = new File JavaDoc(homePath);
163
164         // determine the location of the role configuraton file
165

166         String JavaDoc roleConfigurationFileName = conf.getString(
167                 YaafiComponentService.COMPONENT_ROLE_KEYS,
168                 YaafiComponentService.COMPONENT_ROLE_VALUE
169             );
170
171         // determine the location of component configuration file
172

173         String JavaDoc componentConfigurationFileName = conf.getString(
174                 YaafiComponentService.COMPONENT_CONFIG_KEY,
175                 YaafiComponentService.COMPONENT_CONFIG_VALUE
176             );
177
178         // determine the location of parameters file
179

180         String JavaDoc parametersFileName = conf.getString(
181                 YaafiComponentService.COMPONENT_PARAMETERS_KEY,
182                 YaafiComponentService.COMPONENT_PARAMETERS_VALUE
183             );
184
185         // build up a default context
186

187         DefaultContext context = new DefaultContext();
188         context.put(COMPONENT_APP_ROOT, homePath);
189         context.put(URN_AVALON_HOME, new File JavaDoc( homePath ) );
190         context.put(URN_AVALON_TEMP, new File JavaDoc( homePath ) );
191
192         try
193         {
194             this.container = ServiceManagerFactory.create(
195                 new Log4JLogger( org.apache.log4j.Logger.getLogger( TurbineYaafiComponentService.class ) ),
196                 roleConfigurationFileName,
197                 componentConfigurationFileName,
198                 parametersFileName,
199                 context
200                 );
201             
202         }
203         catch (Throwable JavaDoc t)
204         {
205             throw new InitializationException(
206                     "Failed to initialize YaafiComponentService",t); //EXCEPTION
207
}
208         
209         List JavaDoc lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY,
210                 new ArrayList JavaDoc());
211         
212         for (Iterator JavaDoc it = lookupComponents.iterator(); it.hasNext();)
213         {
214             String JavaDoc component = (String JavaDoc) it.next();
215             try
216             {
217                 Object JavaDoc c = lookup(component);
218                 log.info("Lookup for Component " + c + " successful");
219                 release(c);
220             }
221             catch (Exception JavaDoc e)
222             {
223                 log.error("Lookup for Component " + component + " failed!");
224             }
225         }
226         setInit(true);
227     }
228
229     /**
230      * Disposes of the container and releases resources
231      */

232     public void dispose()
233     {
234         if (this.container != null)
235         {
236             this.container.dispose();
237             this.container = null;
238         }
239     }
240
241     /**
242      * Returns an instance of the named component
243      *
244      * @param roleName Name of the role the component fills.
245      * @return an instance of the named component
246      * @throws Exception generic exception
247      */

248     public Object JavaDoc lookup(String JavaDoc path) throws Exception JavaDoc
249     {
250         return this.container.lookup(path);
251     }
252
253     /**
254      * Releases the component
255      *
256      * @param source. The path to the handler for this component For example, if the object is a
257      * java.sql.Connection object sourced from the "/turbine-merlin/datasource"
258      * component, the call would be :- release("/turbine-merlin/datasource", conn);
259      * @param component the component to release
260      */

261     public void release(Object JavaDoc component)
262     {
263         this.container.release( component );
264     }
265 }
266
Popular Tags