001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.yarn.api.records;
020
021import com.google.common.annotations.VisibleForTesting;
022
023import java.net.URI;
024import java.net.URISyntaxException;
025
026import org.apache.hadoop.classification.InterfaceAudience.Public;
027import org.apache.hadoop.classification.InterfaceAudience.Private;
028import org.apache.hadoop.classification.InterfaceStability.Stable;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider;
032import org.apache.hadoop.yarn.util.Records;
033
034/**
035 * <p><code>URL</code> represents a serializable {@link java.net.URL}.</p>
036 */
037@Public
038@Stable
039public abstract class URL {
040
041  @Public
042  @Stable
043  public static URL newInstance(String scheme, String host, int port, String file) {
044    URL url = Records.newRecord(URL.class);
045    url.setScheme(scheme);
046    url.setHost(host);
047    url.setPort(port);
048    url.setFile(file);
049    return url;
050  }
051
052  /**
053   * Get the scheme of the URL.
054   * @return scheme of the URL
055   */
056  @Public
057  @Stable
058  public abstract String getScheme();
059
060  /**
061   * Set the scheme of the URL
062   * @param scheme scheme of the URL
063   */
064  @Public
065  @Stable
066  public abstract void setScheme(String scheme);
067
068  /**
069   * Get the user info of the URL.
070   * @return user info of the URL
071   */
072  @Public
073  @Stable
074  public abstract String getUserInfo();
075
076  /**
077   * Set the user info of the URL.
078   * @param userInfo user info of the URL
079   */
080  @Public
081  @Stable
082  public abstract void setUserInfo(String userInfo);
083
084  /**
085   * Get the host of the URL.
086   * @return host of the URL
087   */
088  @Public
089  @Stable
090  public abstract String getHost();
091
092  /**
093   * Set the host of the URL.
094   * @param host host of the URL
095   */
096  @Public
097  @Stable
098  public abstract void setHost(String host);
099
100  /**
101   * Get the port of the URL.
102   * @return port of the URL
103   */
104  @Public
105  @Stable
106  public abstract int getPort();
107
108  /**
109   * Set the port of the URL
110   * @param port port of the URL
111   */
112  @Public
113  @Stable
114  public abstract void setPort(int port);
115
116  /**
117   * Get the file of the URL.
118   * @return file of the URL
119   */
120  @Public
121  @Stable
122  public abstract String getFile();
123
124  /**
125   * Set the file of the URL.
126   * @param file file of the URL
127   */
128  @Public
129  @Stable
130  public abstract void setFile(String file);
131
132  @Public
133  @Stable
134  public Path toPath() throws URISyntaxException {
135    return new Path(new URI(getScheme(), getUserInfo(),
136      getHost(), getPort(), getFile(), null, null));
137  }
138
139
140  @Private
141  @VisibleForTesting
142  public static URL fromURI(URI uri, Configuration conf) {
143    URL url =
144        RecordFactoryProvider.getRecordFactory(conf).newRecordInstance(
145            URL.class);
146    if (uri.getHost() != null) {
147      url.setHost(uri.getHost());
148    }
149    if (uri.getUserInfo() != null) {
150      url.setUserInfo(uri.getUserInfo());
151    }
152    url.setPort(uri.getPort());
153    url.setScheme(uri.getScheme());
154    url.setFile(uri.getPath());
155    return url;
156  }
157
158  @Public
159  @Stable
160  public static URL fromURI(URI uri) {
161    return fromURI(uri, null);
162  }
163
164  @Private
165  @VisibleForTesting
166  public static URL fromPath(Path path, Configuration conf) {
167    return fromURI(path.toUri(), conf);
168  }
169
170  @Public
171  @Stable
172  public static URL fromPath(Path path) {
173    return fromURI(path.toUri());
174  }
175}