Java tutorial
/** * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved. * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * http://www.ewcms.com */ package com.sshdemo.common.extendds.generate.factory.bean; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Service; import com.sshdemo.common.extendds.generate.factory.DataSourceFactoryable; import com.sshdemo.common.extendds.generate.service.EwcmsDataSourceServiceable; import com.sshdemo.common.extendds.model.BaseDS; import com.sshdemo.common.extendds.model.BeanDS; import com.sshdemo.common.report.BaseRuntimeException; /** * Bean?? * * @author ? */ @Service public class BeanDataSourceFactory implements DataSourceFactoryable, ApplicationContextAware { private static final Logger logger = LoggerFactory.getLogger(BeanDataSourceFactory.class); ApplicationContext ctx; public BeanDataSourceFactory() { super(); } @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { this.ctx = ctx; } @Override public EwcmsDataSourceServiceable createService(BaseDS alqcDataSource) { if (!(alqcDataSource instanceof BeanDS)) { logger.error("Bean?"); throw new BaseRuntimeException("Bean??", new Object[] { alqcDataSource.getClass() }); } BeanDS beanDataSource = (BeanDS) alqcDataSource; Object bean = ctx.getBean(beanDataSource.getBeanName()); if (bean == null) { logger.error("Bean???"); throw new BaseRuntimeException("Bean???", new Object[] { beanDataSource.getBeanName() }); } if (beanDataSource.getBeanMethod() == null) { if (!(bean instanceof EwcmsDataSourceServiceable)) { logger.error("Bean???EwcmsDataSourceServiceable"); throw new BaseRuntimeException("Bean???EwcmsDataSourceServiceable", new Object[] { beanDataSource.getBeanName() }); } else { return (EwcmsDataSourceServiceable) bean; } } else { Method serviceMethod; try { // serviceMethod = bean.getClass().getMethod(beanDataSource.getBeanMethod(),null); // return (ReportDataSourceServiceable)serviceMethod.invoke(bean,null); serviceMethod = bean.getClass().getMethod(beanDataSource.getBeanMethod(), new Class[0]); return (EwcmsDataSourceServiceable) serviceMethod.invoke(bean, new Object[0]); } catch (SecurityException e) { logger.error("SecurityException", e); throw new BaseRuntimeException(e); } catch (NoSuchMethodException e) { logger.error("bean??", e); throw new BaseRuntimeException("bean??", new Object[] { beanDataSource.getBeanName(), beanDataSource.getBeanMethod() }); } catch (IllegalArgumentException e) { logger.error("IllegalArgumentException", e); throw new BaseRuntimeException(e); } catch (IllegalAccessException e) { logger.error("IllegalAccessException", e); throw new BaseRuntimeException(e); } catch (InvocationTargetException e) { logger.error("InvocationTargetException", e.getMessage()); throw new BaseRuntimeException(e); } } } }