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: */package org.apache.geronimo.connector;
017:
018: import java.io.BufferedReader;
019: import java.io.InputStream;
020: import java.io.InputStreamReader;
021: import java.net.URL;
022: import java.sql.Connection;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.sql.Statement;
026:
027: import javax.resource.ResourceException;
028: import javax.sql.DataSource;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.geronimo.gbean.GBeanInfo;
033: import org.apache.geronimo.gbean.GBeanInfoBuilder;
034: import org.apache.geronimo.naming.ResourceSource;
035: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
036:
037: /**
038: * @version $Rev: 612546 $ $Date: 2008-01-16 11:26:35 -0800 (Wed, 16 Jan 2008) $
039: */
040: public class DatabaseInitializationGBean {
041:
042: private static final Log log = LogFactory
043: .getLog(DatabaseInitializationGBean.class);
044:
045: public DatabaseInitializationGBean(String testSQL, String path,
046: ResourceSource<ResourceException> cfSource,
047: 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(
072: new InputStreamReader(ins));
073: try {
074: String line;
075: StringBuffer buf = new StringBuffer();
076: while ((line = r.readLine()) != null) {
077: line = line.trim();
078: if (!line.startsWith("--")
079: && line.length() > 0) {
080: buf.append(line).append(" ");
081: if (line.endsWith(";")) {
082: int size = buf.length();
083: buf.delete(size - 2, size - 1);
084: String sql = buf.toString();
085: s.execute(sql);
086: buf = new StringBuffer();
087: }
088: }
089: }
090: } catch (Exception ex) {
091: log.error(ex.getMessage());
092: } finally {
093: r.close();
094: }
095: } else {
096: //script need not be run
097: log.debug("Script did not run");
098: }
099: } catch (SQLException e) {
100: log.error(e.getMessage());
101: } finally {
102: s.close();
103: }
104: } finally {
105: c.close();
106: }
107:
108: }
109:
110: public static final GBeanInfo GBEAN_INFO;
111:
112: static {
113: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
114: DatabaseInitializationGBean.class, "GBean");
115: infoBuilder.addAttribute("testSQL", String.class, false);
116: infoBuilder.addAttribute("path", String.class, true);
117: infoBuilder.addReference("DataSource", ResourceSource.class,
118: NameFactory.JCA_MANAGED_CONNECTION_FACTORY);
119: infoBuilder.addAttribute("classLoader", ClassLoader.class,
120: false);
121:
122: infoBuilder.setConstructor(new String[] { "testSQL", "path",
123: "DataSource", "classLoader" });
124:
125: GBEAN_INFO = infoBuilder.getBeanInfo();
126: }
127:
128: public static GBeanInfo getGBeanInfo() {
129: return GBEAN_INFO;
130: }
131:
132: }
|