/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.commons.schedule.executor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Invoke given method of given object.
 *
 * @author Sergii Kabashniuk
 */
public class LoggedRunnable implements Runnable {
  private static final Logger LOG = LoggerFactory.getLogger(LoggedRunnable.class);

  private final Object object;
  private final Method method;

  public LoggedRunnable(Object object, Method method) {
    this.object = object;
    this.method = method;
  }

  @Override
  public void run() {
    long startTime = System.currentTimeMillis();
    try {
      if (object instanceof Runnable
          && method.getName().equals("run")
          && method.getParameterTypes().length == 0) {
        LOG.debug(
            "Invoking method run of class {} instance {}", object.getClass().getName(), object);

        ((Runnable) object).run();

        LOG.debug(
            "Method of class {} instance {} complete  at {}  sec",
            object.getClass().getName(),
            object,
            TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime));
      } else {
        try {
          LOG.debug(
              "Invoking run method of class {} instance {}", object.getClass().getName(), object);

          method.invoke(object);

          LOG.debug(
              "Method of class {} instance {} complete  at {}  sec",
              object.getClass().getName(),
              object,
              TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime));
        } catch (InvocationTargetException | IllegalAccessException e) {
          LOG.error(e.getMessage(), e);
        }
      }
    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
      throw e;
    }
  }
}

@@ -1235,19 +1235,21 @@
  method 
+'
 run
+'
  of clas
@@ -1242,34 +1242,36 @@
  'run' of class 
+'
 %7B%7D
+'
  instance %7B%7D%22, o
@@ -1256,36 +1256,50 @@
 s '%7B%7D' instance 
+'
 %7B%7D
+'
 %22,
+%0A           
  object.getClass
@@ -1303,32 +1303,44 @@
 ass().getName(),
+%0A           
  object);%0A%0A     
@@ -1410,34 +1410,36 @@
 Method of class 
+'
 %7B%7D
+'
  instance %7B%7D com
@@ -1424,34 +1424,39 @@
 s '%7B%7D' instance 
+'
 %7B%7D
+' is
  complete  at %7B%7D
@@ -1444,32 +1444,31 @@
  is complete
-  at
+d in
  %7B%7D
- 
  sec%22,%0A     
@@ -1687,18 +1687,19 @@
 ing 
-run 
 method
+ '%7B%7D'
  of 
@@ -1696,34 +1696,36 @@
 d '%7B%7D' of class 
+'
 %7B%7D
+'
  instance %7B%7D%22, o
@@ -1722,12 +1722,60 @@
 nce 
+'
 %7B%7D
+'
 %22,
+%0A              method.getName(),%0A             
  obj
@@ -1799,16 +1799,30 @@
 tName(),
+%0A             
  object)
@@ -1910,18 +1910,20 @@
 f class 
+'
 %7B%7D
+'
  instanc
@@ -1924,18 +1924,23 @@
 nstance 
+'
 %7B%7D
+' is
  complet
@@ -1944,16 +1944,15 @@
 lete
-  at
+d in
  %7B%7D
- 
  sec
@@ -2192,32 +2192,239 @@
       LOG.error(
+%0A              %22Error occurred during invocation of method '%7B%7D#%7B%7D'. Instance: '%7B%7D'. Error: %7B%7D%22,%0A              object.getClass().getName(),%0A              method.getName(),%0A              object,%0A              
 e.getMessage(), 
@@ -2414,32 +2414,46 @@
  e.getMessage(),
+%0A             
  e);%0A        %7D%0A 
@@ -2503,16 +2503,203 @@
 G.error(
+%0A          %22Error occurred during invocation of method '%7B%7D#%7B%7D'. Instance: '%7B%7D'. Error: %7B%7D%22,%0A          object.getClass().getName(),%0A          method.getName(),%0A          object,%0A          
 e.getMes
@@ -2705,16 +2705,26 @@
 ssage(),
+%0A         
  e);%0A   
@@ -2744,11 +2744,208 @@
    %7D%0A  %7D
+%0A%0A  @Override%0A  public String toString() %7B%0A    return %22LoggedRunnable%7B%22%0A        + %22methodToInvoke=%22%0A        + object.getClass().getName()%0A        + '#'%0A        + method.getName()%0A        + '%7D';%0A  %7D
 %0A%7D%0A

Fix logged messages in commons schedule module (#12283)