001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sql;
005    
006    import java.sql.SQLException;
007    import java.util.ArrayList;
008    import java.util.Collection;
009    import java.util.Iterator;
010    
011    import javax.xml.transform.TransformerException;
012    
013    import org.apache.xpath.XPathAPI;
014    import org.w3c.dom.Element;
015    import org.w3c.dom.Node;
016    import org.w3c.dom.traversal.NodeIterator;
017    
018    import biz.hammurapi.config.Component;
019    import biz.hammurapi.config.ConfigurationException;
020    import biz.hammurapi.config.Context;
021    import biz.hammurapi.config.DomConfigurable;
022    import biz.hammurapi.config.Wrapper;
023    import biz.hammurapi.xml.dom.DOMUtils;
024    
025    
026    public class ConnectionPerThreadDataSourceComponent implements Wrapper, DomConfigurable, Component {
027    
028            private ConnectionPerThreadDataSource master;
029            private String driverClass;
030            private String dbUrl;
031            private String user;
032            private String password;
033    
034            public ConnectionPerThreadDataSourceComponent() {
035                    super();
036            }
037    
038            public Object getMaster() {
039                    if (master==null) {
040                            throw new IllegalStateException("Not yet started");
041                    }
042                    return master;
043            }
044            
045            private Collection initStatements=new ArrayList();
046    
047            public void configure(Node configNode, Context context) throws ConfigurationException {
048                    try {
049                            driverClass=DOMUtils.getSingleElementText((Element) configNode, "driver-class");
050                            dbUrl=DOMUtils.getSingleElementText((Element) configNode, "connection-url");
051                            user=DOMUtils.getSingleElementText((Element) configNode, "user");
052                            password=DOMUtils.getSingleElementText((Element) configNode, "password");
053                            NodeIterator nit=XPathAPI.selectNodeIterator(configNode, "init-connection");
054                            Element el;
055                            while ((el=(Element) nit.nextNode())!=null) {
056                                    initStatements.add(DOMUtils.getElementText(el));
057                            }
058                    } catch (TransformerException e) {
059                            throw new ConfigurationException("Cannot read parameters", e);
060                    }
061    
062            }
063    
064            public void start() throws ConfigurationException {
065                    try {
066                            master=new ConnectionPerThreadDataSource(
067                                            driverClass, 
068                                            dbUrl, 
069                                            user, 
070                                            password,
071                                            new Transaction() {
072    
073                                                    public boolean execute(SQLProcessor processor) throws SQLException {
074                                                            Iterator it=initStatements.iterator();
075                                                            while (it.hasNext()) {
076                                                                    processor.processUpdate((String) it.next(), null);
077                                                            }
078                                                            return true;
079                                                    }
080                                                    
081                                            });
082                    } catch (ClassNotFoundException e) {
083                            throw new ConfigurationException("Driver class not found", e);
084                    }
085            }
086    
087            public void stop() throws ConfigurationException {
088                    if (master!=null) {
089                            master.shutdown();
090                    }
091    
092            }
093    
094            public void setOwner(Object owner) {
095                    // Nothing
096            }
097    }