001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s):
022: * Contributor(s):
023: *
024: * --------------------------------------------------------------------------
025: * $Id: RdbFactory.java 6562 2005-04-15 13:03:55Z joaninh $
026: * --------------------------------------------------------------------------
027: */package org.objectweb.jonas_ejb.container.jorm;
028:
029: import javax.sql.DataSource;
030:
031: import org.objectweb.jonas_ejb.container.JContainer;
032: import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
033:
034: import org.objectweb.jorm.api.PClassMapping;
035: import org.objectweb.jorm.api.PException;
036: import org.objectweb.jorm.lib.MapperJCA;
037: import org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassProp;
038: import org.objectweb.jorm.mapper.rdb.lib.MapperJDBC;
039: import org.objectweb.jorm.mapper.rdb.lib.RdbPrefetchablePCM;
040:
041: /**
042: * @author Sebastien Chassande-Barrioz
043: * @author Philippe Durieux (new Jorm mapping)
044: */
045: public abstract class RdbFactory extends MedorFactory {
046:
047: public RdbFactory() {
048: super ();
049: }
050:
051: public void init(EntityDesc dd, JContainer cont, String mapperName) {
052: super .init(dd, cont, mapperName);
053: }
054:
055: public Object getConnection(Object hints) throws PException {
056: if (hints == null) {
057: return mapper.getConnection();
058: } else {
059: return mapper.getConnection(hints);
060: }
061: }
062:
063: public void releaseConnection(Object conn) throws PException {
064: mapper.closeConnection(conn);
065: }
066:
067: protected void setMapper(String mapperName) throws PException {
068: MapperManager mm = MapperManager.getInstance();
069: synchronized (mm) {
070: mapper = mm.getMapper(cont, datasource);
071: if (mapper == null) {
072: // datasource has been found by its JNDI name in the bean DD
073: if (datasource instanceof DataSource) {
074: mapper = new MapperJDBC(mm.getJormConfigurator());
075: } else {
076: mapper = new MapperJCA(mm.getJormConfigurator());
077: }
078: mapper.setMapperName(mapperName);
079:
080: // to redirect getConnection() on our DataSource
081: mapper.setConnectionFactory(datasource);
082:
083: //register and finish to configure the new mapper
084: mm.addMapper(mapper, cont, datasource);
085: }
086: }
087: }
088:
089: /**
090: * It initializes the prefetching of a genclassMapping with the
091: * PClassMapping of the target class of the multivalued CMR.
092: * @param gcm is the GenClassMapping to initialized
093: * @param targetPCM is the PClassMapping of the target class
094: */
095: protected void initGenClassPrefetch(PClassMapping gcm,
096: PClassMapping targetPCM) {
097: if (gcm instanceof RdbGenClassProp
098: && targetPCM instanceof RdbPrefetchablePCM) {
099: ((RdbGenClassProp) gcm)
100: .setPrefetchElementPCM((RdbPrefetchablePCM) targetPCM);
101: }
102: }
103:
104: }
|