2011-12-28 67 views
0

下面的方法得到一个“路由”(类名称和类方法):定义为迭代通过HashSet的清空的HashMap条目

public Route getRoute(final String method, final String request) { 
    if (hasRoutes) { 
     for (Map.Entry<Pattern, HashMap<String, String>> entry : routes) { 
      Matcher match = entry.getKey().matcher(request); 

      if (match.find()) { 
       HashMap<String, String> methods = entry.getValue(); 

       // ISSUE: Returns FALSE after 1st call of Router.getRoute() 
       if (methods.containsKey(method)) { 
        return new Route(match.group("interface"), "TRUE (" + method + " - " + match.group("interface") + "): " + methods.get(method)); 
       } else { 
        return new Route(match.group("interface"), "FALSE (" + method + " - " + match.group("interface") + "): " + methods.values().toString() + ", SIZE: " + entry.getValue().size()); 
       } 

       //return entry.getValue().containsKey(method) ? new Route(match.group("interface"), entry.getValue().get(method)) : null; 
      } 
     } 
    } 

    return null; 
} 

“路线”:

private Set<Entry<Pattern, HashMap<String, String>>> routes; 

它是一个高速缓存表示支持路由的JSON配置文件,例如:

{ 
    "^/?(?<interface>threads)/?$": { 
     "GET": "list", 
     "POST": "create" 
    }, 
    "^/?(?<interface>threads)/(?<id>\\d+)/?$": { 
     "GET": "get", 
     "POST": "reply", 
     "PUT": "edit", 
     "PATCH": "edit", 
     "DELETE": "delete" 
    } 
} 

编辑,这里是如何填充“路由”从JSON文件的内容:

try { 
     JsonParser parser = JSONFactory.createJsonParser(in); 
     JsonNode root = JSONMapper.readTree(parser); 
     Iterator base = root.getFieldNames(); 
     Iterator node; 
     String match, method; 
     HashMap<Pattern, HashMap<String, String>> routesMap = new HashMap(); 

     while (base.hasNext()) { 
      match = base.next().toString(); 

      if (match != null) { 
       node = root.get(match).getFieldNames(); 
       HashMap<String, String> methods = new HashMap(); 

       while (node.hasNext()) { 
        method = node.next().toString(); 

        if (method != null) { 
         methods.put(method, root.get(match).get(method).getTextValue()); 
        } 
       } 

       if (!methods.isEmpty()) { 
        routesMap.put(Pattern.compile(match), methods); 
       } 
      } 
     } 

     if (!routesMap.isEmpty()) { 
      hasRoutes = true; 
      routes = routesMap.entrySet(); 
     } 

     // Help garbage collection 
     parser = null; 
     root = null; 
     base = null; 
     node = null; 
     match = null; 
     method = null; 
     routesMap = null; 
    } catch (Exception ex) { 
    } 

EDIT 2中,所讨论的属性& init()方法:

public final static JsonFactory JSONFactory = new JsonFactory(); 
public final static ObjectMapper JSONMapper = new ObjectMapper(); 
public static Router router; 
private final Class self = getClass(); 
private final ClassLoader loader = self.getClassLoader(); 

public void init(ServletConfig config) throws ServletException { 
    super.init(config); 

    router = new Router(self.getResourceAsStream("/v1_0/Routes.json"), JSONFactory, JSONMapper); 
} 

出于某种原因,在第一时间之后访问servlet当HashMap中是空的值。 x.size()返回零。

这是从头开始重写一个PHP应用程序,所以如果问题是世俗的,我提前道歉。

完整的源: - Router source - Route source

+1

它是如何设置/缓存/等?代码中没有任何东西可以删除 – 2011-12-28 15:18:33

+0

@Dave Newton,添加了“缓存”HashSet中JSON文件表示的过程。 – 2011-12-28 15:26:08

+0

我在这里找不到的是,你在哪里以及如何缓存?它是否缓存在Servlet init()阶段并在随后的阶段中使用? – kosa 2011-12-28 15:30:46

回答

0

您的getOptions()方法会在迭代时删除此地图中的所有条目。因此,在调用getOptions()一次后,地图是空的。

顺便说一下,分配null变量确实不是“帮助垃圾回收器”。垃圾收集器知道当变量的作用域退出时,该变量不再引用该对象。实际上,通过分配永远不会被读取的值(以及使您的代码产生反作用的噪音),您实际上正在放慢速度。一个好的静态分析工具,如FindBugs会警告你这是错误的代码。

+0

你是英雄伙伴,业余爱好者,但我很乐意将它压扁。感谢关于垃圾回收的提示。 – 2011-12-28 16:18:02

0

通过迭代HashSet的清空HashMap中的条目

这不会发生。简单地迭代HashSet对集合的内容没有副作用。

还有其他事情正在导致您的问题。

+0

您好斯蒂芬,路由器和路由类的代码的完整代码: [link](http://martingallagher.com/code/Router.java) [链接](http://martingallagher.com/code /Route.java) – 2011-12-28 15:41:14