KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.objectweb.jorm.metainfo.lib;
24
25 import org.objectweb.jorm.metainfo.api.Class;
26 import org.objectweb.jorm.metainfo.api.Mapping;
27 import org.objectweb.jorm.metainfo.api.ClassProject;
28 import org.objectweb.jorm.metainfo.api.MetaObject;
29 import org.objectweb.util.monolog.api.BasicLevel;
30
31 import java.util.Collection JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Set JavaDoc;
34
35
36 /**
37  * Implementation of ClassProject.
38  *
39  * The notion of project allows the developer to define multiple mappings
40  * for a class. There is at most one mapping per mapper name in a project.
41  */

42 public class BasicClassProject extends BasicMetaObject implements ClassProject {
43     /**
44      * The project name
45      */

46     String JavaDoc projectName;
47
48     /**
49      * This hashmap associates a mapper name with a Mapping object.
50      */

51     HashMap JavaDoc mappings;
52
53     /**
54      * Builds a new BasicClassProject object.
55      *
56      * The parent object is a Class object.
57      *
58      * @param projectName the project name,
59      * parent the parent of the current object.
60      */

61     public BasicClassProject(String JavaDoc projectName, MetaObject parent) {
62         super(parent);
63         this.projectName = projectName;
64         mappings = new HashMap JavaDoc();
65     }
66
67     ///////////////////////////////////////////////////////////////////
68
// from ClassProject interface
69
///////////////////////////////////////////////////////////////////
70

71     /**
72      * Returns the project name.
73      * @return the project name.
74      */

75     public String JavaDoc getProjectName() {
76         return projectName;
77     }
78
79     /**
80      * Sets the project name.
81      * @param name the project name.
82      */

83     public void setProjectName(String JavaDoc name) {
84         this.projectName = name;
85     }
86
87     /**
88      * Returns a collection of Mapping objects.
89      * @return a collection.
90      */

91     public Collection JavaDoc getMappings() {
92         return mappings.values();
93     }
94
95     /**
96      * Returns a Mapping object.
97      *
98      * @param mapperName a mapper name.
99      * @return a collection.
100      */

101     public Mapping getMapping(String JavaDoc mapperName) {
102         Mapping mapping = (Mapping) mappings.get(mapperName);
103         return mapping;
104     }
105
106     /**
107      * Returns a set of mapper names.
108      * @return a set of mapper names.
109      */

110     public Set JavaDoc getMappers() {
111         return mappings.keySet();
112     }
113
114     /**
115      * Creates a new Mapping object using a MappingFactory object.
116      *
117      * @param mapperName the name of the mapper.
118      * @return a new Mapping object or an existing one.
119      */

120     public Mapping createMapping(String JavaDoc mapperName) {
121         if (debug) {
122             logger.log(BasicLevel.DEBUG,
123                             "Create a new Mapping (" + mapperName +
124                             ") for the current ClassProject (" + projectName + ")");
125         }
126         // verifies whether the Mapping object already exists
127
Mapping mp = (Mapping) mappings.get(mapperName);
128         if (mp == null) {
129             mp = getManager().getMappingFactory(mapperName)
130                     .createMapping(mapperName, this);
131             setLoggingOnChild(mp);
132             mappings.put(mapperName, mp);
133         } else {
134             String JavaDoc className = ((Class JavaDoc) getParent()).getName();
135             if (debug) {
136                 logger.log(BasicLevel.DEBUG,
137                             "try to map twice the class (" + className +
138                             ") using the mapper (" + mapperName +
139                             "), the existing mapping is returned.");
140             }
141         }
142         return mp;
143     }
144
145     protected Collection JavaDoc getChildren() {
146         return mappings.values();
147     }
148 }
149
Popular Tags