View Javadoc

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