View Javadoc

1   package org.apache.turbine.services.avaloncomponent;
2   
3   
4   /*
5    * Copyright 2001-2004 The Apache Software Foundation.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License")
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.avalon.excalibur.component.DefaultRoleManager;
26  import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
27  import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
28  import org.apache.avalon.excalibur.logger.LoggerManager;
29  import org.apache.avalon.framework.activity.Disposable;
30  import org.apache.avalon.framework.activity.Initializable;
31  import org.apache.avalon.framework.component.Component;
32  import org.apache.avalon.framework.component.ComponentException;
33  import org.apache.avalon.framework.configuration.Configuration;
34  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
35  import org.apache.avalon.framework.context.DefaultContext;
36  import org.apache.avalon.framework.logger.Logger;
37  import org.apache.avalon.framework.service.ServiceException;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.turbine.Turbine;
41  import org.apache.turbine.services.InitializationException;
42  import org.apache.turbine.services.InstantiationException;
43  import org.apache.turbine.services.TurbineBaseService;
44  
45  /***
46   * An implementation of AvalonComponentService which loads all the
47   * components given in the TurbineResources.properties File.
48   * <p>
49   * For component which require the location of the application or
50   * context root, there are two ways to get it.
51   * <ol>
52   * <li>
53   *   Implement the Contextualizable interface.  The full path to the
54   *   correct OS directory can be found under the ComponentAppRoot key.
55   * </li>
56   * <li>
57   *   The system property "applicationRoot" is also set to the full path
58   *   of the correct OS directory.
59   * </li>
60   * </ol>
61   * If you want to initialize Torque by using the AvalonComponentService, you
62   * must activate Torque at initialization time by specifying
63   *
64   * services.AvalonComponentService.lookup = org.apache.torque.Torque
65   *
66   * in your TurbineResources.properties.
67   *
68   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
69   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
70   * @version $Id: TurbineAvalonComponentService.java 330049 2005-11-01 12:44:34Z sgoeschl $
71   */
72  public class TurbineAvalonComponentService
73          extends TurbineBaseService
74          implements AvalonComponentService, Initializable, Disposable
75  {
76      /*** Logging */
77      private static Log log = LogFactory.getLog(
78              TurbineAvalonComponentService.class);
79  
80      /*** Component manager */
81      private ExcaliburComponentManager manager = null;
82  
83      // -------------------------------------------------------------
84      // Service initialization
85      // -------------------------------------------------------------
86  
87      /***
88       * Load all configured components and initialize them. This is
89       * a zero parameter variant which queries the Turbine Servlet
90       * for its config.
91       *
92       * @throws InitializationException Something went wrong in the init
93       *         stage
94       */
95      public void init()
96              throws InitializationException
97      {
98          try
99          {
100             initialize();
101 
102             setInit(true);
103         }
104         catch (Exception e)
105         {
106             throw new InitializationException("init failed", e);
107         }
108     }
109 
110     /***
111      * Shuts the Component Service down, calls dispose on the components that
112      * implement this interface
113      *
114      */
115     public void shutdown()
116     {
117         dispose();
118         setInit(false);
119     }
120 
121     // -------------------------------------------------------------
122     // Avalon lifecycle interfaces
123     // -------------------------------------------------------------
124 
125     /***
126      * Initializes the container
127      *
128      * @throws Exception generic exception
129      */
130     public void initialize() throws Exception
131     {
132         org.apache.commons.configuration.Configuration conf
133                 = getConfiguration();
134 
135         // get the filenames and expand them relative to webapp root
136         String sysConfigFilename = Turbine.getRealPath(
137                 conf.getString(COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE));
138         String roleConfigFilename = Turbine.getRealPath(
139                 conf.getString(COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE));
140 
141         log.debug("Config File: " + sysConfigFilename);
142         log.debug("Role File:   " + roleConfigFilename);
143 
144         // process configuration files
145 
146         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
147         Configuration sysConfig  = builder.buildFromFile(sysConfigFilename);
148         Configuration roleConfig = builder.buildFromFile(roleConfigFilename);
149 
150         // Create the LoggerManager for Log4J
151         LoggerManager lm = new Log4JLoggerManager();
152 
153         // Setup the RoleManager
154         DefaultRoleManager roles = new DefaultRoleManager();
155 
156         Logger logger = lm.getLoggerForCategory(AVALON_LOG_CATEGORY);
157 
158         roles.enableLogging(logger);
159         roles.configure(roleConfig);
160 
161         // Setup ECM
162         manager = new ExcaliburComponentManager();
163 
164         manager.setLoggerManager(lm);
165         manager.enableLogging(logger);
166 
167         DefaultContext context = new DefaultContext();
168         String realPath = Turbine.getRealPath("/");
169 
170         context.put(AvalonComponentService.COMPONENT_APP_ROOT, realPath);
171         // urn:avalon:home is used by Merlinized components.  Makes things
172         // a bit more backwards compatible.
173         context.put("urn:avalon:home", realPath);
174         System.setProperty("applicationRoot", realPath);
175         System.setProperty("urn:avalon:home", realPath);
176 
177         log.debug("Application Root is " + realPath);
178 
179         manager.contextualize(context);
180         manager.setRoleManager(roles);
181         manager.configure(sysConfig);
182 
183         // Init ECM!!!!
184         manager.initialize();
185 
186         List lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY,
187                 new ArrayList());
188 
189         for (Iterator it = lookupComponents.iterator(); it.hasNext();)
190         {
191             String component = (String) it.next();
192             try
193             {
194                 Component c = manager.lookup(component);
195                 log.info("Lookup for Component " + component + " successful");
196                 manager.release(c);
197             }
198             catch (Exception e)
199             {
200                 log.error("Lookup for Component " + component + " failed!");
201             }
202         }
203     }
204 
205     /***
206      * Disposes of the container and releases resources
207      */
208     public void dispose()
209     {
210         manager.dispose();
211     }
212 
213     /***
214      * Returns an instance of the named component
215      *
216      * @param roleName Name of the role the component fills.
217      * @return an instance of the named component
218      * @throws ComponentException generic exception
219      */
220     public Object lookup(String roleName)
221             throws ServiceException
222     {
223         try
224         {
225             return manager.lookup(roleName);
226         }
227         catch (ComponentException e)
228         {
229             throw new ServiceException(name, e.getMessage());
230         }
231     }
232 
233     /***
234      * Releases the component
235      *
236      * @param component the component to release
237      */
238     public void release(Object component)
239     {
240         if( component instanceof Component )
241         {
242             manager.release((Component)component);
243         }
244     }
245         
246     /***
247      * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
248      */
249     public boolean hasService(String roleName)
250     {
251         return manager.hasComponent(roleName);
252     }
253 
254     // -------------------------------------------------------------
255     // TurbineServiceProvider
256     // -------------------------------------------------------------
257     
258     /***
259      * @see org.apache.turbine.services.TurbineServiceProvider#exists(java.lang.String)
260      */
261     public boolean exists(String roleName)
262     {
263         return this.hasService(roleName);
264     }
265     
266     /***
267      * @see org.apache.turbine.services.TurbineServiceProvider#get(java.lang.String)
268      */
269     public Object get(String roleName) throws InstantiationException
270     {
271         try
272         {
273             return this.lookup(roleName);
274         }
275         catch (ServiceException e)
276         {
277             String msg = "Unable to get the following service : " + roleName;
278             log.error(msg);
279             throw new InstantiationException(msg);
280         }
281         catch (Throwable t)
282         {
283             String msg = "Unable to get the following service : " + roleName;
284             log.error(msg,t);
285             throw new InstantiationException(msg,t);
286         }                
287     }
288 }