Example usage for org.springframework.context ApplicationContext getClass

List of usage examples for org.springframework.context ApplicationContext getClass

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.jacpfx.vertx.spring.VertxLifecycleAspect.java

/**
 * When a verticle will be stopped the stop() method will be executed.
 * In this case check if there is a running spring context, if so close it.
 * @param joinPoint the verticle stop method
 */// w w w .j a va 2  s  .  co m
@After(value = "execution(* io.vertx.core.Verticle+.stop())")
public void afterStop(JoinPoint joinPoint) {
    final Object target = joinPoint.getTarget();
    log.debug("Stop invoked - Terminating spring context for verticle");
    if (target.getClass().isAnnotationPresent(SpringVerticle.class)) {
        if (AnnotationConfigApplicationContext.class.isAssignableFrom(context.getClass())) {
            final ApplicationContext parent = AnnotationConfigApplicationContext.class.cast(context)
                    .getParent();
            if (parent == null) {
                AnnotationConfigApplicationContext.class.cast(context).stop();
            } else {
                if (GenericApplicationContext.class.isAssignableFrom(parent.getClass())) {
                    GenericApplicationContext.class.cast(parent).stop();
                }
            }
        }
    }

}

From source file:com.alibaba.dubbo.config.spring.ServiceBean.java

public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
    SpringExtensionFactory.addApplicationContext(applicationContext);
    if (applicationContext != null) {
        SPRING_CONTEXT = applicationContext;
        try {//from   w  w w . ja v  a2 s  .  c om
            Method method = applicationContext.getClass().getMethod("addApplicationListener",
                    new Class<?>[] { ApplicationListener.class }); // Spring2.0.1
            method.invoke(applicationContext, new Object[] { this });
            supportedApplicationListener = true;
        } catch (Throwable t) {
            if (applicationContext instanceof AbstractApplicationContext) {
                try {
                    Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener",
                            new Class<?>[] { ApplicationListener.class }); // Spring2.0.1
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(applicationContext, new Object[] { this });
                    supportedApplicationListener = true;
                } catch (Throwable t2) {
                }
            }
        }
    }
}

From source file:com.sinosoft.one.mvc.MvcFilter.java

/**
 *  ApplicationContext WEB-INF?WEB-INF/classes?
 * jarspring???? ApplicationContext //  w  ww  .j  a va 2  s .c om
 * 
 * @return
 * @throws IOException
 */
private WebApplicationContext prepareRootApplicationContext() throws IOException {

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] starting ...");
    }

    ApplicationContext oldRootContext = (ApplicationContext) getServletContext()
            .getAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

    // web.xml?springrootcontext ...... ??
    // mvcFilter?????rootContext?????
    // ?Listenerinit mvc context
    if (oldRootContext != null) {
        if (oldRootContext.getClass() != MvcWebAppContext.class) {
            throw new IllegalStateException(
                    "Cannot initialize context because there is already a root application context present - "
                            + "check whether you have multiple ContextLoader* definitions in your web.xml!");
        }
        if (logger.isInfoEnabled()) {
            logger.info("[init/rootContext] the root context exists:" + oldRootContext);
        }
        return (MvcWebAppContext) oldRootContext;
    }

    MvcWebAppContext rootContext = new MvcWebAppContext(getServletContext(), load, false);

    String contextConfigLocation = this.contextConfigLocation;
    // applicationContext?
    if (StringUtils.isBlank(contextConfigLocation)) {
        String webxmlContextConfigLocation = getServletContext().getInitParameter("contextConfigLocation");
        if (StringUtils.isBlank(webxmlContextConfigLocation)) {
            contextConfigLocation = MvcWebAppContext.DEFAULT_CONFIG_LOCATION;
        } else {
            contextConfigLocation = webxmlContextConfigLocation;
        }
    }
    rootContext.setConfigLocation(contextConfigLocation);
    rootContext.setId("mvc.root");
    rootContext.refresh();

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] exits");
    }

    /* enable: WebApplicationContextUtils.getWebApplicationContext() */
    getServletContext().setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootContext);

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] Published mvc.root WebApplicationContext [" + rootContext
                + "] as ServletContext attribute with name [" + ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
    }

    return rootContext;
}

From source file:com.laxser.blitz.BlitzFilter.java

/**
 *  ApplicationContext WEB-INF?WEB-INF/classes?
 * jarspring???? ApplicationContext /* www . j  a v  a  2s  . c o m*/
 * 
 * @return
 * @throws IOException
 */
private WebApplicationContext prepareRootApplicationContext() throws IOException {

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] starting ...");
    }

    ApplicationContext oldRootContext = (ApplicationContext) getServletContext()
            .getAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

    // web.xml?springrootcontext ...... ??
    // BlitzFilter?????rootContext?????
    // ?Listenerinit Blitz context
    if (oldRootContext != null) {
        if (oldRootContext.getClass() != BlitzWebAppContext.class) {
            throw new IllegalStateException(
                    "Cannot initialize context because there is already a root application context present - "
                            + "check whether you have multiple ContextLoader* definitions in your web.xml!");
        }
        if (logger.isInfoEnabled()) {
            logger.info("[init/rootContext] the root context exists:" + oldRootContext);
        }
        return (BlitzWebAppContext) oldRootContext;
    }

    BlitzWebAppContext rootContext = new BlitzWebAppContext(getServletContext(), load, false);

    String contextConfigLocation = this.contextConfigLocation;
    // applicationContext?
    if (StringUtils.isBlank(contextConfigLocation)) {
        String webxmlContextConfigLocation = getServletContext().getInitParameter("contextConfigLocation");
        if (StringUtils.isBlank(webxmlContextConfigLocation)) {
            contextConfigLocation = BlitzWebAppContext.DEFAULT_CONFIG_LOCATION;
        } else {
            contextConfigLocation = webxmlContextConfigLocation;
        }
    }
    rootContext.setConfigLocation(contextConfigLocation);
    rootContext.setId("Blitz.root");
    rootContext.refresh();

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] exits");
    }

    /* enable: WebApplicationContextUtils.getWebApplicationContext() */
    getServletContext().setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootContext);

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] Published Blitz.root WebApplicationContext [" + rootContext
                + "] as ServletContext attribute with name [" + ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
    }

    return rootContext;
}

From source file:org.apache.tapestry5.internal.spring.SpringModuleDef.java

public SpringModuleDef(ServletContext servletContext) {
    this.servletContext = servletContext;

    compatibilityMode = Boolean/*from w ww . j a  v a2 s .  com*/
            .parseBoolean(servletContext.getInitParameter(SpringConstants.USE_EXTERNAL_SPRING_CONTEXT));

    final ApplicationContext externalContext = compatibilityMode ? locateExternalContext() : null;

    if (compatibilityMode)
        addServiceDefsForSpringBeans(externalContext);

    ServiceDef applicationContextServiceDef = new ServiceDef() {
        @Override
        public ObjectCreator createServiceCreator(final ServiceBuilderResources resources) {
            if (compatibilityMode)
                return new StaticObjectCreator(externalContext,
                        "externally configured Spring ApplicationContext");

            ApplicationContextCustomizer customizer = resources.getService("ApplicationContextCustomizer",
                    ApplicationContextCustomizer.class);

            return constructObjectCreatorForApplicationContext(resources, customizer);
        }

        @Override
        public String getServiceId() {
            return SERVICE_ID;
        }

        @Override
        public Set<Class> getMarkers() {
            return Collections.emptySet();
        }

        @Override
        public Class getServiceInterface() {
            return compatibilityMode ? externalContext.getClass() : ConfigurableWebApplicationContext.class;
        }

        @Override
        public String getServiceScope() {
            return ScopeConstants.DEFAULT;
        }

        @Override
        public boolean isEagerLoad() {
            return false;
        }
    };

    services.put(SERVICE_ID, applicationContextServiceDef);
}

From source file:net.paoding.rose.RoseFilter.java

/**
 *  ApplicationContext WEB-INF?WEB-INF/classes?
 * jarspring???? ApplicationContext /* www.j a  va 2  s .  c om*/
 * 
 * @return
 * @throws IOException
 */
private WebApplicationContext prepareRootApplicationContext() throws IOException {

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] starting ...");
    }

    ApplicationContext oldRootContext = (ApplicationContext) getServletContext()
            .getAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

    // web.xml?springrootcontext ...... ??
    // roseFilter?????rootContext?????
    // ?Listenerinit rose context
    if (oldRootContext != null) {
        if (oldRootContext.getClass() != RoseWebAppContext.class) {
            throw new IllegalStateException(
                    "Cannot initialize context because there is already a root application context present - "
                            + "check whether you have multiple ContextLoader* definitions in your web.xml!");
        }
        if (logger.isInfoEnabled()) {
            logger.info("[init/rootContext] the root context exists:" + oldRootContext);
        }
        return (RoseWebAppContext) oldRootContext;
    }

    RoseWebAppContext rootContext = new RoseWebAppContext(getServletContext(), load, false);

    String contextConfigLocation = this.contextConfigLocation;
    // applicationContext?
    if (StringUtils.isBlank(contextConfigLocation)) {
        String webxmlContextConfigLocation = getServletContext().getInitParameter("contextConfigLocation");
        if (StringUtils.isBlank(webxmlContextConfigLocation)) {
            contextConfigLocation = RoseWebAppContext.DEFAULT_CONFIG_LOCATION;
        } else {
            contextConfigLocation = webxmlContextConfigLocation;
        }
    }
    rootContext.setConfigLocation(contextConfigLocation);
    rootContext.setId("rose.root");
    rootContext.refresh();

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] exits");
    }

    /* enable: WebApplicationContextUtils.getWebApplicationContext() */
    getServletContext().setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootContext);

    if (logger.isInfoEnabled()) {
        logger.info("[init/rootContext] Published rose.root WebApplicationContext [" + rootContext
                + "] as ServletContext attribute with name [" + ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
    }

    return rootContext;
}

From source file:org.atricore.idbus.kernel.main.mediation.SpringMediationUnit.java

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    logger.info("Using ApplicationContext "
            + (applicationContext != null ? applicationContext.getClass().getName() : "null"));
    this.applicationContext = applicationContext;
}

From source file:org.springframework.test.context.junit.jupiter.AbstractExpressionEvaluatingCondition.java

private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext,
        Class<A> annotationType, ExtensionContext context) {

    Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
    AnnotatedElement element = context.getElement().get();
    GenericApplicationContext gac = null;
    ApplicationContext applicationContext;

    if (loadContext) {
        applicationContext = SpringExtension.getApplicationContext(context);
    } else {/* ww w .  j a v a2 s .c  om*/
        gac = new GenericApplicationContext();
        gac.refresh();
        applicationContext = gac;
    }

    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = applicationContext.getClass().getName();
            logger.warn(String.format(
                    "@%s(\"%s\") could not be evaluated on [%s] since the test "
                            + "ApplicationContext [%s] is not a ConfigurableApplicationContext",
                    annotationType.getSimpleName(), expression, element, contextType));
        }
        return false;
    }

    ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext)
            .getBeanFactory();
    BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
    Assert.state(expressionResolver != null, "No BeanExpressionResolver");
    BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);

    Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression),
            beanExpressionContext);

    if (gac != null) {
        gac.close();
    }

    if (result instanceof Boolean) {
        return (Boolean) result;
    } else if (result instanceof String) {
        String str = ((String) result).trim().toLowerCase();
        if ("true".equals(str)) {
            return true;
        }
        Assert.state("false".equals(str),
                () -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"",
                        annotationType.getSimpleName(), expression, element, result));
        return false;
    } else {
        String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s",
                annotationType.getSimpleName(), expression, element,
                (result != null ? result.getClass().getName() : "null"));
        throw new IllegalStateException(message);
    }
}

From source file:org.springframework.test.context.junit.jupiter.DisabledIfCondition.java

private boolean isDisabled(String expression, ExtensionContext extensionContext) {
    ApplicationContext applicationContext = SpringExtension.getApplicationContext(extensionContext);

    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = (applicationContext != null ? applicationContext.getClass().getName()
                    : "null");
            logger.warn(String.format(
                    "@DisabledIf(\"%s\") could not be evaluated on [%s] since the test "
                            + "ApplicationContext [%s] is not a ConfigurableApplicationContext",
                    expression, extensionContext.getElement(), contextType));
        }/*from   www .  jav  a 2 s . c o  m*/
        return false;
    }

    ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext)
            .getBeanFactory();
    BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
    BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);

    Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression),
            beanExpressionContext);

    Assert.state((result instanceof Boolean || result instanceof String),
            () -> String.format("@DisabledIf(\"%s\") must evaluate to a String or a Boolean, not %s",
                    expression, (result != null ? result.getClass().getName() : "null")));

    boolean disabled = (result instanceof Boolean && ((Boolean) result).booleanValue())
            || (result instanceof String && Boolean.parseBoolean((String) result));

    return disabled;
}