Debian Codegraph @

src/main/java/jnr/netdb/Service.java

raw
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 import java.util.Collections;
23
24 /**
25 * Holds information about TCP and UDP services on a host.
26 */
27 public final class Service {
28 /** Official service name */
29 private final String serviceName;
30 /** TCP or UDP port number */
31 private final int port;
32
33 /** Protocol to use */
34 final String proto;
35
36 /** All the aliases for this service */
37 private final Collection<String> aliases;
38
39 Service(String name, int port, String proto, Collection<String> aliases) {
40 this.serviceName = name;
41 this.port = port;
42 this.proto = proto;
43 this.aliases = aliases;
44 }
45
46 /**
47 * Locates a service by name and protocol.
48 *
49 * @param name the official IANA name for the service, or an alias.
50 * @param proto the protocol for the service. Usually "tcp" or "udp".
51 * @return a {@code Service} instance, or {@code null} if the service could not be found.
52 */
53 public static final Service getServiceByName(String name, String proto) {
54 return getServicesDB().getServiceByName(name, proto);
55 }
56
57
58 /**
59 * Locates a service by port and protocol.
60 *
61 * @param port the TCP or UDP port of the service.
62 * @param proto the protocol for the service. Usually "tcp" or "udp".
63 * @return a {@code Service} instance, or {@code null} if the service could not be found.
64 */
65 public static final Service getServiceByPort(int port, String proto) {
66 return getServicesDB().getServiceByPort(port, proto);
67 }
68
69 /**
70 * Returns a list of all services.
71 *
72 * @return a {@code Collection} of {@code Service} instances.
73 */
74 public static final Collection<Service> getAllServices() {
75 return Collections.emptyList();
76 }
77
78 /**
79 * Returns the official IANA name of this {@code Service}
80 *
81 * @return The name of this {@code Service} as a {@code String}
82 */
83 public final String getName() {
84 return serviceName;
85 }
86
87 /**
88 * Returns the official IANA port for this {@code Service}
89 *
90 * @return The port for this {@code Service} as an {@code int}
91 */
92 public final int getPort() {
93 return port;
94 }
95
96 /**
97 * Gets a list of aliases this {@code Service} is also known as.
98 *
99 * @return A {@code Collection} of Strings for aliases this {@code Service}
100 */
101 public final Collection<String> getAliases() {
102 return aliases;
103 }
104
105 /**
106 * Returns the currently loaded ServicesDB
107 *
108 * @return an instance of {@code ServicesDB}
109 */
110 private static final ServicesDB getServicesDB() {
111 return ServicesDBSingletonHolder.INSTANCE;
112 }
113
114 /**
115 * Holds the global lazily-loaded instance of the ServicesDB
116 */
117 private static final class ServicesDBSingletonHolder {
118 static final ServicesDB INSTANCE = load();
119
120 private static final ServicesDB load() {
121 // Try to use the native functions if possible
122 ServicesDB db = NativeServicesDB.load();
123
124 // Fall back to parsing /etc/services directly.
125 if (db == null) {
126 db = FileServicesDB.getInstance();
127 }
128
129 // As a last resort, fall back to the hard coded table
130 return db != null ? db : IANAServicesDB.getInstance();
131 }
132 }
133
134 @Override
135 public String toString() {
136 return String.format("<Service: Name: %s, Port: %d, Proto: %s, Aliases: %s>",
137 serviceName, port, proto, aliases);
138 };
139 }