001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql.xml;
024:
025: import java.sql.PreparedStatement;
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Iterator;
030:
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033: import javax.sql.DataSource;
034: import javax.xml.transform.TransformerException;
035:
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038:
039: import biz.hammurapi.config.ConfigurationException;
040: import biz.hammurapi.config.Context;
041: import biz.hammurapi.config.DomConfigurable;
042: import biz.hammurapi.sql.SQLProcessor;
043: import biz.hammurapi.xml.dom.AbstractDomObject;
044:
045: /**
046: * @author Pavel Vlasov
047: * @version $Revision: 1.2 $
048: */
049: public class SQLCommand extends AbstractDomObject implements
050: DomConfigurable {
051:
052: protected static class Parameterizer implements
053: biz.hammurapi.sql.Parameterizer {
054: private static class Parameter {
055: String name;
056: Object value;
057: }
058:
059: Parameter[] parameters;
060:
061: Parameterizer(Collection parameterNames) {
062: parameters = new Parameter[parameterNames.size()];
063: Iterator it = parameterNames.iterator();
064: for (int i = 0; it.hasNext(); i++) {
065: parameters[i] = new Parameter();
066: parameters[i].name = (String) it.next();
067: }
068: }
069:
070: public void parameterize(PreparedStatement ps)
071: throws SQLException {
072: for (int i = 0; i < parameters.length; i++) {
073: ps.setObject(i + 1, parameters[i].value);
074: }
075:
076: }
077:
078: boolean setContext(Context context) {
079: for (int i = 0; i < parameters.length; i++) {
080: Object o = context.get(parameters[i].name);
081: parameters[i].value = o;
082: if (parameters[i].value == null) {
083: return false;
084: }
085: }
086: return true;
087: }
088: }
089:
090: protected String name;
091: protected String sql;
092: protected Parameterizer parameterizer;
093: protected String errorMessage;
094: private SQLProcessor processor;
095:
096: protected SQLProcessor getProcessor() {
097: return processor;
098: }
099:
100: public void configure(Node configNode, Context context)
101: throws ConfigurationException {
102: Element configElement = (Element) configNode;
103: if (configElement.hasAttribute("name")) {
104: name = configElement.getAttribute("name");
105: }
106:
107: if (configElement.hasAttribute("error-message")) {
108: errorMessage = configElement.getAttribute("error-message");
109: }
110:
111: processor = initProcessor(configNode);
112:
113: try {
114: sql = getElementText(configElement, "sql", null);
115: if (sql == null) {
116: throw new ConfigurationException(
117: "<sql> element not found");
118: }
119:
120: Collection parameters = new ArrayList();
121: StringBuffer sqlBuf = new StringBuffer(sql);
122: for (int i = sqlBuf.indexOf("?["); i != -1; i = sqlBuf
123: .indexOf("?[")) {
124: int j = sqlBuf.indexOf("]?", i + 2);
125: if (j == -1) {
126: break;
127: } else {
128: parameters.add(sqlBuf.substring(i + 2, j));
129: sqlBuf.delete(i + 1, j + 2);
130: }
131: }
132: sql = sqlBuf.toString();
133: if (!parameters.isEmpty()) {
134: parameterizer = new Parameterizer(parameters);
135: }
136: } catch (TransformerException e) {
137: throw new ConfigurationException(e);
138: }
139: }
140:
141: /**
142: * @param configNode
143: * @throws ConfigurationException
144: */
145: static SQLProcessor initProcessor(Node configNode)
146: throws ConfigurationException {
147: try {
148: String jndiName = getElementText((Element) configNode,
149: "data-source", null);
150: if (jndiName == null) {
151: return null;
152: } else {
153: DataSource ds = (DataSource) new InitialContext()
154: .lookup(jndiName);
155: return new SQLProcessor(ds, null);
156: }
157: } catch (TransformerException e) {
158: throw new ConfigurationException(
159: "Cannot read datasource JNDI name", e);
160: } catch (NamingException e) {
161: throw new ConfigurationException(
162: "Datasource JNDI lookup failed: " + e, e);
163: }
164: }
165:
166: }
|