move into the com.verisign.tat package (leaving unbound behind. sniff.)
[captive-validator.git] / src / com / versign / tat / dnssec / Util.java
1 /*
2  * $Id$
3  * 
4  * Copyright (c) 2005 VeriSign. All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer. 2. Redistributions in
11  * binary form must reproduce the above copyright notice, this list of
12  * conditions and the following disclaimer in the documentation and/or other
13  * materials provided with the distribution. 3. The name of the author may not
14  * be used to endorse or promote products derived from this software without
15  * specific prior written permission.
16  * 
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *  
28  */
29
30 package com.versign.tat.dnssec;
31
32 import java.util.*;
33
34 import org.xbill.DNS.Flags;
35 import org.xbill.DNS.Header;
36 import org.xbill.DNS.Name;
37
38 /**
39  * Some basic utility functions.
40  * 
41  * @author davidb
42  * @version $Revision$
43  */
44 public class Util
45 {
46
47   /**
48    * Convert a DNS name into a string suitable for use as a cache key.
49    * 
50    * @param name The name to convert.
51    * @return A string representing the name. This isn't ever meant to be
52    *         converted back into a DNS name.
53    */
54   public static String nameToString(Name name)
55   {
56     if (name.equals(Name.root)) return ".";
57
58     String n = name.toString().toLowerCase();
59     if (n.endsWith(".")) n = n.substring(0, n.length() - 1);
60
61     return n;
62   }
63
64 //  public static SMessage errorMessage(Request request, int rcode)
65 //  {
66 //    SMessage m = new SMessage(request.getID());
67 //    Header h = m.getHeader();
68 //    h.setRcode(rcode);
69 //    h.setFlag(Flags.QR);
70 //    m.setQuestion(request.getQuestion());
71 //    m.setOPT(request.getOPT());
72 //
73 //    return m;
74 //  }
75 //
76 //  public static SMessage errorMessage(SMessage message, int rcode)
77 //  {
78 //    Header h = message.getHeader();
79 //    SMessage m = new SMessage(h.getID());
80 //    h = m.getHeader();
81 //    h.setRcode(rcode);
82 //    h.setFlag(Flags.QR);
83 //    m.setQuestion(message.getQuestion());
84 //    m.setOPT(message.getOPT());
85 //
86 //    return m;
87 //  }
88
89   public static int parseInt(String s, int def)
90   {
91     if (s == null) return def;
92     try
93     {
94       return Integer.parseInt(s);
95     }
96     catch (NumberFormatException e)
97     {
98       return def;
99     }
100   }
101
102   public static long parseLong(String s, long def)
103   {
104     if (s == null) return def;
105     try
106     {
107       return Long.parseLong(s);
108     }
109     catch (NumberFormatException e)
110     {
111       return def;
112     }
113   }
114   
115   public static class ConfigEntry
116   {
117     public String key;
118     public String value;
119     
120     public ConfigEntry(String key, String value)
121     {
122       this.key = key; this.value = value;
123     }
124   }
125   
126   public static List parseConfigPrefix(Properties config, String prefix)
127   {
128     if (! prefix.endsWith("."))
129     {
130       prefix = prefix + ".";
131     }
132     
133     List res = new ArrayList();
134     
135     for (Iterator i = config.entrySet().iterator(); i.hasNext(); )
136     {
137       Map.Entry entry = (Map.Entry) i.next();
138       String key = (String) entry.getKey();
139       if (key.startsWith(prefix))
140       {
141         key = key.substring(prefix.length());
142         
143         res.add(new ConfigEntry(key, (String) entry.getValue()));
144       }
145     }
146     
147     return res;
148   }
149 }