KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > storage > implementation > database > DatabaseStorageLookup


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.storage.implementation.database;
11
12 import java.sql.*;
13
14 import org.w3c.dom.*;
15 import org.xml.sax.InputSource JavaDoc;
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 /**
25  * Represents a xml document that can be used to determine the database configuration resource,
26  * based on a database's metadata.
27  *
28  * @author Pierre van Rooden
29  * @since MMBase-1.7
30  * @version $Id: DatabaseStorageLookup.java,v 1.7 2005/12/17 15:47:49 michiel Exp $
31  */

32 public class DatabaseStorageLookup extends DocumentReader {
33
34     private static final Logger log = Logging.getLoggerInstance(DatabaseStorageLookup.class);
35
36     private static String JavaDoc DATABASE_STORAGE_LOOKUP_RESOURCE_PATH_FALLBACK = "/org/mmbase/storage/implementation/database/resources/lookup.xml";
37     private static String JavaDoc DATABASE_STORAGE_LOOKUP_RESOURCE_PATH = "storage/databases/lookup.xml";
38
39     /** Public ID of the Storage DTD version 1.0 */
40     public static final String JavaDoc PUBLIC_ID_DATABASE_STORAGE_LOOKUP_1_0 = "-//MMBase//DTD storage config 1.0//EN";
41     /** DTD resource filename of the Database DTD version 1.0 */
42     public static final String JavaDoc DTD_DATABASE_STORAGE_LOOKUP_1_0 = "storage_1_0.dtd";
43
44     /** Public ID of the most recent Database DTD */
45     public static final String JavaDoc PUBLIC_ID_DATABASE_STORAGE_LOOKUP = PUBLIC_ID_DATABASE_STORAGE_LOOKUP_1_0;
46     /** DTD resource filename of the most Database DTD */
47     public static final String JavaDoc DTD_DATABASE_STORAGE_LOOKUP = DTD_DATABASE_STORAGE_LOOKUP_1_0;
48
49     /**
50      * Register the Public Ids for DTDs used by StorageReader
51      * This method is called by XMLEntityResolver.
52      */

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     /**
58      * @since MMBase-1.8
59      */

60     private static InputSource JavaDoc getInputSource() {
61         InputSource JavaDoc is = null;
62         try {
63             is = ResourceLoader.getConfigurationRoot().getInputSource(DATABASE_STORAGE_LOOKUP_RESOURCE_PATH);
64         } catch (java.io.IOException JavaDoc ioe) {
65         }
66         if (is == null) { // 1.7 compatibility
67
return new InputSource JavaDoc(DatabaseStorageLookup.class.getResourceAsStream(DATABASE_STORAGE_LOOKUP_RESOURCE_PATH_FALLBACK));
68         } else {
69             return is;
70         }
71     }
72
73     /**
74      * Constructor, accesses the storage lookup xml resource
75      */

76     DatabaseStorageLookup() {
77         super(getInputSource(), DocumentReader.validate(), DatabaseStorageLookup.class);
78     }
79
80     /**
81      * Obtain an path to a database configuration resource
82      * @param dmd the database meta data
83      * @return The database configuration resource, or <code>null</code> if it cannot be determined
84      */

85     String JavaDoc 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 JavaDoc 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         // not found, return null
97
return null;
98     }
99
100     /**
101      * Returns an given connection URL for a given Driver CLass. Or <code>null</code> if no such
102      * thing was defined in lookup.xml. In that case the configured URL in MMBase can be used.
103      *
104      * @since MMBase-1.8
105      */

106     String JavaDoc getMetaURL(Class JavaDoc 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 JavaDoc driverClass = url.getAttribute("driver-class");
112             if (clazz.getName().startsWith(driverClass)) {
113                 return getNodeTextValue(url);
114             }
115         }
116         // not found, return null
117
return null;
118     }
119
120     /**
121      * Tests if an given filterset applies
122      * @param filterNode The element containing all filters
123      * @param dmd the database meta data
124      * @return <code>true</code> when true, otherwise <code>false</code>
125      */

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 JavaDoc 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     /**
154      * Tests if an element value matches a value specified
155      * @param node the Element of which the body value has to be checked
156      * @param value the Value which has to be compared
157      * @return <code>true</code> when true, otherwise <code>false</code>
158      */

159     private boolean match(Element node, String JavaDoc value) {
160         return value.equals(getNodeTextValue(node));
161     }
162
163     /**
164      * Tests if an string starts with the value of the node
165      * @param node the Element of which the body value has to be checked
166      * @param value the Value which has to be compared
167      * @return <code>true</code> when true, otherwise <code>false</code>
168      */

169     private boolean startMatch(Element node, String JavaDoc value) {
170         return value.startsWith(getNodeTextValue(node));
171     }
172
173     /**
174      * Tests a condition from an attibute of the Element applies to the
175      * value of the element with the given int value
176      * @param node the Element of which the body value has to be checked
177      * @param value the Value which has to be compared
178      * @return <code>true</code> when true, otherwise <code>false</code>
179      */

180     private boolean match(Element node, int value) throws StorageConfigurationException {
181         int foundValue = Integer.parseInt(getNodeTextValue(node));
182         String JavaDoc 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