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 @SuppressWarnings("serial")
25 public class HostMap<TYPE> extends HashMap<String, TYPE>
26 {
27
28 /* --------------------------------------------------------------- */
29 /** Construct empty HostMap.
30 */
31 public HostMap()
32 {
33 super(11);
34 }
35
36 /* --------------------------------------------------------------- */
37 /** Construct empty HostMap.
38 *
39 * @param capacity initial capacity
40 */
41 public HostMap(int capacity)
42 {
43 super (capacity);
44 }
45
46 /* ------------------------------------------------------------ */
47 /**
48 * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
49 */
50 @Override
51 public TYPE put(String host, TYPE object)
52 throws IllegalArgumentException
53 {
54 return super.put(host, object);
55 }
56
57 /* ------------------------------------------------------------ */
58 /**
59 * @see java.util.HashMap#get(java.lang.Object)
60 */
61 @Override
62 public TYPE get(Object key)
63 {
64 return super.get(key);
65 }
66
67 /* ------------------------------------------------------------ */
68 /**
69 * Retrieve a lazy list of map entries associated with specified
70 * hostname by taking into account the domain suffix matches.
71 *
72 * @param host hostname
73 * @return lazy list of map entries
74 */
75 public Object getLazyMatches(String host)
76 {
77 if (host == null)
78 return LazyList.getList(super.entrySet());
79
80 int idx = 0;
81 String domain = host.trim();
82 HashSet<String> domains = new HashSet<String>();
83 do {
84 domains.add(domain);
85 if ((idx = domain.indexOf('.')) > 0)
86 {
87 domain = domain.substring(idx+1);
88 }
89 } while (idx > 0);
90
91 Object entries = null;
92 for(Map.Entry<String, TYPE> entry: super.entrySet())
93 {
94 if (domains.contains(entry.getKey()))
95 {
96 entries = LazyList.add(entries,entry);
97 }
98 }
99
100 return entries;
101 }
102
103 }