View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.connector;
20  
21  import java.io.BufferedReader;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.net.URL;
25  import java.sql.Connection;
26  import java.sql.Statement;
27  import java.sql.SQLException;
28  
29  import javax.sql.DataSource;
30  
31  import org.apache.geronimo.gbean.GBeanInfo;
32  import org.apache.geronimo.gbean.GBeanInfoBuilder;
33  import org.apache.geronimo.connector.outbound.ConnectionFactorySource;
34  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
35  
36  /**
37   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
38   */
39  public class DatabaseInitializationGBean {
40  
41  
42      public DatabaseInitializationGBean(String testSQL, String path, ConnectionFactorySource cfSource, ClassLoader classLoader) throws Exception {
43  
44          DataSource ds = (DataSource) cfSource.$getResource();
45          Connection c = ds.getConnection();
46          try {
47              Statement s = c.createStatement();
48              try {
49                  try {
50                      s.execute(testSQL);
51                      //script does not need to be run
52                      return;
53                  } catch (SQLException e) {
54                      //script needs to be run
55                  }
56                  URL sourceURL = classLoader.getResource(path);
57                  InputStream ins = sourceURL.openStream();
58                  BufferedReader r = new BufferedReader(new InputStreamReader(ins));
59                  try {
60                      String line;
61                      StringBuffer buf = new StringBuffer();
62                      while ((line = r.readLine()) != null) {
63                          line = line.trim();
64                          if (!line.startsWith("--") && line.length() > 0) {
65                              buf.append(line).append(" ");
66                              if (line.endsWith(";")) {
67                                  int size = buf.length();
68                                  buf.delete(size - 2, size - 1);
69                                  String sql = buf.toString();
70                                  s.execute(sql);
71                                  buf = new StringBuffer();
72                              }
73                          }
74                      }
75                  } finally {
76                      r.close();
77                  }
78              } finally {
79                  s.close();
80              }
81          } finally {
82              c.close();
83          }
84  
85      }
86  
87      public static final GBeanInfo GBEAN_INFO;
88  
89      static {
90          GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DatabaseInitializationGBean.class, "GBean");
91          infoBuilder.addAttribute("testSQL", String.class, true);
92          infoBuilder.addAttribute("path", String.class, true);
93          infoBuilder.addReference("DataSource", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY);
94          infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
95  
96          infoBuilder.setConstructor(new String[]{"testSQL", "path", "DataSource", "classLoader"});
97  
98          GBEAN_INFO = infoBuilder.getBeanInfo();
99      }
100 
101     public static GBeanInfo getGBeanInfo() {
102         return GBEAN_INFO;
103     }
104 
105 }