001 /* 002 * Copyright 2010 the original author or authors. 003 * 004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 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 */ 016 package com.hs.mail.container.simple; 017 018 import java.io.File; 019 020 import org.apache.commons.beanutils.ConstructorUtils; 021 import org.apache.commons.beanutils.MethodUtils; 022 import org.apache.log4j.Logger; 023 024 /** 025 * Simple spring component container 026 * 027 * @author Won Chul Doh 028 * @since Jan 11, 2010 029 */ 030 public class SimpleSpringContainer { 031 032 private static Logger logger = Logger.getLogger(SimpleSpringContainer.class); 033 034 private static final String IMPL_CLASS_NAME = "org.springframework.context.support.FileSystemXmlApplicationContext"; 035 036 protected String[] configLocations; 037 038 protected Object applicationContext; 039 040 public SimpleSpringContainer(String[] configLocations) { 041 super(); 042 this.configLocations = configLocations; 043 } 044 045 public Object createFileSystemXmlApplicationContext(String[] configLocations) 046 throws Exception { 047 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 048 Class klass = classLoader.loadClass(IMPL_CLASS_NAME); 049 050 Object[] args = new Object[] { configLocations, new Boolean(false) }; 051 Object applicationContext = ConstructorUtils.invokeConstructor(klass, args); 052 MethodUtils.invokeMethod(applicationContext, "refresh", null); 053 054 return applicationContext; 055 } 056 057 public void start() throws Exception { 058 this.applicationContext = createFileSystemXmlApplicationContext(this.configLocations); 059 SpringContainerShutdownHook hook = new SpringContainerShutdownHook(this); 060 Runtime.getRuntime().addShutdownHook(hook); 061 } 062 063 public void forceShutdown() { 064 try { 065 MethodUtils.invokeMethod(this.applicationContext, "stop", null); 066 MethodUtils.invokeMethod(this.applicationContext, "close", null); 067 } catch (Exception e) { 068 } 069 } 070 071 /** 072 * @param args 073 */ 074 public static void main(String[] args) { 075 String configLocation = (args.length > 0) ? args[0].trim() 076 : "conf/applicationContext.xml"; 077 078 try { 079 String[] configLocations = new String[1]; 080 configLocations[0] = new File(configLocation).getCanonicalPath(); 081 System.setProperty("app.home", new File(configLocations[0]).getParentFile().getParent()); 082 SimpleSpringContainer container = new SimpleSpringContainer( 083 configLocations); 084 container.start(); 085 } catch (Exception e) { 086 String errMsg = (e.getMessage() != null) ? e.getMessage() : e.getCause().getMessage(); 087 System.err.println(errMsg); 088 logger.fatal(errMsg, e); 089 } 090 } 091 } 092 093 final class SpringContainerShutdownHook extends Thread { 094 private SimpleSpringContainer container; 095 096 protected SpringContainerShutdownHook(SimpleSpringContainer container) { 097 this.container = container; 098 } 099 100 public void run() { 101 this.container.forceShutdown(); 102 } 103 }