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.net.URL;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.Set;
025:
026: /**
027: * MetaData representation of a "persistence.xml" persistence unit.
028: * Corresponds to the JPA spec section 6.2.1
029: *
030: * @since 1.1
031: * @version $Revision: 1.8 $
032: */
033: public class PersistenceUnitMetaData extends MetaData {
034: /** Name of the persistence unit. */
035: String name = null;
036:
037: /** Transaction type for this persistence unit. */
038: TransactionType transactionType = null;
039:
040: /** Description of the persistence unit. */
041: String description = null;
042:
043: /** Provider for the persistence unit. */
044: String provider = null;
045:
046: /** JTA data source for the persistence unit. */
047: String jtaDataSource = null;
048:
049: /** Non-JTA data source for the persistence unit. */
050: String nonJtaDataSource = null;
051:
052: /** Names of the classes specified. */
053: HashSet classNames = null;
054:
055: /** Names/URLs of the JAR files specified. */
056: HashSet jarFiles = null;
057:
058: /** Names of the mapping files specified. */
059: HashSet mappingFileNames = null;
060:
061: /** Vendor properties. */
062: Properties properties = null;
063:
064: /** Whether to exclude unlisted classes. */
065: boolean excludeUnlistedClasses = false;
066:
067: /**
068: * Constructor.
069: * @param parent MetaData of parent element
070: * @param name Name of the persistence unit
071: * @param transactionType Transaction type for this unit
072: */
073: public PersistenceUnitMetaData(MetaData parent, String name,
074: String transactionType) {
075: super (null);
076: this .name = name;
077: this .transactionType = TransactionType
078: .getValue(transactionType);
079: }
080:
081: /**
082: * Accessor for the persistence unit name.
083: * @return Name of the persistence unit
084: */
085: public String getName() {
086: return name;
087: }
088:
089: /**
090: * Accessor for the persistence unit transaction type
091: * @return Transaction type for the persistence unit
092: */
093: public TransactionType getTransactionType() {
094: return transactionType;
095: }
096:
097: /**
098: * Accessor for the persistence unit description.
099: * @return Description of the persistence unit
100: */
101: public String getDescription() {
102: return description;
103: }
104:
105: /**
106: * Mutator for the unit description
107: * @param desc The description
108: */
109: public void setDescription(String desc) {
110: this .description = desc;
111: }
112:
113: /**
114: * Accessor for the persistence unit provider.
115: * @return Provider for the persistence unit
116: */
117: public String getProvider() {
118: return provider;
119: }
120:
121: /**
122: * Mutator for the unit provider
123: * @param provider The provider
124: */
125: public void setProvider(String provider) {
126: this .provider = provider;
127: }
128:
129: /**
130: * Accessor for the persistence unit JTA data source.
131: * @return JTA data source for the persistence unit
132: */
133: public String getJtaDataSource() {
134: return jtaDataSource;
135: }
136:
137: /**
138: * Mutator for the unit JTA data source
139: * @param data The JTA data source
140: */
141: public void setJtaDataSource(String data) {
142: this .jtaDataSource = data;
143: }
144:
145: /**
146: * Accessor for the persistence unit non-JTA data source.
147: * @return non-JTA data source for the persistence unit
148: */
149: public String getNonJtaDataSource() {
150: return nonJtaDataSource;
151: }
152:
153: /**
154: * Mutator for the unit non-JTA data source
155: * @param data The non-JTA data source
156: */
157: public void setNonJtaDataSource(String data) {
158: this .nonJtaDataSource = data;
159: }
160:
161: /**
162: * Method to exclude unlisted classes
163: */
164: public void setExcludeUnlistedClasses() {
165: excludeUnlistedClasses = true;
166: }
167:
168: /**
169: * Method to add a class name to the persistence unit.
170: * @param className Name of the class
171: */
172: public void addClassName(String className) {
173: if (classNames == null) {
174: classNames = new HashSet();
175: }
176: classNames.add(className);
177: }
178:
179: /**
180: * Method to add a jar file to the persistence unit.
181: * @param jarName Jar file name
182: */
183: public void addJarFile(String jarName) {
184: if (jarFiles == null) {
185: jarFiles = new HashSet();
186: }
187: jarFiles.add(jarName);
188: }
189:
190: /**
191: * Method to add a jar file to the persistence unit.
192: * @param jarURL Jar file URL
193: */
194: public void addJarFile(URL jarURL) {
195: if (jarFiles == null) {
196: jarFiles = new HashSet();
197: }
198: jarFiles.add(jarURL);
199: }
200:
201: /**
202: * Convenience method to clear out all jar files.
203: */
204: public void clearJarFiles() {
205: if (jarFiles != null) {
206: jarFiles.clear();
207: }
208: jarFiles = null;
209: }
210:
211: /**
212: * Method to add a mapping file to the persistence unit.
213: * @param mappingFile Mapping file name
214: */
215: public void addMappingFile(String mappingFile) {
216: if (mappingFileNames == null) {
217: mappingFileNames = new HashSet();
218: }
219: mappingFileNames.add(mappingFile);
220: }
221:
222: /**
223: * Method to add a vendor property to the persistence unit.
224: * @param key Property name
225: * @param value Property value
226: */
227: public void addProperty(String key, String value) {
228: if (key == null || value == null) {
229: // Ignore null properties
230: return;
231: }
232: if (properties == null) {
233: properties = new Properties();
234: }
235: properties.setProperty(key, value);
236: }
237:
238: /**
239: * Accessor for the class names for this persistence unit.
240: * @return The class names
241: */
242: public HashSet getClassNames() {
243: return classNames;
244: }
245:
246: /**
247: * Accessor for the class names for this persistence unit.
248: * @return The mapping files
249: */
250: public HashSet getMappingFiles() {
251: return mappingFileNames;
252: }
253:
254: /**
255: * Accessor for the jar files for this persistence unit.
256: * The contents of the Set may be Strings (the names) or URLs
257: * @return The jar names
258: */
259: public HashSet getJarFiles() {
260: return jarFiles;
261: }
262:
263: /**
264: * Accessor for the properties for this persistence unit.
265: * @return The properties
266: */
267: public Properties getProperties() {
268: return properties;
269: }
270:
271: // ------------------------------- Utilities -------------------------------
272:
273: /**
274: * Returns a string representation of the object using a prefix
275: * This can be used as part of a facility to output a MetaData file.
276: * @param prefix prefix string
277: * @param indent indent string
278: * @return a string representation of the object.
279: */
280: public String toString(String prefix, String indent) {
281: // Field needs outputting so generate metadata
282: StringBuffer sb = new StringBuffer();
283: sb.append(prefix).append(
284: "<persistence-unit name=\"" + name + "\"");
285: if (transactionType != null) {
286: sb.append(" transaction-type=\"" + transactionType + "\"");
287: }
288: sb.append(">\n");
289:
290: // Description of unit
291: if (description != null) {
292: sb.append(prefix).append(indent).append(
293: "<description>" + description + "</description>\n");
294: }
295:
296: // Provider for unit
297: if (provider != null) {
298: sb.append(prefix).append(indent).append(
299: "<provider>" + provider + "</provider>\n");
300: }
301:
302: // JTA data source for unit
303: if (jtaDataSource != null) {
304: sb.append(prefix).append(indent).append(
305: "<jta-data-source>" + jtaDataSource
306: + "</jta-data-source>\n");
307: }
308:
309: // Non-JTA data source for unit
310: if (nonJtaDataSource != null) {
311: sb.append(prefix).append(indent).append(
312: "<non-jta-data-source>" + nonJtaDataSource
313: + "</non-jta-data-source>\n");
314: }
315:
316: // Add class names
317: if (classNames != null) {
318: Iterator iter = classNames.iterator();
319: while (iter.hasNext()) {
320: sb.append(prefix).append(indent).append(
321: "<class>" + iter.next() + "</class>\n");
322: }
323: }
324:
325: // Add mapping files
326: if (mappingFileNames != null) {
327: Iterator iter = mappingFileNames.iterator();
328: while (iter.hasNext()) {
329: sb.append(prefix).append(indent).append(
330: "<mapping-file>" + iter.next()
331: + "</mapping-file>\n");
332: }
333: }
334:
335: // Add jar files
336: if (jarFiles != null) {
337: Iterator iter = jarFiles.iterator();
338: while (iter.hasNext()) {
339: sb.append(prefix).append(indent).append(
340: "<jar-file>" + iter.next() + "</jar-file>\n");
341: }
342: }
343:
344: // Add properties
345: if (properties != null) {
346: sb.append(prefix).append(indent).append("<properties>\n");
347: Set entries = properties.entrySet();
348: Iterator iter = entries.iterator();
349: while (iter.hasNext()) {
350: Map.Entry entry = (Map.Entry) iter.next();
351: sb.append(prefix).append(indent).append(indent).append(
352: "<property name=" + entry.getKey() + " value="
353: + entry.getValue() + "</property>\n");
354: }
355: sb.append(prefix).append(indent).append("</properties>\n");
356: }
357:
358: if (excludeUnlistedClasses) {
359: sb
360: .append(prefix)
361: .append(indent)
362: .append(
363: "<exclude-unlisted-classes></exclude-unlisted-classes>\n");
364: }
365:
366: sb.append(prefix).append("</persistence-unit>\n");
367: return sb.toString();
368: }
369: }
|