%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.avalon.TorqueComponent |
|
|
1 | package org.apache.torque.avalon; |
|
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.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 | 0 | private Context context = null; |
57 | ||
58 | /** The instance of Torque used by this component. */ |
|
59 | 0 | private TorqueInstance torqueInstance = null; |
60 | ||
61 | /** The configuration file for Torque. */ |
|
62 | 0 | private String configFile = null; |
63 | ||
64 | ||
65 | /** |
|
66 | * Creates a new instance. Default constructor used by Avalon. |
|
67 | */ |
|
68 | public TorqueComponent() |
|
69 | 0 | { |
70 | // If we simply do a "new TorqueInstance()" here, we will get |
|
71 | // into trouble when some internal classes (e.g. the DatasSource Factory) |
|
72 | // simply calls Torque.<xxx> and gets a different TorqueInstance |
|
73 | // than the one we configured here. Make sure that we use the |
|
74 | // same object as the Facade class does. |
|
75 | 0 | this.torqueInstance = org.apache.torque.Torque.getInstance(); |
76 | 0 | } |
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 | 0 | { |
86 | 0 | this.torqueInstance = torqueInstance; |
87 | 0 | } |
88 | ||
89 | /** |
|
90 | * @return A reference to our instance of the Torque core. |
|
91 | */ |
|
92 | private TorqueInstance getTorque() |
|
93 | { |
|
94 | 0 | return torqueInstance; |
95 | } |
|
96 | ||
97 | /* |
|
98 | * ======================================================================== |
|
99 | * |
|
100 | * Avalon Component Interfaces |
|
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 | 0 | getLogger().debug("configure(" + configuration + ")"); |
112 | ||
113 | 0 | String configFile = configuration.getChild("configfile").getValue(); |
114 | 0 | String appRoot = null; |
115 | ||
116 | try |
|
117 | { |
|
118 | 0 | appRoot = (context == null) |
119 | ? null : (String) context.get("componentAppRoot"); |
|
120 | } |
|
121 | 0 | catch (ContextException ce) |
122 | { |
|
123 | 0 | getLogger().error("Could not load Application Root from Context"); |
124 | 0 | } |
125 | ||
126 | 0 | if (StringUtils.isNotEmpty(appRoot)) |
127 | { |
|
128 | 0 | if (appRoot.endsWith("/")) |
129 | { |
|
130 | 0 | appRoot = appRoot.substring(0, appRoot.length() - 1); |
131 | 0 | getLogger().debug("Application Root changed to " + appRoot); |
132 | } |
|
133 | ||
134 | 0 | if (configFile.startsWith("/")) |
135 | { |
|
136 | 0 | configFile = configFile.substring(1); |
137 | 0 | getLogger().debug("Config File changes to " + configFile); |
138 | } |
|
139 | ||
140 | 0 | StringBuffer sb = new StringBuffer(); |
141 | 0 | sb.append(appRoot); |
142 | 0 | sb.append('/'); |
143 | 0 | sb.append(configFile); |
144 | ||
145 | 0 | configFile = sb.toString(); |
146 | } |
|
147 | ||
148 | 0 | getLogger().debug("Config File is " + configFile); |
149 | ||
150 | 0 | this.configFile = configFile; |
151 | 0 | } |
152 | ||
153 | /** |
|
154 | * @see org.apache.avalon.framework.context.Contextualizable |
|
155 | */ |
|
156 | public void contextualize(Context context) |
|
157 | throws ContextException |
|
158 | { |
|
159 | 0 | this.context = context; |
160 | 0 | } |
161 | ||
162 | /** |
|
163 | * @see org.apache.avalon.framework.activity.Initializable#initialize() |
|
164 | */ |
|
165 | public void initialize() |
|
166 | throws Exception |
|
167 | { |
|
168 | 0 | getLogger().debug("initialize()"); |
169 | 0 | getTorque().init(configFile); |
170 | 0 | } |
171 | ||
172 | /** |
|
173 | * @see org.apache.avalon.framework.activity.Startable#start() |
|
174 | */ |
|
175 | public void start() |
|
176 | { |
|
177 | 0 | getLogger().debug("start()"); |
178 | 0 | } |
179 | ||
180 | /** |
|
181 | * @see org.apache.avalon.framework.activity.Startable#stop() |
|
182 | */ |
|
183 | public void stop() |
|
184 | { |
|
185 | 0 | getLogger().debug("stop()"); |
186 | try |
|
187 | { |
|
188 | 0 | getTorque().shutdown(); |
189 | } |
|
190 | 0 | catch (Exception e) |
191 | { |
|
192 | 0 | getLogger().error("Error while stopping Torque", e); |
193 | 0 | } |
194 | 0 | } |
195 | ||
196 | ||
197 | /* |
|
198 | * ======================================================================== |
|
199 | * |
|
200 | * Torque Methods, accessible from the Component |
|
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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | getTorque().registerMapBuilder(className); |
287 | 0 | } |
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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | getTorque().closeConnection(con); |
365 | 0 | } |
366 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |