1 // ========================================================================
2 // Copyright (c) 2010 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14
15 package org.eclipse.jetty.util;
16
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20
21 /* ------------------------------------------------------------ */
22 /**
23 */
24 public class HostMap<TYPE> extends HashMap<String, TYPE>
25 {
26 /* --------------------------------------------------------------- */
27 /** Construct empty HostMap.
28 */
29 public HostMap()
30 {
31 super(11);
32 }
33
34 /* --------------------------------------------------------------- */
35 /** Construct empty HostMap.
36 *
37 * @param capacity initial capacity
38 */
39 public HostMap(int capacity)
40 {
41 super (capacity);
42 }
43
44 /* ------------------------------------------------------------ */
45 /**
46 * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
47 */
48 @Override
49 public TYPE put(String host, TYPE object)
50 throws IllegalArgumentException
51 {
52 return super.put(host, object);
53 }
54
55 /* ------------------------------------------------------------ */
56 /**
57 * @see java.util.HashMap#get(java.lang.Object)
58 */
59 @Override
60 public TYPE get(Object key)
61 {
62 return super.get(key);
63 }
64
65 /* ------------------------------------------------------------ */
66 /**
67 * Retrieve a lazy list of map entries associated with specified
68 * hostname by taking into account the domain suffix matches.
69 *
70 * @param host hostname
71 * @return lazy list of map entries
72 */
73 public Object getLazyMatches(String host)
74 {
75 if (host == null)
76 return LazyList.getList(super.entrySet());
77
78 int idx = 0;
79 String domain = host.trim();
80 HashSet<String> domains = new HashSet<String>();
81 do {
82 domains.add(domain);
83 if ((idx = domain.indexOf('.')) > 0)
84 {
85 domain = domain.substring(idx+1);
86 }
87 } while (idx > 0);
88
89 Object entries = null;
90 for(Map.Entry<String, TYPE> entry: super.entrySet())
91 {
92 if (domains.contains(entry.getKey()))
93 {
94 entries = LazyList.add(entries,entry);
95 }
96 }
97
98 return entries;
99 }
100
101 }