KickJava   Java API By Example, From Geeks To Geeks.

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


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.MetaObject;
30 import org.objectweb.jorm.metainfo.api.Package;
31 import org.objectweb.jorm.util.api.Loggable;
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.util.monolog.api.Logger;
34 import org.objectweb.util.monolog.api.LoggerFactory;
35
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * BasicPackage is an implementation of the Package interface.
44  * This object defines a set of classes and generic classes declared in a Package
45  * tag. Its parent is the Manager.
46  * @author X. Spengler
47  */

48 public class BasicPackage extends BasicMetaObject implements Package JavaDoc {
49     /**
50      * This structure contains all declared classes for the
51      * current schema object.
52      * key: the name of the class
53      * value: the Class MetaObject object
54      */

55     protected Map JavaDoc classes;
56
57     /**
58      * This structure contains all declared compositename for the
59      * current schema object.
60      * key: the name of the class
61      * value: the Class MetaObject object
62      */

63     protected Map JavaDoc compositeNames;
64
65     /**
66      * The name of the schema, this name could be null if
67      * the name is not defined by the user.
68      */

69     protected String JavaDoc name;
70
71     /**
72      * Builds a new Package object.
73      * This object is defined by its name and its parent.
74      *
75      * @param name the name of the schema to create
76      * @param parent the parent meta-object
77      */

78     public BasicPackage(String JavaDoc name, Manager parent) {
79         super(parent);
80         this.name = name;
81         classes = new HashMap JavaDoc();
82         compositeNames = new HashMap JavaDoc();
83     }
84
85     ///////////////////////////////////////////////////////////////////
86
// from Package interface
87
///////////////////////////////////////////////////////////////////
88

89     /**
90      * Returns the name of the schema.
91      *
92      * @return the string representation of the name of the schema.
93      * Null is returned if there is no schema name else a not empty
94      * dotted string.
95      */

96     public String JavaDoc getName() {
97         return name;
98     }
99
100     /**
101      * Returns a Class created with a class name.
102      *
103      * @param className the string representation of the class name
104      * @return a Class corresponding to its name. If the Class does
105      * not exist, null is returned.
106      */

107     public Class JavaDoc getClass(String JavaDoc className) {
108         if (debug && classes.get(className) == null) {
109             logger.log(BasicLevel.DEBUG, "There is no class with this name ("
110                 + className + ") for the current Package (" + name + ")");
111         }
112         return (Class JavaDoc) classes.get(className);
113     }
114
115
116     /**
117      * Returns a compositename identified with its name.
118      *
119      * @param cn_Name the string representation of the name of the composite name.
120      * @return a compositename corresponding to its name. If the
121      * compositename does not exist, null is returned.
122      */

123     public CompositeName getCompositeName(String JavaDoc cn_Name) {
124         if (debug && compositeNames.get(cn_Name) == null) {
125             logger.log(BasicLevel.DEBUG,
126                 "There is no composite name with this name (" + cn_Name
127                 + ") for the current Package (" + name + ")");
128         }
129         return (CompositeName) compositeNames.get(cn_Name);
130     }
131
132     /**
133      * Returns a new compositename identified by its name.
134      * The MetaObject is added to the list of compositename classes managed by the
135      * current schema. If the compositename already exists, it is returned.
136      *
137      * @param CN_Name the string representation of the name of the compositename
138      * @return a compositename corresponding to the created compositename
139      * and registered, or an existing one if already
140      * defined
141      */

142     public CompositeName createCompositeName(String JavaDoc CN_Name) {
143         if (debug)
144             logger.log(BasicLevel.DEBUG, "create a new CompositeName ("
145                 + CN_Name + ") for the current Package (" + name + ")");
146
147         // verify the class is not yet in the current schema
148
CompositeName result = (CompositeName) compositeNames.get(CN_Name);
149         if (result != null) {
150             if (debug) {
151                 logger.log(BasicLevel.DEBUG,
152                     "return the existing CompositeName" + CN_Name);
153             }
154         } else {
155             result = new BasicCompositeName(CN_Name, this);
156             setLoggingOnChild(result);
157             compositeNames.put(CN_Name, result);
158         }
159         return result;
160     }
161
162     /**
163      * Allows to know all the registered compositename into the current schema.
164      * This method returns an iterator on compositename object.
165      *
166      * @return an iterator for compositename object. If there is no class, an empty
167      * iterator is returned.
168      */

169     public Iterator JavaDoc iterateCompositeName() {
170         return compositeNames.values().iterator();
171     }
172
173     /**
174      * Returns a new Class created with a class name.
175      * The MetaObject is added to the list of classes managed by the current
176      * schema. An isAbstract boolean is defined to know if the current class
177      * is an abstract class or not.
178      *
179      * @param className the string representation of the class name
180      * @return a Class corresponding to the created class and
181      * registered, or an existing one if already defined
182      */

183     public Class JavaDoc createClass(String JavaDoc className) {
184         if (debug) {
185             logger.log(BasicLevel.DEBUG, "create a new Class (" + className
186                 + ") for the current Package (" + name + ")");
187         }
188         // verify the class is not yet in the current schema
189
Class JavaDoc result = (Class JavaDoc) classes.get(className);
190         if (result != null) {
191             if (debug) {
192                 logger.log(BasicLevel.DEBUG, "return the existing Class " + className);
193             }
194         } else {
195             result = new BasicClass(className, this);
196             setLoggingOnChild(result);
197             classes.put(className, result);
198         }
199         return result;
200     }
201
202     /**
203      * Adds an existing Class to the current Package
204      *
205      * @param aClass the Class object to add
206      */

207     public void addClass(Class JavaDoc aClass) {
208         String JavaDoc aClassName = aClass.getName();
209         if (classes.get(aClassName) != null) {
210             if (logger != null) {
211                 logger.log(BasicLevel.INFO, "try to add an existing Class ("
212                     + aClassName + ")");
213             }
214         } else {
215             classes.put(aClassName, aClass);
216         }
217     }
218
219     /**
220      * Adds an existing CompositeName to the current Package
221      *
222      * @param composite the CompositeName object to add
223      */

224     public void addCompositeName(CompositeName composite) {
225         String JavaDoc compositeName = composite.getName();
226         if (compositeNames.get(compositeName) != null) {
227             if (logger != null) {
228                 logger.log(BasicLevel.INFO, "try to add an existing CompositeName ("
229                         + compositeName + ")");
230             }
231         } else {
232             compositeNames.put(compositeName, composite);
233         }
234     }
235
236
237     /**
238      * Allows to know all the registered classes into the current schema.
239      * This method returns an iterator on Class object.
240      *
241      * @return an iterator for Class object. If there is no class, an empty
242      * iterator is returned.
243      */

244     public Collection JavaDoc getClasses() {
245         return classes.values();
246     }
247
248     public Collection JavaDoc getCompositeNames() {
249         return compositeNames.values();
250     }
251
252     protected Collection JavaDoc getChildren() {
253         ArrayList JavaDoc res = new ArrayList JavaDoc();
254         res.addAll(classes.values());
255         res.addAll(compositeNames.values());
256         return res;
257     }
258 }
259
Popular Tags