Sail workingSail = new MemoryStore();
workingSail.initialize();
try {
Sail streamingSail = new UdpTransactionSail(workingSail, address, ports);
try {
TweetStore store = new TweetStore(streamingSail);
store.doNotRefreshCoreMetadata();
store.initialize();
try {
// A connection with which to repeatedly clear the working store
final SailConnection c = workingSail.getConnection();
try {
c.begin();
// Offline persister
final TweetPersister persister = new TweetPersister(store, null);
try {
CustomTwitterClient client = new CustomTwitterClient();
// Note: this is only for serving local files.
store.startServer(client);
TweetPersistedLogger pLogger = new TweetPersistedLogger(client.getStatistics(), persister);
TweetFilterCriterion crit = new TweetFilterCriterion(TwitLogic.getConfiguration());
Filter<Tweet> f = new Filter<Tweet>(crit, pLogger);
// Add a "topic sniffer".
TopicSniffer topicSniffer = new TopicSniffer(f);
// Add a tweet annotator.
Matcher matcher = new MultiMatcher(
new DemoAfterthoughtMatcher());
final Handler<Tweet> annotator
= new TweetAnnotator(matcher, topicSniffer);
Handler<Tweet> adder = new Handler<Tweet>() {
public boolean isOpen() {
return annotator.isOpen();
}
public void handle(final Tweet tweet) throws HandlerException {
try {
c.clear();
c.commit();
c.begin();
} catch (SailException e) {
throw new HandlerException(e);
}
annotator.handle(tweet);
}
};
Handler<Tweet> deleter = new TweetDeleter(store);
TweetReceivedLogger rLogger = new TweetReceivedLogger(client.getStatistics(), adder);
Set<User> users = TwitLogic.findFollowList(client);
Set<String> terms = TwitLogic.findTrackTerms();
if (0 < users.size() || 0 < terms.size()) {
client.processFilterStream(users, terms, null, rLogger, deleter, 0);
} else {
client.processSampleStream(rLogger, deleter);
}
} finally {
persister.close();
}
} finally {
c.rollback();
c.close();
}
} finally {
store.shutDown();
}
} finally {
streamingSail.shutDown();
}
} finally {
workingSail.shutDown();
}
}