[1cff17]: src / org / esb / hive / DirectoryScanner.cpp  Maximize  Restore  History

Download this file

128 lines (107 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "DirectoryScanner.h"
//#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include "org/esb/lang/Thread.h"
#include "org/esb/config/config.h"
#include "org/esb/io/File.h"
//#include "org/esb/sql/ResultSet.h"
#include "org/esb/io/FileFilter.h"
#include "org/esb/sql/Connection.h"
#include "org/esb/sql/PreparedStatement.h"
#include "org/esb/hive/FileImporter.h"
//#include "import.cpp"
#include <boost/algorithm/string.hpp>
#include "org/esb/util/Log.h"
namespace org {
namespace esb {
namespace hive {
using namespace org::esb::io;
using namespace org::esb::sql;
using namespace org::esb::lang;
class MyFileFilter : public FileFilter {
public:
bool accept(File file) {
return !(boost::starts_with(file.getPath(), "."));
}
};
DirectoryScanner::DirectoryScanner(std::string dir, int interval) {
_halt = true;
_interval = interval;
_dir = dir;
}
DirectoryScanner::DirectoryScanner() {
_halt = true;
}
DirectoryScanner::~DirectoryScanner() {
if (th)
delete th;
}
void DirectoryScanner::onMessage(org::esb::signal::Message & msg) {
if (msg.getProperty("directoryscan") == "start") {
_halt = false;
if (msg.containsProperty("directory")) {
_dir = msg.getProperty("directory");
}
if (msg.containsProperty("interval")) {
_interval = atoi(msg.getProperty("interval").c_str())*1000;
}
th = new boost::thread(boost::bind(&DirectoryScanner::scan, this));
logdebug("Directory Scanner running with interval:" << _interval);
// boost::thread t(boost::bind(&DirectoryScanner::scan, this));
}else
if (msg.getProperty("directoryscan") == "stop") {
_halt = true;
//#if BOOST_VERSION > 103500
th->interrupt();
//#endif
logdebug("Directory Scanner stopped:");
}
}
void DirectoryScanner::scan() {
while (!_halt) {
if (File(_dir.c_str()).exists()) {
scan(_dir);
}else {
_halt = true;
}
Thread::sleep2(_interval);
}
}
void DirectoryScanner::scan(std::string dir) {
logdebug("Directory Scanner loop:" << ":" << dir);
MyFileFilter filter;
FileList list = File(dir.c_str()).listFiles();
FileList::iterator it = list.begin();
for (; it != list.end(); it++) {
if ((*it)->isDirectory())
scan((*it)->getPath());
else
computeFile(*it->get());
}
}
void DirectoryScanner::computeFile(File & file) {
Connection con(org::esb::config::Config::getProperty("db.connection"));
PreparedStatement stmt(con.prepareStatement("select * from files where filename=:name and path=:path"));
stmt.setString("name", file.getFileName());
stmt.setString("path", file.getFilePath());
ResultSet rs = stmt.executeQuery();
if (!rs.next()) {
// found=str.find_first_of("aeiou");
if (file.isFile()) {
const char * filename = 0;
std::string name = file.getPath();
// filename=name.data();
char * argv[] = {"", (char*) name.c_str()};
int fileid = import(2, argv);
}
// std::string sql("insert into files (filename, path) values (\""+file.getFileName()+"\",\""+file.getFilePath()+"\")");
// std::cout << sql <<std::endl;
// PreparedStatement pstmt=con.prepareStatement("insert into files (filename, path) values (:name,:path)");
// pstmt.setString("name", file.getFileName());
// pstmt.setString("path", file.getFilePath());
// pstmt.execute();
}
}
}
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks