001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sql.hypersonic;
005    
006    import java.io.IOException;
007    import java.sql.SQLException;
008    
009    import org.w3c.dom.Element;
010    import org.w3c.dom.Node;
011    
012    import biz.hammurapi.CarryOverException;
013    import biz.hammurapi.config.ConfigurationException;
014    import biz.hammurapi.config.Context;
015    import biz.hammurapi.sql.SQLProcessor;
016    import biz.hammurapi.sql.Transaction;
017    
018    
019    public class HypersonicStandaloneDataSourceComponent extends HypersonicDataSourceComponent {
020    
021            private HypersonicStandaloneDataSource master;
022    
023            public HypersonicStandaloneDataSourceComponent() {
024                    super();
025            }
026    
027            public Object getMaster() {
028                    if (master==null) {
029                            throw new IllegalStateException("Not yet started");
030                    }
031                    return master;
032            }
033            
034            private String location;
035    
036            public void configure(Node configNode, Context context) throws ConfigurationException {
037                    super.configure(configNode, context);
038                    location=((Element) configNode).getAttribute("location");
039            }
040    
041            public void start() throws ConfigurationException {
042                    try {
043                            master=new HypersonicStandaloneDataSource(
044                                            location,
045                                            new Transaction() {
046    
047                                                    public boolean execute(SQLProcessor processor) throws SQLException {
048                                                            try {
049                                                                    processor.executeScript(getReader());
050                                                            } catch (IOException e) {
051                                                                    throw new CarryOverException(e);
052                                                            }
053                                                            return true;
054                                                    }
055                                                    
056                                            }); 
057                    } catch (ClassNotFoundException e) {
058                            throw new ConfigurationException("Driver class not found", e);
059                    } catch (SQLException e) {
060                            throw new ConfigurationException("Could not initialize database: "+e, e);
061                    } catch (CarryOverException e) {
062                            throw new ConfigurationException("Could not initialize databse: "+e.getCause(), e.getCause());
063                    }
064            }
065    
066            public void stop() throws ConfigurationException {
067                    if (master!=null) {
068                            master.shutdown();
069                    }
070            }
071    }