KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > jorm > MapperManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): S.Chassande_________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: MapperManager.java,v 1.12 2005/04/28 16:52:59 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 package org.objectweb.jonas_ejb.container.jorm;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.objectweb.jonas_ejb.container.JContainer;
36 import org.objectweb.jonas_ejb.container.TraceEjb;
37
38 import org.objectweb.jorm.lib.JormConfiguratorImpl;
39 import org.objectweb.jorm.api.JormConfigurator;
40 import org.objectweb.jorm.api.PException;
41 import org.objectweb.jorm.api.PMapper;
42 import org.objectweb.jorm.lib.Mapper;
43 import org.objectweb.jorm.util.api.Loggable;
44
45 import org.objectweb.medor.eval.prefetch.lib.PrefetchCacheImpl;
46
47 import org.objectweb.util.monolog.api.BasicLevel;
48 import org.objectweb.util.monolog.api.Logger;
49
50 /**
51  * This class manages Jorm mappers. A mapper can be registered for each data
52  * source (connection factory) used in each EJB container.
53  * The Mapper manager provides also the JormConfigurator instance used to
54  * configure Jorm.
55  *
56  * @author Sebastien Chassande-Barrioz
57  */

58 public class MapperManager {
59
60     /**
61      * The singleton instance of the class
62      */

63     private static MapperManager singleton = null;
64
65     /**
66      * It retrieves the unique instance of MapperManager.
67      */

68     public static MapperManager getInstance() {
69         if (singleton == null) {
70             singleton = new MapperManager();
71         }
72         return singleton;
73     }
74
75     /**
76      * The Logger used in this class
77      */

78     private Logger logger = null;
79
80     /**
81      * This fields contains the association between a mapper ant its name.
82      * key: JContainer instance
83      * Value: a map (key = connection factory / value = PMapper instance)
84      *
85      */

86     private HashMap JavaDoc mappers = new HashMap JavaDoc();
87
88     /**
89      * permits to configure Jorm and its mappers
90      */

91     private JormConfigurator jormConfigurator;
92
93     protected MapperManager() {
94         logger = TraceEjb.logger;
95         jormConfigurator = new JormConfiguratorImpl();
96         Properties JavaDoc prop = new Properties JavaDoc();
97         prop.put("jorm.generator", "org.objectweb.jorm.generator.lib.JormGenerator");
98         prop.put("jorm.mimanager", "org.objectweb.jorm.metainfo.lib.JormManager");
99         prop.put("jorm.parser", "org.objectweb.jorm.xml2mi.lib.BasicDomParser");
100         prop.put("jorm.verifier", "org.objectweb.jorm.verifier.lib.JormVerifier");
101         prop.put("jorm.writer", "org.objectweb.jorm.mi2xml.lib.BasicDomWriter");
102         prop.put("jorm.mapper.list", "rdb");
103         prop.put("jorm.mapper.mifactory.rdb", "org.objectweb.jorm.mapper.rdb.metainfo.RdbMappingFactory");
104         prop.put("jorm.mapper.mopfactory.rdb", "org.objectweb.jorm.mapper.rdb.generator.RdbMOPFactory");
105         prop.put("jorm.mapper.gcmapping.rdb", "org.objectweb.jorm.mapper.rdb.genclass.RdbGenClassMapping");
106         prop.put("jorm.mapper.schmgr.rdb", "org.objectweb.jorm.mapper.rdb.lib.RdbPMappingStructuresManager");
107         jormConfigurator.configure(prop);
108         jormConfigurator.setLoggerFactory(TraceEjb.loggerFactory);
109         if (logger.isLoggable(BasicLevel.DEBUG)) {
110             logger.log(BasicLevel.DEBUG, "JormConfigurator created");
111         }
112     }
113
114     /**
115      * Lookup a jorm mapper for a given container and a given connection factory
116      * @param c is the container asking the mapper
117      * @param cf is the connection factory (datasource for example) represented
118      * by the expected mapper.
119      * @return the PMapper instance if it exists, otherwise a null value.
120      */

121     public PMapper getMapper(JContainer c, Object JavaDoc cf) {
122         Map JavaDoc m = (Map JavaDoc) mappers.get(c);
123         return (m == null ? null : (Mapper) m.get(cf));
124     }
125
126     /**
127      * Register and start PMapper for a container and a connection factory if
128      * another mapper is not registered with the same container and the same
129      * connection factory.
130      * @param m the mapper to register (never null)
131      * @param c the container using the mapper (never null)
132      * @param cf the connection factory represented by the mapper (never null)
133      * @return the mapper associated to the given container and the given
134      * connection factory. The returned mapper can be different from the mapper
135      * instance given in parameter if another similar mapper (same container
136      * and same connection factory) is already registered.
137      * @throws PException if it is not possible to start the mapper.
138      */

139     public PMapper addMapper(PMapper m, JContainer c, Object JavaDoc cf) throws PException {
140         Map JavaDoc map;
141         PMapper pm = null;
142         synchronized (mappers) {
143             map = (Map JavaDoc) mappers.get(c);
144             if (map == null) {
145                 map = new HashMap JavaDoc();
146                 mappers.put(c, map);
147             }
148         }
149         pm = (PMapper) map.get(cf);
150         if (pm != null) {
151             return pm;
152         }
153         synchronized(map) {
154             pm = (PMapper) map.get(cf);
155             if (pm == null) {
156                 pm = m;
157                 //Allocate a new Cache of PreftechBuffer
158
m.setPrefetchCache(new PrefetchCacheImpl(logger));
159                 //Add a logger
160
if (TraceEjb.loggerFactory == null) {
161                     m.setLogger(logger);
162                 } else {
163                     m.setLogger(TraceEjb.loggerFactory.getLogger(TraceEjb.prefix
164                             + ".mapper." + m.getMapperName()));
165                     if (m instanceof Loggable) {
166                         ((Loggable) m).setLoggerFactory(TraceEjb.loggerFactory);
167                     }
168                 }
169                 // start the mapper
170
m.start();
171                 //register mapper
172
map.put(cf, m);
173                 if (logger.isLoggable(BasicLevel.DEBUG)) {
174                     logger.log(BasicLevel.DEBUG, "Mapper (" + m.getMapperName()
175                             + ", " + cf + ") initialized");
176                 }
177             }
178         }
179         return pm;
180     }
181
182     public Logger getLogger() {
183         return logger;
184     }
185
186     public JormConfigurator getJormConfigurator() {
187         return jormConfigurator;
188     }
189 }
190
Popular Tags