View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Supply SQL from a location containing pre-generated SQL statements
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 }