Coverage Report - org.apache.camel.spring.util.MainRunner
 
Classes in this File Line Coverage Branch Coverage Complexity
MainRunner
0% 
0% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.spring.util;
 18  
 
 19  
 import java.lang.reflect.InvocationTargetException;
 20  
 import java.lang.reflect.Method;
 21  
 import java.lang.reflect.Modifier;
 22  
 import java.util.Arrays;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 import org.springframework.beans.factory.InitializingBean;
 28  
 
 29  
 import static org.apache.camel.util.ObjectHelper.name;
 30  
 
 31  
 /**
 32  
  * A simple helper bean for running main classes from within the spring.xml
 33  
  * usually asynchronous in a background thread; which is useful for demos such
 34  
  * as running Swing programs in the same JVM.
 35  
  * 
 36  
  * @version $Revision: $
 37  
  */
 38  0
 public class MainRunner implements InitializingBean, Runnable {
 39  0
     private static final Log LOG = LogFactory.getLog(MainRunner.class);
 40  
 
 41  
     private Class main;
 42  0
     private String[] args = {};
 43  0
     private boolean asyncRun = true;
 44  
     private long delay;
 45  
 
 46  
     public String toString() {
 47  0
         return "MainRunner(" + name(main) + " " + Arrays.asList(getArgs()) + ")";
 48  
     }
 49  
 
 50  
     public void run() {
 51  
         try {
 52  0
             runMethodWithoutCatchingExceptions();
 53  0
         } catch (NoSuchMethodException e) {
 54  0
             LOG.error("Class: " + name(main) + " does not have a main method: " + e, e);
 55  0
         } catch (IllegalAccessException e) {
 56  0
             LOG.error("Failed to run: " + this + ". Reason: " + e, e);
 57  0
         } catch (InvocationTargetException e) {
 58  0
             Throwable throwable = e.getTargetException();
 59  0
             LOG.error("Failed to run: " + this + ". Reason: " + throwable, throwable);
 60  0
         }
 61  0
     }
 62  
 
 63  
     public void runMethodWithoutCatchingExceptions() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
 64  0
         if (delay > 0) {
 65  
             try {
 66  0
                 Thread.sleep(delay);
 67  0
             } catch (InterruptedException e) {
 68  0
                 LOG.info("Caught: " + e, e);
 69  0
             }
 70  
         }
 71  0
         Method method = main.getMethod("main", String[].class);
 72  0
         if (!Modifier.isStatic(method.getModifiers())) {
 73  0
             throw new IllegalArgumentException("The main method is not static!: " + method);
 74  
         }
 75  0
         Object[] arguments = {getArgs()};
 76  0
         method.invoke(null, arguments);
 77  0
     }
 78  
 
 79  
     public String[] getArgs() {
 80  0
         return args;
 81  
     }
 82  
 
 83  
     public void setArgs(String[] args) {
 84  0
         this.args = args;
 85  0
     }
 86  
 
 87  
     public boolean isAsyncRun() {
 88  0
         return asyncRun;
 89  
     }
 90  
 
 91  
     public void setAsyncRun(boolean asyncRun) {
 92  0
         this.asyncRun = asyncRun;
 93  0
     }
 94  
 
 95  
     public Class getMain() {
 96  0
         return main;
 97  
     }
 98  
 
 99  
     public void setMain(Class main) {
 100  0
         this.main = main;
 101  0
     }
 102  
 
 103  
     public long getDelay() {
 104  0
         return delay;
 105  
     }
 106  
 
 107  
     public void setDelay(long delay) {
 108  0
         this.delay = delay;
 109  0
     }
 110  
 
 111  
     public void afterPropertiesSet() throws Exception {
 112  0
         if (main == null) {
 113  0
             throw new IllegalArgumentException("You must specify a main class!");
 114  
         }
 115  0
         if (isAsyncRun()) {
 116  0
             Thread thread = new Thread(this, "Thread for: " + this);
 117  0
             thread.start();
 118  0
         } else {
 119  0
             runMethodWithoutCatchingExceptions();
 120  
         }
 121  0
     }
 122  
 }