001: /*
002: * $Id: EntityConfigUtil.java,v 1.5 2003/08/20 23:00:53 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.entity.config;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031: import java.util.Map;
032:
033: import org.ofbiz.base.config.GenericConfigException;
034: import org.ofbiz.base.config.ResourceLoader;
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.UtilValidate;
037: import org.ofbiz.base.util.UtilXml;
038: import org.ofbiz.entity.GenericEntityConfException;
039: import org.ofbiz.entity.GenericEntityException;
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042:
043: /**
044: * Misc. utility method for dealing with the entityengine.xml file
045: *
046: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
047: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
048: * @version $Revision: 1.5 $
049: * @since 2.0
050: */
051: public class EntityConfigUtil {
052:
053: public static final String module = EntityConfigUtil.class
054: .getName();
055: public static final String ENTITY_ENGINE_XML_FILENAME = "entityengine.xml";
056:
057: // ========== engine info fields ==========
058: protected static String txFactoryClass;
059: protected static String txFactoryUserTxJndiName;
060: protected static String txFactoryUserTxJndiServerName;
061: protected static String txFactoryTxMgrJndiName;
062: protected static String txFactoryTxMgrJndiServerName;
063:
064: protected static Map resourceLoaderInfos = new HashMap();
065: protected static Map delegatorInfos = new HashMap();
066: protected static Map entityModelReaderInfos = new HashMap();
067: protected static Map entityGroupReaderInfos = new HashMap();
068: protected static Map entityEcaReaderInfos = new HashMap();
069: protected static Map entityDataReaderInfos = new HashMap();
070: protected static Map fieldTypeInfos = new HashMap();
071: protected static Map datasourceInfos = new HashMap();
072:
073: protected static Element getXmlRootElement()
074: throws GenericEntityConfException {
075: try {
076: return ResourceLoader
077: .getXmlRootElement(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME);
078: } catch (GenericConfigException e) {
079: throw new GenericEntityConfException(
080: "Could not get entity engine XML root element", e);
081: }
082: }
083:
084: protected static Document getXmlDocument()
085: throws GenericEntityConfException {
086: try {
087: return ResourceLoader
088: .getXmlDocument(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME);
089: } catch (GenericConfigException e) {
090: throw new GenericEntityConfException(
091: "Could not get entity engine XML document", e);
092: }
093: }
094:
095: static {
096: try {
097: initialize(getXmlRootElement());
098: } catch (Exception e) {
099: Debug.logError(e, "Error loading entity config XML file "
100: + ENTITY_ENGINE_XML_FILENAME, module);
101: }
102: }
103:
104: public static synchronized void reinitialize()
105: throws GenericEntityException {
106: try {
107: ResourceLoader
108: .invalidateDocument(ENTITY_ENGINE_XML_FILENAME);
109: initialize(getXmlRootElement());
110: } catch (Exception e) {
111: throw new GenericEntityException(
112: "Error reloading entity config XML file "
113: + ENTITY_ENGINE_XML_FILENAME, e);
114: }
115: }
116:
117: public static void initialize(Element rootElement)
118: throws GenericEntityException {
119: Element transactionFactoryElement = UtilXml.firstChildElement(
120: rootElement, "transaction-factory");
121: if (transactionFactoryElement == null) {
122: throw new GenericEntityConfException(
123: "ERROR: no transaction-factory definition was found in "
124: + ENTITY_ENGINE_XML_FILENAME);
125: }
126:
127: EntityConfigUtil.txFactoryClass = transactionFactoryElement
128: .getAttribute("class");
129:
130: Element userTxJndiElement = UtilXml.firstChildElement(
131: transactionFactoryElement, "user-transaction-jndi");
132: if (userTxJndiElement != null) {
133: EntityConfigUtil.txFactoryUserTxJndiName = userTxJndiElement
134: .getAttribute("jndi-name");
135: EntityConfigUtil.txFactoryUserTxJndiServerName = userTxJndiElement
136: .getAttribute("jndi-server-name");
137: } else {
138: EntityConfigUtil.txFactoryUserTxJndiName = null;
139: EntityConfigUtil.txFactoryUserTxJndiServerName = null;
140: }
141:
142: Element txMgrJndiElement = UtilXml.firstChildElement(
143: transactionFactoryElement, "transaction-manager-jndi");
144: if (txMgrJndiElement != null) {
145: EntityConfigUtil.txFactoryTxMgrJndiName = txMgrJndiElement
146: .getAttribute("jndi-name");
147: EntityConfigUtil.txFactoryTxMgrJndiServerName = txMgrJndiElement
148: .getAttribute("jndi-server-name");
149: } else {
150: EntityConfigUtil.txFactoryTxMgrJndiName = null;
151: EntityConfigUtil.txFactoryTxMgrJndiServerName = null;
152: }
153:
154: // not load all of the maps...
155: List childElements = null;
156: Iterator elementIter = null;
157:
158: // resource-loader - resourceLoaderInfos
159: childElements = UtilXml.childElementList(rootElement,
160: "resource-loader");
161: elementIter = childElements.iterator();
162: while (elementIter.hasNext()) {
163: Element curElement = (Element) elementIter.next();
164: EntityConfigUtil.ResourceLoaderInfo resourceLoaderInfo = new EntityConfigUtil.ResourceLoaderInfo(
165: curElement);
166: EntityConfigUtil.resourceLoaderInfos.put(
167: resourceLoaderInfo.name, resourceLoaderInfo);
168: }
169:
170: // delegator - delegatorInfos
171: childElements = UtilXml.childElementList(rootElement,
172: "delegator");
173: elementIter = childElements.iterator();
174: while (elementIter.hasNext()) {
175: Element curElement = (Element) elementIter.next();
176: EntityConfigUtil.DelegatorInfo delegatorInfo = new EntityConfigUtil.DelegatorInfo(
177: curElement);
178: EntityConfigUtil.delegatorInfos.put(delegatorInfo.name,
179: delegatorInfo);
180: }
181:
182: // entity-model-reader - entityModelReaderInfos
183: childElements = UtilXml.childElementList(rootElement,
184: "entity-model-reader");
185: elementIter = childElements.iterator();
186: while (elementIter.hasNext()) {
187: Element curElement = (Element) elementIter.next();
188: EntityConfigUtil.EntityModelReaderInfo entityModelReaderInfo = new EntityConfigUtil.EntityModelReaderInfo(
189: curElement);
190: EntityConfigUtil.entityModelReaderInfos.put(
191: entityModelReaderInfo.name, entityModelReaderInfo);
192: }
193:
194: // entity-group-reader - entityGroupReaderInfos
195: childElements = UtilXml.childElementList(rootElement,
196: "entity-group-reader");
197: elementIter = childElements.iterator();
198: while (elementIter.hasNext()) {
199: Element curElement = (Element) elementIter.next();
200: EntityConfigUtil.EntityGroupReaderInfo entityGroupReaderInfo = new EntityConfigUtil.EntityGroupReaderInfo(
201: curElement);
202: EntityConfigUtil.entityGroupReaderInfos.put(
203: entityGroupReaderInfo.name, entityGroupReaderInfo);
204: }
205:
206: // entity-eca-reader - entityEcaReaderInfos
207: childElements = UtilXml.childElementList(rootElement,
208: "entity-eca-reader");
209: elementIter = childElements.iterator();
210: while (elementIter.hasNext()) {
211: Element curElement = (Element) elementIter.next();
212: EntityConfigUtil.EntityEcaReaderInfo entityEcaReaderInfo = new EntityConfigUtil.EntityEcaReaderInfo(
213: curElement);
214: EntityConfigUtil.entityEcaReaderInfos.put(
215: entityEcaReaderInfo.name, entityEcaReaderInfo);
216: }
217:
218: // entity-data-reader - entityDataReaderInfos
219: childElements = UtilXml.childElementList(rootElement,
220: "entity-data-reader");
221: elementIter = childElements.iterator();
222: while (elementIter.hasNext()) {
223: Element curElement = (Element) elementIter.next();
224: EntityConfigUtil.EntityDataReaderInfo entityDataReaderInfo = new EntityConfigUtil.EntityDataReaderInfo(
225: curElement);
226: EntityConfigUtil.entityDataReaderInfos.put(
227: entityDataReaderInfo.name, entityDataReaderInfo);
228: }
229:
230: // field-type - fieldTypeInfos
231: childElements = UtilXml.childElementList(rootElement,
232: "field-type");
233: elementIter = childElements.iterator();
234: while (elementIter.hasNext()) {
235: Element curElement = (Element) elementIter.next();
236: EntityConfigUtil.FieldTypeInfo fieldTypeInfo = new EntityConfigUtil.FieldTypeInfo(
237: curElement);
238: EntityConfigUtil.fieldTypeInfos.put(fieldTypeInfo.name,
239: fieldTypeInfo);
240: }
241:
242: // datasource - datasourceInfos
243: childElements = UtilXml.childElementList(rootElement,
244: "datasource");
245: elementIter = childElements.iterator();
246: while (elementIter.hasNext()) {
247: Element curElement = (Element) elementIter.next();
248: EntityConfigUtil.DatasourceInfo datasourceInfo = new EntityConfigUtil.DatasourceInfo(
249: curElement);
250: EntityConfigUtil.datasourceInfos.put(datasourceInfo.name,
251: datasourceInfo);
252: }
253: }
254:
255: public static String getTxFactoryClass() {
256: return txFactoryClass;
257: }
258:
259: public static String getTxFactoryUserTxJndiName() {
260: return txFactoryUserTxJndiName;
261: }
262:
263: public static String getTxFactoryUserTxJndiServerName() {
264: return txFactoryUserTxJndiServerName;
265: }
266:
267: public static String getTxFactoryTxMgrJndiName() {
268: return txFactoryTxMgrJndiName;
269: }
270:
271: public static String getTxFactoryTxMgrJndiServerName() {
272: return txFactoryTxMgrJndiServerName;
273: }
274:
275: public static EntityConfigUtil.ResourceLoaderInfo getResourceLoaderInfo(
276: String name) {
277: return (EntityConfigUtil.ResourceLoaderInfo) resourceLoaderInfos
278: .get(name);
279: }
280:
281: public static EntityConfigUtil.DelegatorInfo getDelegatorInfo(
282: String name) {
283: return (EntityConfigUtil.DelegatorInfo) delegatorInfos
284: .get(name);
285: }
286:
287: public static EntityConfigUtil.EntityModelReaderInfo getEntityModelReaderInfo(
288: String name) {
289: return (EntityConfigUtil.EntityModelReaderInfo) entityModelReaderInfos
290: .get(name);
291: }
292:
293: public static EntityConfigUtil.EntityGroupReaderInfo getEntityGroupReaderInfo(
294: String name) {
295: return (EntityConfigUtil.EntityGroupReaderInfo) entityGroupReaderInfos
296: .get(name);
297: }
298:
299: public static EntityConfigUtil.EntityEcaReaderInfo getEntityEcaReaderInfo(
300: String name) {
301: return (EntityConfigUtil.EntityEcaReaderInfo) entityEcaReaderInfos
302: .get(name);
303: }
304:
305: public static EntityConfigUtil.EntityDataReaderInfo getEntityDataReaderInfo(
306: String name) {
307: return (EntityConfigUtil.EntityDataReaderInfo) entityDataReaderInfos
308: .get(name);
309: }
310:
311: public static EntityConfigUtil.FieldTypeInfo getFieldTypeInfo(
312: String name) {
313: return (EntityConfigUtil.FieldTypeInfo) fieldTypeInfos
314: .get(name);
315: }
316:
317: public static EntityConfigUtil.DatasourceInfo getDatasourceInfo(
318: String name) {
319: return (EntityConfigUtil.DatasourceInfo) datasourceInfos
320: .get(name);
321: }
322:
323: public static Map getDatasourceInfos() {
324: return datasourceInfos;
325: }
326:
327: public static class ResourceLoaderInfo {
328: public String name;
329: public String className;
330: public String prependEnv;
331: public String prefix;
332:
333: public ResourceLoaderInfo(Element element) {
334: this .name = element.getAttribute("name");
335: this .className = element.getAttribute("class");
336: this .prependEnv = element.getAttribute("prepend-env");
337: this .prefix = element.getAttribute("prefix");
338: }
339: }
340:
341: public static class DelegatorInfo {
342: public String name;
343: public String entityModelReader;
344: public String entityGroupReader;
345: public String entityEcaReader;
346: public boolean useDistributedCacheClear;
347: public String distributedCacheClearClassName;
348: public String distributedCacheClearUserLoginId;
349: public Map groupMap = new HashMap();
350:
351: public DelegatorInfo(Element element) {
352: this .name = element.getAttribute("name");
353: this .entityModelReader = element
354: .getAttribute("entity-model-reader");
355: this .entityGroupReader = element
356: .getAttribute("entity-group-reader");
357: this .entityEcaReader = element
358: .getAttribute("entity-eca-reader");
359: // this defaults to false, ie anything but true is false
360: this .useDistributedCacheClear = "true".equals(element
361: .getAttribute("distributed-cache-clear-enabled"));
362: this .distributedCacheClearClassName = element
363: .getAttribute("distributed-cache-clear-class-name");
364: if (UtilValidate
365: .isEmpty(this .distributedCacheClearClassName))
366: this .distributedCacheClearClassName = "org.ofbiz.entityext.cache.EntityCacheServices";
367:
368: this .distributedCacheClearUserLoginId = element
369: .getAttribute("distributed-cache-clear-user-login-id");
370: if (UtilValidate
371: .isEmpty(this .distributedCacheClearUserLoginId))
372: this .distributedCacheClearUserLoginId = "admin";
373:
374: List groupMapList = UtilXml.childElementList(element,
375: "group-map");
376: Iterator groupMapIter = groupMapList.iterator();
377:
378: while (groupMapIter.hasNext()) {
379: Element groupMapElement = (Element) groupMapIter.next();
380:
381: groupMap
382: .put(
383: groupMapElement
384: .getAttribute("group-name"),
385: groupMapElement
386: .getAttribute("datasource-name"));
387: }
388: }
389: }
390:
391: public static class EntityModelReaderInfo {
392: public String name;
393: public List resourceElements;
394:
395: public EntityModelReaderInfo(Element element) {
396: this .name = element.getAttribute("name");
397: resourceElements = UtilXml.childElementList(element,
398: "resource");
399: }
400: }
401:
402: public static class EntityGroupReaderInfo {
403: public String name;
404: public List resourceElements;
405:
406: public EntityGroupReaderInfo(Element element) {
407: this .name = element.getAttribute("name");
408: String loader = element.getAttribute("loader");
409: String location = element.getAttribute("location");
410:
411: resourceElements = new LinkedList();
412: if (loader != null && loader.length() > 0
413: && location != null && location.length() > 0) {
414: resourceElements.add(element);
415: }
416: resourceElements.addAll(UtilXml.childElementList(element,
417: "resource"));
418: }
419: }
420:
421: public static class EntityEcaReaderInfo {
422: public String name;
423: public List resourceElements;
424:
425: public EntityEcaReaderInfo(Element element) {
426: this .name = element.getAttribute("name");
427: resourceElements = UtilXml.childElementList(element,
428: "resource");
429: }
430: }
431:
432: public static class EntityDataReaderInfo {
433: public String name;
434: public List resourceElements;
435:
436: public EntityDataReaderInfo(Element element) {
437: this .name = element.getAttribute("name");
438: resourceElements = UtilXml.childElementList(element,
439: "resource");
440: }
441: }
442:
443: public static class FieldTypeInfo {
444: public String name;
445: public Element resourceElement;
446:
447: public FieldTypeInfo(Element element) {
448: this .name = element.getAttribute("name");
449: resourceElement = element;
450: }
451: }
452:
453: public static class DatasourceInfo {
454: public String name;
455: public String helperClass;
456: public String fieldTypeName;
457: public List sqlLoadPaths = new LinkedList();
458: public List readDatas = new LinkedList();
459: public Element datasourceElement;
460:
461: public static final int TYPE_JNDI_JDBC = 1;
462: public static final int TYPE_INLINE_JDBC = 2;
463: public static final int TYPE_TYREX_DATA_SOURCE = 3;
464: public static final int TYPE_OTHER = 4;
465:
466: public Element jndiJdbcElement;
467: public Element tyrexDataSourceElement;
468: public Element inlineJdbcElement;
469:
470: public String schemaName = null;
471: public boolean checkOnStart = true;
472: public boolean addMissingOnStart = false;
473: public boolean useFks = true;
474: public boolean useFkIndices = true;
475: public boolean checkForeignKeysOnStart = false;
476: public boolean checkFkIndicesOnStart = false;
477: public boolean usePkConstraintNames = true;
478: public int constraintNameClipLength = 30;
479: public String fkStyle = null;
480: public boolean useFkInitiallyDeferred = true;
481: public boolean useIndices = true;
482: public boolean checkIndicesOnStart = false;
483: public String joinStyle = null;
484: public boolean aliasViews = true;
485:
486: public DatasourceInfo(Element element) {
487: this .name = element.getAttribute("name");
488: this .helperClass = element.getAttribute("helper-class");
489: this .fieldTypeName = element
490: .getAttribute("field-type-name");
491:
492: sqlLoadPaths = UtilXml.childElementList(element,
493: "sql-load-path");
494: readDatas = UtilXml.childElementList(element, "read-data");
495: datasourceElement = element;
496:
497: if (datasourceElement == null) {
498: Debug.logWarning("datasource def not found with name "
499: + this .name
500: + ", using default for schema-name (none)",
501: module);
502: Debug.logWarning("datasource def not found with name "
503: + this .name
504: + ", using default for check-on-start (true)",
505: module);
506: Debug
507: .logWarning(
508: "datasource def not found with name "
509: + this .name
510: + ", using default for add-missing-on-start (false)",
511: module);
512: Debug
513: .logWarning(
514: "datasource def not found with name "
515: + this .name
516: + ", using default for use-foreign-keys (true)",
517: module);
518: Debug
519: .logWarning(
520: "datasource def not found with name "
521: + this .name
522: + ", using default use-foreign-key-indices (true)",
523: module);
524: Debug
525: .logWarning(
526: "datasource def not found with name "
527: + this .name
528: + ", using default for check-fks-on-start (false)",
529: module);
530: Debug
531: .logWarning(
532: "datasource def not found with name "
533: + this .name
534: + ", using default for check-fk-indices-on-start (false)",
535: module);
536: Debug
537: .logWarning(
538: "datasource def not found with name "
539: + this .name
540: + ", using default for use-pk-constraint-names (true)",
541: module);
542: Debug
543: .logWarning(
544: "datasource def not found with name "
545: + this .name
546: + ", using default for constraint-name-clip-length (30)",
547: module);
548: Debug
549: .logWarning(
550: "datasource def not found with name "
551: + this .name
552: + ", using default for fk-style (name_constraint)",
553: module);
554: Debug
555: .logWarning(
556: "datasource def not found with name "
557: + this .name
558: + ", using default for use-fk-initially-deferred (true)",
559: module);
560: Debug.logWarning("datasource def not found with name "
561: + this .name
562: + ", using default for use-indices (true)",
563: module);
564: Debug
565: .logWarning(
566: "datasource def not found with name "
567: + this .name
568: + ", using default for check-indices-on-start (false)",
569: module);
570: Debug.logWarning("datasource def not found with name "
571: + this .name
572: + ", using default for join-style (ansi)",
573: module);
574: } else {
575: schemaName = datasourceElement
576: .getAttribute("schema-name");
577: // anything but false is true
578: checkOnStart = !"false".equals(datasourceElement
579: .getAttribute("check-on-start"));
580: // anything but true is false
581: addMissingOnStart = "true".equals(datasourceElement
582: .getAttribute("add-missing-on-start"));
583: // anything but false is true
584: useFks = !"false".equals(datasourceElement
585: .getAttribute("use-foreign-keys"));
586: // anything but false is true
587: useFkIndices = !"false".equals(datasourceElement
588: .getAttribute("use-foreign-key-indices"));
589: // anything but true is false
590: checkForeignKeysOnStart = "true"
591: .equals(datasourceElement
592: .getAttribute("check-fks-on-start"));
593: // anything but true is false
594: checkFkIndicesOnStart = "true".equals(datasourceElement
595: .getAttribute("check-fk-indices-on-start"));
596: // anything but false is true
597: usePkConstraintNames = !"false"
598: .equals(datasourceElement
599: .getAttribute("use-pk-constraint-names"));
600: try {
601: constraintNameClipLength = Integer
602: .parseInt(datasourceElement
603: .getAttribute("constraint-name-clip-length"));
604: } catch (Exception e) {
605: Debug.logError(
606: "Could not parse constraint-name-clip-length value for datasource with name "
607: + this .name
608: + ", using default value of 30",
609: module);
610: }
611: fkStyle = datasourceElement.getAttribute("fk-style");
612: // anything but true is false
613: useFkInitiallyDeferred = "true"
614: .equals(datasourceElement
615: .getAttribute("use-fk-initially-deferred"));
616: // anything but false is true
617: useIndices = !"false".equals(datasourceElement
618: .getAttribute("use-indices"));
619: // anything but true is false
620: checkIndicesOnStart = "true".equals(datasourceElement
621: .getAttribute("check-indices-on-start"));
622: joinStyle = datasourceElement
623: .getAttribute("join-style");
624: aliasViews = !"false".equals(datasourceElement
625: .getAttribute("alias-view-columns"));
626: }
627: if (fkStyle == null || fkStyle.length() == 0)
628: fkStyle = "name_constraint";
629: if (joinStyle == null || joinStyle.length() == 0)
630: joinStyle = "ansi";
631:
632: jndiJdbcElement = UtilXml.firstChildElement(
633: datasourceElement, "jndi-jdbc");
634: tyrexDataSourceElement = UtilXml.firstChildElement(
635: datasourceElement, "tyrex-dataSource");
636: inlineJdbcElement = UtilXml.firstChildElement(
637: datasourceElement, "inline-jdbc");
638: }
639: }
640: }
|