Debian Codegraph @

src/main/java/jnr/netdb/FileServicesDB.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.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.LinkedList;
29 import java.util.List;
30
31 /**
32 * Parses /etc/services
33 */
34 final class FileServicesDB implements ServicesDB {
35 public static String fileName = "/etc/services";
36
37 public static final ServicesDB getInstance() {
38 return SingletonHolder.INSTANCE;
39 }
40
41 private static final class SingletonHolder {
42 public static final ServicesDB INSTANCE = load();
43 }
44
45 private static final ServicesDB load() {
46 try {
47 // Fail unless /etc/services can be read and contains at least one valid entry
48 NetDBParser parser = parseServicesFile();
49 try {
50 parser.iterator().next();
51 } finally {
52 parser.close();
53 }
54
55 return new FileServicesDB();
56
57 } catch (Throwable t) {
58 return null;
59 }
60 }
61
62 static final NetDBParser parseServicesFile() {
63 try {
64 return new NetDBParser(new FileReader(new File(fileName)));
65 } catch (FileNotFoundException ex) {
66 return new NetDBParser(new StringReader(""));
67 }
68 }
69
70 private static final Service parseServicesEntry(NetDBEntry e) {
71
72 String[] portproto = e.data.split("/");
73 if (portproto.length < 2) {
74 return null;
75 }
76
77 int port;
78 try {
79 port = Integer.parseInt(portproto[0], 10);
80 } catch (NumberFormatException ex) {
81 return null;
82 }
83
84 return new Service(e.name, port, portproto[1], e.aliases);
85 }
86
87 private static interface Filter {
88 boolean filter(Service s);
89 }
90
91 private final Service parse(Filter filter) {
92 NetDBParser parser = parseServicesFile();
93
94 try {
95 for (NetDBEntry e : parser) {
96 Service s = parseServicesEntry(e);
97 if (s != null && filter.filter(s)) {
98 return s;
99 }
100 }
101
102 } finally {
103 try {
104 parser.close();
105 } catch (IOException ex) {
106 throw new RuntimeException(ex);
107 }
108 }
109
110 return null;
111 }
112
113 public Service getServiceByName(final String name, final String proto) {
114 return parse(new Filter() {
115
116 public boolean filter(Service s) {
117 if (!s.proto.equals(proto) && proto != null) {
118 return false;
119 }
120
121 if (s.getName().equals(name)) {
122 return true;
123 }
124
125 for (String alias : s.getAliases()) {
126 if (alias.equals(name)) {
127 return true;
128 }
129 }
130
131 return false;
132 }
133 });
134 }
135
136 public Service getServiceByPort(final Integer port, final String proto) {
137 return parse(new Filter() {
138
139 public boolean filter(Service s) {
140 return s.getPort() == port.intValue() && (s.proto.equals(proto) || proto == null);
141 }
142 });
143 }
144
145 public Collection<Service> getAllServices() {
146
147 final List<Service> allServices = new LinkedList<Service>();
148
149 parse(new Filter() {
150
151 public boolean filter(Service s) {
152 allServices.add(s);
153 return false;
154 }
155 });
156
157 return Collections.unmodifiableList(allServices);
158 }
159 }