001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.sql.hypersonic; 005 006 import java.io.ByteArrayInputStream; 007 import java.io.ByteArrayOutputStream; 008 import java.io.File; 009 import java.io.FileInputStream; 010 import java.io.IOException; 011 import java.io.InputStream; 012 import java.io.InputStreamReader; 013 import java.io.Reader; 014 import java.net.URL; 015 016 import org.w3c.dom.Element; 017 import org.w3c.dom.Node; 018 import org.w3c.dom.NodeList; 019 020 import biz.hammurapi.config.Component; 021 import biz.hammurapi.config.ConfigurationException; 022 import biz.hammurapi.config.Context; 023 import biz.hammurapi.config.DomConfigurable; 024 import biz.hammurapi.config.Wrapper; 025 import biz.hammurapi.util.StreamPumper; 026 import biz.hammurapi.xml.dom.DOMUtils; 027 028 029 public abstract class HypersonicDataSourceComponent implements Wrapper, DomConfigurable, Component { 030 031 private byte[] scriptBuf; 032 033 protected Reader getReader() { 034 try { 035 return new InputStreamReader(new ByteArrayInputStream(scriptBuf)); 036 } finally { 037 scriptBuf=null; 038 } 039 } 040 041 public void configure(Node configNode, Context context) throws ConfigurationException { 042 ByteArrayOutputStream baos=new ByteArrayOutputStream(); 043 NodeList children=configNode.getChildNodes(); 044 for (int i=0, length=children.getLength(); i<length; i++) { 045 Node child=children.item(i); 046 if (child instanceof Element) { 047 try { 048 String value=DOMUtils.getElementText((Element) child); 049 InputStream in; 050 051 if ("file".equals(child.getNodeName())) { 052 in = new FileInputStream(new File(value)); 053 } else if ("resource".equals(child.getNodeName())) { 054 in = getClass().getClassLoader().getResourceAsStream(value); 055 if (in==null) { 056 throw new ConfigurationException("Resource not found: "+value); 057 } 058 } else if ("url".equals(child.getNodeName())) { 059 in = new URL(value).openStream(); 060 } else { 061 continue; 062 } 063 064 new StreamPumper(in, baos, null, false).run(); 065 in.close(); 066 067 } catch (Exception e) { 068 throw new ConfigurationException("Could not load script: "+e, e); 069 } 070 } 071 } 072 try { 073 baos.close(); 074 } catch (IOException e) { 075 throw new ConfigurationException("Should never happen: "+e, e); 076 } 077 scriptBuf=baos.toByteArray(); 078 } 079 080 public void setOwner(Object owner) { 081 // No functionality 082 } 083 }