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.ResultSet; 026 import java.sql.SQLException; 027 import java.sql.Statement; 028 029 import javax.resource.ResourceException; 030 import javax.sql.DataSource; 031 032 import org.apache.commons.logging.Log; 033 import org.apache.commons.logging.LogFactory; 034 import org.apache.geronimo.gbean.GBeanInfo; 035 import org.apache.geronimo.gbean.GBeanInfoBuilder; 036 import org.apache.geronimo.naming.ResourceSource; 037 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 038 039 /** 040 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 041 */ 042 public class DatabaseInitializationGBean { 043 044 045 private static final Log log = LogFactory.getLog(DatabaseInitializationGBean.class); 046 047 public DatabaseInitializationGBean(String testSQL, String path, ResourceSource<ResourceException> cfSource, ClassLoader classLoader) throws Exception { 048 049 DataSource ds = (DataSource) cfSource.$getResource(); 050 Connection c = ds.getConnection(); 051 try { 052 Statement s = c.createStatement(); 053 try { 054 boolean pass = true; 055 // SQL statement in testSQL can be used to determine if the sql script in path attribute should be executed. 056 // This attribute can be left blank or skipped altogether. 057 if ( testSQL != null && !testSQL.trim().equals("") ) { 058 ResultSet rs = s.executeQuery(testSQL); 059 // passes sql test when there are one or more elements 060 while ( rs.next() ) { 061 pass = false; 062 break; 063 } 064 } 065 066 if ( pass ) { 067 //script needs to be run 068 log.debug("Executing script: " + path); 069 URL sourceURL = classLoader.getResource(path); 070 InputStream ins = sourceURL.openStream(); 071 BufferedReader r = new BufferedReader(new InputStreamReader(ins)); 072 try { 073 String line; 074 StringBuffer buf = new StringBuffer(); 075 while ( (line = r.readLine()) != null ) { 076 line = line.trim(); 077 if ( !line.startsWith("--") && line.length() > 0 ) { 078 buf.append(line).append(" "); 079 if ( line.endsWith(";") ) { 080 int size = buf.length(); 081 buf.delete(size - 2, size - 1); 082 String sql = buf.toString(); 083 s.execute(sql); 084 buf = new StringBuffer(); 085 } 086 } 087 } 088 } 089 catch ( Exception ex ) { 090 log.error(ex.getMessage()); 091 } 092 finally { 093 r.close(); 094 } 095 } 096 else { 097 //script need not be run 098 log.debug("Script did not run"); 099 } 100 } 101 catch ( SQLException e ) { 102 log.error(e.getMessage()); 103 } 104 finally { 105 s.close(); 106 } 107 } 108 finally { 109 c.close(); 110 } 111 112 } 113 114 public static final GBeanInfo GBEAN_INFO; 115 116 static { 117 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DatabaseInitializationGBean.class, "GBean"); 118 infoBuilder.addAttribute("testSQL", String.class, false); 119 infoBuilder.addAttribute("path", String.class, true); 120 infoBuilder.addReference("DataSource", ResourceSource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY); 121 infoBuilder.addAttribute("classLoader", ClassLoader.class, false); 122 123 infoBuilder.setConstructor(new String[]{"testSQL", "path", "DataSource", "classLoader"}); 124 125 GBEAN_INFO = infoBuilder.getBeanInfo(); 126 } 127 128 public static GBeanInfo getGBeanInfo() { 129 return GBEAN_INFO; 130 } 131 132 }