1 package org.apache.turbine.services.avaloncomponent;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
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
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
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
179
180 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
181 Configuration sysConfig = builder.buildFromFile(sysConfigFilename);
182 Configuration roleConfig = builder.buildFromFile(roleConfigFilename);
183
184
185 LoggerManager lm = new Log4JLoggerManager();
186
187
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
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
206
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
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 }