1 17 18 package org.apache.geronimo.connector; 19 20 import java.io.BufferedReader ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.net.URL ; 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.SQLException ; 27 28 import javax.sql.DataSource ; 29 30 import org.apache.geronimo.gbean.GBeanInfo; 31 import org.apache.geronimo.gbean.GBeanInfoBuilder; 32 import org.apache.geronimo.connector.outbound.ConnectionFactorySource; 33 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 34 35 38 public class DatabaseInitializationGBean { 39 40 41 public DatabaseInitializationGBean(String testSQL, String path, ConnectionFactorySource cfSource, ClassLoader classLoader) throws Exception { 42 43 DataSource ds = (DataSource ) cfSource.$getResource(); 44 Connection c = ds.getConnection(); 45 try { 46 Statement s = c.createStatement(); 47 try { 48 try { 49 s.execute(testSQL); 50 return; 52 } catch (SQLException e) { 53 } 55 URL sourceURL = classLoader.getResource(path); 56 InputStream ins = sourceURL.openStream(); 57 BufferedReader r = new BufferedReader (new InputStreamReader (ins)); 58 try { 59 String line; 60 StringBuffer buf = new StringBuffer (); 61 while ((line = r.readLine()) != null) { 62 line = line.trim(); 63 if (!line.startsWith("--") && line.length() > 0) { 64 buf.append(line).append(" "); 65 if (line.endsWith(";")) { 66 int size = buf.length(); 67 buf.delete(size - 2, size - 1); 68 String sql = buf.toString(); 69 s.execute(sql); 70 buf = new StringBuffer (); 71 } 72 } 73 } 74 } finally { 75 r.close(); 76 } 77 } finally { 78 s.close(); 79 } 80 } finally { 81 c.close(); 82 } 83 84 } 85 86 public static final GBeanInfo GBEAN_INFO; 87 88 static { 89 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DatabaseInitializationGBean.class, "GBean"); 90 infoBuilder.addAttribute("testSQL", String .class, true); 91 infoBuilder.addAttribute("path", String .class, true); 92 infoBuilder.addReference("DataSource", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY); 93 infoBuilder.addAttribute("classLoader", ClassLoader .class, false); 94 95 infoBuilder.setConstructor(new String []{"testSQL", "path", "DataSource", "classLoader"}); 96 97 GBEAN_INFO = infoBuilder.getBeanInfo(); 98 } 99 100 public static GBeanInfo getGBeanInfo() { 101 return GBEAN_INFO; 102 } 103 104 } 105 | Popular Tags |