1 package org.apache.torque.avalon;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.sql.Connection;
20
21 import org.apache.avalon.framework.activity.Initializable;
22 import org.apache.avalon.framework.activity.Startable;
23 import org.apache.avalon.framework.component.Component;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.context.Context;
28 import org.apache.avalon.framework.context.ContextException;
29 import org.apache.avalon.framework.context.Contextualizable;
30 import org.apache.avalon.framework.logger.AbstractLogEnabled;
31 import org.apache.avalon.framework.thread.ThreadSafe;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.torque.TorqueException;
34 import org.apache.torque.TorqueInstance;
35 import org.apache.torque.adapter.DB;
36 import org.apache.torque.manager.AbstractBaseManager;
37 import org.apache.torque.map.DatabaseMap;
38
39 /***
40 * Avalon component for Torque.
41 *
42 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
43 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44 * @version $Id: TorqueComponent.java 239636 2005-08-24 12:38:09Z henning $
45 */
46 public class TorqueComponent
47 extends AbstractLogEnabled
48 implements Component,
49 Configurable,
50 Initializable,
51 Contextualizable,
52 Startable,
53 ThreadSafe
54 {
55 /*** The Avalon Context */
56 private Context context = null;
57
58 /*** The instance of Torque used by this component. */
59 private TorqueInstance torqueInstance = null;
60
61 /*** The configuration file for Torque. */
62 private String configFile = null;
63
64
65 /***
66 * Creates a new instance. Default constructor used by Avalon.
67 */
68 public TorqueComponent()
69 {
70
71
72
73
74
75 this.torqueInstance = org.apache.torque.Torque.getInstance();
76 }
77
78 /***
79 * Creates a new instance.
80 *
81 * @param torqueInstance The instance of the Torque core used by
82 * this component.
83 */
84 protected TorqueComponent(TorqueInstance torqueInstance)
85 {
86 this.torqueInstance = torqueInstance;
87 }
88
89 /***
90 * @return A reference to our instance of the Torque core.
91 */
92 private TorqueInstance getTorque()
93 {
94 return torqueInstance;
95 }
96
97
98
99
100
101
102
103
104
105 /***
106 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
107 */
108 public void configure(Configuration configuration)
109 throws ConfigurationException
110 {
111 getLogger().debug("configure(" + configuration + ")");
112
113 String configFile = configuration.getChild("configfile").getValue();
114 String appRoot = null;
115
116 try
117 {
118 appRoot = (context == null)
119 ? null : (String) context.get("componentAppRoot");
120 }
121 catch (ContextException ce)
122 {
123 getLogger().error("Could not load Application Root from Context");
124 }
125
126 if (StringUtils.isNotEmpty(appRoot))
127 {
128 if (appRoot.endsWith("/"))
129 {
130 appRoot = appRoot.substring(0, appRoot.length() - 1);
131 getLogger().debug("Application Root changed to " + appRoot);
132 }
133
134 if (configFile.startsWith("/"))
135 {
136 configFile = configFile.substring(1);
137 getLogger().debug("Config File changes to " + configFile);
138 }
139
140 StringBuffer sb = new StringBuffer();
141 sb.append(appRoot);
142 sb.append('/');
143 sb.append(configFile);
144
145 configFile = sb.toString();
146 }
147
148 getLogger().debug("Config File is " + configFile);
149
150 this.configFile = configFile;
151 }
152
153 /***
154 * @see org.apache.avalon.framework.context.Contextualizable
155 */
156 public void contextualize(Context context)
157 throws ContextException
158 {
159 this.context = context;
160 }
161
162 /***
163 * @see org.apache.avalon.framework.activity.Initializable#initialize()
164 */
165 public void initialize()
166 throws Exception
167 {
168 getLogger().debug("initialize()");
169 getTorque().init(configFile);
170 }
171
172 /***
173 * @see org.apache.avalon.framework.activity.Startable#start()
174 */
175 public void start()
176 {
177 getLogger().debug("start()");
178 }
179
180 /***
181 * @see org.apache.avalon.framework.activity.Startable#stop()
182 */
183 public void stop()
184 {
185 getLogger().debug("stop()");
186 try
187 {
188 getTorque().shutdown();
189 }
190 catch (Exception e)
191 {
192 getLogger().error("Error while stopping Torque", e);
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205 /***
206 * Determine whether Torque has already been initialized.
207 *
208 * @return true if Torque is already initialized
209 */
210 public boolean isInit()
211 {
212 return getTorque().isInit();
213 }
214
215 /***
216 * Get the configuration for this component.
217 *
218 * @return the Configuration
219 */
220 public org.apache.commons.configuration.Configuration getConfiguration()
221 {
222 return getTorque().getConfiguration();
223 }
224
225 /***
226 * This method returns a Manager for the given name.
227 *
228 * @param name name of the manager
229 * @return a Manager
230 */
231 public AbstractBaseManager getManager(String name)
232 {
233 return getTorque().getManager(name);
234 }
235
236 /***
237 * This methods returns either the Manager from the configuration file,
238 * or the default one provided by the generated code.
239 *
240 * @param name name of the manager
241 * @param defaultClassName the class to use if name has not been configured
242 * @return a Manager
243 */
244 public AbstractBaseManager getManager(String name,
245 String defaultClassName)
246 {
247 return getTorque().getManager(name, defaultClassName);
248 }
249
250 /***
251 * Returns the default database map information.
252 *
253 * @return A DatabaseMap.
254 * @throws TorqueException Any exceptions caught during processing will be
255 * rethrown wrapped into a TorqueException.
256 */
257 public DatabaseMap getDatabaseMap()
258 throws TorqueException
259 {
260 return getTorque().getDatabaseMap();
261 }
262
263 /***
264 * Returns the database map information. Name relates to the name
265 * of the connection pool to associate with the map.
266 *
267 * @param name The name of the database corresponding to the
268 * <code>DatabaseMap</code> to retrieve.
269 * @return The named <code>DatabaseMap</code>.
270 * @throws TorqueException Any exceptions caught during processing will be
271 * rethrown wrapped into a TorqueException.
272 */
273 public DatabaseMap getDatabaseMap(String name)
274 throws TorqueException
275 {
276 return getTorque().getDatabaseMap(name);
277 }
278
279 /***
280 * Register a MapBuilder
281 *
282 * @param className the MapBuilder
283 */
284 public void registerMapBuilder(String className)
285 {
286 getTorque().registerMapBuilder(className);
287 }
288
289 /***
290 * This method returns a Connection from the default pool.
291 *
292 * @return The requested connection.
293 * @throws TorqueException Any exceptions caught during processing will be
294 * rethrown wrapped into a TorqueException.
295 */
296 public Connection getConnection()
297 throws TorqueException
298 {
299 return getTorque().getConnection();
300 }
301
302 /***
303 *
304 * @param name The database name.
305 * @return a database connection
306 * @throws TorqueException Any exceptions caught during processing will be
307 * rethrown wrapped into a TorqueException.
308 */
309 public Connection getConnection(String name)
310 throws TorqueException
311 {
312 return getTorque().getConnection(name);
313 }
314
315 /***
316 * This method returns a Connecton using the given parameters.
317 * You should only use this method if you need user based access to the
318 * database!
319 *
320 * @param name The database name.
321 * @param username The name of the database user.
322 * @param password The password of the database user.
323 * @return A Connection.
324 * @throws TorqueException Any exceptions caught during processing will be
325 * rethrown wrapped into a TorqueException.
326 */
327 public Connection getConnection(String name, String username,
328 String password)
329 throws TorqueException
330 {
331 return getTorque().getConnection(name, username, password);
332 }
333 /***
334 * Returns database adapter for a specific connection pool.
335 *
336 * @param name A pool name.
337 * @return The corresponding database adapter.
338 * @throws TorqueException Any exceptions caught during processing will be
339 * rethrown wrapped into a TorqueException.
340 */
341 public DB getDB(String name)
342 throws TorqueException
343 {
344 return getTorque().getDB(name);
345 }
346
347 /***
348 * Returns the name of the default database.
349 *
350 * @return name of the default DB
351 */
352 public String getDefaultDB()
353 {
354 return getTorque().getDefaultDB();
355 }
356
357 /***
358 * Closes a connection.
359 *
360 * @param con A Connection to close.
361 */
362 public void closeConnection(Connection con)
363 {
364 getTorque().closeConnection(con);
365 }
366 }