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