View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.base;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import java.util.logging.Level;
36  import java.util.logging.Logger;
37  import javax.annotation.Nullable;
38  
39  
40  /**
41   * @author zoly
42   */
43  @SuppressFBWarnings("ACEM_ABSTRACT_CLASS_EMPTY_METHODS")
44  public abstract class AbstractRunnable implements Runnable {
45  
46    @Deprecated
47    public static final int ERROR_EXIT_CODE = SysExits.EX_SOFTWARE.exitCode();
48  
49  
50    public static final Runnable NOP = () -> { };
51  
52    private final boolean lenient;
53  
54    private final String threadName;
55  
56    /**
57     * Create runnable lenient or not with a specific thread name during its execution.
58     *
59     * @param lenient - If lenient is true, it means that nobody is waiting for this runnable's result(finish) so To not
60     * loose the exception, the runnable will LOG it as an error, and not retrow it
61     * @param threadName - the thread name during the execution of this runnable.
62     */
63    public AbstractRunnable(final boolean lenient, @Nullable final String threadName) {
64      this.lenient = lenient;
65      this.threadName = threadName;
66    }
67  
68    /**
69     * create runnable.
70     *
71     * @param lenient - If lenient is true, it means that nobody is waiting for this runnable's result(finish) so To not
72     * loose the exception, the runnable will LOG it as an error, and not retrow it
73     */
74    public AbstractRunnable(final boolean lenient) {
75      this(lenient, null);
76    }
77  
78    public AbstractRunnable() {
79      this(false, null);
80    }
81  
82    public AbstractRunnable(final String threadName) {
83      this(false, threadName);
84    }
85  
86    @Override
87    public final void run() {
88      Thread thread = null;
89      String origName = null;
90      if (threadName != null) {
91        thread = Thread.currentThread();
92        origName = thread.getName();
93        thread.setName(threadName);
94      }
95  
96      try {
97        doRun();
98      } catch (Exception ex) {
99        if (org.spf4j.base.Throwables.containsNonRecoverable(ex)) {
100         Runtime.goDownWithError(ex, SysExits.EX_SOFTWARE);
101       }
102       if (lenient) {
103         Logger.getLogger(AbstractRunnable.class.getName()).log(Level.SEVERE, "Exception in runnable: ", ex);
104       } else {
105         throw new UncheckedExecutionException(ex);
106       }
107     } catch (Throwable ex) {
108       if (org.spf4j.base.Throwables.containsNonRecoverable(ex)) {
109         Runtime.goDownWithError(ex, SysExits.EX_SOFTWARE);
110       }
111       throw ex;
112     } finally {
113       if (thread != null) {
114         thread.setName(origName);
115       }
116     }
117   }
118 
119   public abstract void doRun() throws Exception;
120 
121 }