View Javadoc
1 package org.apache.torque.avalon; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2003 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import java.sql.Connection; 58 59 import org.apache.avalon.framework.activity.Initializable; 60 import org.apache.avalon.framework.activity.Startable; 61 import org.apache.avalon.framework.component.Component; 62 import org.apache.avalon.framework.configuration.Configurable; 63 import org.apache.avalon.framework.configuration.Configuration; 64 import org.apache.avalon.framework.configuration.ConfigurationException; 65 import org.apache.avalon.framework.context.Context; 66 import org.apache.avalon.framework.context.ContextException; 67 import org.apache.avalon.framework.context.Contextualizable; 68 import org.apache.avalon.framework.logger.AbstractLogEnabled; 69 import org.apache.avalon.framework.thread.ThreadSafe; 70 71 import org.apache.commons.lang.StringUtils; 72 73 import org.apache.torque.TorqueException; 74 import org.apache.torque.TorqueInstance; 75 import org.apache.torque.adapter.DB; 76 import org.apache.torque.manager.AbstractBaseManager; 77 import org.apache.torque.map.DatabaseMap; 78 79 /*** 80 * Avalon component for Torque. 81 * 82 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> 83 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 84 * @version $Id: TorqueComponent.java,v 1.7 2003/07/24 10:31:27 henning Exp $ 85 */ 86 public class TorqueComponent 87 extends AbstractLogEnabled 88 implements Component, 89 Configurable, 90 Initializable, 91 Contextualizable, 92 Startable, 93 ThreadSafe 94 { 95 /*** The Avalon Context */ 96 private Context context = null; 97 98 /*** The instance of Torque used by this component. */ 99 private TorqueInstance torqueInstance = null; 100 101 /*** The configuration file for Torque. */ 102 private String configFile = null; 103 104 105 /*** 106 * Creates a new instance. Default constructor used by Avalon. 107 */ 108 public TorqueComponent() 109 { 110 // If we simply do a "new TorqueInstance()" here, we will get 111 // into trouble when some internal classes (e.g. the DatasSource Factory) 112 // simply calls Torque.<xxx> and gets a different TorqueInstance 113 // than the one we configured here. Make sure that we use the 114 // same object as the Facade class does. 115 this.torqueInstance = org.apache.torque.Torque.getInstance(); 116 } 117 118 /*** 119 * Creates a new instance. 120 * 121 * @param torqueInstance The instance of the Torque core used by 122 * this component. 123 */ 124 protected TorqueComponent(TorqueInstance torqueInstance) 125 { 126 this.torqueInstance = torqueInstance; 127 } 128 129 /*** 130 * @return A reference to our instance of the Torque core. 131 */ 132 private TorqueInstance getTorque() 133 { 134 return torqueInstance; 135 } 136 137 /* 138 * ======================================================================== 139 * 140 * Avalon Component Interfaces 141 * 142 * ======================================================================== 143 */ 144 145 /*** 146 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) 147 */ 148 public void configure(Configuration configuration) 149 throws ConfigurationException 150 { 151 getLogger().debug("configure(" + configuration + ")"); 152 153 String configFile = configuration.getChild("configfile").getValue(); 154 String appRoot = null; 155 156 try 157 { 158 appRoot = (context == null) 159 ? null : (String) context.get("componentAppRoot"); 160 } 161 catch (ContextException ce) 162 { 163 getLogger().error("Could not load Application Root from Context"); 164 } 165 166 if (StringUtils.isNotEmpty(appRoot)) 167 { 168 if (appRoot.endsWith("/")) 169 { 170 appRoot = appRoot.substring(0, appRoot.length() - 1); 171 getLogger().debug("Application Root changed to " + appRoot); 172 } 173 174 if (configFile.startsWith("/")) 175 { 176 configFile = configFile.substring(1); 177 getLogger().debug("Config File changes to " + configFile); 178 } 179 180 StringBuffer sb = new StringBuffer(); 181 sb.append(appRoot); 182 sb.append('/'); 183 sb.append(configFile); 184 185 configFile = sb.toString(); 186 } 187 188 getLogger().debug("Config File is " + configFile); 189 190 this.configFile = configFile; 191 } 192 193 /*** 194 * @see org.apache.avalon.framework.context.Contextualizable 195 */ 196 public void contextualize(Context context) 197 throws ContextException 198 { 199 this.context = context; 200 } 201 202 /*** 203 * @see org.apache.avalon.framework.activity.Initializable#initialize() 204 */ 205 public void initialize() 206 throws Exception 207 { 208 getLogger().debug("initialize()"); 209 getTorque().init(configFile); 210 } 211 212 /*** 213 * @see org.apache.avalon.framework.activity.Startable#start() 214 */ 215 public void start() 216 { 217 getLogger().debug("start()"); 218 } 219 220 /*** 221 * @see org.apache.avalon.framework.activity.Startable#stop() 222 */ 223 public void stop() 224 { 225 getLogger().debug("stop()"); 226 getTorque().shutdown(); 227 } 228 229 230 /* 231 * ======================================================================== 232 * 233 * Torque Methods, accessible from the Component 234 * 235 * ======================================================================== 236 */ 237 238 /*** 239 * Determine whether Torque has already been initialized. 240 * 241 * @return true if Torque is already initialized 242 */ 243 public boolean isInit() 244 { 245 return getTorque().isInit(); 246 } 247 248 /*** 249 * Get the configuration for this component. 250 * 251 * @return the Configuration 252 */ 253 public org.apache.commons.configuration.Configuration getConfiguration() 254 { 255 return getTorque().getConfiguration(); 256 } 257 258 /*** 259 * This method returns a Manager for the given name. 260 * 261 * @param name name of the manager 262 * @return a Manager 263 */ 264 public AbstractBaseManager getManager(String name) 265 { 266 return getTorque().getManager(name); 267 } 268 269 /*** 270 * This methods returns either the Manager from the configuration file, 271 * or the default one provided by the generated code. 272 * 273 * @param name name of the manager 274 * @param defaultClassName the class to use if name has not been configured 275 * @return a Manager 276 */ 277 public AbstractBaseManager getManager(String name, 278 String defaultClassName) 279 { 280 return getTorque().getManager(name, defaultClassName); 281 } 282 283 /*** 284 * Returns the default database map information. 285 * 286 * @return A DatabaseMap. 287 * @throws TorqueException Any exceptions caught during processing will be 288 * rethrown wrapped into a TorqueException. 289 */ 290 public DatabaseMap getDatabaseMap() 291 throws TorqueException 292 { 293 return getTorque().getDatabaseMap(); 294 } 295 296 /*** 297 * Returns the database map information. Name relates to the name 298 * of the connection pool to associate with the map. 299 * 300 * @param name The name of the database corresponding to the 301 * <code>DatabaseMap</code> to retrieve. 302 * @return The named <code>DatabaseMap</code>. 303 * @throws TorqueException Any exceptions caught during processing will be 304 * rethrown wrapped into a TorqueException. 305 */ 306 public DatabaseMap getDatabaseMap(String name) 307 throws TorqueException 308 { 309 return getTorque().getDatabaseMap(name); 310 } 311 312 /*** 313 * Register a MapBuilder 314 * 315 * @param className the MapBuilder 316 */ 317 public void registerMapBuilder(String className) 318 { 319 getTorque().registerMapBuilder(className); 320 } 321 322 /*** 323 * This method returns a Connection from the default pool. 324 * 325 * @return The requested connection. 326 * @throws TorqueException Any exceptions caught during processing will be 327 * rethrown wrapped into a TorqueException. 328 */ 329 public Connection getConnection() 330 throws TorqueException 331 { 332 return getTorque().getConnection(); 333 } 334 335 /*** 336 * 337 * @param name The database name. 338 * @return a database connection 339 * @throws TorqueException Any exceptions caught during processing will be 340 * rethrown wrapped into a TorqueException. 341 */ 342 public Connection getConnection(String name) 343 throws TorqueException 344 { 345 return getTorque().getConnection(name); 346 } 347 348 /*** 349 * This method returns a Connecton using the given parameters. 350 * You should only use this method if you need user based access to the 351 * database! 352 * 353 * @param name The database name. 354 * @param username The name of the database user. 355 * @param password The password of the database user. 356 * @return A Connection. 357 * @throws TorqueException Any exceptions caught during processing will be 358 * rethrown wrapped into a TorqueException. 359 */ 360 public Connection getConnection(String name, String username, 361 String password) 362 throws TorqueException 363 { 364 return getTorque().getConnection(name, username, password); 365 } 366 /*** 367 * Returns database adapter for a specific connection pool. 368 * 369 * @param name A pool name. 370 * @return The corresponding database adapter. 371 * @throws TorqueException Any exceptions caught during processing will be 372 * rethrown wrapped into a TorqueException. 373 */ 374 public DB getDB(String name) 375 throws TorqueException 376 { 377 return getTorque().getDB(name); 378 } 379 380 /*** 381 * Returns the name of the default database. 382 * 383 * @return name of the default DB 384 */ 385 public String getDefaultDB() 386 { 387 return getTorque().getDefaultDB(); 388 } 389 390 /*** 391 * Closes a connection. 392 * 393 * @param con A Connection to close. 394 */ 395 public void closeConnection(Connection con) 396 { 397 getTorque().closeConnection(con); 398 } 399 }

This page was automatically generated by Maven