PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/castor-1.3.3/cpa/src/main/java/org/castor/jdo/engine/DatabaseContext.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 322 lines | 165 code | 45 blank | 112 comment | 16 complexity | 12dcbe72b655db6ff9e3568a54d838ae MD5 | raw file
  1. /*
  2. * Copyright 2005 Werner Guttmann, Ralf Joachim
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.castor.jdo.engine;
  17. import java.util.Enumeration;
  18. import java.util.Properties;
  19. import javax.transaction.TransactionManager;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.castor.core.util.AbstractProperties;
  23. import org.castor.core.util.Messages;
  24. import org.castor.cpa.CPAProperties;
  25. import org.castor.cpa.persistence.sql.connection.ConnectionFactory;
  26. import org.castor.cpa.util.JDOClassDescriptorResolver;
  27. import org.castor.cpa.util.JDOClassDescriptorResolverImpl;
  28. import org.castor.jdo.conf.Database;
  29. import org.castor.jdo.conf.JdoConf;
  30. import org.castor.jdo.util.JDOConfAdapter;
  31. import org.castor.mapping.BindingType;
  32. import org.castor.mapping.MappingUnmarshaller;
  33. import org.castor.transactionmanager.TransactionManagerAcquireException;
  34. import org.castor.transactionmanager.TransactionManagerRegistry;
  35. import org.exolab.castor.mapping.Mapping;
  36. import org.exolab.castor.mapping.MappingException;
  37. import org.exolab.castor.mapping.MappingLoader;
  38. import org.exolab.castor.persist.LockEngine;
  39. import org.exolab.castor.persist.PersistenceEngineFactory;
  40. import org.exolab.castor.persist.PersistenceFactoryRegistry;
  41. import org.exolab.castor.persist.spi.PersistenceFactory;
  42. /**
  43. * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
  44. * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
  45. * @version $Revision: 8994 $ $Date: 2011-08-02 01:40:59 +0200 (Di, 02 Aug 2011) $
  46. * @since 0.9.9
  47. */
  48. public final class DatabaseContext {
  49. /** The name of the generic SQL engine, if no SQL engine specified. */
  50. public static final String GENERIC_ENGINE = "generic";
  51. /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta
  52. * Commons Logging</a> instance used for all logging. */
  53. private static final Log LOG = LogFactory.getLog(DatabaseContext.class);
  54. /** Has the factory been initialized? */
  55. private boolean _initialized = false;
  56. /** The JDO configuration. */
  57. private JdoConf _jdoConf;
  58. /** Index of the database configuration in the jdo configuration. */
  59. private int _index;
  60. /** The name of the database configuration. */
  61. private String _name;
  62. /** The mapping to load. */
  63. private Mapping _mapping;
  64. /** The transaction manager. */
  65. private TransactionManager _txManager;
  66. /** The LockEngine only available after initialization. */
  67. private LockEngine _engine = null;
  68. /** PersistenceFactory needed to initialize ConnectionFactory. */
  69. private PersistenceFactory _persistenceFactory = null;
  70. /**
  71. * A {@link ClassDescriptorResolver} instance to be used for class to
  72. * class descriptor resolution.
  73. */
  74. private JDOClassDescriptorResolver _classDescriptorResolver = null;
  75. private ConnectionFactory _factory;
  76. /**
  77. * Constructs a new AbstractConnectionFactory with given name, engine and mapping.
  78. * Factory will be ready to use without calling initialize first.
  79. *
  80. * @param name The Name of the database configuration.
  81. * @param engine The Name of the persistence factory to use.
  82. * @param txManager The transaction manager to use.
  83. * @param mapping The previously loaded mapping.
  84. * @throws MappingException If LockEngine could not be initialized.
  85. */
  86. protected DatabaseContext(final String name, final String engine,
  87. final Mapping mapping,
  88. final TransactionManager txManager,
  89. final ConnectionFactory factory)
  90. throws MappingException {
  91. _jdoConf = null;
  92. _index = -1;
  93. _name = name;
  94. _mapping = mapping;
  95. _txManager = txManager;
  96. _factory = factory;
  97. if (_classDescriptorResolver == null) {
  98. _classDescriptorResolver = new JDOClassDescriptorResolverImpl();
  99. }
  100. initializeEngine(engine);
  101. _initialized = true;
  102. }
  103. /**
  104. * Constructs a new AbstractConnectionFactory with given database and mapping.
  105. * Initialize needs to be called before using the factory to create connections.
  106. *
  107. * @param jdoConf The JDO configuration.
  108. * @param index Index of the database configuration in the JDO configuration.
  109. * @param mapping The mapping to load.
  110. */
  111. protected DatabaseContext(final JdoConf jdoConf, final int index,
  112. final Mapping mapping, final ConnectionFactory factory) {
  113. _jdoConf = jdoConf;
  114. _index = index;
  115. _name = jdoConf.getDatabase(index).getName();
  116. _mapping = mapping;
  117. _txManager = null;
  118. _factory = factory;
  119. }
  120. /**
  121. * Initialize factory if it had not been initialized before.
  122. *
  123. * @throws MappingException If concrete factory or LockEngine fail to initialize
  124. * or mapping could not be loaded.
  125. */
  126. public void initialize() throws MappingException {
  127. // If the factory was already initialized, ignore
  128. // this request to initialize it.
  129. if (!_initialized) {
  130. if (_classDescriptorResolver == null) {
  131. _classDescriptorResolver = new JDOClassDescriptorResolverImpl();
  132. }
  133. initializeMapping();
  134. JDOConfAdapter adapt = new JDOConfAdapter(_jdoConf);
  135. String name = adapt.getName();
  136. String txm = adapt.getTransactionManager();
  137. Properties prop = adapt.getTransactionManagerParameters();
  138. AbstractProperties properties = CPAProperties.getInstance();
  139. TransactionManagerRegistry txr = new TransactionManagerRegistry(properties);
  140. try {
  141. txr.registerTransactionManager(name, txm, prop);
  142. _txManager = txr.getTransactionManager(name);
  143. } catch (TransactionManagerAcquireException ex) {
  144. throw new MappingException(ex);
  145. }
  146. initializeEngine(_jdoConf.getDatabase(_index).getEngine());
  147. initializeFactory();
  148. _initialized = true;
  149. }
  150. }
  151. /**
  152. * Load mapping.
  153. *
  154. * @throws MappingException If mapping could not be loaded.
  155. */
  156. private void initializeMapping() throws MappingException {
  157. try {
  158. // Initialize all the class mappings of the database.
  159. Enumeration<? extends org.castor.jdo.conf.ClassMapping> classMappings =
  160. _jdoConf.getDatabase(_index).enumerateClassMapping();
  161. while (classMappings.hasMoreElements()) {
  162. org.castor.jdo.conf.ClassMapping classConf = classMappings.nextElement();
  163. String className = classConf.getName();
  164. _classDescriptorResolver.addClass(Class.forName(className));
  165. }
  166. // Initialize all the package mappings of the database.
  167. Enumeration<? extends org.castor.jdo.conf.PackageMapping> packageMappings =
  168. _jdoConf.getDatabase(_index).enumeratePackageMapping();
  169. while (packageMappings.hasMoreElements()) {
  170. org.castor.jdo.conf.PackageMapping packageConf = packageMappings.nextElement();
  171. String packageName = packageConf.getName();
  172. _classDescriptorResolver.addPackage(packageName);
  173. }
  174. // Initialize all the mappings of the database.
  175. Enumeration<? extends org.castor.jdo.conf.Mapping> mappings =
  176. _jdoConf.getDatabase(_index).enumerateMapping();
  177. while (mappings.hasMoreElements()) {
  178. org.castor.jdo.conf.Mapping mapConf = mappings.nextElement();
  179. if (LOG.isDebugEnabled()) {
  180. LOG.debug("Loading the mapping descriptor: " + mapConf.getHref());
  181. }
  182. if (mapConf.getHref() != null) {
  183. _mapping.loadMapping(mapConf.getHref());
  184. }
  185. }
  186. } catch (MappingException ex) {
  187. throw ex;
  188. } catch (Exception ex) {
  189. throw new MappingException(ex);
  190. }
  191. }
  192. /**
  193. * Initialize LockEngine.
  194. *
  195. * @param engine Name of the persistence factory to use.
  196. * @throws MappingException If LockEngine could not be initialized.
  197. */
  198. private void initializeEngine(final String engine) throws MappingException {
  199. // Complain if no database engine was specified, otherwise get
  200. // a persistence factory for that database engine.
  201. if (engine == null) {
  202. _persistenceFactory = PersistenceFactoryRegistry.getPersistenceFactory(GENERIC_ENGINE);
  203. } else {
  204. _persistenceFactory = PersistenceFactoryRegistry.getPersistenceFactory(engine);
  205. }
  206. if (_persistenceFactory == null) {
  207. String msg = Messages.format("jdo.noSuchEngine", engine);
  208. LOG.error(msg);
  209. throw new MappingException(msg);
  210. }
  211. MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller();
  212. MappingLoader mappingLoader = mappingUnmarshaller.getMappingLoader(
  213. _mapping, BindingType.JDO, _persistenceFactory);
  214. _classDescriptorResolver.setMappingLoader(mappingLoader);
  215. _engine = new PersistenceEngineFactory().createEngine(
  216. this, _classDescriptorResolver, _persistenceFactory);
  217. }
  218. /**
  219. * Method initializing ConnectionFactory.
  220. *
  221. * @throws MappingException If concrete factory could not be initialized.
  222. */
  223. public void initializeFactory() throws MappingException {
  224. _factory.initializeFactory(_persistenceFactory);
  225. }
  226. public ConnectionFactory getConnectionFactory() {
  227. return _factory;
  228. }
  229. /**
  230. * Get the name of the database configuration.
  231. *
  232. * @return The name of the database configuration.
  233. */
  234. public String getName() {
  235. return _name;
  236. }
  237. /**
  238. * Get the database configuration.
  239. *
  240. * @return The database configuration.
  241. */
  242. public Database getDatabase() {
  243. return _jdoConf.getDatabase(_index);
  244. }
  245. /**
  246. * Get the mapping to load.
  247. *
  248. * @return The mapping to load.
  249. */
  250. public Mapping getMapping() {
  251. return _mapping;
  252. }
  253. /**
  254. * Get the transaction manager.
  255. *
  256. * @return The transaction manager.
  257. */
  258. public TransactionManager getTransactionManager() {
  259. return _txManager;
  260. }
  261. /**
  262. * Get the LockEngine only available after initialization.
  263. *
  264. * @return The LockEngine.
  265. */
  266. public LockEngine getEngine() {
  267. return _engine;
  268. }
  269. /**
  270. * Sets a custom {@link ClassDescriptorResolver} instance.
  271. * @param classDescriptorResolver A custom {@link ClassDescriptorResolver} instance to be used.
  272. */
  273. public void setClassDescriptorResolver(
  274. final JDOClassDescriptorResolver classDescriptorResolver) {
  275. _classDescriptorResolver = classDescriptorResolver;
  276. }
  277. }