001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.connector;
020    
021    import java.io.BufferedReader;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    import java.net.URL;
025    import java.sql.Connection;
026    import java.sql.Statement;
027    import java.sql.SQLException;
028    
029    import javax.sql.DataSource;
030    
031    import org.apache.geronimo.gbean.GBeanInfo;
032    import org.apache.geronimo.gbean.GBeanInfoBuilder;
033    import org.apache.geronimo.connector.outbound.ConnectionFactorySource;
034    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
035    
036    /**
037     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
038     */
039    public class DatabaseInitializationGBean {
040    
041    
042        public DatabaseInitializationGBean(String testSQL, String path, ConnectionFactorySource cfSource, ClassLoader classLoader) throws Exception {
043    
044            DataSource ds = (DataSource) cfSource.$getResource();
045            Connection c = ds.getConnection();
046            try {
047                Statement s = c.createStatement();
048                try {
049                    try {
050                        s.execute(testSQL);
051                        //script does not need to be run
052                        return;
053                    } catch (SQLException e) {
054                        //script needs to be run
055                    }
056                    URL sourceURL = classLoader.getResource(path);
057                    InputStream ins = sourceURL.openStream();
058                    BufferedReader r = new BufferedReader(new InputStreamReader(ins));
059                    try {
060                        String line;
061                        StringBuffer buf = new StringBuffer();
062                        while ((line = r.readLine()) != null) {
063                            line = line.trim();
064                            if (!line.startsWith("--") && line.length() > 0) {
065                                buf.append(line).append(" ");
066                                if (line.endsWith(";")) {
067                                    int size = buf.length();
068                                    buf.delete(size - 2, size - 1);
069                                    String sql = buf.toString();
070                                    s.execute(sql);
071                                    buf = new StringBuffer();
072                                }
073                            }
074                        }
075                    } finally {
076                        r.close();
077                    }
078                } finally {
079                    s.close();
080                }
081            } finally {
082                c.close();
083            }
084    
085        }
086    
087        public static final GBeanInfo GBEAN_INFO;
088    
089        static {
090            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DatabaseInitializationGBean.class, "GBean");
091            infoBuilder.addAttribute("testSQL", String.class, true);
092            infoBuilder.addAttribute("path", String.class, true);
093            infoBuilder.addReference("DataSource", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY);
094            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
095    
096            infoBuilder.setConstructor(new String[]{"testSQL", "path", "DataSource", "classLoader"});
097    
098            GBEAN_INFO = infoBuilder.getBeanInfo();
099        }
100    
101        public static GBeanInfo getGBeanInfo() {
102            return GBEAN_INFO;
103        }
104    
105    }