001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.main; 017 018import java.util.Collections; 019import java.util.Map; 020 021import org.kuali.common.util.spring.SpringExecutable; 022import org.kuali.common.util.spring.service.SpringContext; 023import org.kuali.common.util.spring.service.SpringService; 024 025public class MainUtils { 026 027 public static final String MAIN_CONTEXT_BEAN_NAME = "mainContext"; 028 public static final String MAIN_PROFILE_NAME = "main"; 029 030 /** 031 * Load the @Configuration <code>mainClass</code> using Spring and then terminate the JVM. 032 */ 033 public static void runAndExit(Class<?> mainClass, String[] args) { 034 runAndExit(mainClass, args, false); 035 } 036 037 /** 038 * Load the @Configuration <code>mainClass</code> using Spring and then terminate the JVM. 039 */ 040 public static void runAndExit(Class<?> mainClass, String[] args, boolean stacktrace) { 041 run(mainClass, args, stacktrace, true); 042 } 043 044 /** 045 * Load the @Configuration <code>mainClass</code> using Spring 046 */ 047 public static void run(Class<?> mainClass, String[] args, boolean stacktrace) { 048 run(mainClass, args, stacktrace, false); 049 } 050 051 /** 052 * Load the @Configuration <code>mainClass</code> using Spring 053 */ 054 public static void run(Class<?> mainClass, String[] args) { 055 run(mainClass, args, true, false); 056 } 057 058 /** 059 * 060 */ 061 public static void run(Class<?> mainClass, String[] args, boolean stacktrace, boolean exit) { 062 try { 063 // Preserve the context info from the class where main(String[] args) was invoked 064 MainContext mainContext = new MainContext(mainClass, args); 065 066 // Create a map containing the context so we can register it with Spring 067 Map<String, Object> beans = Collections.singletonMap(MAIN_CONTEXT_BEAN_NAME, (Object) mainContext); 068 069 // Create a SpringContext using the map and main class, with 1 active profile called "main" 070 SpringContext context = new SpringContext(beans, mainClass, MAIN_PROFILE_NAME); 071 072 // DefaultSpringService does what we need 073 SpringService service = SpringExecutable.DEFAULT_SPRING_SERVICE; 074 075 // This causes Spring to load the @Configuration annotated main class 076 new SpringExecutable(service, context).execute(); 077 078 // Exit with zero if there is no exception 079 if (exit) { 080 System.exit(Status.SUCCESS.getValue()); 081 } 082 } catch (Exception e) { 083 handleException(e, stacktrace, exit); 084 } 085 } 086 087 protected static void handleException(Exception e, boolean stacktrace, boolean exit) { 088 if (stacktrace) { 089 e.printStackTrace(); 090 } else { 091 System.err.print(e.getMessage()); 092 } 093 if (exit) { 094 System.exit(Status.FAILURE.getValue()); 095 } 096 } 097 098}