001: /**********************************************************************
002: Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.metadata;
018:
019: import java.util.HashSet;
020: import java.util.Iterator;
021:
022: /**
023: * Representation of a Meta-Data "persistence.xml" file.
024: * Contains a set of "persistence-unit" meta-data.
025: *
026: * @since 1.1
027: * @version $Revision: 1.4 $
028: */
029: public class PersistenceFileMetaData extends MetaData {
030: /** Filename of the "persistence.xml" */
031: protected String filename = null;
032:
033: /** Persistence units defined in this file. */
034: protected HashSet persistenceUnits = new HashSet();
035:
036: /**
037: * Constructor.
038: * @param filename The file where this is stored (or null).
039: */
040: public PersistenceFileMetaData(String filename) {
041: super (null);
042:
043: this .filename = filename;
044: }
045:
046: // ------------------------------ Accessors --------------------------------
047:
048: /**
049: * Accessor for the filename
050: * @return The filename of this MetaData file.
051: */
052: public String getFilename() {
053: return filename;
054: }
055:
056: /**
057: * Accessor for the number of persistence units.
058: * @return no of persistence units.
059: */
060: public int getNoOfPersistenceUnits() {
061: return persistenceUnits.size();
062: }
063:
064: /**
065: * Accessor for the Meta-Data of a persistence unit with a given name.
066: * @param name Name of the persistence unit
067: * @return Meta-Data for the persistence unit
068: */
069: public PersistenceUnitMetaData getPersistenceUnit(String name) {
070: Iterator iter = persistenceUnits.iterator();
071: while (iter.hasNext()) {
072: PersistenceUnitMetaData p = (PersistenceUnitMetaData) iter
073: .next();
074: if (p.name.equals(name)) {
075: return p;
076: }
077: }
078: return null;
079: }
080:
081: /**
082: * Accessor for the persistence units in this "persistence.xml" file.
083: * @return The persistence units
084: */
085: public PersistenceUnitMetaData[] getPersistenceUnits() {
086: if (persistenceUnits.size() == 0) {
087: return null;
088: }
089:
090: return (PersistenceUnitMetaData[]) persistenceUnits
091: .toArray(new PersistenceUnitMetaData[persistenceUnits
092: .size()]);
093: }
094:
095: // -------------------------------- Mutators -------------------------------
096:
097: /**
098: * Mutator for the filename for this MetaData file.
099: * @param filename The filename of this MetaData file.
100: */
101: public void setFilename(String filename) {
102: this .filename = filename;
103: }
104:
105: /**
106: * Method to add a persistence unit
107: * @param pumd The PersistenceUnitMetaData to add.
108: **/
109: public void addPersistenceUnit(PersistenceUnitMetaData pumd) {
110: if (pumd == null) {
111: return;
112: }
113: Iterator iter = persistenceUnits.iterator();
114: while (iter.hasNext()) {
115: PersistenceUnitMetaData p = (PersistenceUnitMetaData) iter
116: .next();
117: // Check if already exists
118: if (pumd.getName().equals(p.getName())) {
119: return;
120: }
121: }
122: persistenceUnits.add(pumd);
123: }
124:
125: // -------------------------------- Utilities ------------------------------
126:
127: /**
128: * Returns a string representation of the object.
129: * @param indent The indent
130: * @return a string representation of the object.
131: */
132: public String toString(String indent) {
133: if (indent == null) {
134: indent = "";
135: }
136:
137: StringBuffer sb = new StringBuffer();
138: sb.append("<persistence>\n");
139:
140: // Add persistence units
141: Iterator iter = persistenceUnits.iterator();
142: while (iter.hasNext()) {
143: sb.append(((PersistenceUnitMetaData) iter.next()).toString(
144: indent, indent));
145: }
146:
147: sb.append("</persistence>");
148: return sb.toString();
149: }
150: }
|