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.hypersonic;
024:
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.InputStreamReader;
032: import java.io.Reader;
033: import java.net.URL;
034:
035: import javax.xml.transform.TransformerException;
036:
037: import org.w3c.dom.Element;
038: import org.w3c.dom.Node;
039: import org.w3c.dom.NodeList;
040:
041: import biz.hammurapi.config.Component;
042: import biz.hammurapi.config.ConfigurationException;
043: import biz.hammurapi.config.Context;
044: import biz.hammurapi.config.DomConfigurable;
045: import biz.hammurapi.config.Wrapper;
046: import biz.hammurapi.util.StreamPumper;
047: import biz.hammurapi.xml.dom.DOMUtils;
048:
049: public abstract class HypersonicDataSourceComponent implements Wrapper,
050: DomConfigurable, Component {
051:
052: private byte[] scriptBuf;
053:
054: protected Reader getReader() {
055: try {
056: return new InputStreamReader(new ByteArrayInputStream(
057: scriptBuf));
058: } finally {
059: scriptBuf = null;
060: }
061: }
062:
063: public void configure(Node configNode, Context context)
064: throws ConfigurationException {
065: ByteArrayOutputStream baos = new ByteArrayOutputStream();
066: NodeList children = configNode.getChildNodes();
067: for (int i = 0, length = children.getLength(); i < length; i++) {
068: Node child = children.item(i);
069: if (child instanceof Element) {
070: try {
071: String value = DOMUtils
072: .getElementText((Element) child);
073: InputStream in;
074:
075: if ("file".equals(child.getNodeName())) {
076: in = new FileInputStream(new File(value));
077: } else if ("resource".equals(child.getNodeName())) {
078: in = getClass().getClassLoader()
079: .getResourceAsStream(value);
080: if (in == null) {
081: throw new ConfigurationException(
082: "Resource not found: " + value);
083: }
084: } else if ("url".equals(child.getNodeName())) {
085: in = new URL(value).openStream();
086: } else {
087: continue;
088: }
089:
090: new StreamPumper(in, baos, null, false).run();
091: in.close();
092:
093: } catch (TransformerException e) {
094: throw new ConfigurationException(
095: "Could not load script: " + e, e);
096: } catch (IOException e) {
097: throw new ConfigurationException(
098: "Could not load script: " + e, e);
099: }
100: }
101: }
102: try {
103: baos.close();
104: } catch (IOException e) {
105: throw new ConfigurationException("Should never happen: "
106: + e, e);
107: }
108: scriptBuf = baos.toByteArray();
109: }
110:
111: public void setOwner(Object owner) {
112: // No functionality
113: }
114: }
|