init add remove

This commit is contained in:
jackdaw@loafle.com 2017-04-13 18:17:30 +09:00
parent d0c55a7aea
commit 39db010dbe

View File

@ -1,6 +1,10 @@
package com.loafle.overflow.crawler; package com.loafle.overflow.crawler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.crawler.config.Config;
import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -9,36 +13,57 @@ import java.util.Map;
*/ */
public abstract class Crawler { public abstract class Crawler {
private Map<String,Map<String,Object>> configs; private Map<String,Config> configs;
private String configPath;
public Config getConfig(String id) throws Exception {
return configs.get(id);
}
public void putConfig(String id,Config o) throws Exception {
if (configs == null) {
configs = new HashMap<String,Config>();
}
configs.put(id,o);
}
public String getConfigPath() {
return configPath;
}
public void setConfigPath(String path) {
if (path.endsWith("/")) {
this.configPath = path;
} else {
this.configPath = path + "/";
}
}
public Object add(String id) throws Exception { public Object add(String id) throws Exception {
return null; ObjectMapper mapper = new ObjectMapper();
Config c = mapper.readValue(new File(this.getConfigPath() + id),Config.class);
this.putConfig(id,c);
return true;
} }
public Object get(String id) throws Exception { public Object get(String id) throws Exception {
return getInternal(getConfig(id)); return getInternal(getConfig(id));
} }
public abstract Object getInternal(Map<String, Object> params) throws Exception;
public Map<String,Object> getConfig(String id) throws Exception {
return configs.get(id);
}
public void putConfig(String id,Map<String,Object> o) throws Exception {
if (configs == null) {
configs = new HashMap<String,Map<String,Object>>();
}
configs.put(id,o);
}
public Object init(String config) throws Exception { public Object init(String config) throws Exception {
return null; this.setConfigPath(config);
File dir = new File(config);
for (File file : dir.listFiles()) {
this.add(file.getName());
}
return true;
} }
public Object remove(String id) throws Exception { public Object remove(String id) throws Exception {
return null; this.configs.remove(id);
return true;
} }
public abstract Object getInternal(Config c) throws Exception;
} }