1 25 package org.ofbiz.entity.config; 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 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 ; 40 import org.w3c.dom.Element ; 41 42 50 public class EntityConfigUtil { 51 52 public static final String module = EntityConfigUtil.class.getName(); 53 public static final String ENTITY_ENGINE_XML_FILENAME = "entityengine.xml"; 54 55 protected static String txFactoryClass; 57 protected static String txFactoryUserTxJndiName; 58 protected static String txFactoryUserTxJndiServerName; 59 protected static String txFactoryTxMgrJndiName; 60 protected static String txFactoryTxMgrJndiServerName; 61 62 protected static Map resourceLoaderInfos = FastMap.newInstance(); 63 protected static Map delegatorInfos = FastMap.newInstance(); 64 protected static Map entityModelReaderInfos = FastMap.newInstance(); 65 protected static Map entityGroupReaderInfos = FastMap.newInstance(); 66 protected static Map entityEcaReaderInfos = FastMap.newInstance(); 67 protected static Map entityDataReaderInfos = FastMap.newInstance(); 68 protected static Map fieldTypeInfos = FastMap.newInstance(); 69 protected static Map datasourceInfos = FastMap.newInstance(); 70 71 protected static Element 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 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 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 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 rootElement) throws GenericEntityException { 105 Element 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 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 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 List childElements = null; 132 Iterator elementIter = null; 133 134 childElements = UtilXml.childElementList(rootElement, "resource-loader"); 136 elementIter = childElements.iterator(); 137 while (elementIter.hasNext()) { 138 Element curElement = (Element ) elementIter.next(); 139 ResourceLoaderInfo resourceLoaderInfo = new ResourceLoaderInfo(curElement); 140 resourceLoaderInfos.put(resourceLoaderInfo.name, resourceLoaderInfo); 141 } 142 143 childElements = UtilXml.childElementList(rootElement, "delegator"); 145 elementIter = childElements.iterator(); 146 while (elementIter.hasNext()) { 147 Element curElement = (Element ) elementIter.next(); 148 DelegatorInfo delegatorInfo = new DelegatorInfo(curElement); 149 delegatorInfos.put(delegatorInfo.name, delegatorInfo); 150 } 151 152 childElements = UtilXml.childElementList(rootElement, "entity-model-reader"); 154 elementIter = childElements.iterator(); 155 while (elementIter.hasNext()) { 156 Element curElement = (Element ) elementIter.next(); 157 EntityModelReaderInfo entityModelReaderInfo = new EntityModelReaderInfo(curElement); 158 entityModelReaderInfos.put(entityModelReaderInfo.name, entityModelReaderInfo); 159 } 160 161 childElements = UtilXml.childElementList(rootElement, "entity-group-reader"); 163 elementIter = childElements.iterator(); 164 while (elementIter.hasNext()) { 165 Element curElement = (Element ) elementIter.next(); 166 EntityGroupReaderInfo entityGroupReaderInfo = new EntityGroupReaderInfo(curElement); 167 entityGroupReaderInfos.put(entityGroupReaderInfo.name, entityGroupReaderInfo); 168 } 169 170 childElements = UtilXml.childElementList(rootElement, "entity-eca-reader"); 172 elementIter = childElements.iterator(); 173 while (elementIter.hasNext()) { 174 Element curElement = (Element ) elementIter.next(); 175 EntityEcaReaderInfo entityEcaReaderInfo = new EntityEcaReaderInfo(curElement); 176 entityEcaReaderInfos.put(entityEcaReaderInfo.name, entityEcaReaderInfo); 177 } 178 179 childElements = UtilXml.childElementList(rootElement, "entity-data-reader"); 181 elementIter = childElements.iterator(); 182 while (elementIter.hasNext()) { 183 Element curElement = (Element ) elementIter.next(); 184 EntityDataReaderInfo entityDataReaderInfo = new EntityDataReaderInfo(curElement); 185 entityDataReaderInfos.put(entityDataReaderInfo.name, entityDataReaderInfo); 186 } 187 188 childElements = UtilXml.childElementList(rootElement, "field-type"); 190 elementIter = childElements.iterator(); 191 while (elementIter.hasNext()) { 192 Element curElement = (Element ) elementIter.next(); 193 FieldTypeInfo fieldTypeInfo = new FieldTypeInfo(curElement); 194 fieldTypeInfos.put(fieldTypeInfo.name, fieldTypeInfo); 195 } 196 197 childElements = UtilXml.childElementList(rootElement, "datasource"); 199 elementIter = childElements.iterator(); 200 while (elementIter.hasNext()) { 201 Element curElement = (Element ) elementIter.next(); 202 DatasourceInfo datasourceInfo = new DatasourceInfo(curElement); 203 datasourceInfos.put(datasourceInfo.name, datasourceInfo); 204 } 205 } 206 207 public static String getTxFactoryClass() { 208 return txFactoryClass; 209 } 210 211 public static String getTxFactoryUserTxJndiName() { 212 return txFactoryUserTxJndiName; 213 } 214 215 public static String getTxFactoryUserTxJndiServerName() { 216 return txFactoryUserTxJndiServerName; 217 } 218 219 public static String getTxFactoryTxMgrJndiName() { 220 return txFactoryTxMgrJndiName; 221 } 222 223 public static String getTxFactoryTxMgrJndiServerName() { 224 return txFactoryTxMgrJndiServerName; 225 } 226 227 public static ResourceLoaderInfo getResourceLoaderInfo(String name) { 228 return (ResourceLoaderInfo) resourceLoaderInfos.get(name); 229 } 230 231 public static DelegatorInfo getDelegatorInfo(String name) { 232 return (DelegatorInfo) delegatorInfos.get(name); 233 } 234 235 public static EntityModelReaderInfo getEntityModelReaderInfo(String name) { 236 return (EntityModelReaderInfo) entityModelReaderInfos.get(name); 237 } 238 239 public static EntityGroupReaderInfo getEntityGroupReaderInfo(String name) { 240 return (EntityGroupReaderInfo) entityGroupReaderInfos.get(name); 241 } 242 243 public static EntityEcaReaderInfo getEntityEcaReaderInfo(String name) { 244 return (EntityEcaReaderInfo) entityEcaReaderInfos.get(name); 245 } 246 247 public static EntityDataReaderInfo getEntityDataReaderInfo(String name) { 248 return (EntityDataReaderInfo) entityDataReaderInfos.get(name); 249 } 250 251 public static FieldTypeInfo getFieldTypeInfo(String name) { 252 return (FieldTypeInfo) fieldTypeInfos.get(name); 253 } 254 255 public static DatasourceInfo getDatasourceInfo(String name) { 256 return (DatasourceInfo) datasourceInfos.get(name); 257 } 258 259 public static Map getDatasourceInfos() { 260 return datasourceInfos; 261 } 262 } 263 | Popular Tags |