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 org.w3c.dom.Element;
012    import org.w3c.dom.Node;
013    import org.w3c.dom.NodeList;
014    
015    import biz.hammurapi.config.Component;
016    import biz.hammurapi.config.ConfigurationException;
017    import biz.hammurapi.config.Context;
018    import biz.hammurapi.config.DomConfigurable;
019    import biz.hammurapi.config.Wrapper;
020    import biz.hammurapi.xml.dom.DOMUtils;
021    import biz.hammurapi.xml.dom.DOMUtils;
022    
023    
024    public class ConnectionPerThreadDataSourceComponent implements Wrapper, DomConfigurable, Component {
025    
026            private ConnectionPerThreadDataSource master;
027            private String driverClass;
028            private String dbUrl;
029            private String user;
030            private String password;
031    
032            public ConnectionPerThreadDataSourceComponent() {
033                    super();
034            }
035    
036            public Object getMaster() {
037                    if (master==null) {
038                            throw new IllegalStateException("Not yet started");
039                    }
040                    return master;
041            }
042            
043            private Collection initStatements=new ArrayList();
044    
045            public void configure(Node configNode, Context context) throws ConfigurationException {
046                    try {
047                            driverClass=DOMUtils.getSingleElementText((Element) configNode, "driver-class");
048                            dbUrl=DOMUtils.getSingleElementText((Element) configNode, "connection-url");
049                            user=DOMUtils.getSingleElementText((Element) configNode, "user");
050                            password=DOMUtils.getSingleElementText((Element) configNode, "password");
051                            NodeList nl=DOMUtils.selectNodeList(configNode, "init-connection");
052                            for (int i=0, l=nl.getLength(); i<l; ++i) {
053                                    Element el = (Element) nl.item(i);
054                                    initStatements.add(DOMUtils.getElementText(el));
055                            }
056                    } catch (Exception e) {
057                            throw new ConfigurationException("Cannot read parameters", e);
058                    }
059    
060            }
061    
062            public void start() throws ConfigurationException {
063                    try {
064                            master=new ConnectionPerThreadDataSource(
065                                            driverClass, 
066                                            dbUrl, 
067                                            user, 
068                                            password,
069                                            new Transaction() {
070    
071                                                    public boolean execute(SQLProcessor processor) throws SQLException {
072                                                            Iterator it=initStatements.iterator();
073                                                            while (it.hasNext()) {
074                                                                    processor.processUpdate((String) it.next(), null);
075                                                            }
076                                                            return true;
077                                                    }
078                                                    
079                                            });
080                    } catch (ClassNotFoundException e) {
081                            throw new ConfigurationException("Driver class not found", e);
082                    }
083            }
084    
085            public void stop() throws ConfigurationException {
086                    if (master!=null) {
087                            master.shutdown();
088                    }
089    
090            }
091    
092            public void setOwner(Object owner) {
093                    // Nothing
094            }
095    }