001package com.nimbusds.infinispan.persistence.sql.config; 002 003 004import javax.xml.stream.XMLStreamConstants; 005import javax.xml.stream.XMLStreamException; 006 007import static org.infinispan.commons.util.StringPropertyReplacer.replaceProperties; 008 009import org.infinispan.commons.CacheConfigurationException; 010import org.infinispan.configuration.cache.ConfigurationBuilder; 011import org.infinispan.configuration.cache.PersistenceConfigurationBuilder; 012import org.infinispan.configuration.parsing.*; 013import org.jooq.SQLDialect; 014import org.kohsuke.MetaInfServices; 015 016 017/** 018 * XML configuration parser for XML schema v2.7 019 */ 020@MetaInfServices 021@Namespaces({ 022 @Namespace(uri = "urn:infinispan:config:store:sql:2.7", root = "sql-store"), 023 @Namespace(root = "sql-store") 024}) 025public class SQLStoreConfigurationParser27 implements ConfigurationParser { 026 027 028 @Override 029 public void readElement(final XMLExtendedStreamReader reader, final ConfigurationBuilderHolder holder) 030 throws XMLStreamException { 031 ConfigurationBuilder builder = holder.getCurrentConfigurationBuilder(); 032 033 Element element = Element.forName(reader.getLocalName()); 034 switch (element) { 035 case SQL_STORE: { 036 this.parseSQLStore(reader, builder.persistence(), holder.getClassLoader()); 037 break; 038 } 039 default: { 040 throw ParseUtils.unexpectedElement(reader); 041 } 042 } 043 } 044 045 046 /** 047 * Parses an SQL store XML element. 048 */ 049 private void parseSQLStore(final XMLExtendedStreamReader reader, 050 final PersistenceConfigurationBuilder persistenceBuilder, 051 final ClassLoader classLoader) 052 throws XMLStreamException { 053 054 SQLStoreConfigurationBuilder builder = new SQLStoreConfigurationBuilder(persistenceBuilder); 055 056 this.parseSQLStoreAttributes(reader, builder, classLoader); 057 058 while (reader.hasNext() && (reader.nextTag() != XMLStreamConstants.END_ELEMENT)) { 059 Element element = Element.forName(reader.getLocalName()); 060 switch (element) { 061 default: { 062 Parser.parseStoreElement(reader, builder); 063 break; 064 } 065 } 066 } 067 068 persistenceBuilder.addStore(builder); 069 } 070 071 072 /** 073 * Parses the attributes of an XML store element. 074 */ 075 private void parseSQLStoreAttributes(final XMLExtendedStreamReader reader, 076 final SQLStoreConfigurationBuilder builder, 077 final ClassLoader classLoader) 078 throws XMLStreamException { 079 080 for (int i = 0; i < reader.getAttributeCount(); i++) { 081 ParseUtils.requireNoNamespaceAttribute(reader, i); 082 String value = replaceProperties(reader.getAttributeValue(i)); 083 Attribute attribute = Attribute.forName(reader.getAttributeLocalName(i)); 084 switch (attribute) { 085 case RECORD_TRANSFORMER: { 086 try { 087 builder.recordTransformerClass(classLoader.loadClass(value)); 088 } catch (ClassNotFoundException e) { 089 throw new CacheConfigurationException(e); 090 } 091 break; 092 } 093 094 case QUERY_EXECUTOR: { 095 try { 096 builder.queryExecutorClass(classLoader.loadClass(value)); 097 } catch (ClassNotFoundException e) { 098 throw new CacheConfigurationException(e); 099 } 100 break; 101 } 102 103 case SQL_DIALECT: { 104 builder.sqlDialect(SQLDialect.valueOf(value.toUpperCase())); 105 break; 106 } 107 108 case CREATE_TABLE_IF_MISSING: { 109 if ("true".equals(value)) { 110 builder.createTableIfMissing(true); 111 } else if ("false".equals(value)){ 112 builder.createTableIfMissing(false); 113 } else { 114 throw new CacheConfigurationException("Invalid " + attribute.getLocalName() + ": Must be true or false"); 115 } 116 break; 117 } 118 119 case CONNECTION_POOL: { 120 if (value != null && ! value.trim().isEmpty()) { 121 builder.connectionPool(value); 122 } 123 break; 124 } 125 126 default: { 127 Parser.parseStoreAttribute(reader, i, builder); 128 break; 129 } 130 } 131 } 132 } 133 134 135 @Override 136 public Namespace[] getNamespaces() { 137 return ParseUtils.getNamespaceAnnotations(getClass()); 138 } 139}