Skip to content

Instantly share code, notes, and snippets.

@jeantil
Last active February 4, 2016 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeantil/b8a176070d7de25c6d0a to your computer and use it in GitHub Desktop.
Save jeantil/b8a176070d7de25c6d0a to your computer and use it in GitHub Desktop.
James
public class MockDNSService implements DNSService{
private Map<String,DNSRecord> records=new HashMap<String, DNSRecord>();
public void registerRecord(String hostname, InetAddress[] addresses,Collection<String> MXRecords, Collection<String> TXTRecords ){
records.put(hostname,new DNSRecord(addresses, MXRecords, TXTRecords));
}
public void dropRecord(String hostname){
records.remove(hostname);
}
@Override
public Collection<String> findMXRecords(final String hostname) throws TemporaryResolutionException {
return hostRecord(hostname).MXRecords;
}
@Override
public Collection<String> findTXTRecords(String hostname) {
return hostRecord(hostname).TXTRecords;
}
@Override
public InetAddress[] getAllByName(String host) throws UnknownHostException {
return hostRecord(host).addresses;
}
@Override
public InetAddress getByName(String host) throws UnknownHostException {
return hostRecord(host).addresses[0];
}
private DNSRecord hostRecord(String host) {
return records.entrySet().stream().filter((entry)->entry.getKey().equals(host)).findFirst().get().getValue();
}
@Override
public InetAddress getLocalHost() throws UnknownHostException {
return InetAddress.getLocalHost();
}
@Override
public String getHostName(InetAddress addr) {
return records.entrySet().stream().filter((entry)->entry.getValue().contains(addr)).findFirst().get().getKey();
}
}
class DNSRecord{
InetAddress[] addresses;
Collection<String> MXRecords;
Collection<String> TXTRecords;
private List<InetAddress> addressList=Arrays.asList(addresses);
DNSRecord(InetAddress[] adresses, Collection<String> mxRecords, Collection<String> txtRecords) {
this.addresses = adresses;
MXRecords = mxRecords;
TXTRecords = txtRecords;
}
boolean contains(InetAddress address){
return addressList.contains(address);
}
}
package org.apache.james.mpt.imapmailbox.inmemory;
import java.io.IOException;
import java.nio.file.*;
import java.util.concurrent.TimeUnit;
public class WatchDirectory {
private final Path path;
private final WatchService watcher;
private final WatchKey key;
private String data;
public WatchDirectory(WatchService watcher, Path path) {
this.path = path;
this.watcher = watcher;
try {
this.key = this.path.register(this.watcher,//StandardWatchEventKinds.ENTRY_CREATE,
//StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Path update() {
WatchService pe;
try {
while ((watcher.poll(20, TimeUnit.SECONDS)) != null) {
for (WatchEvent<?> we : key.pollEvents()) {
WatchEvent.Kind<?> kind = we.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
WatchEvent<Path> ev = (WatchEvent<Path>) we;
Path filename = ev.context();
data = filename.toString();
return filename;
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String getUpdate() {
update();
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment