1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.jdbc.supplier;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.apache.commons.io.IOUtils;
23 import org.kuali.common.jdbc.DefaultSqlReader;
24 import org.kuali.common.jdbc.JdbcUtils;
25 import org.kuali.common.jdbc.SqlReader;
26 import org.kuali.common.util.LocationUtils;
27 import org.springframework.util.Assert;
28
29
30
31
32 public class SqlLocationSupplier extends AbstractSupplier implements LocationSupplier {
33
34 private final static String DEFAULT_ENCODING = "UTF-8";
35
36 protected BufferedReader in;
37
38 String location;
39 String encoding = DEFAULT_ENCODING;
40 SqlReader reader = new DefaultSqlReader();
41
42 public SqlLocationSupplier() {
43 this(null);
44 }
45
46 public SqlLocationSupplier(String location) {
47 super();
48 this.location = location;
49 }
50
51 @Override
52 public void open() throws IOException {
53 Assert.hasText(location, "location has no text");
54 Assert.notNull(reader, "reader is null");
55 in = LocationUtils.getBufferedReader(location, encoding);
56 }
57
58 @Override
59 public List<String> getSql() throws IOException {
60 return reader.getSql(in);
61 }
62
63 @Override
64 public void close() {
65 IOUtils.closeQuietly(in);
66 }
67
68 @Override
69 public void fillInMetaData() {
70 Assert.hasText(location, "location has no text");
71 BufferedReader in = null;
72 try {
73 in = LocationUtils.getBufferedReader(location, encoding);
74 this.metaData = JdbcUtils.getSqlMetaData(in, reader);
75 } catch (IOException e) {
76 throw new IllegalStateException(e);
77 } finally {
78 IOUtils.closeQuietly(in);
79 }
80 }
81
82 @Override
83 public String getLocation() {
84 return location;
85 }
86
87 @Override
88 public void setLocation(String location) {
89 this.location = location;
90 }
91
92 public String getEncoding() {
93 return encoding;
94 }
95
96 public void setEncoding(String encoding) {
97 this.encoding = encoding;
98 }
99
100 public SqlReader getReader() {
101 return reader;
102 }
103
104 public void setReader(SqlReader reader) {
105 this.reader = reader;
106 }
107 }