KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > DatabaseInitializationGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 import javax.sql.DataSource JavaDoc;
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 /**
36  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class DatabaseInitializationGBean {
39
40
41     public DatabaseInitializationGBean(String JavaDoc testSQL, String JavaDoc path, ConnectionFactorySource cfSource, ClassLoader JavaDoc classLoader) throws Exception JavaDoc {
42
43         DataSource JavaDoc ds = (DataSource JavaDoc) cfSource.$getResource();
44         Connection JavaDoc c = ds.getConnection();
45         try {
46             Statement JavaDoc s = c.createStatement();
47             try {
48                 try {
49                     s.execute(testSQL);
50                     //script does not need to be run
51
return;
52                 } catch (SQLException JavaDoc e) {
53                     //script needs to be run
54
}
55                 URL JavaDoc sourceURL = classLoader.getResource(path);
56                 InputStream JavaDoc ins = sourceURL.openStream();
57                 BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(ins));
58                 try {
59                     String JavaDoc line;
60                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc sql = buf.toString();
69                                 s.execute(sql);
70                                 buf = new StringBuffer JavaDoc();
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 JavaDoc.class, true);
91         infoBuilder.addAttribute("path", String JavaDoc.class, true);
92         infoBuilder.addReference("DataSource", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY);
93         infoBuilder.addAttribute("classLoader", ClassLoader JavaDoc.class, false);
94
95         infoBuilder.setConstructor(new String JavaDoc[]{"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