From 39db010dbe295960433d10c2a8d1fbe98f16092f Mon Sep 17 00:00:00 2001 From: "jackdaw@loafle.com" Date: Thu, 13 Apr 2017 18:17:30 +0900 Subject: [PATCH] init add remove --- .../com/loafle/overflow/crawler/Crawler.java | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/loafle/overflow/crawler/Crawler.java b/src/main/java/com/loafle/overflow/crawler/Crawler.java index c3db014..6cf3349 100644 --- a/src/main/java/com/loafle/overflow/crawler/Crawler.java +++ b/src/main/java/com/loafle/overflow/crawler/Crawler.java @@ -1,6 +1,10 @@ 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.Map; @@ -9,36 +13,57 @@ import java.util.Map; */ public abstract class Crawler { - private Map> configs; + private Map 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(); + } + 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 { - 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 { return getInternal(getConfig(id)); } - public abstract Object getInternal(Map params) throws Exception; - - public Map getConfig(String id) throws Exception { - return configs.get(id); - } - - public void putConfig(String id,Map o) throws Exception { - if (configs == null) { - configs = new HashMap>(); - } - configs.put(id,o); - } 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 { - return null; + this.configs.remove(id); + return true; } + public abstract Object getInternal(Config c) throws Exception; + + }