1 10 package org.mmbase.storage.implementation.database; 11 12 import java.sql.*; 13 14 import org.w3c.dom.*; 15 import org.xml.sax.InputSource ; 16 17 import org.mmbase.storage.*; 18 19 import org.mmbase.util.ResourceLoader; 20 import org.mmbase.util.xml.DocumentReader; 21 import org.mmbase.util.logging.Logger; 22 import org.mmbase.util.logging.Logging; 23 24 32 public class DatabaseStorageLookup extends DocumentReader { 33 34 private static final Logger log = Logging.getLoggerInstance(DatabaseStorageLookup.class); 35 36 private static String DATABASE_STORAGE_LOOKUP_RESOURCE_PATH_FALLBACK = "/org/mmbase/storage/implementation/database/resources/lookup.xml"; 37 private static String DATABASE_STORAGE_LOOKUP_RESOURCE_PATH = "storage/databases/lookup.xml"; 38 39 40 public static final String PUBLIC_ID_DATABASE_STORAGE_LOOKUP_1_0 = "-//MMBase//DTD storage config 1.0//EN"; 41 42 public static final String DTD_DATABASE_STORAGE_LOOKUP_1_0 = "storage_1_0.dtd"; 43 44 45 public static final String PUBLIC_ID_DATABASE_STORAGE_LOOKUP = PUBLIC_ID_DATABASE_STORAGE_LOOKUP_1_0; 46 47 public static final String DTD_DATABASE_STORAGE_LOOKUP = DTD_DATABASE_STORAGE_LOOKUP_1_0; 48 49 53 static { 54 org.mmbase.util.XMLEntityResolver.registerPublicID(PUBLIC_ID_DATABASE_STORAGE_LOOKUP_1_0, DTD_DATABASE_STORAGE_LOOKUP_1_0, DatabaseStorageLookup.class); 55 } 56 57 60 private static InputSource getInputSource() { 61 InputSource is = null; 62 try { 63 is = ResourceLoader.getConfigurationRoot().getInputSource(DATABASE_STORAGE_LOOKUP_RESOURCE_PATH); 64 } catch (java.io.IOException ioe) { 65 } 66 if (is == null) { return new InputSource (DatabaseStorageLookup.class.getResourceAsStream(DATABASE_STORAGE_LOOKUP_RESOURCE_PATH_FALLBACK)); 68 } else { 69 return is; 70 } 71 } 72 73 76 DatabaseStorageLookup() { 77 super(getInputSource(), DocumentReader.validate(), DatabaseStorageLookup.class); 78 } 79 80 85 String getResourcePath(DatabaseMetaData dmd) throws SQLException, StorageConfigurationException { 86 Element root = document.getDocumentElement(); 87 NodeList filterList = root.getElementsByTagName("filter"); 88 for (int i = 0; i < filterList.getLength(); i++) { 89 Element filter = (Element)filterList.item(i); 90 String resourcePath = filter.getAttribute("resource"); 91 if (match(filter, dmd)) { 92 log.service("Auto detection selected '" + resourcePath + "' for the current database."); 93 return resourcePath; 94 } 95 } 96 return null; 98 } 99 100 106 String getMetaURL(Class clazz) { 107 Element root = document.getDocumentElement(); 108 NodeList urlList = root.getElementsByTagName("url"); 109 for (int i = 0; i < urlList.getLength(); i++) { 110 Element url = (Element) urlList.item(i); 111 String driverClass = url.getAttribute("driver-class"); 112 if (clazz.getName().startsWith(driverClass)) { 113 return getNodeTextValue(url); 114 } 115 } 116 return null; 118 } 119 120 126 private boolean match(Element filterNode, DatabaseMetaData dmd) throws SQLException, StorageConfigurationException { 127 NodeList conditionList = filterNode.getElementsByTagName("*"); 128 boolean match = true; 129 for (int i = 0; match && i < conditionList.getLength(); i++) { 130 Element condition = (Element)conditionList.item(i); 131 String conditionName = condition.getTagName(); 132 if (conditionName.equals("driver-class")) { 133 match = startMatch(condition, dmd.getConnection().getClass().getName()); 134 } else if(conditionName.equals("driver-name")) { 135 match = match(condition, dmd.getDriverName()); 136 } else if(conditionName.equals("driver-version")) { 137 match = match(condition, dmd.getDriverVersion()); 138 } else if(conditionName.equals("database-product-name")) { 139 match = match(condition, dmd.getDatabaseProductName()); 140 } else if(conditionName.equals("database-product-version")) { 141 match = match(condition, dmd.getDatabaseProductVersion()); 142 } else if(conditionName.equals("driver-major-version")) { 143 match = match(condition, dmd.getDriverMajorVersion()); 144 } else if(conditionName.equals("driver-minor-version")) { 145 match = match(condition, dmd.getDriverMinorVersion()); 146 } else { 147 throw new StorageConfigurationException("tag with name:'"+conditionName+"' unknown."); 148 } 149 } 150 return match; 151 } 152 153 159 private boolean match(Element node, String value) { 160 return value.equals(getNodeTextValue(node)); 161 } 162 163 169 private boolean startMatch(Element node, String value) { 170 return value.startsWith(getNodeTextValue(node)); 171 } 172 173 180 private boolean match(Element node, int value) throws StorageConfigurationException { 181 int foundValue = Integer.parseInt(getNodeTextValue(node)); 182 String condition = node.getAttribute("condition"); 183 if ((condition == null) || condition.equals("equals")) { 184 return foundValue == value; 185 } else if (condition.equals("from")) { 186 return foundValue <= value; 187 } else if(condition.equals("until")) { 188 return foundValue > value; 189 } else { 190 throw new StorageConfigurationException("condition: '" + condition + "' unknown"); 191 } 192 } 193 } 194 | Popular Tags |