KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > lib > Mapper


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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 of the License, or (at your option) 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 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.lib;
25
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.EventObject JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import org.objectweb.jorm.api.ClassMappedEvent;
34 import org.objectweb.jorm.api.JormConfigurator;
35 import org.objectweb.jorm.api.PClassMapping;
36 import org.objectweb.jorm.api.PException;
37 import org.objectweb.jorm.api.PExceptionIO;
38 import org.objectweb.jorm.api.PExceptionProtocol;
39 import org.objectweb.jorm.api.PMapCluster;
40 import org.objectweb.jorm.api.PMapper;
41 import org.objectweb.jorm.api.PMapperListener;
42 import org.objectweb.jorm.api.PMappingCallback;
43 import org.objectweb.jorm.api.PMappingRequiredEvent;
44 import org.objectweb.jorm.api.PMappingStructuresManager;
45 import org.objectweb.jorm.metainfo.api.Manager;
46 import org.objectweb.jorm.type.api.PExceptionTyping;
47 import org.objectweb.jorm.type.api.PTypeSpace;
48 import org.objectweb.jorm.type.lib.PTypeSpacePAAH;
49 import org.objectweb.jorm.util.api.Loggable;
50 import org.objectweb.jorm.xml2mi.api.Parser;
51 import org.objectweb.medor.eval.prefetch.api.PrefetchCache;
52 import org.objectweb.util.monolog.api.BasicLevel;
53 import org.objectweb.util.monolog.api.Logger;
54 import org.objectweb.util.monolog.api.LoggerFactory;
55
56 /**
57  * @author P. Dechamboux
58  */

59 public abstract class Mapper implements PMapper, Loggable, PMappingCallback {
60     /**
61      * This is the initial size of the map used to store the mapped classes.
62      */

63     static final int INITIALMAPSIZE = 100;
64     /**
65      * This is the name of the kind of mapper used (e.g., file, rdb, etc).
66      */

67     String JavaDoc mapperName = null;
68     /**
69      * This is the Manager that manages the meta-information associated with
70      * classes thnat are mapped within this mapper. It may stay null.
71      */

72     Manager metainfoManager = null;
73     /**
74      * This is the Parser that is used to load the mate-information associated
75      * with classes thnat are mapped within this mapper. It may stay null.
76      */

77     Parser parser = null;
78     /**
79      * This is the TypeSpace associated with this mapper. It is mandatory for
80      * mapping classes.
81      */

82     PTypeSpace typeSpace = null;
83     /**
84      * This is the structure that registers the classes that have been mapped
85      * within this mapper.
86      */

87     HashMap JavaDoc mappedClasses = new HashMap JavaDoc(INITIALMAPSIZE);
88     /**
89      * This is the optional prefetch cache that can be associated with a mapper.
90      * It is activated when the dedicated read operation is performed on
91      * bindings.
92      */

93     PrefetchCache prefetchCache = null;
94
95     /**
96      * The logger associated with this mapper.
97      */

98     protected Logger logger = null;
99     /**
100      * The logger factory associated with this mapper.
101      */

102     protected LoggerFactory loggerFactory = null;
103
104     /**
105      * The configurator used to configure this mapper.
106      */

107     private JormConfigurator jormConfigurator = null;
108
109     private Class JavaDoc gcmClass = null;
110
111     PMappingStructuresManager pMappingStructuresManager = null;
112
113     private ArrayList JavaDoc listenerList = new ArrayList JavaDoc();
114
115
116     /**
117      * It builds a JormConfigurator with the default JormCompilerParameter and
118      * JormCompilerParameter.
119      */

120     public Mapper() throws PException {
121         jormConfigurator = new JormConfiguratorImpl();
122         jormConfigurator.configure(PMapper.JORMPROPFILE);
123         jormConfigurator.configureLog(PMapper.JORMPROPFILE);
124     }
125
126     public Mapper(JormConfigurator c) {
127         jormConfigurator = c;
128     }
129
130     public void setJormConfigurator(JormConfigurator jormConfigurator) {
131         this.jormConfigurator = jormConfigurator;
132     }
133
134     public JormConfigurator getJormConfigurator() {
135         return jormConfigurator;
136     }
137     // IMPLEMENTATION OF METHODS FROM THE PMapingCallback INTERFACE
138

139     /**
140      * Schedules an mapper-related event.
141      * @param e The event to be scheduled.
142      * @throws PException Cannot schedule this event.
143      */

144     public void scheduleEvent(EventObject JavaDoc e) throws PException {
145         Iterator JavaDoc it = listenerList.iterator();
146         while (it.hasNext()) {
147             PMapperListener l = (PMapperListener) it.next();
148             if (e instanceof PMappingRequiredEvent) {
149                 l.mappingRequired((PMappingRequiredEvent) e);
150             } else if (e instanceof ClassMappedEvent) {
151                 l.classMapped((ClassMappedEvent) e);
152             } else {
153                 throw new PException("Mapper cannot schedule this kind of event: " + e);
154             }
155         }
156     }
157
158     // IMPLEMENTATION OF METHODS FROM THE PMapper INTERFACE
159

160     /**
161      * The name yielded by this method defines what kind of PMapper is used
162      * on a particular DS.
163      * @return The name defining the PMapper kind.
164      */

165     public String JavaDoc getMapperName() {
166         return mapperName;
167     }
168
169     /**
170      * Retrieves the PMappingStructuresManager associated with this mapper.
171      * @return The associated PMappingStructuresManager.
172      */

173     public PMappingStructuresManager getPMappingStructuresManager() {
174         return pMappingStructuresManager;
175     }
176
177     /**
178      * It looks for the PClassMapping object associated to a JORM class within
179      * this PMapper.
180      * @return The PClassMapping that maps the corresponding class, or null if
181      * none exists
182      */

183     public PClassMapping lookup(String JavaDoc classname) {
184         return (PClassMapping) mappedClasses.get(classname);
185     }
186
187     /**
188      * It registers a PClassMapping associated to a particular JORM class within
189      * this PMapper. It initializes the storage structures if necessary. It
190      * previously removes them if the "dsrem" flag is set. If the mapper is
191      * ready to manage the meta-information, it is loaded for this JORM class.
192      * @param pcm The class mapping to add to this mapper.
193      * @exception PExceptionIO It is raised when an I/O problem occurs within
194      * the underlying DS.
195      * @exception PExceptionProtocol It is raised when a problem occurs
196      * during initialisation of the class
197      * mapping.
198      * @exception PExceptionTyping It is raised when a typing problem occurs
199      * during initialisation of the class mapping.
200      */

201     public void map(PClassMapping pcm)
202             throws PException {
203         map(null, pcm);
204     }
205
206     /**
207      * It registers a PClassMapping associated to a particular JORM class within
208      * this PMapper. It initializes the storage structures if necessary. It
209      * previously removes them if the "dsrem" flag is set. If the mapper is
210      * ready to manage the meta-information, it is loaded for this JORM class.
211      * @param conn The connection to access to the underlying DS.
212      * @param pcm The class mapping to add to this mapper.
213      * @exception PExceptionIO It is raised when an I/O problem occurs within
214      * the underlying DS.
215      * @exception PExceptionProtocol It is raised when a problem occurs
216      * during initialisation of the class
217      * mapping.
218      * @exception PExceptionTyping It is raised when a typing problem occurs
219      * during initialisation of the class mapping.
220      */

221     public void map(Object JavaDoc conn, PClassMapping pcm)
222             throws PException {
223         if (pcm == null)
224             throw new PExceptionProtocol(
225                     "Cannot map null class mapping.");
226         PClassMapping ecm
227                 = (PClassMapping) mappedClasses.get(pcm.getClassName());
228         if (ecm == pcm)
229             return;
230         if (ecm != null)
231             throw new PExceptionProtocol(
232                     "Class already mapped within this mapper.");
233         if (!pcm.isConform(mapperName))
234             throw new PExceptionProtocol("Class mapping (" + pcm.getClass().getName()
235                                          + ") cannot be managed by this mapper ("
236                                          + mapperName + ").");
237         if (typeSpace == null)
238             typeSpace = new PTypeSpacePAAH();
239         ((Loggable) pcm).setLogger(loggerFactory.getLogger(
240                 "org.objectweb.jorm." + mapperName + "." + pcm.getClassName()));
241         pcm.init(this, null);
242         mappedClasses.put(pcm.getClassName(), pcm);
243         scheduleEvent(new ClassMappedEvent(this, pcm.getClassName()));
244         if (logger != null)
245             logger.log(BasicLevel.INFO,
246                        "New mapped class: " + pcm.getClassName());
247     }
248
249     /**
250      * It registers a PClassMapping associated to a particular JORM class within
251      * this PMapper. It initializes the storage structures if necessary. It
252      * previously removes them if the "dsrem" flag is set. It also loads the
253      * meta-information related to this class if the "loadmeta" flag is set.
254      * @param conn The connection to access to the underlying DS.
255      * @param pcm The class mapping to add to this mapper.
256      * @param loadmeta It specifies that meta-information should be loaded for
257      * this class to map.
258      * @exception PExceptionIO It is raised when an I/O problem occurs within
259      * the underlying DS.
260      * @exception PExceptionProtocol It is raised when a problem occurs
261      * during initialisation of the class
262      * mapping.
263      * @exception PExceptionTyping It is raised when a typing problem occurs
264      * during initialisation of the class mapping.
265      */

266     public void map(Object JavaDoc conn, PClassMapping pcm, boolean loadmeta)
267             throws PException {
268         if (loadmeta && ((metainfoManager == null) || (parser == null))) {
269             throw new PExceptionProtocol(
270                     "Not enough means to load meta-information.");
271         }
272         map(pcm);
273         if (!loadmeta)
274             return;
275         parser.parse(
276                 pcm.getClassName().replace('.', File.pathSeparatorChar) + ".pd");
277     }
278
279     /**
280      * It assigns the name of the mapper that is instanciated. It is mandatory
281      * to assign it at mapper initialization as the load of meta-information
282      * depends on it.
283      * @param mappername The name of the maping as defined within the XML
284      * JORM definition files.
285      */

286     public void setMapperName(String JavaDoc mappername) {
287         mapperName = mappername;
288     }
289
290     public PClassMapping createGenClassMapping() throws PException {
291         try {
292             return (PClassMapping) gcmClass.newInstance();
293         } catch (InstantiationException JavaDoc e) {
294             throw new PException(e, "Cannot instanciate gen class mapping Class.");
295         } catch (IllegalAccessException JavaDoc e) {
296             throw new PException(e, "Cannot access to gen class mapping Class.");
297         }
298     }
299
300     public Manager getMetaInfoManager() {
301         return metainfoManager;
302     }
303
304     /**
305      * It returns a PTypeSpace that may be used to create PType
306      * associated to JORM classes mapped within this PMapper.
307      */

308     public PTypeSpace getPTypeSpace() {
309         return typeSpace;
310     }
311
312     /**
313      * Starts this mapper.
314      * @throws PException
315      */

316     public void start() throws PException {
317         String JavaDoc basemn = mapperName;
318         int ind = mapperName.indexOf(".");
319         if (ind != -1) {
320             basemn = mapperName.substring(0, ind);
321         }
322         metainfoManager = jormConfigurator.getMIManager();
323         parser = jormConfigurator.getParser();
324         pMappingStructuresManager = jormConfigurator.getSchMgr(basemn);
325         pMappingStructuresManager.setPMapper(this);
326         pMappingStructuresManager.setLogger(logger);
327         typeSpace = metainfoManager.getPTypeSpace();
328         gcmClass = jormConfigurator.getGcmClass(basemn);
329     }
330
331     /**
332      * Stops this mapper.
333      * @throws PException
334      */

335     public void stop() throws PException {
336     }
337
338     /**
339      * It unmaps a particular JORM class within this PMapper.
340      * @param classname The name of the JORM class to be unmapped.
341      */

342     public void unmap(String JavaDoc classname) throws PException {
343         mappedClasses.remove(classname);
344     }
345
346     /**
347      * Assigns a prefetch cache to this mapper. It is optional.
348      * @exception PExceptionProtocol It is raised if a prefetch cache
349      * has already been assigned.
350      */

351     public void setPrefetchCache(PrefetchCache pc) throws PException {
352         if (prefetchCache != null) {
353             throw new PExceptionProtocol("A prefetch cache has already been assigned to this mapper");
354         }
355         prefetchCache = pc;
356     }
357
358     /**
359      * Returns the prefetch cache associated to this mapper. It can be null.
360      */

361     public PrefetchCache getPrefetchCache() {
362         return prefetchCache;
363     }
364
365     // IMPLEMENTATION OF METHODS FROM THE PMappingStructuresManager INTERFACE
366

367     /**
368      * Asks for the map cluster associated with a JORM class.
369      * @return The relevant map cluster.
370      * @throws PException This class has not been mapped.
371      */

372     public PMapCluster getPMapCluster(String JavaDoc jcname) throws PException {
373         if (pMappingStructuresManager == null) {
374             throw new PException("No pMappingStructuresManager associated with this mapper");
375         }
376         return pMappingStructuresManager.getPMapCluster(jcname);
377     }
378
379     /**
380      * Asks for all the map clusters defined within this mapper.
381      * @return The Iterator over the collection of map clusters.
382      */

383     public Collection JavaDoc getPMapClusters() {
384         if (pMappingStructuresManager == null) {
385             return new ArrayList JavaDoc();
386         }
387         return pMappingStructuresManager.getPMapClusters();
388     }
389
390     public void addDependency(String JavaDoc jcname1, String JavaDoc jcname2) throws PException {
391         if (pMappingStructuresManager == null) {
392             throw new PException("No pMappingStructuresManager associated with this mapper");
393         }
394         pMappingStructuresManager.addDependency(jcname1, jcname2);
395     }
396
397     public void declareClass(String JavaDoc jcname) {
398         pMappingStructuresManager.declareClass(jcname);
399     }
400
401     public void classDefined(String JavaDoc jcname) throws PException {
402         if (pMappingStructuresManager == null) {
403             throw new PException("No pMappingStructuresManager associated with this mapper");
404         }
405         pMappingStructuresManager.classDefined(jcname);
406     }
407
408     /**
409      * Adds the given mapper listener to this mapper. If it already exists,
410      * does nothing.
411      * @param listener The corresponding listener.
412      */

413     public synchronized void addMapperEventListener(PMapperListener listener) {
414         if (listenerList.indexOf(listener) == -1) {
415             listenerList.add(listener);
416         }
417     }
418
419     /**
420      * Removes the given mapper listener from this mapper. If it is not
421      * present among this list, does nothing.
422      * @param listener The corresponding listener.
423      */

424     public synchronized void removeMapperEventListener(PMapperListener listener) {
425         if (listenerList.indexOf(listener) != -1) {
426             listenerList.remove(listener);
427         }
428     }
429
430     /**
431      * Assigns a mapper to this schema manager.
432      * @param pm The concerned mapper.
433      */

434     public void setPMapper(PMapper pm) {
435     }
436
437     // IMPLEMENTATION OF METHODS FROM THE Loggable INTERFACE
438

439     /**
440      * It assigns a logger to an component that wants to log things.
441      * @param logger The logger object.
442      */

443     public void setLogger(Logger logger) {
444     }
445
446     /**
447      * It assigns a logger factory that allows the creation of new loggers.
448      * @param loggerfactory The LoggerFactory object to obtain new loggers.
449      */

450     public void setLoggerFactory(LoggerFactory loggerfactory) {
451         loggerFactory = loggerfactory;
452         logger = loggerFactory.getLogger("org.objectweb.jorm." + mapperName);
453     }
454
455     public Logger getLogger() {
456         return logger;
457     }
458
459     public LoggerFactory getLoggerFactory() {
460         return loggerFactory;
461     }
462
463     public String JavaDoc cn2mn(String JavaDoc cn) {
464         String JavaDoc shortMapperName = getMapperName();
465         int idx = shortMapperName.indexOf(".");
466         if (idx != -1) {
467             shortMapperName = shortMapperName.substring(0, idx);
468         }
469         int ldot = cn.lastIndexOf('.');
470         if (ldot == -1)
471             return shortMapperName + "." + cn;
472         return cn.substring(0, ldot + 1) + shortMapperName + cn.substring(ldot);
473     }
474
475     // IMPLEMENTATION OF METHODS FROM THE Object CLASS
476

477     public boolean equals(Object JavaDoc o) {
478         if (this == o)
479             return true;
480         if (!(o instanceof PMapper))
481             return false;
482         PMapper m = (PMapper) o;
483         return m.getMapperName().equals(mapperName);
484     }
485
486     public int hashCode() {
487         return mapperName.hashCode();
488     }
489 }
490
Popular Tags