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