001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.jdbc.suppliers;
017
018 import java.io.BufferedReader;
019 import java.io.IOException;
020 import java.util.ArrayList;
021 import java.util.List;
022
023 import org.apache.commons.io.IOUtils;
024 import org.kuali.common.jdbc.service.MetaDataUtils;
025 import org.kuali.common.jdbc.sql.model.SqlMetaData;
026 import org.kuali.common.util.Assert;
027 import org.kuali.common.util.LocationUtils;
028
029 /**
030 * Supply SQL from a location containing nothing but SQL statements
031 */
032 public final class SqlLocationSupplier extends AbstractSupplier implements SqlSupplier {
033
034 private final SqlLocationContext context;
035 private final String location;
036
037 private SqlMetaData metaData;
038 private boolean open = false;
039 private boolean done = false;
040 private BufferedReader in;
041
042 public SqlLocationSupplier(String location, SqlLocationContext context) {
043 Assert.noNulls(context);
044 Assert.noBlanks(location);
045 Assert.exists(location);
046 this.location = location;
047 this.context = context;
048 }
049
050 @Override
051 public synchronized void open() throws IOException {
052 Assert.isFalse(open, "Already open");
053 this.open = true;
054 this.done = false;
055 this.in = LocationUtils.getBufferedReader(location, context.getEncoding());
056 }
057
058 @Override
059 public synchronized List<String> getSql() throws IOException {
060 Assert.isTrue(open, "Not open");
061 if (done) {
062 return null;
063 }
064 List<String> sql = getSqlList();
065 if (sql.size() == 0) {
066 this.done = true;
067 return null;
068 } else {
069 return sql;
070 }
071 }
072
073 @Override
074 public synchronized void close() {
075 Assert.isTrue(open, "Not open");
076 this.open = false;
077 IOUtils.closeQuietly(in);
078 }
079
080 protected List<String> getSqlList() throws IOException {
081 int count = 0;
082 int size = 0;
083 List<String> list = new ArrayList<String>();
084 String sql = context.getReader().getSql(in);
085 while (sql != null) {
086 list.add(sql);
087 count++;
088 size += sql.length();
089 if (count > context.getMaxCount() || size > context.getMaxSize()) {
090 break;
091 }
092 sql = context.getReader().getSql(in);
093 }
094 return list;
095 }
096
097 @Override
098 public synchronized SqlMetaData getMetaData() {
099 if (metaData == null) {
100 this.metaData = MetaDataUtils.getSqlMetaData(location, context);
101 }
102 return this.metaData;
103 }
104
105 public SqlLocationContext getContext() {
106 return context;
107 }
108
109 public String getLocation() {
110 return location;
111 }
112
113 }