| 1 |
/* |
| 2 |
* Copyright (C) 2010 Wayne Meissner |
| 3 |
* |
| 4 |
* This file is part of jnr. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package jnr.netdb; |
| 20 |
|
| 21 |
import java.util.Collection; |
| 22 |
|
| 23 |
/** |
| 24 |
* An entry in the system protocol database |
| 25 |
*/ |
| 26 |
public final class Protocol { |
| 27 |
/** Official protocol name */ |
| 28 |
private final String protocolName; |
| 29 |
|
| 30 |
private final int proto; |
| 31 |
|
| 32 |
/** All the aliases for this protocol */ |
| 33 |
private final Collection<String> aliases; |
| 34 |
|
| 35 |
Protocol(String name, int proto, Collection<String> aliases) { |
| 36 |
this.protocolName = name; |
| 37 |
this.proto = proto; |
| 38 |
this.aliases = aliases; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Locates a protocol by name. |
| 43 |
* |
| 44 |
* @param name the official IANA name for the protocol, or an alias. |
| 45 |
* @return a {@code Protocol} instance, or {@code null} if the protocol could not be found. |
| 46 |
*/ |
| 47 |
public static final Protocol getProtocolByName(String name) { |
| 48 |
return getProtocolDB().getProtocolByName(name); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Locates a protocol by number. |
| 54 |
* |
| 55 |
* @param proto the internet protocol number of the protocol. |
| 56 |
* @return a {@code Protocol} instance, or {@code null} if the protocol could not be found. |
| 57 |
*/ |
| 58 |
public static final Protocol getProtocolByNumber(int proto) { |
| 59 |
return getProtocolDB().getProtocolByNumber(proto); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the official IANA name of this {@code Protocol} |
| 65 |
* |
| 66 |
* @return The name of this {@code Protocol} as a {@code String} |
| 67 |
*/ |
| 68 |
public final String getName() { |
| 69 |
return protocolName; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the official IANA protocol number for this {@code Protocol} |
| 74 |
* |
| 75 |
* @return The protocol number for this {@code Protocol} as an {@code int} |
| 76 |
*/ |
| 77 |
public final int getProto() { |
| 78 |
return proto; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns a list of aliases this {@code Protocol} is also known by. |
| 83 |
* |
| 84 |
* @return A {@code Collection} of Strings for aliases this {@code Protocol} |
| 85 |
*/ |
| 86 |
public final Collection<String> getAliases() { |
| 87 |
return aliases; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns the currently loaded ProtocolDB |
| 92 |
* |
| 93 |
* @return an instance of {@code ProtocolDB} |
| 94 |
*/ |
| 95 |
private static final ProtocolsDB getProtocolDB() { |
| 96 |
return ProtocolDBSingletonHolder.INSTANCE; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Holds the global lazily-loaded instance of the ProtocolDB |
| 101 |
*/ |
| 102 |
private static final class ProtocolDBSingletonHolder { |
| 103 |
static final ProtocolsDB INSTANCE = load(); |
| 104 |
|
| 105 |
private static final ProtocolsDB load() { |
| 106 |
// Try to use the native functions if possible |
| 107 |
ProtocolsDB db = NativeProtocolsDB.getInstance(); |
| 108 |
|
| 109 |
// Fall back to parsing /etc/protocols directly. |
| 110 |
if (db == null) { |
| 111 |
db = FileProtocolsDB.getInstance(); |
| 112 |
} |
| 113 |
|
| 114 |
// As a last resort, fall back to the hard coded table |
| 115 |
return db != null ? db : IANAProtocolsDB.getInstance(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public String toString() { |
| 121 |
return String.format("<Protocol: Name: %s, Proto: %d, Aliases: %s>", |
| 122 |
protocolName, proto, aliases); |
| 123 |
}; |
| 124 |
} |