001/**
002 *
003 * Copyright 2013-2014 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util.dns.javax;
018
019import java.util.ArrayList;
020import java.util.Hashtable;
021import java.util.List;
022
023import javax.naming.NamingEnumeration;
024import javax.naming.NamingException;
025import javax.naming.directory.Attribute;
026import javax.naming.directory.Attributes;
027import javax.naming.directory.DirContext;
028import javax.naming.directory.InitialDirContext;
029
030import org.jivesoftware.smack.initializer.SmackInitializer;
031import org.jivesoftware.smack.util.DNSUtil;
032import org.jivesoftware.smack.util.dns.DNSResolver;
033import org.jivesoftware.smack.util.dns.SRVRecord;
034
035/**
036 * A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namespace.
037 * 
038 * @author Florian Schmaus
039 *
040 */
041public class JavaxResolver implements SmackInitializer, DNSResolver {
042    
043    private static JavaxResolver instance;
044    private static DirContext dirContext;
045    
046    static {
047        try {
048            Hashtable<String, String> env = new Hashtable<String, String>();
049            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
050            dirContext = new InitialDirContext(env);
051        } catch (Exception e) {
052            // Ignore.
053        }
054
055        // Try to set this DNS resolver as primary one
056        setup();
057    }
058
059    public static synchronized DNSResolver getInstance() {
060        if (instance == null && isSupported()) {
061            instance = new JavaxResolver();
062        }
063        return instance;
064    }
065
066    public static boolean isSupported() {
067        return dirContext != null;
068    }
069
070    public static void setup() {
071        DNSUtil.setDNSResolver(getInstance());
072    }
073
074    @Override
075    public List<SRVRecord> lookupSRVRecords(String name) throws NamingException {
076        List<SRVRecord> res = new ArrayList<SRVRecord>();
077
078        Attributes dnsLookup = dirContext.getAttributes(name, new String[] { "SRV" });
079        Attribute srvAttribute = dnsLookup.get("SRV");
080        if (srvAttribute == null)
081            return res;
082        @SuppressWarnings("unchecked")
083        NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
084        while (srvRecords.hasMore()) {
085            String srvRecordString = srvRecords.next();
086            String[] srvRecordEntries = srvRecordString.split(" ");
087            int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
088            int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
089            int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
090            String host = srvRecordEntries[srvRecordEntries.length - 1];
091
092            SRVRecord srvRecord = new SRVRecord(host, port, priority, weight);
093            res.add(srvRecord);
094        }
095        return res;
096    }
097
098    @Override
099    public List<Exception> initialize() {
100        setup();
101        return null;
102    }
103
104}