118 }
119 }
120 return getContext(domain, urls, env);
121 }
122
123 /*
124 * Returns all the servers specified in a set of URLs.
125 * If a URL has no host (or port), the servers configured on the
126 * underlying platform are used if possible. If no configured
127 * servers can be found, then fall back to the old behavior of
128 * using "localhost".
129 * There must be at least one URL.
130 */
131 private static String[] serversForUrls(DnsUrl[] urls)
132 throws NamingException {
133
134 if (urls.length == 0) {
135 throw new ConfigurationException("DNS pseudo-URL required");
136 }
137
138 List servers = new ArrayList();
139
140 for (int i = 0; i < urls.length; i++) {
141 String server = urls[i].getHost();
142 int port = urls[i].getPort();
143
144 if (server == null && port < 0) {
145 // No server or port given, so look to underlying platform.
146 // ResolverConfiguration does some limited caching, so the
147 // following is reasonably efficient even if called rapid-fire.
148 List platformServers = filterNameServers(
149 ResolverConfiguration.open().nameservers(), false);
150 if (!platformServers.isEmpty()) {
151 servers.addAll(platformServers);
152 continue; // on to next URL (if any, which is unlikely)
153 }
154 }
155
156 if (server == null) {
157 server = "localhost";
158 }
159 servers.add((port < 0)
160 ? server
161 : server + ":" + port);
162 }
163 return (String[]) servers.toArray(
164 new String[servers.size()]);
165 }
166
167 /*
168 * Returns true if serversForUrls(urls) would make use of servers
169 * from the underlying platform.
170 */
171 private static boolean platformServersUsed(DnsUrl[] urls) {
172 if (!platformServersAvailable()) {
173 return false;
174 }
175 for (int i = 0; i < urls.length; i++) {
176 if (urls[i].getHost() == null &&
177 urls[i].getPort() < 0) {
178 return true;
179 }
180 }
181 return false;
182 }
183
184 /*
207 buf.append("dns://").append(servers[i]).append(path);
208 }
209 return buf.toString();
210 }
211
212 /*
213 * Reads environment to find URL(s) of initial context.
214 * Default URL is "dns:".
215 */
216 private static String getInitCtxUrl(Hashtable env) {
217 String url = (String) env.get(Context.PROVIDER_URL);
218 return ((url != null) ? url : DEFAULT_URL);
219 }
220
221 /**
222 * Removes any DNS server that's not permitted to access
223 * @param input the input server[:port] list, must not be null
224 * @param oneIsEnough return output once there exists one ok
225 * @return the filtered list, all non-permitted input removed
226 */
227 private static List filterNameServers(List input, boolean oneIsEnough) {
228 SecurityManager security = System.getSecurityManager();
229 if (security == null || input == null || input.isEmpty()) {
230 return input;
231 } else {
232 List output = new ArrayList();
233 for (Object o: input) {
234 if (o instanceof String) {
235 String platformServer = (String)o;
236 int colon = platformServer.indexOf(':',
237 platformServer.indexOf(']') + 1);
238
239 int p = (colon < 0)
240 ? DEFAULT_PORT
241 : Integer.parseInt(
242 platformServer.substring(colon + 1));
243 String s = (colon < 0)
244 ? platformServer
245 : platformServer.substring(0, colon);
246 try {
247 security.checkConnect(s, p);
248 output.add(platformServer);
249 if (oneIsEnough) {
250 return output;
251 }
252 } catch (SecurityException se) {
253 continue;
254 }
255 }
256 }
257 return output;
258 }
259 }
260 }
|
118 }
119 }
120 return getContext(domain, urls, env);
121 }
122
123 /*
124 * Returns all the servers specified in a set of URLs.
125 * If a URL has no host (or port), the servers configured on the
126 * underlying platform are used if possible. If no configured
127 * servers can be found, then fall back to the old behavior of
128 * using "localhost".
129 * There must be at least one URL.
130 */
131 private static String[] serversForUrls(DnsUrl[] urls)
132 throws NamingException {
133
134 if (urls.length == 0) {
135 throw new ConfigurationException("DNS pseudo-URL required");
136 }
137
138 List<String> servers = new ArrayList<String>();
139
140 for (int i = 0; i < urls.length; i++) {
141 String server = urls[i].getHost();
142 int port = urls[i].getPort();
143
144 if (server == null && port < 0) {
145 // No server or port given, so look to underlying platform.
146 // ResolverConfiguration does some limited caching, so the
147 // following is reasonably efficient even if called rapid-fire.
148 List<String> platformServers = filterNameServers(
149 ResolverConfiguration.open().nameservers(), false);
150 if (!platformServers.isEmpty()) {
151 servers.addAll(platformServers);
152 continue; // on to next URL (if any, which is unlikely)
153 }
154 }
155
156 if (server == null) {
157 server = "localhost";
158 }
159 servers.add((port < 0)
160 ? server
161 : server + ":" + port);
162 }
163 return servers.toArray(new String[servers.size()]);
164 }
165
166 /*
167 * Returns true if serversForUrls(urls) would make use of servers
168 * from the underlying platform.
169 */
170 private static boolean platformServersUsed(DnsUrl[] urls) {
171 if (!platformServersAvailable()) {
172 return false;
173 }
174 for (int i = 0; i < urls.length; i++) {
175 if (urls[i].getHost() == null &&
176 urls[i].getPort() < 0) {
177 return true;
178 }
179 }
180 return false;
181 }
182
183 /*
206 buf.append("dns://").append(servers[i]).append(path);
207 }
208 return buf.toString();
209 }
210
211 /*
212 * Reads environment to find URL(s) of initial context.
213 * Default URL is "dns:".
214 */
215 private static String getInitCtxUrl(Hashtable env) {
216 String url = (String) env.get(Context.PROVIDER_URL);
217 return ((url != null) ? url : DEFAULT_URL);
218 }
219
220 /**
221 * Removes any DNS server that's not permitted to access
222 * @param input the input server[:port] list, must not be null
223 * @param oneIsEnough return output once there exists one ok
224 * @return the filtered list, all non-permitted input removed
225 */
226 private static List<String> filterNameServers(List<String> input, boolean oneIsEnough) {
227 SecurityManager security = System.getSecurityManager();
228 if (security == null || input == null || input.isEmpty()) {
229 return input;
230 } else {
231 List<String> output = new ArrayList<String>();
232 for (String platformServer : input) {
233 int colon = platformServer.indexOf(':', platformServer.indexOf(']') + 1);
234 int p = (colon < 0)
235 ? DEFAULT_PORT
236 : Integer.parseInt(platformServer.substring(colon + 1));
237 String s = (colon < 0)
238 ? platformServer : platformServer.substring(0, colon);
239 try {
240 security.checkConnect(s, p);
241 output.add(platformServer);
242 if (oneIsEnough) {
243 return output;
244 }
245 } catch (SecurityException se) {
246 continue;
247 }
248 }
249 return output;
250 }
251 }
252 }
|