| 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 |
import jnr.ffi.Platform; |
| 32 |
|
| 33 |
import static jnr.ffi.Platform.OS.WINDOWS; |
| 34 |
|
| 35 |
/** |
| 36 |
* |
| 37 |
*/ |
| 38 |
class FileProtocolsDB implements ProtocolsDB { |
| 39 |
private final File protocolsFile; |
| 40 |
|
| 41 |
public static final FileProtocolsDB getInstance() { |
| 42 |
return SingletonHolder.INSTANCE; |
| 43 |
} |
| 44 |
|
| 45 |
private static final class SingletonHolder { |
| 46 |
public static final FileProtocolsDB INSTANCE = load(); |
| 47 |
} |
| 48 |
|
| 49 |
FileProtocolsDB(File protocolsFile) { |
| 50 |
this.protocolsFile = protocolsFile; |
| 51 |
} |
| 52 |
|
| 53 |
private static final File locateProtocolsFile() { |
| 54 |
if (Platform.getNativePlatform().getOS().equals(WINDOWS)) { |
| 55 |
String systemRoot; |
| 56 |
try { |
| 57 |
// FIXME: %SystemRoot% is typically *not* present in Java env, |
| 58 |
// so we need a better way to obtain the Windows location. |
| 59 |
// One possible solution: Win32API's SHGetFolderPath() with |
| 60 |
// parameter CSIDL_SYSTEM or CSIDL_WINDOWS. |
| 61 |
systemRoot = System.getProperty("SystemRoot", "C:\\windows"); |
| 62 |
} catch (SecurityException se) { |
| 63 |
// whoops, try the most logical one |
| 64 |
systemRoot = "C:\\windows"; |
| 65 |
} |
| 66 |
|
| 67 |
return new File(systemRoot + "\\system32\\drivers\\etc\\protocol"); |
| 68 |
|
| 69 |
} else { |
| 70 |
return new File("/etc/protocols"); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
private static FileProtocolsDB load() { |
| 75 |
try { |
| 76 |
File protocolsFile = locateProtocolsFile(); |
| 77 |
// Fail unless /etc/protocols can be read and contains at least one valid entry |
| 78 |
NetDBParser parser = new NetDBParser(new FileReader(protocolsFile)); |
| 79 |
try { |
| 80 |
parser.iterator().next(); |
| 81 |
} finally { |
| 82 |
parser.close(); |
| 83 |
} |
| 84 |
|
| 85 |
return new FileProtocolsDB(protocolsFile); |
| 86 |
|
| 87 |
} catch (Throwable t) { |
| 88 |
return null; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public Protocol getProtocolByName(final String name) { |
| 93 |
|
| 94 |
return parse(new Filter() { |
| 95 |
|
| 96 |
public boolean filter(Protocol p) { |
| 97 |
if (p.getName().equals(name)) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
for (String alias : p.getAliases()) { |
| 102 |
if (alias.equals(name)) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
}); |
| 110 |
} |
| 111 |
|
| 112 |
public Protocol getProtocolByNumber(final Integer proto) { |
| 113 |
return parse(new Filter() { |
| 114 |
|
| 115 |
public boolean filter(Protocol p) { |
| 116 |
return p.getProto() == proto.intValue(); |
| 117 |
} |
| 118 |
}); |
| 119 |
} |
| 120 |
|
| 121 |
public Collection<Protocol> getAllProtocols() { |
| 122 |
|
| 123 |
final List<Protocol> allProtocols = new LinkedList<Protocol>(); |
| 124 |
|
| 125 |
parse(new Filter() { |
| 126 |
|
| 127 |
public boolean filter(Protocol s) { |
| 128 |
allProtocols.add(s); |
| 129 |
return false; |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
return Collections.unmodifiableList(allProtocols); |
| 134 |
} |
| 135 |
|
| 136 |
private final NetDBParser loadProtocolsFile() { |
| 137 |
try { |
| 138 |
return new NetDBParser(new FileReader(protocolsFile)); |
| 139 |
} catch (FileNotFoundException ex) { |
| 140 |
return new NetDBParser(new StringReader("")); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
private static interface Filter { |
| 145 |
boolean filter(Protocol s); |
| 146 |
} |
| 147 |
|
| 148 |
private final Protocol parse(Filter filter) { |
| 149 |
NetDBParser parser = loadProtocolsFile(); |
| 150 |
|
| 151 |
try { |
| 152 |
for (NetDBEntry e : parser) { |
| 153 |
try { |
| 154 |
Protocol p = new Protocol(e.name, Integer.parseInt(e.data, 10), e.aliases); |
| 155 |
if (filter.filter(p)) { |
| 156 |
return p; |
| 157 |
} |
| 158 |
} catch (NumberFormatException ex) {} |
| 159 |
} |
| 160 |
|
| 161 |
} finally { |
| 162 |
try { |
| 163 |
parser.close(); |
| 164 |
} catch (IOException ex) { |
| 165 |
throw new RuntimeException(ex); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return null; |
| 170 |
} |
| 171 |
} |