Quadcap Embeddable Server

com/quadcap/services/DataSourcesParser.java

Go to the documentation of this file.
00001 package com.quadcap.services; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.Reader; 00042 import java.io.IOException; 00043 00044 import java.util.Hashtable; 00045 00046 import org.xml.sax.AttributeList; 00047 import org.xml.sax.DocumentHandler; 00048 import org.xml.sax.ErrorHandler; 00049 import org.xml.sax.InputSource; 00050 import org.xml.sax.Parser; 00051 import org.xml.sax.Locator; 00052 import org.xml.sax.SAXException; 00053 import org.xml.sax.SAXParseException; 00054 00055 import org.xml.sax.helpers.ParserFactory; 00056 00057 import com.quadcap.text.sax.Handler; 00058 00059 /** 00060 * Parser for Quadcap DataSource Deployment Descriptor. 00061 * 00062 * @author Stan Bailes 00063 */ 00064 /*{com.quadcap.services.DataSources.xml} 00065 * 00066 * <el><name>data-sources</name> 00067 * <p>This document is used to define and initialize JDBC DataSources 00068 * and bind them to a JNDI Context.</p> 00069 * 00070 * <el><name>data-source</name> 00071 * <p>Each DataSource definition has the following 00072 * allowed elements:</p> 00073 * 00074 * <el><name>jndi-name</name> 00075 * <p>The JNDI name to bind this data source to.</p></el> 00076 * 00077 * <el><name>jdbc-url</name> 00078 * <p>The JDBC URL of the connection.</p></el> 00079 * 00080 * <el><name>connection-param</name> 00081 * <p>A connection property, as a name/value pair. The properties 00082 * so specified are stored in a 00083 * <code>java.util.Properties</code> 00084 * object which is passed to the JDBC Driver's 00085 * <code>getConnection()</code> method.</p> 00086 * 00087 * <el><name>param-name</name> 00088 * <p>The name of a connection property</p></el> 00089 * <el><name>param-value</name> 00090 * <p>The value of a connection property</p></el> 00091 * </el> 00092 * </el> 00093 * </el> 00094 */ 00095 public class DataSourcesParser extends Handler { 00096 DataSources dss; 00097 DataSource ds; 00098 int state = INIT; 00099 00100 final static int INIT = 0; 00101 final static int DATA_SOURCE = 1; 00102 00103 /** 00104 * Parser constructor 00105 */ 00106 public DataSourcesParser() throws Exception { 00107 super(); 00108 } 00109 00110 /** 00111 * Parse the specified deployment descriptor in the context of the 00112 * specified DataSources object 00113 * 00114 * @param dd the reader containing the deployment descriptor 00115 * @param ds the DataSources object 00116 */ 00117 public void parse(Reader dd, DataSources dss) 00119 { 00120 this.dss = dss; 00121 this.state = INIT; 00122 super.parse(dd, dss); 00123 } 00124 00125 00126 /** 00127 * SAX parser callback function called for the end of an element. 00128 * 00129 * @param name the name of this element 00130 * @exception SAXException may be thrown 00131 */ 00133 try { 00134 switch (state) { 00135 case DATA_SOURCE: 00136 if (name.equals("data-source")) { 00137 dss.addDataSource(ds); 00138 env.clear(); 00139 state = INIT; 00140 } else if (name.equals("jndi-name")) { 00141 ds.setName(consumeData()); 00142 } else if (name.equals("driver-class")) { 00143 ds.setDriverClass(consumeData()); 00144 } else if (name.equals("jdbc-url")) { 00145 ds.setUrl(consumeData()); 00146 } else if (name.equals("connection-param")) { 00147 ds.addConnectionProperty(consume("param-name"), 00148 consume("param-value")); 00149 } else { 00150 env.put(name, consumeData()); 00151 } 00152 break; 00153 case INIT: 00154 break; 00155 } 00157 throw e; 00158 } catch (Exception e) { 00160 } 00161 } 00162 00163 /** 00164 * SAX parser callback for the start of an element. 00165 * 00166 * @param name the element name 00167 * @param attrs the element's attributes 00168 * 00169 * @exception SAXException may be thrown 00170 */ 00173 { 00174 data.setLength(0); 00175 if (name.equals("data-source")) { 00176 ds = new DataSource(); 00177 state = DATA_SOURCE; 00178 } 00179 } 00180 00181 } 00182