001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.spring.util;
018
019import java.lang.reflect.InvocationTargetException;
020import java.lang.reflect.Method;
021import java.lang.reflect.Modifier;
022import java.util.Arrays;
023
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.springframework.beans.factory.InitializingBean;
027
028import static org.apache.camel.util.ObjectHelper.name;
029
030/**
031 * A simple helper bean for running main classes from within the spring.xml
032 * usually asynchronous in a background thread; which is useful for demos such
033 * as running Swing programs in the same JVM.
034 */
035public class MainRunner implements InitializingBean, Runnable {
036    private static final Logger LOG = LoggerFactory.getLogger(MainRunner.class);
037
038    private Class<?> main;
039    private String[] args = {};
040    private boolean asyncRun = true;
041    private long delay;
042
043    public String toString() {
044        return "MainRunner(" + name(main) + " " + Arrays.asList(getArgs()) + ")";
045    }
046
047    public void run() {
048        try {
049            runMethodWithoutCatchingExceptions();
050        } catch (NoSuchMethodException e) {
051            LOG.error("Class: " + name(main) + " does not have a main method: " + e, e);
052        } catch (IllegalAccessException e) {
053            LOG.error("Failed to run: " + this + ". Reason: " + e, e);
054        } catch (InvocationTargetException e) {
055            Throwable throwable = e.getTargetException();
056            LOG.error("Failed to run: " + this + ". Reason: " + throwable, throwable);
057        }
058    }
059
060    public void runMethodWithoutCatchingExceptions() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
061        if (delay > 0) {
062            try {
063                Thread.sleep(delay);
064            } catch (InterruptedException e) {
065                Thread.currentThread().interrupt();
066            }
067        }
068        Method method = main.getMethod("main", String[].class);
069        if (!Modifier.isStatic(method.getModifiers())) {
070            throw new IllegalArgumentException("The main method is not static!: " + method);
071        }
072        Object[] arguments = {getArgs()};
073        method.invoke(null, arguments);
074    }
075
076    public String[] getArgs() {
077        return args;
078    }
079
080    public void setArgs(String[] args) {
081        this.args = args;
082    }
083
084    public boolean isAsyncRun() {
085        return asyncRun;
086    }
087
088    public void setAsyncRun(boolean asyncRun) {
089        this.asyncRun = asyncRun;
090    }
091
092    public Class<?> getMain() {
093        return main;
094    }
095
096    public void setMain(Class<?> main) {
097        this.main = main;
098    }
099
100    public long getDelay() {
101        return delay;
102    }
103
104    public void setDelay(long delay) {
105        this.delay = delay;
106    }
107
108    public void afterPropertiesSet() throws Exception {
109        if (main == null) {
110            throw new IllegalArgumentException("You must specify a main class!");
111        }
112        if (isAsyncRun()) {
113            Thread thread = new Thread(this, "Thread for: " + this);
114            thread.start();
115        } else {
116            runMethodWithoutCatchingExceptions();
117        }
118    }
119}