KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > metainfo > lib > JormManager


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.metainfo.lib;
25
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.metainfo.api.CompositeName;
28 import org.objectweb.jorm.metainfo.api.Manager;
29 import org.objectweb.jorm.metainfo.api.MappingFactory;
30 import org.objectweb.jorm.metainfo.api.MetaObject;
31 import org.objectweb.jorm.metainfo.api.Package;
32 import org.objectweb.jorm.type.api.PTypeSpace;
33 import org.objectweb.jorm.type.lib.PTypeSpacePAAH;
34 import org.objectweb.jorm.util.api.Loggable;
35 import org.objectweb.util.monolog.api.BasicLevel;
36 import org.objectweb.util.monolog.api.Logger;
37 import org.objectweb.util.monolog.api.LoggerFactory;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Map JavaDoc;
44
45 /**
46  * Implementation object of the Manager interface.
47  * This interface defines methods to create or find objects which construct
48  * the meta information from the description files.
49  * This object is managed by the parser system.
50  * @author X. Spengler
51  */

52 public class JormManager extends BasicMetaObject implements Manager {
53     /**
54      * this structure defines all declared schema in the current manager.
55      * key: the name of the schema
56      * value: a Package object
57      */

58     protected Map JavaDoc packages;
59
60     /**
61      * this structure defines all the mapping factories declared in the meta
62      * information manager.
63      * key: the name of the mapper
64      * value: a MappingFactory object
65      */

66     protected Map JavaDoc mappingFactories;
67
68     /**
69      * TBD
70      */

71     protected PTypeSpace ptypeSpace;
72
73     public JormManager() {
74         super(null);
75         init();
76     }
77
78     ///////////////////////////////////////////////////////////////////
79
// from Manager interface
80
///////////////////////////////////////////////////////////////////
81

82     /**
83      * Initializes the current Manager.
84      * This method replaces the constructor method.
85      */

86     public void init() {
87         packages = new HashMap JavaDoc();
88         mappingFactories = new HashMap JavaDoc();
89         ptypeSpace = new PTypeSpacePAAH();
90     }
91
92     /**
93      * Returns an existing schema.
94      * If no Package corresponds to the given name, null is returned.
95      *
96      * @param schemaName the name of the schema
97      * @return a Package object. If the schema does not exist, null is returned.
98      */

99     public Package JavaDoc getPackage(String JavaDoc schemaName) {
100         return (Package JavaDoc) packages.get(schemaName);
101     }
102
103     /**
104      * Returns a new schema.
105      * This method is the factory method to create schema object. If the
106      * schemaName schema already exists, it is returned.
107      *
108      * @param schemaName the name of the schema
109      * @return a new schema object, or the existing one if already defined
110      */

111     public Package JavaDoc createPackage(String JavaDoc schemaName) {
112         if (debug) {
113             logger.log(BasicLevel.DEBUG,
114                        "create a new Package (" + schemaName + ") for the current Manager");
115         }
116         // verify if the schema is not yet in the database
117
if (packages.get(schemaName) != null) {
118             if (debug)
119                 logger.log(BasicLevel.DEBUG, "The Package already exist ("
120                     + schemaName + "), return existing one");
121             return (Package JavaDoc) packages.get(schemaName);
122         }
123
124         // else create a new one
125
Package JavaDoc newPackage = new BasicPackage(schemaName, this);
126         setLoggingOnChild(newPackage);
127         packages.put(schemaName, newPackage);
128         return newPackage;
129     }
130
131     /**
132      * Allows to know all the declared schemas into the current metainformation
133      * manager.
134      * This iterator contains Package object.
135      *
136      * @return an iterator on registered Package object. If there is no schema,
137      * an empty iterator is returned.
138      */

139     public Collection JavaDoc getPackages() {
140         return packages.values();
141     }
142
143     /**
144      * Adds a mapping factory to the current metainformation manager.
145      * A mapping factory is a factory to map object (class, field, ...).
146      * @param mapperName the name of the mapper (i.e: OR for rdb)
147      * @param factory the mapping factory to add to the list of
148      * existing mapping factories
149      */

150     public void addMappingFactory(String JavaDoc mapperName, MappingFactory factory) {
151         setLoggingOnChild(factory);
152         mappingFactories.put(mapperName, factory);
153     }
154
155     /**
156      * Returns a mapping factory corresponding to a mapper name.
157      * If no MappingFactory corresponds to the mapper name, null is returned.
158      *
159      * @param mapperName the name of the mapper to obtain
160      * @return a mapping factory. If the mapping factory does not exist for
161      * the mapper, null is returned.
162      */

163     public MappingFactory getMappingFactory(String JavaDoc mapperName) {
164         if (debug) {
165             logger.log(BasicLevel.DEBUG, "get the mappingFactory from "
166                 + mapperName);
167         }
168         return (MappingFactory) mappingFactories.get(mapperName);
169     }
170
171     public Collection JavaDoc getMappingFactories() {
172         return mappingFactories.values();
173     }
174
175     /**
176      * TBD
177      */

178     public PTypeSpace getPTypeSpace() {
179         return ptypeSpace;
180     }
181
182     /**
183      * build an iterator to iterates all classes of the meta-info
184      * @return a new iterator
185      */

186     public Collection JavaDoc getClasses() {
187         ArrayList JavaDoc v = new ArrayList JavaDoc();
188         Iterator JavaDoc allPackages = packages.values().iterator();
189         while (allPackages.hasNext()) {
190             v.addAll(((Package JavaDoc) allPackages.next()).getClasses());
191         }
192         return v;
193     }
194
195     /**
196      * build an iterator to iterates all classes of the meta-info
197      * @return a new iterator
198      */

199     public Collection JavaDoc getCompositeNames() {
200         ArrayList JavaDoc v = new ArrayList JavaDoc();
201         Iterator JavaDoc allPackages = packages.values().iterator();
202         while (allPackages.hasNext()) {
203             v.addAll(((Package JavaDoc) allPackages.next()).getCompositeNames());
204         }
205         return v;
206     }
207
208     public Collection JavaDoc getJormObjects() {
209         ArrayList JavaDoc v = new ArrayList JavaDoc();
210         Iterator JavaDoc allPackages = packages.values().iterator();
211         while (allPackages.hasNext()) {
212             Package JavaDoc p = (Package JavaDoc) allPackages.next();
213             v.addAll(p.getClasses());
214             v.addAll(p.getCompositeNames());
215         }
216         return v;
217     }
218
219     /**
220      * searches a class into all the declared schemas and returns it.
221      * If no class corresponds to this name, null is returned.
222      * @param className the name of the class to search
223      * @return the Class object found, null if not found.
224      */

225     public Class JavaDoc getClass(String JavaDoc className) {
226         if (packages == null || className == null) {
227             return null;
228         }
229         int idx = className.lastIndexOf(".");
230         Package JavaDoc p = null;
231         if (idx == -1) {
232             p = (Package JavaDoc) packages.get("");
233             if (p != null) {
234                 return p.getClass(className);
235             }
236         } else {
237             p = (Package JavaDoc) packages.get(className.substring(0, idx));
238             if (p != null) {
239                 return p.getClass(className.substring(idx + 1));
240             }
241         }
242         return null;
243     }
244
245     public Class JavaDoc createClass(String JavaDoc fqclassName) {
246         if (packages == null || fqclassName == null) {
247             return null;
248         }
249         int idx = fqclassName.lastIndexOf(".");
250         if (idx == -1) {
251             return createPackage("").createClass(fqclassName);
252         } else {
253             return createPackage(fqclassName.substring(0, idx))
254                     .createClass(fqclassName.substring(idx + 1));
255         }
256     }
257
258     public CompositeName getCompositeName(String JavaDoc fqcompositeNameName) {
259         if (packages == null || fqcompositeNameName == null) {
260             return null;
261         }
262         int idx = fqcompositeNameName.lastIndexOf(".");
263         Package JavaDoc p = null;
264         if (idx == -1) {
265             p = (Package JavaDoc) packages.get("");
266             if (p != null) {
267                 return p.getCompositeName(fqcompositeNameName);
268             }
269         } else {
270             p = (Package JavaDoc) packages.get(fqcompositeNameName.substring(0, idx));
271             if (p != null) {
272                 return p.getCompositeName(fqcompositeNameName.substring(idx + 1));
273             }
274         }
275         return null;
276     }
277
278     public CompositeName createCompositeName(String JavaDoc fqcompositeNameName) {
279         if (packages == null || fqcompositeNameName == null) {
280             return null;
281         }
282         int idx = fqcompositeNameName.lastIndexOf(".");
283         if (idx == -1) {
284             return createPackage("").createCompositeName(fqcompositeNameName);
285         } else {
286             return createPackage(fqcompositeNameName.substring(0, idx))
287                     .createCompositeName(fqcompositeNameName.substring(idx + 1));
288         }
289     }
290
291     protected Collection JavaDoc getChildren() {
292         return packages.values();
293     }
294 }
295
Popular Tags