瀏覽代碼

Tidy up SRVRecord.

Adds an AutoCloseable version of InitialDirContext.
Fixes-Issue: CLIENT-521

Change-Id: I5248d2f50ec8da01c90fd607e72c5eac66451cf3
Reviewed-on: http://gerrit.dmdirc.com/3950
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
changes/50/3950/6
Greg Holmes 9 年之前
父節點
當前提交
31d3ebc5be

+ 46
- 0
src/com/dmdirc/parser/common/AutoCloseableInitialDirContext.java 查看文件

@@ -0,0 +1,46 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.parser.common;
24
+
25
+import java.util.Hashtable;
26
+import java.util.Map;
27
+
28
+import javax.naming.NamingException;
29
+import javax.naming.directory.InitialDirContext;
30
+
31
+/**
32
+ * {@link AutoCloseable} {@link InitialDirContext}.
33
+ */
34
+public class AutoCloseableInitialDirContext extends InitialDirContext implements AutoCloseable {
35
+
36
+    protected AutoCloseableInitialDirContext(final boolean lazy) throws NamingException {
37
+        super(lazy);
38
+    }
39
+
40
+    public AutoCloseableInitialDirContext() throws NamingException {
41
+    }
42
+
43
+    public AutoCloseableInitialDirContext(final Map<?, ?> environment) throws NamingException {
44
+        super(new Hashtable<>(environment)); // NOPMD - Required by InitialDirContext
45
+    }
46
+}

+ 10
- 13
src/com/dmdirc/parser/common/SRVRecord.java 查看文件

@@ -22,13 +22,12 @@
22 22
 package com.dmdirc.parser.common;
23 23
 
24 24
 import java.util.ArrayList;
25
-import java.util.Hashtable;
25
+import java.util.HashMap;
26 26
 import java.util.List;
27 27
 
28 28
 import javax.naming.NamingEnumeration;
29 29
 import javax.naming.NamingException;
30 30
 import javax.naming.directory.Attribute;
31
-import javax.naming.directory.InitialDirContext;
32 31
 
33 32
 /**
34 33
  * Class to represent an SRV Record.
@@ -61,7 +60,7 @@ public class SRVRecord implements Comparable<SRVRecord> {
61 60
             priority = Integer.parseInt(bits[0]);
62 61
             weight = Integer.parseInt(bits[1]);
63 62
             port = Integer.parseInt(bits[2]);
64
-            host = (bits[3].charAt(bits[3].length() - 1) == '.') ? bits[3].substring(0, bits[3].length() - 1) : bits[3];
63
+            host = bits[3].charAt(bits[3].length() - 1) == '.' ? bits[3].substring(0, bits[3].length() - 1) : bits[3];
65 64
         } catch (final NumberFormatException nfe) {
66 65
             throw new NamingException("Unable to parse SRV Record parameters."); // NOPMD
67 66
         }
@@ -105,25 +104,23 @@ public class SRVRecord implements Comparable<SRVRecord> {
105 104
 
106 105
     @Override
107 106
     public String toString() {
108
-        return priority + " " + weight + " " + port + " " + host + ".";
107
+        return priority + " " + weight + ' ' + port + ' ' + host + '.';
109 108
     }
110 109
 
111 110
     @Override
112 111
     public int compareTo(final SRVRecord o) {
113
-        if (this.priority < o.priority) { return -1; }
114
-        if (this.priority > o.priority) { return 1; }
112
+        if (priority < o.getPriority()) { return -1; }
113
+        if (priority > o.getPriority()) { return 1; }
115 114
         return 0;
116 115
     }
117 116
 
118 117
     public static List<SRVRecord> getRecords(final String host) {
119 118
         final List<SRVRecord> result = new ArrayList<>();
120
-
121
-        try {
122
-            // Obsolete Collection. yeah yeah...
123
-            final Hashtable<String, String> env = new Hashtable<>(); // NOPMD - Required by InitialDirContext
124
-            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
125
-            env.put("java.naming.provider.url", "dns:");
126
-            final Attribute attr = new InitialDirContext(env).getAttributes(host, new String [] { "SRV" }).get("SRV");
119
+        final HashMap<String, String> env = new HashMap<>();
120
+        env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
121
+        env.put("java.naming.provider.url", "dns:");
122
+        try (AutoCloseableInitialDirContext dirContext = new AutoCloseableInitialDirContext(env)) {
123
+            final Attribute attr = dirContext.getAttributes(host, new String [] { "SRV" }).get("SRV");
127 124
 
128 125
             if (attr != null) {
129 126
                 final NamingEnumeration<?> ne = attr.getAll();

Loading…
取消
儲存