All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.gateway.services.hostmap.FileBasedHostMapper Maven / Gradle / Ivy

Go to download

The Service Provider Interface for extending the capabilities of the gateway.

There is a newer version: 2.0.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.gateway.services.hostmap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class FileBasedHostMapper implements HostMapper {

  private Map inbound = new HashMap();
  private Map outbound = new HashMap();

  public FileBasedHostMapper( URL url ) throws IOException {
    if( url != null ) {
      InputStream stream = url.openStream();
      BufferedReader reader = new BufferedReader( new InputStreamReader( stream ) );
      String line = reader.readLine();
      while( line != null ) {
        String[] lineSplit = line.split( "=" );
        if( lineSplit.length >= 2 ) {
          String[] externalSplit = lineSplit[ 0 ].split( "," );
          String[] internalSplit = lineSplit[ 1 ].split( "," );
          if( externalSplit.length >= 1 && internalSplit.length >= 1 ) {
            for( String external : externalSplit ) {
              inbound.put( external.trim(), internalSplit[ 0 ].trim() );
            }
            for( String internal : internalSplit ) {
              outbound.put( internal.trim(), externalSplit[ 0 ].trim() );
            }
          }
        }
        line = reader.readLine();
      }
      reader.close();
    }
  }

  /* (non-Javadoc)
   * @see org.apache.hadoop.gateway.services.hostmap.HostMapper#resolveInboundHostName(java.lang.String)
   */
  @Override
  public String resolveInboundHostName( String hostName ) {
    String resolvedHostName = inbound.get( hostName );
    if( resolvedHostName == null ) {
      resolvedHostName = hostName;
    }
    return resolvedHostName;
  }

  /* (non-Javadoc)
   * @see org.apache.hadoop.gateway.services.hostmap.HostMapper#resolveOutboundHostName(java.lang.String)
   */
  @Override
  public String resolveOutboundHostName( String hostName ) {
    String resolvedHostName = outbound.get( hostName );
    if( resolvedHostName == null ) {
      resolvedHostName = hostName;
    }
    return resolvedHostName;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy