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): S.Chassande_________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: MapperManager.java 6673 2005-04-28 16:53:00Z benoitf $
026: * --------------------------------------------------------------------------
027: */package org.objectweb.jonas_ejb.container.jorm;
028:
029: import java.util.HashMap;
030: import java.util.Map;
031: import java.util.Properties;
032:
033: import org.objectweb.jonas_ejb.container.JContainer;
034: import org.objectweb.jonas_ejb.container.TraceEjb;
035:
036: import org.objectweb.jorm.lib.JormConfiguratorImpl;
037: import org.objectweb.jorm.api.JormConfigurator;
038: import org.objectweb.jorm.api.PException;
039: import org.objectweb.jorm.api.PMapper;
040: import org.objectweb.jorm.lib.Mapper;
041: import org.objectweb.jorm.util.api.Loggable;
042:
043: import org.objectweb.medor.eval.prefetch.lib.PrefetchCacheImpl;
044:
045: import org.objectweb.util.monolog.api.BasicLevel;
046: import org.objectweb.util.monolog.api.Logger;
047:
048: /**
049: * This class manages Jorm mappers. A mapper can be registered for each data
050: * source (connection factory) used in each EJB container.
051: * The Mapper manager provides also the JormConfigurator instance used to
052: * configure Jorm.
053: *
054: * @author Sebastien Chassande-Barrioz
055: */
056: public class MapperManager {
057:
058: /**
059: * The singleton instance of the class
060: */
061: private static MapperManager singleton = null;
062:
063: /**
064: * It retrieves the unique instance of MapperManager.
065: */
066: public static MapperManager getInstance() {
067: if (singleton == null) {
068: singleton = new MapperManager();
069: }
070: return singleton;
071: }
072:
073: /**
074: * The Logger used in this class
075: */
076: private Logger logger = null;
077:
078: /**
079: * This fields contains the association between a mapper ant its name.
080: * key: JContainer instance
081: * Value: a map (key = connection factory / value = PMapper instance)
082: *
083: */
084: private HashMap mappers = new HashMap();
085:
086: /**
087: * permits to configure Jorm and its mappers
088: */
089: private JormConfigurator jormConfigurator;
090:
091: protected MapperManager() {
092: logger = TraceEjb.logger;
093: jormConfigurator = new JormConfiguratorImpl();
094: Properties prop = new Properties();
095: prop.put("jorm.generator",
096: "org.objectweb.jorm.generator.lib.JormGenerator");
097: prop.put("jorm.mimanager",
098: "org.objectweb.jorm.metainfo.lib.JormManager");
099: prop.put("jorm.parser",
100: "org.objectweb.jorm.xml2mi.lib.BasicDomParser");
101: prop.put("jorm.verifier",
102: "org.objectweb.jorm.verifier.lib.JormVerifier");
103: prop.put("jorm.writer",
104: "org.objectweb.jorm.mi2xml.lib.BasicDomWriter");
105: prop.put("jorm.mapper.list", "rdb");
106: prop
107: .put("jorm.mapper.mifactory.rdb",
108: "org.objectweb.jorm.mapper.rdb.metainfo.RdbMappingFactory");
109: prop
110: .put("jorm.mapper.mopfactory.rdb",
111: "org.objectweb.jorm.mapper.rdb.generator.RdbMOPFactory");
112: prop
113: .put("jorm.mapper.gcmapping.rdb",
114: "org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassMapping");
115: prop
116: .put("jorm.mapper.schmgr.rdb",
117: "org.objectweb.jorm.mapper.rdb.lib.RdbPMappingStructuresManager");
118: jormConfigurator.configure(prop);
119: jormConfigurator.setLoggerFactory(TraceEjb.loggerFactory);
120: if (logger.isLoggable(BasicLevel.DEBUG)) {
121: logger.log(BasicLevel.DEBUG, "JormConfigurator created");
122: }
123: }
124:
125: /**
126: * Lookup a jorm mapper for a given container and a given connection factory
127: * @param c is the container asking the mapper
128: * @param cf is the connection factory (datasource for example) represented
129: * by the expected mapper.
130: * @return the PMapper instance if it exists, otherwise a null value.
131: */
132: public PMapper getMapper(JContainer c, Object cf) {
133: Map m = (Map) mappers.get(c);
134: return (m == null ? null : (Mapper) m.get(cf));
135: }
136:
137: /**
138: * Register and start PMapper for a container and a connection factory if
139: * another mapper is not registered with the same container and the same
140: * connection factory.
141: * @param m the mapper to register (never null)
142: * @param c the container using the mapper (never null)
143: * @param cf the connection factory represented by the mapper (never null)
144: * @return the mapper associated to the given container and the given
145: * connection factory. The returned mapper can be different from the mapper
146: * instance given in parameter if another similar mapper (same container
147: * and same connection factory) is already registered.
148: * @throws PException if it is not possible to start the mapper.
149: */
150: public PMapper addMapper(PMapper m, JContainer c, Object cf)
151: throws PException {
152: Map map;
153: PMapper pm = null;
154: synchronized (mappers) {
155: map = (Map) mappers.get(c);
156: if (map == null) {
157: map = new HashMap();
158: mappers.put(c, map);
159: }
160: }
161: pm = (PMapper) map.get(cf);
162: if (pm != null) {
163: return pm;
164: }
165: synchronized (map) {
166: pm = (PMapper) map.get(cf);
167: if (pm == null) {
168: pm = m;
169: //Allocate a new Cache of PreftechBuffer
170: m.setPrefetchCache(new PrefetchCacheImpl(logger));
171: //Add a logger
172: if (TraceEjb.loggerFactory == null) {
173: m.setLogger(logger);
174: } else {
175: m.setLogger(TraceEjb.loggerFactory
176: .getLogger(TraceEjb.prefix + ".mapper."
177: + m.getMapperName()));
178: if (m instanceof Loggable) {
179: ((Loggable) m)
180: .setLoggerFactory(TraceEjb.loggerFactory);
181: }
182: }
183: // start the mapper
184: m.start();
185: //register mapper
186: map.put(cf, m);
187: if (logger.isLoggable(BasicLevel.DEBUG)) {
188: logger.log(BasicLevel.DEBUG, "Mapper ("
189: + m.getMapperName() + ", " + cf
190: + ") initialized");
191: }
192: }
193: }
194: return pm;
195: }
196:
197: public Logger getLogger() {
198: return logger;
199: }
200:
201: public JormConfigurator getJormConfigurator() {
202: return jormConfigurator;
203: }
204: }
|