View Javadoc

1   package org.apache.turbine.services.avaloncomponent;
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.util.ArrayList;
58  import java.util.Iterator;
59  import java.util.List;
60  
61  import org.apache.avalon.excalibur.component.DefaultRoleManager;
62  import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
63  import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
64  import org.apache.avalon.excalibur.logger.LoggerManager;
65  import org.apache.avalon.framework.activity.Disposable;
66  import org.apache.avalon.framework.activity.Initializable;
67  import org.apache.avalon.framework.component.Component;
68  import org.apache.avalon.framework.component.ComponentException;
69  import org.apache.avalon.framework.configuration.Configuration;
70  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
71  import org.apache.avalon.framework.context.DefaultContext;
72  import org.apache.avalon.framework.logger.Logger;
73  import org.apache.commons.logging.Log;
74  import org.apache.commons.logging.LogFactory;
75  import org.apache.turbine.Turbine;
76  import org.apache.turbine.services.InitializationException;
77  import org.apache.turbine.services.TurbineBaseService;
78  
79  /***
80   * An implementation of AvalonComponentService which loads all the
81   * components given in the TurbineResources.properties File.
82   * <p>
83   * For component which require the location of the application or
84   * context root, there are two ways to get it.
85   * <ol>
86   * <li>
87   *   Implement the Contextualizable interface.  The full path to the
88   *   correct OS directory can be found under the ComponentAppRoot key.
89   * </li>
90   * <li>
91   *   The system property "applicationRoot" is also set to the full path
92   *   of the correct OS directory.
93   * </li>
94   * </ol>
95   * If you want to initialize Torque by using the AvalonComponentService, you
96   * must activate Torque at initialization time by specifying
97   *
98   * services.AvalonComponentService.lookup = org.apache.torque.Torque 
99   *
100  * in your TurbineResources.properties.
101  *
102  * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
103  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
104  * @version $Id: TurbineAvalonComponentService.java,v 1.19 2004/03/28 17:36:20 epugh Exp $
105  */
106 public class TurbineAvalonComponentService
107         extends TurbineBaseService
108         implements AvalonComponentService, Initializable, Disposable
109 {
110     /*** Logging */
111     private static Log log = LogFactory.getLog(
112             TurbineAvalonComponentService.class);
113 
114     /*** Component manager */
115     private ExcaliburComponentManager manager = null;
116 
117     // -------------------------------------------------------------
118     // Service initialization
119     // -------------------------------------------------------------
120 
121     /***
122      * Load all configured components and initialize them. This is
123      * a zero parameter variant which queries the Turbine Servlet
124      * for its config.
125      *
126      * @throws InitializationException Something went wrong in the init
127      *         stage
128      */
129     public void init()
130             throws InitializationException
131     {
132         try
133         {
134             initialize();
135 
136             setInit(true);
137         }
138         catch (Exception e)
139         {
140             throw new InitializationException("init failed", e);
141         }
142     }
143 
144     /***
145      * Shuts the Component Service down, calls dispose on the components that
146      * implement this interface
147      *
148      */
149     public void shutdown()
150     {
151         dispose();
152         setInit(false);
153     }
154 
155     // -------------------------------------------------------------
156     // Avalon lifecycle interfaces
157     // -------------------------------------------------------------
158 
159     /***
160      * Initializes the container
161      *
162      * @throws Exception generic exception
163      */
164     public void initialize() throws Exception
165     {
166         org.apache.commons.configuration.Configuration conf 
167                 = getConfiguration();
168 
169         // get the filenames and expand them relative to webapp root
170         String sysConfigFilename = Turbine.getRealPath(
171                 conf.getString(COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE));
172         String roleConfigFilename = Turbine.getRealPath(
173                 conf.getString(COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE));
174 
175         log.debug("Config File: " + sysConfigFilename);
176         log.debug("Role File:   " + roleConfigFilename);
177 
178         // process configuration files
179 
180         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
181         Configuration sysConfig  = builder.buildFromFile(sysConfigFilename);
182         Configuration roleConfig = builder.buildFromFile(roleConfigFilename);
183 
184         // Create the LoggerManager for Log4J
185         LoggerManager lm = new Log4JLoggerManager();
186 
187         // Setup the RoleManager
188         DefaultRoleManager roles = new DefaultRoleManager();
189 
190         Logger logger = lm.getLoggerForCategory(AVALON_LOG_CATEGORY);
191 
192         roles.enableLogging(logger);
193         roles.configure(roleConfig);
194 
195         // Setup ECM
196         manager = new ExcaliburComponentManager();
197 
198         manager.setLoggerManager(lm);
199         manager.enableLogging(logger);
200 
201         DefaultContext context = new DefaultContext();
202         String realPath = Turbine.getRealPath("/");
203 
204         context.put(AvalonComponentService.COMPONENT_APP_ROOT, realPath);
205         // urn:avalon:home is used by Merlinized components.  Makes things
206         // a bit more backwards compatible.
207         context.put("urn:avalon:home", realPath);
208         System.setProperty("applicationRoot", realPath);
209         System.setProperty("urn:avalon:home", realPath);
210 
211         log.debug("Application Root is " + realPath);
212 
213         manager.contextualize(context);
214         manager.setRoleManager(roles);
215         manager.configure(sysConfig);
216 
217         // Init ECM!!!!
218         manager.initialize();
219 
220         List lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY,
221                 new ArrayList());
222         
223         for (Iterator it = lookupComponents.iterator(); it.hasNext();)
224         {
225             String component = (String) it.next();
226             try
227             {
228                 Component c = manager.lookup(component);
229                 log.info("Lookup for Component " + component + " successful");
230                 manager.release(c);
231             }
232             catch (Exception e)
233             {
234                 log.error("Lookup for Component " + component + " failed!");
235             }
236         }
237     }
238 
239     /***
240      * Disposes of the container and releases resources
241      */
242     public void dispose()
243     {
244         manager.dispose();
245     }
246 
247     /***
248      * Returns an instance of the named component
249      *
250      * @param roleName Name of the role the component fills.
251      * @return an instance of the named component
252      * @throws ComponentException generic exception
253      */
254     public Component lookup(String roleName)
255             throws ComponentException
256     {
257         return manager.lookup(roleName);
258     }
259 
260     /***
261      * Releases the component
262      *
263      * @param component the component to release
264      */
265     public void release(Component component)
266     {
267         manager.release(component);
268     }
269 
270 }