1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. 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 | package org.apache.hadoop.fs; |
---|
19 | |
---|
20 | import java.net.URLStreamHandlerFactory; |
---|
21 | import java.util.HashMap; |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | import org.apache.hadoop.conf.Configuration; |
---|
25 | |
---|
26 | /** |
---|
27 | * Factory for URL stream handlers. |
---|
28 | * |
---|
29 | * There is only one handler whose job is to create UrlConnections. A |
---|
30 | * FsUrlConnection relies on FileSystem to choose the appropriate FS |
---|
31 | * implementation. |
---|
32 | * |
---|
33 | * Before returning our handler, we make sure that FileSystem knows an |
---|
34 | * implementation for the requested scheme/protocol. |
---|
35 | */ |
---|
36 | public class FsUrlStreamHandlerFactory implements |
---|
37 | URLStreamHandlerFactory { |
---|
38 | |
---|
39 | // The configuration holds supported FS implementation class names. |
---|
40 | private Configuration conf; |
---|
41 | |
---|
42 | // This map stores whether a protocol is know or not by FileSystem |
---|
43 | private Map<String, Boolean> protocols = new HashMap<String, Boolean>(); |
---|
44 | |
---|
45 | // The URL Stream handler |
---|
46 | private java.net.URLStreamHandler handler; |
---|
47 | |
---|
48 | public FsUrlStreamHandlerFactory() { |
---|
49 | this.conf = new Configuration(); |
---|
50 | // force the resolution of the configuration files |
---|
51 | // this is required if we want the factory to be able to handle |
---|
52 | // file:// URLs |
---|
53 | this.conf.getClass("fs.file.impl", null); |
---|
54 | this.handler = new FsUrlStreamHandler(this.conf); |
---|
55 | } |
---|
56 | |
---|
57 | public FsUrlStreamHandlerFactory(Configuration conf) { |
---|
58 | this.conf = new Configuration(conf); |
---|
59 | // force the resolution of the configuration files |
---|
60 | this.conf.getClass("fs.file.impl", null); |
---|
61 | this.handler = new FsUrlStreamHandler(this.conf); |
---|
62 | } |
---|
63 | |
---|
64 | public java.net.URLStreamHandler createURLStreamHandler(String protocol) { |
---|
65 | if (!protocols.containsKey(protocol)) { |
---|
66 | boolean known = |
---|
67 | (conf.getClass("fs." + protocol + ".impl", null) != null); |
---|
68 | protocols.put(protocol, known); |
---|
69 | } |
---|
70 | if (protocols.get(protocol)) { |
---|
71 | return handler; |
---|
72 | } else { |
---|
73 | // FileSystem does not know the protocol, let the VM handle this |
---|
74 | return null; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | } |
---|