KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > config > EntityConfigUtil


1 /*
2  * $Id: EntityConfigUtil.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.config;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javolution.util.FastMap;
32
33 import org.ofbiz.base.config.GenericConfigException;
34 import org.ofbiz.base.config.ResourceLoader;
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.UtilXml;
37 import org.ofbiz.entity.GenericEntityConfException;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * Misc. utility method for dealing with the entityengine.xml file
44  *
45  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 2.0
49  */

50 public class EntityConfigUtil {
51     
52     public static final String JavaDoc module = EntityConfigUtil.class.getName();
53     public static final String JavaDoc ENTITY_ENGINE_XML_FILENAME = "entityengine.xml";
54
55     // ========== engine info fields ==========
56
protected static String JavaDoc txFactoryClass;
57     protected static String JavaDoc txFactoryUserTxJndiName;
58     protected static String JavaDoc txFactoryUserTxJndiServerName;
59     protected static String JavaDoc txFactoryTxMgrJndiName;
60     protected static String JavaDoc txFactoryTxMgrJndiServerName;
61
62     protected static Map JavaDoc resourceLoaderInfos = FastMap.newInstance();
63     protected static Map JavaDoc delegatorInfos = FastMap.newInstance();
64     protected static Map JavaDoc entityModelReaderInfos = FastMap.newInstance();
65     protected static Map JavaDoc entityGroupReaderInfos = FastMap.newInstance();
66     protected static Map JavaDoc entityEcaReaderInfos = FastMap.newInstance();
67     protected static Map JavaDoc entityDataReaderInfos = FastMap.newInstance();
68     protected static Map JavaDoc fieldTypeInfos = FastMap.newInstance();
69     protected static Map JavaDoc datasourceInfos = FastMap.newInstance();
70
71     protected static Element JavaDoc getXmlRootElement() throws GenericEntityConfException {
72         try {
73             return ResourceLoader.getXmlRootElement(ENTITY_ENGINE_XML_FILENAME);
74         } catch (GenericConfigException e) {
75             throw new GenericEntityConfException("Could not get entity engine XML root element", e);
76         }
77     }
78
79     protected static Document JavaDoc getXmlDocument() throws GenericEntityConfException {
80         try {
81             return ResourceLoader.getXmlDocument(ENTITY_ENGINE_XML_FILENAME);
82         } catch (GenericConfigException e) {
83             throw new GenericEntityConfException("Could not get entity engine XML document", e);
84         }
85     }
86
87     static {
88         try {
89             initialize(getXmlRootElement());
90         } catch (Exception JavaDoc e) {
91             Debug.logError(e, "Error loading entity config XML file " + ENTITY_ENGINE_XML_FILENAME, module);
92         }
93     }
94
95     public static synchronized void reinitialize() throws GenericEntityException {
96         try {
97             ResourceLoader.invalidateDocument(ENTITY_ENGINE_XML_FILENAME);
98             initialize(getXmlRootElement());
99         } catch (Exception JavaDoc e) {
100             throw new GenericEntityException("Error reloading entity config XML file " + ENTITY_ENGINE_XML_FILENAME, e);
101         }
102     }
103
104     public static void initialize(Element JavaDoc rootElement) throws GenericEntityException {
105         Element JavaDoc transactionFactoryElement = UtilXml.firstChildElement(rootElement, "transaction-factory");
106         if (transactionFactoryElement == null) {
107             throw new GenericEntityConfException("ERROR: no transaction-factory definition was found in " + ENTITY_ENGINE_XML_FILENAME);
108         }
109
110         txFactoryClass = transactionFactoryElement.getAttribute("class");
111
112         Element JavaDoc userTxJndiElement = UtilXml.firstChildElement(transactionFactoryElement, "user-transaction-jndi");
113         if (userTxJndiElement != null) {
114             txFactoryUserTxJndiName = userTxJndiElement.getAttribute("jndi-name");
115             txFactoryUserTxJndiServerName = userTxJndiElement.getAttribute("jndi-server-name");
116         } else {
117             txFactoryUserTxJndiName = null;
118             txFactoryUserTxJndiServerName = null;
119         }
120
121         Element JavaDoc txMgrJndiElement = UtilXml.firstChildElement(transactionFactoryElement, "transaction-manager-jndi");
122         if (txMgrJndiElement != null) {
123             txFactoryTxMgrJndiName = txMgrJndiElement.getAttribute("jndi-name");
124             txFactoryTxMgrJndiServerName = txMgrJndiElement.getAttribute("jndi-server-name");
125         } else {
126             txFactoryTxMgrJndiName = null;
127             txFactoryTxMgrJndiServerName = null;
128         }
129
130         // not load all of the maps...
131
List JavaDoc childElements = null;
132         Iterator JavaDoc elementIter = null;
133
134         // resource-loader - resourceLoaderInfos
135
childElements = UtilXml.childElementList(rootElement, "resource-loader");
136         elementIter = childElements.iterator();
137         while (elementIter.hasNext()) {
138             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
139             ResourceLoaderInfo resourceLoaderInfo = new ResourceLoaderInfo(curElement);
140             resourceLoaderInfos.put(resourceLoaderInfo.name, resourceLoaderInfo);
141         }
142
143         // delegator - delegatorInfos
144
childElements = UtilXml.childElementList(rootElement, "delegator");
145         elementIter = childElements.iterator();
146         while (elementIter.hasNext()) {
147             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
148             DelegatorInfo delegatorInfo = new DelegatorInfo(curElement);
149             delegatorInfos.put(delegatorInfo.name, delegatorInfo);
150         }
151
152         // entity-model-reader - entityModelReaderInfos
153
childElements = UtilXml.childElementList(rootElement, "entity-model-reader");
154         elementIter = childElements.iterator();
155         while (elementIter.hasNext()) {
156             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
157             EntityModelReaderInfo entityModelReaderInfo = new EntityModelReaderInfo(curElement);
158             entityModelReaderInfos.put(entityModelReaderInfo.name, entityModelReaderInfo);
159         }
160
161         // entity-group-reader - entityGroupReaderInfos
162
childElements = UtilXml.childElementList(rootElement, "entity-group-reader");
163         elementIter = childElements.iterator();
164         while (elementIter.hasNext()) {
165             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
166             EntityGroupReaderInfo entityGroupReaderInfo = new EntityGroupReaderInfo(curElement);
167             entityGroupReaderInfos.put(entityGroupReaderInfo.name, entityGroupReaderInfo);
168         }
169
170         // entity-eca-reader - entityEcaReaderInfos
171
childElements = UtilXml.childElementList(rootElement, "entity-eca-reader");
172         elementIter = childElements.iterator();
173         while (elementIter.hasNext()) {
174             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
175             EntityEcaReaderInfo entityEcaReaderInfo = new EntityEcaReaderInfo(curElement);
176             entityEcaReaderInfos.put(entityEcaReaderInfo.name, entityEcaReaderInfo);
177         }
178
179         // entity-data-reader - entityDataReaderInfos
180
childElements = UtilXml.childElementList(rootElement, "entity-data-reader");
181         elementIter = childElements.iterator();
182         while (elementIter.hasNext()) {
183             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
184             EntityDataReaderInfo entityDataReaderInfo = new EntityDataReaderInfo(curElement);
185             entityDataReaderInfos.put(entityDataReaderInfo.name, entityDataReaderInfo);
186         }
187
188         // field-type - fieldTypeInfos
189
childElements = UtilXml.childElementList(rootElement, "field-type");
190         elementIter = childElements.iterator();
191         while (elementIter.hasNext()) {
192             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
193             FieldTypeInfo fieldTypeInfo = new FieldTypeInfo(curElement);
194             fieldTypeInfos.put(fieldTypeInfo.name, fieldTypeInfo);
195         }
196
197         // datasource - datasourceInfos
198
childElements = UtilXml.childElementList(rootElement, "datasource");
199         elementIter = childElements.iterator();
200         while (elementIter.hasNext()) {
201             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
202             DatasourceInfo datasourceInfo = new DatasourceInfo(curElement);
203             datasourceInfos.put(datasourceInfo.name, datasourceInfo);
204         }
205     }
206
207     public static String JavaDoc getTxFactoryClass() {
208         return txFactoryClass;
209     }
210
211     public static String JavaDoc getTxFactoryUserTxJndiName() {
212         return txFactoryUserTxJndiName;
213     }
214
215     public static String JavaDoc getTxFactoryUserTxJndiServerName() {
216         return txFactoryUserTxJndiServerName;
217     }
218
219     public static String JavaDoc getTxFactoryTxMgrJndiName() {
220         return txFactoryTxMgrJndiName;
221     }
222
223     public static String JavaDoc getTxFactoryTxMgrJndiServerName() {
224         return txFactoryTxMgrJndiServerName;
225     }
226
227     public static ResourceLoaderInfo getResourceLoaderInfo(String JavaDoc name) {
228         return (ResourceLoaderInfo) resourceLoaderInfos.get(name);
229     }
230
231     public static DelegatorInfo getDelegatorInfo(String JavaDoc name) {
232         return (DelegatorInfo) delegatorInfos.get(name);
233     }
234
235     public static EntityModelReaderInfo getEntityModelReaderInfo(String JavaDoc name) {
236         return (EntityModelReaderInfo) entityModelReaderInfos.get(name);
237     }
238
239     public static EntityGroupReaderInfo getEntityGroupReaderInfo(String JavaDoc name) {
240         return (EntityGroupReaderInfo) entityGroupReaderInfos.get(name);
241     }
242
243     public static EntityEcaReaderInfo getEntityEcaReaderInfo(String JavaDoc name) {
244         return (EntityEcaReaderInfo) entityEcaReaderInfos.get(name);
245     }
246
247     public static EntityDataReaderInfo getEntityDataReaderInfo(String JavaDoc name) {
248         return (EntityDataReaderInfo) entityDataReaderInfos.get(name);
249     }
250
251     public static FieldTypeInfo getFieldTypeInfo(String JavaDoc name) {
252         return (FieldTypeInfo) fieldTypeInfos.get(name);
253     }
254
255     public static DatasourceInfo getDatasourceInfo(String JavaDoc name) {
256         return (DatasourceInfo) datasourceInfos.get(name);
257     }
258     
259     public static Map JavaDoc getDatasourceInfos() {
260         return datasourceInfos;
261     }
262 }
263
Popular Tags