package org.o3project.odenos.core.logging.message;

import java.text.MessageFormat;
import org.apache.logging.log4j.message.Message;
import java.util.Random;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.ByteBuffer;
import javax.xml.bind.DatatypeConverter;

import org.apache.logging.log4j.ThreadContext;

/**
 * Logging message DTO builder.
 */
public class LogMessage {

  private static int txidOffset = 0;
  private static int txidSerial = 0;

  private static Random rnd = new Random(System.currentTimeMillis());

  private String txid = null;
  private Object[] parameters;
  private String format = null;

  /**
   * Alloc ThreadLocal variable.
   *
   */
  private static ThreadLocal<String> savedTxid = new ThreadLocal<String>() {
    @Override
    protected String initialValue() {
      return new String();
    }
  };

  /**
   * Init parameters.
   *
   * @param offset base number of transactioin ID
   */
  public static void initParameters(int offset) {
    txidOffset = offset;
  }

  /**
   * Sets a transaction ID (txid).
   * 
   * @param txid a transaction ID
   * @return LogMessage
   */
  public LogMessage setTxid(String txid) {
    this.txid = txid;
    return this;
  }

  /**
   * Sets a log message as a plain text.
   * 
   * @param msg string of log message
   * @return LogMessage
   */
  public LogMessage setMessage(String msg) {
    this.parameters = new Object[1];
    this.parameters[0] = (Object) msg;
    return this;
  }

  /**
   * Sets a formatted log message.
   * 
   * @param fmt format
   * @param msg message parameter
   * @return LogMessage
   */
  public LogMessage setFormatedMessage(String fmt, Object... msg) {
    this.format = fmt;
    int len = msg.length;
    this.parameters = new Object[len];
    for (int i = 0; i < len; i++) {
      this.parameters[i] = msg[i];
    }
    return this;
  }

  /**
   * Set a transactionID.
   * @param id transaction ID
   */
  public static void setSavedTxid(String id) {
    String txid = id;
    if(txid == null || txid.length() <= 1) {
      txid = createTxid();
    }
    savedTxid.set(txid);
    ThreadContext.put("txid", txid);
  }

  /**
   * Get a transactionID.
   *
   * @return saved transaction ID
   */
  public static String getSavedTxid() {
    return savedTxid.get();
  }

  /**
   * Create a transactionID (txid).
   *
   * @return created transaction ID
   */
  public static String createTxid() {
    String uuid = "";

    txidSerial++;
    int serial = txidOffset + txidSerial;

    int ethNum = 9;
    try {
      for(int i = 0; i <= ethNum; i++) {
        NetworkInterface nic = NetworkInterface.getByName("eth" + i);
        if(nic != null) {
          uuid = DatatypeConverter.printHexBinary(nic.getHardwareAddress());
          break;
        } 
      }
    } catch (SocketException ex) {
    } finally {
      if(uuid == ""){
        long rLong = rnd.nextLong() & 0xFFFF_FFFF_FFFFL;
        uuid = Long.toHexString(rLong);
      }
    }

    String uuid_str = uuid.toLowerCase() + "-" + String.format("%07d", serial);
    return uuid_str;
  }

  /**
   * Delete a transactionID.
   *
   */
  public static void delSavedTxid() {
    savedTxid.set("");
    ThreadContext.clearAll();
  }
}

@@ -49,89 +49,8 @@
 e;%0A%0A
-import java.text.MessageFormat;%0Aimport org.apache.logging.log4j.message.Message;%0A
 impo
@@ -474,1331 +474,168 @@
 %0A%0A  
-private String txid = null;%0A  private Object%5B%5D parameters;%0A  private String format = null;%0A%0A  /**%0A   * Alloc ThreadLocal variable.%0A   *%0A   */%0A  private static ThreadLocal%3CString%3E savedTxid = new ThreadLocal%3CString%3E() %7B%0A    @Override%0A    protected String initialValue() %7B%0A      return new String();%0A    %7D%0A  %7D;%0A%0A  /**%0A   * Init parameters.%0A   *%0A   * @param offset base number of transactioin ID%0A   */%0A  public static void initParameters(int offset) %7B%0A    txidOffset = offset;%0A  %7D%0A%0A  /**%0A   * Sets a transaction ID (txid).%0A   * %0A   * @param txid a transaction ID%0A   * @return LogMessage%0A   */%0A  public LogMessage setTxid(String txid) %7B%0A    this.txid = txid;%0A    return this;%0A  %7D%0A%0A  /**%0A   * Sets a log message as a plain text.%0A   * %0A   * @param msg string of log message%0A   * @return LogMessage%0A   */%0A  public LogMessage setMessage(String msg) %7B%0A    this.parameters = new Object%5B1%5D;%0A    this.parameters%5B0%5D = (Object) msg;%0A    return this;%0A  %7D%0A%0A  /**%0A   * Sets a formatted log message.%0A   * %0A   * @param fmt format%0A   * @param msg message parameter%0A   * @return LogMessage%0A   */%0A  public LogMessage setFormatedMessage(String fmt, Object... msg) %7B%0A    this.format = fmt;%0A    int len = msg.length;%0A    this.parameters = new Object%5Blen%5D;%0A    for (int i = 0; i %3C len; i++) %7B%0A      this.parameters%5Bi%5D = msg%5Bi%5D;%0A    %7D%0A    return this
+/**%0A   * Init parameters.%0A   *%0A   * @param offset base number of transactioin ID%0A   */%0A  public static void initParameters(int offset) %7B%0A    txidOffset = offset
 ;%0A  
@@ -818,9 +818,9 @@
  %3C= 
-1
+0
 ) %7B%0A
@@ -856,33 +856,8 @@
   %7D%0A
-    savedTxid.set(txid);%0A
     
@@ -1019,30 +1019,133 @@
     
-return savedTxid.get()
+String txid = ThreadContext.get(%22txid%22);%0A    if(txid == null %7C%7C txid.length() %3C= 0) %7B%0A      txid = %22-%22;%0A    %7D%0A    return txid
 ;%0A  
@@ -2017,31 +2017,8 @@
 ) %7B%0A
-    savedTxid.set(%22%22);%0A
     
@@ -2040,11 +2040,11 @@
 lear
-All
+Map
 ();%0A

Delete not use code. Leep same format before.