001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.metadata;
027:
028: import org.objectweb.speedo.api.SpeedoException;
029: import org.objectweb.speedo.sequence.lib.SpeedoSequence;
030: import org.objectweb.util.monolog.api.Logger;
031:
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.Map;
035:
036: /**
037: * Describes a package which contains persistence capable classes.
038: * @author S.Chassande-Barrioz
039: */
040: public class SpeedoPackage extends SpeedoElement {
041:
042: /**
043: * Comment for <code>serialVersionUID</code>
044: */
045: private static final long serialVersionUID = -4376613964706359037L;
046:
047: /**
048: * Package name.
049: */
050: public String name;
051:
052: /**
053: * Persistence capable classes descriptors of the package. The HashMap key
054: * is the class name.
055: */
056: public Map classes = new HashMap();
057:
058: /**
059: * Sequences descriptors of the package. The HashMap key
060: * is the sequence name.
061: */
062: public Map sequences = new HashMap();
063:
064: /**
065: * Descriptor to which this package is associated.
066: */
067: public SpeedoXMLDescriptor xmlDescriptor;
068:
069: public SpeedoPackage() {
070: SpeedoDefaults.setDefaults(this );
071: }
072:
073: /**
074: * Transforms a SpeedoPackage into a String.
075: * @return the corresponding String.
076: */
077: public String toString() {
078: String s = "package\tname : " + name;
079: s += "\n\tclasses: ";
080: Iterator it = classes.values().iterator();
081: while (it.hasNext()) {
082: s = s + " " + it.next().toString();
083: }
084: s += "\n\tsequences: ";
085: it = sequences.values().iterator();
086: while (it.hasNext()) {
087: s = s + " " + it.next().toString();
088: }
089: return s;
090: }
091:
092: /**
093: * Adds a class descriptor to the package descriptor.
094: * @param clazz class to add.
095: * @param failsOnError if an error provoques an exception or a warning message.
096: * @param logger logger where to put warning message.
097: * @exception SpeedoException If a field of the class descriptor is already
098: * defined into the package descriptor.
099: */
100: public void addClass(Object clazz, boolean failsOnError,
101: Logger logger) throws SpeedoException {
102: SpeedoClass sc = (SpeedoClass) clazz;
103: //the class already exists
104: if (classes.containsKey(sc.name)) {
105: //we add its fields
106: SpeedoClass cref = (SpeedoClass) classes.get(sc.name);
107: Iterator it = sc.fields.values().iterator();
108: while (it.hasNext()) {
109: cref.add(it.next(), failsOnError, logger);
110: }
111: }
112: //we add the entire class;
113: else {
114: sc.moPackage = this ;
115: classes.put(sc.name, sc);
116: }
117: }
118:
119: /**
120: * Adds a sequence descriptor to the package descriptor.
121: * If a sequence with the same name is already registered, nothing is done.
122: * @param sequence the sequence to add.
123: */
124: public void addSequence(Object sequence) {
125: SpeedoSequence ss = (SpeedoSequence) sequence;
126: //if the sequence is not already registered
127: if (!sequences.containsKey(ss.name)) {
128: //set the package name of the sequence
129: ss.packageName = this .name;
130: //add it to the list
131: sequences.put(ss.name, ss);
132: }
133: }
134: } // end SpeedoPackage
|