/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.logserver;

import alluxio.AlluxioRemoteLogFilter;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.LoggingEvent;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * Reads {@link org.apache.log4j.spi.LoggingEvent} objects from remote logging
 * clients and writes the objects to designated log files. For each logging client,
 * logging server creates a {@link org.apache.log4j.spi.LoggingEvent} object and starts
 * a thread serving the logging requests of the client.
 */
public class AlluxioLog4jSocketNode implements Runnable {
  private AlluxioLogServerProcess mLogServerProcess;
  private Socket mSocket;
  private LoggerRepository mHierarchy;
  private ObjectInputStream mObjectInputStream;

  /**
   * Constructor of {@link AlluxioLog4jSocketNode}.
   *
   * @param process main log server process
   * @param socket client socket from which to read {@link org.apache.log4j.spi.LoggingEvent}
   * @throws IOException if {@link Socket#getInputStream()} encounters an I/O error when creating
   *                     the {@link java.io.InputStream}, the socket is closed, the socket is not
   *                     connected, the socket input has been shutdown with using
   *                     {@link Socket#shutdownInput()}, or an I/O error occurs while reading the
   *                     stream header in the constructor of {@link ObjectInputStream}
   */
  public AlluxioLog4jSocketNode(AlluxioLogServerProcess process, Socket socket)
      throws IOException {
    mLogServerProcess = process;
    mSocket = socket;
    mObjectInputStream = new ObjectInputStream(
        new BufferedInputStream(mSocket.getInputStream()));
  }

  @Override
  public void run() {
    LoggingEvent event;
    Logger remoteLogger;
    if (mObjectInputStream == null) {
      return;
    }
    try {
      while (true) {
        event = (LoggingEvent) mObjectInputStream.readObject();
        if (mHierarchy == null) {
          mHierarchy = mLogServerProcess.configureHierarchy(
              mSocket.getInetAddress(),
              event.getMDC(AlluxioRemoteLogFilter.REMOTE_LOG_MDC_APPENDER_NAME_KEY).toString());
          if (mHierarchy == null) {
            // TODO(yanqin) better handling
            continue;
          }
        }
        remoteLogger = mHierarchy.getLogger(event.getLoggerName());
        if (event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
          remoteLogger.callAppenders(event);
        }
      }
    } catch (Exception e) {
      // TODO(yanqin) attempt to recover.
    } finally {
      if (mObjectInputStream != null) {
        try {
          mObjectInputStream.close();
        } catch (Exception e) {
          // Ignore the exception caused by faulty client.
        }
      }
      if (mSocket != null) {
        try {
          mSocket.close();
        } catch (Exception e) {
          // Ignore the exception caused by closing socket.
        }
      }
    }
  }
}

@@ -1313,56 +1313,8 @@
 chy;
-%0A  private ObjectInputStream mObjectInputStream;
 %0A%0A  
@@ -2147,116 +2147,8 @@
 et;%0A
-    mObjectInputStream = new ObjectInputStream(%0A        new BufferedInputStream(mSocket.getInputStream()));%0A
   %7D%0A
@@ -2231,29 +2231,29 @@
 Logger;%0A    
-if
+try
  (
-m
 ObjectInputS
@@ -2262,46 +2262,110 @@
 eam 
-== null) %7B%0A      return;
+objectInputStream = new ObjectInputStream(
 %0A    
-%7D%0A
     
-try
+new BufferedInputStream(mSocket.getInputStream())))
  %7B%0A 
@@ -2415,18 +2415,17 @@
 gEvent) 
-mO
+o
 bjectInp
@@ -3107,209 +3107,8 @@
 y %7B%0A
-      if (mObjectInputStream != null) %7B%0A        try %7B%0A          mObjectInputStream.close();%0A        %7D catch (Exception e) %7B%0A          // Ignore the exception caused by faulty client.%0A        %7D%0A      %7D%0A
     

Replace a member variable with a local variable.