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