
metridoc.camel.component.sqlplus.SqlFileRouteProcessor Maven / Gradle / Ivy
/*
* Copyright 2010 Trustees of the University of Pennsylvania Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.osedu.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package metridoc.camel.component.sqlplus;
import metridoc.camel.component.sqlplus.dao.SqlPlusDao;
import metridoc.camel.component.sqlplus.impl.dao.SpringSqlPlusDao;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
*
* @author Narine Ghochikyan
*
*/
public class SqlFileRouteProcessor implements Processor {
private String filePath;
private SqlPlusDao sqlPlusDao;
public SqlFileRouteProcessor(String filePath, DataSource dataSource){
this.filePath = filePath;
this.sqlPlusDao = new SpringSqlPlusDao(dataSource);
}
@Override
public void process(Exchange exchange) throws Exception {
ClassLoader parent = getClass().getClassLoader();
InputStream fileStream = parent.getResourceAsStream(filePath);
String fileContent = getSqlContent(fileStream);
String[] queries = fileContent.split(";");
sqlPlusDao.update(queries);
}
private static String getSqlContent(InputStream in) throws IOException {
BufferedReader bufferedReader = null;
StringBuilder buffer = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(in));
buffer = new StringBuilder();
String line;
boolean isInComments = false;
while ((line = bufferedReader.readLine()) != null) {
line = line.trim();
if(!line.startsWith("--") && !line.startsWith("#")){
if(line.startsWith("/*")){
isInComments = true;
}
if(!isInComments){
buffer.append(line + "\n");
}else{
isInComments = !line.endsWith("*/");
}
}
}
return buffer.toString();
} finally {
in.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy