/** * Returns current {@link VariableEnvironment} object contains all the variables in current application. */ public static VariableEnvironment container(){ return AppContext.factory().getBean(VariableEnvironment.class); }
/** * Executes the main. */ protected static void run(Class<? extends AppMainRunner> mainClass, Object[] args) { AppContext.initStandalone(); context = AppContext.current(); config = context.getConfig(); factory = context.getBeanFactory(); Try.throwUnchecked(() -> factory.createBean(mainClass).run(args)); }
@Override protected final void setUp() throws Exception { if (null == servletContext) { servletContext = rootServletContext; contextPath = ""; }else{ contextPath = servletContext.getContextPath(); } appContext = null == servletContext ? null : AppContext.get(servletContext); if(null != appContext) { appConfig = appContext.getConfig(); beanFactory = appContext.getBeanFactory(); } this.doSetUp(); }
/** * Returns current application's {@link BeanFactory}. */ public static BeanFactory factory(){ return current().getBeanFactory(); }
/** * @see AppContext#getServletContext() */ public static javax.servlet.ServletContext servletContext() throws IllegalStateException { return current().getServletContext(); }
@Override public BeanFactory factory() { return context.getBeanFactory(); }
@SuppressWarnings("unchecked") private static Map<String,DbPlatform> currentPlatforms(AppContext context){ Map<String, DbPlatform> platforms = (Map<String, DbPlatform>)context.getAttribute(CONTEXT_ATTRIBUTE_NAME); if(null == platforms) { platforms = new ConcurrentHashMap<String, DbPlatform>(); for(DbPlatform p : context.getBeanFactory().getBeans(DbPlatform.class)) { platforms.put(p.getName().toLowerCase(), p); } context.setAttribute(CONTEXT_ATTRIBUTE_NAME, platforms); } return platforms; }
@Override public AppConfig config() { return context.getConfig(); }
@Override public void init(ServletConfig config) throws ServletException { String servletClassName = config.getInitParameter(INIT_PARAM_SERVLET_CLASS); if(Strings.isEmpty(servletClassName)) { throw new ServletException("The init param '" + INIT_PARAM_SERVLET_CLASS + "' must be configured"); } Class<? extends Servlet> servletClass = (Class<? extends Servlet>)Classes.forName(servletClassName); AppContext context = AppContext.get(config.getServletContext()); if(null == context) { throw new ServletException("AppContext must be initialized!"); } this.config = config; servlet = context.getBeanFactory().getOrCreateBean(servletClass); servlet.init(config); }
protected void processBeanCreation(BeanDefinitionBase bd, Object bean) { try { setBeanCurrentlyInCreation(bd, bean); doBeanAware(bd, bean); doBeanConfigure(bd, bean); if(bean instanceof PostConfigureBean){ ((PostConfigureBean) bean).postConfigure(appContext.getBeanFactory(), appContext.getConfig()); } doBeanSetProperties(bd,bean); doBeanInjection(bd,bean); doBeanInvokeMethods(bd,bean); doInitBean(bd, bean); if(bean instanceof LoadableBean){ if(!((LoadableBean) bean).load(appContext.getBeanFactory())){ return; } } } catch (Throwable e) { throw errorCreateBean(bd,e); } if(bd.isSingleton()){ doBeanValidation(bd,bean); bd.setSingletonInstance(bean); } }
AppContext context = AppContext.current(); Map<DataSource,Db> dbs = (Map<DataSource,Db>)context.getAttribute(DB_CACHED_KEY); context.setAttribute(DB_CACHED_KEY, dbs); }else{ db = dbs.get(ds);
public DefaultAopProvider() { AppContext context = AppContext.tryGetCurrent(); if(null != context) { config = context.getConfig().getExtension(AopConfig.class); } }
protected DbBase(String name, DataSource ds, DatabaseMetaData md, DbPlatform platform, DbMetadata metadata, DbDialect dialect, DbComparator comparator){ Args.notEmpty(name); Args.notNull(ds); Args.notNull(md); Args.notNull(platform); Args.notNull(metadata); Args.notNull(dialect); Args.notNull(comparator); this.name = name; this.server = extractServerFromJdbcUrl(md); this.description = metadata.getProductName() + " " + metadata.getProductVersion(); this.dataSourceDescription = Strings.format("url: {0}, username: {1}", Urls.removeQueryString(metadata.getURL()), metadata.getUsername()); this.platform = platform; this.dataSource = ds; this.metadata = metadata; this.dialect = dialect; this.comparator = comparator; AppContext context = AppContext.tryGetCurrent(); if(null != context) { tp = context.getBeanFactory().getBean(TransactionManager.class).getProvider(ds); }else{ tp = null; } this.awareObjects(); this.log = getLog(this.getClass()); this.init(); }
protected static void onInited(AppContext context) { try { context.postInit(); for (AppContextInitializable bean : context.getBeanFactory().getBeans(AppContextInitializable.class)) { bean.postInit(context); } context.getBeanFactory().postInit(context); } catch (Throwable e) { Exceptions.uncheckAndThrow(e); } }
public static SysContext current(){ return (SysContext)AppContext.current().getAttribute(SYS_CONTEXT_ATTRIBUTE_KEY); }
protected void doBeanAware(BeanDefinitionBase bd,Object bean){ if(bean instanceof ServletContextAware){ if(appContext.isServletEnvironment()){ ((ServletContextAware) bean).setServletContext(appContext.getServletContext()); } } if(bean instanceof BeanFactoryAware){ ((BeanFactoryAware) bean).setBeanFactory(beanFactory); } if(bean instanceof AppContextAware){ ((AppContextAware) bean).setAppContext(appContext); } if(bean instanceof AppConfigAware){ ((AppConfigAware) bean).setAppConfig(appContext.getConfig()); } //TODO : check the bean name is empty? if(bean instanceof BeanNameAware){ ((BeanNameAware) bean).setBeanName(bd.getName()); } if(bean instanceof BeanPrimaryAware) { ((BeanPrimaryAware) bean).setBeanPrimary(bd.isPrimary()); } }
/** * Initializes standalone app context if current context did not initialized. * * @see AppContextInitializer#initStandalone(); */ public static AppContext initStandalone(){ if(tryGetCurrent() == null){ AppContextInitializer.initStandalone(); } return current(); }
@Override public void run(ApplicationArguments args) throws Exception { AppContextInitializer.setBasePackage(Global.bp); AppContext.initStandalone(); final AppContext context = AppContext.current(); Global.leap = new Global.LeapContext() { @Override public AppConfig config() { return context.getConfig(); } @Override public BeanFactory factory() { return context.getBeanFactory(); } @Override public AppContext context() { return context; } }; }
@Override public AppContext getAppContext() { return AppContext.current(); }