/**
 * 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 com.cloudera.llama.am.server.thrift;

import com.cloudera.llama.am.impl.FastFormat;
import com.cloudera.llama.am.server.AbstractServer;
import org.apache.hadoop.conf.Configuration;
import org.apache.thrift.TProcessor;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportFactory;

import javax.security.auth.Subject;
import java.net.InetAddress;
import java.security.PrivilegedExceptionAction;

public abstract class ThriftServer<T extends TProcessor> extends
    AbstractServer {
  private TServer tServer;
  private TServerSocket tServerSocket;
  private Subject subject;

  protected ThriftServer(String serviceName) {
    super(serviceName);
  }

  @Override
  public void setConf(Configuration conf) {
    if (conf.get(ServerConfiguration.CONFIG_DIR_KEY) == null) {
      throw new RuntimeException(FastFormat.format(
          "Required configuration property '{}' missing", 
          ServerConfiguration.CONFIG_DIR_KEY));
    }
    super.setConf(conf);
  }

  @Override
  protected void startTransport() {
    try {
      subject = Security.loginServerSubject(getConf());
      Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
        @Override
        public Object run() throws Exception {
          int minThreads = getConf().getInt(ServerConfiguration
              .SERVER_MIN_THREADS_KEY, ServerConfiguration
              .SERVER_MIN_THREADS_DEFAULT);
          int maxThreads = getConf().getInt(ServerConfiguration
              .SERVER_MAX_THREADS_KEY, ServerConfiguration
              .SERVER_MAX_THREADS_DEFAULT);
          tServerSocket = ThriftEndPoint.createTServerSocket(getConf());
          TTransportFactory tTransportFactory = ThriftEndPoint
              .createTTransportFactory(getConf());
          T processor = createServiceProcessor();
          TThreadPoolServer.Args args = new TThreadPoolServer.Args
              (tServerSocket);
          args.transportFactory(tTransportFactory);
          args = args.minWorkerThreads(minThreads);
          args = args.maxWorkerThreads(maxThreads);
          args = args.processor(processor);
          tServer = new TThreadPoolServer(args);
          tServer.serve();
          return null;
        }
      });
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  protected void stopTransport() {
    tServer.stop();
    Security.logout(subject);
  }

  @Override
  public String getAddressHost() {
    return (tServerSocket != null && tServerSocket.getServerSocket().isBound()) 
           ? getHostname(tServerSocket.getServerSocket().getInetAddress().
        getHostName()) : null;
  }

  @Override
  public int getAddressPort() {
    return (tServerSocket != null && tServerSocket.getServerSocket().isBound())
           ? tServerSocket.getServerSocket().getLocalPort() : 0;
  }
  
  protected abstract T createServiceProcessor();

  static String getHostname(String address) {
    try {
      if (address.startsWith("0.0.0.0")) {
        address = InetAddress.getLocalHost().getCanonicalHostName();
      } else {
        int i = address.indexOf(":");
        if (i > -1) {
          address = address.substring(0, i);
        }
      }
      return address;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }

}

@@ -1515,16 +1515,63 @@
 subject;
+%0A  private String hostname;%0A  private int port;
 %0A%0A  prot
@@ -3391,16 +3391,29 @@
  public 
+synchronized 
 String g
@@ -3426,38 +3426,71 @@
 essHost() %7B%0A    
-return
+if (hostname == null) %7B%0A     hostname =
  (tServerSocket 
@@ -3492,32 +3492,42 @@
 cket != null && 
+%0A         
 tServerSocket.ge
@@ -3565,16 +3565,21 @@
         
+     
 ? getHos
@@ -3616,16 +3616,37 @@
 ocket().
+%0A                    
 getInetA
@@ -3654,25 +3654,16 @@
 dress().
-%0A        
 getHostN
@@ -3673,14 +3673,58 @@
 ()) 
-: null
+%0A                : null;%0A    %7D%0A    return hostname
 ;%0A  
@@ -3747,16 +3747,29 @@
  public 
+synchronized 
 int getA
@@ -3787,22 +3787,45 @@
 ) %7B%0A    
-return
+if (port == 0) %7B%0A      port =
  (tServe
@@ -3842,16 +3842,26 @@
  null &&
+%0A         
  tServer
@@ -3896,16 +3896,18 @@
 ound())%0A
+  
         
@@ -3963,16 +3963,39 @@
 () : 0;%0A
+    %7D%0A    return port;%0A
   %7D%0A  %0A 

Fix for flaky failure in TestClientNotifier