Search in sources :

Example 56 with Advised

use of org.springframework.aop.framework.Advised in project openmrs-core by openmrs.

the class ServiceContext method removeAddedAdvice.

/**
 * Removes all the advice added by the ServiceContext.
 *
 * @param cls the class of the caches service to cleanup
 */
private void removeAddedAdvice(Class cls) {
    Advised advisedService = (Advised) services.get(cls);
    Set<Advice> adviceToRemove = addedAdvice.get(cls);
    if (advisedService != null && adviceToRemove != null) {
        for (Advice advice : adviceToRemove.toArray(new Advice[] {})) {
            removeAdvice(cls, advice);
        }
    }
}
Also used : Advised(org.springframework.aop.framework.Advised) Advice(org.aopalliance.aop.Advice)

Example 57 with Advised

use of org.springframework.aop.framework.Advised in project openmrs-core by openmrs.

the class ServiceContext method setService.

/**
 * Allow other services to be added to our service layer
 *
 * @param cls Interface to proxy
 * @param classInstance the actual instance of the <code>cls</code> interface
 */
public void setService(Class cls, Object classInstance) {
    log.debug("Setting service: " + cls);
    if (cls != null && classInstance != null) {
        try {
            Advised cachedService = (Advised) services.get(cls);
            boolean noExistingService = cachedService == null;
            boolean replacingService = cachedService != null && cachedService != classInstance;
            boolean serviceAdvised = classInstance instanceof Advised;
            if (noExistingService || replacingService) {
                Advised advisedService;
                if (!serviceAdvised) {
                    // Adding a bare service, wrap with AOP proxy
                    Class[] interfaces = { cls };
                    ProxyFactory factory = new ProxyFactory(interfaces);
                    factory.setTarget(classInstance);
                    advisedService = (Advised) factory.getProxy(OpenmrsClassLoader.getInstance());
                } else {
                    advisedService = (Advised) classInstance;
                }
                if (replacingService) {
                    moveAddedAOP(cachedService, advisedService);
                }
                services.put(cls, advisedService);
            }
            log.debug("Service: " + cls + " set successfully");
        } catch (Exception e) {
            throw new APIException("service.unable.create.proxy.factory", new Object[] { classInstance.getClass().getName() }, e);
        }
    }
}
Also used : APIException(org.openmrs.api.APIException) Advised(org.springframework.aop.framework.Advised) ProxyFactory(org.springframework.aop.framework.ProxyFactory) APIException(org.openmrs.api.APIException) BeansException(org.springframework.beans.BeansException)

Example 58 with Advised

use of org.springframework.aop.framework.Advised in project spring-integration by spring-projects.

the class TransactionalPollerWithMixedAopConfigTests method validateTransactionalProxyIsolationToThePollerOnly.

@Test
public void validateTransactionalProxyIsolationToThePollerOnly() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("TransactionalPollerWithMixedAopConfig-context.xml", this.getClass());
    assertTrue(!(context.getBean("foo") instanceof Advised));
    assertTrue(!(context.getBean("inputChannel") instanceof Advised));
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) Test(org.junit.Test)

Example 59 with Advised

use of org.springframework.aop.framework.Advised in project spring-integration by spring-projects.

the class ConsumerEndpointFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    if (this.beanName == null) {
        logger.error("The MessageHandler [" + this.handler + "] will be created without a 'componentName'. " + "Consider specifying the 'beanName' property on this ConsumerEndpointFactoryBean.");
    } else {
        try {
            if (!this.beanName.startsWith("org.springframework")) {
                MessageHandler targetHandler = this.handler;
                if (AopUtils.isAopProxy(targetHandler)) {
                    Object target = ((Advised) targetHandler).getTargetSource().getTarget();
                    if (target instanceof MessageHandler) {
                        targetHandler = (MessageHandler) target;
                    }
                }
                if (targetHandler instanceof IntegrationObjectSupport) {
                    ((IntegrationObjectSupport) targetHandler).setComponentName(this.beanName);
                }
            }
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not set component name for handler " + this.handler + " for " + this.beanName + " :" + e.getMessage());
            }
        }
    }
    if (!CollectionUtils.isEmpty(this.adviceChain)) {
        /*
			 *  ARPMHs advise the handleRequestMessage method internally and already have the advice chain injected.
			 *  So we only advise handlers that are not reply-producing.
			 *  Or if one (or more) of advices is IdempotentReceiverInterceptor.
			 *  If the handler is already advised,
			 *  add the configured advices to its chain, otherwise create a proxy.
			 */
        Class<?> targetClass = AopUtils.getTargetClass(this.handler);
        boolean replyMessageHandler = AbstractReplyProducingMessageHandler.class.isAssignableFrom(targetClass);
        for (Advice advice : this.adviceChain) {
            if (!replyMessageHandler || advice instanceof HandleMessageAdvice) {
                NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
                handlerAdvice.addMethodName("handleMessage");
                if (this.handler instanceof Advised) {
                    ((Advised) this.handler).addAdvisor(handlerAdvice);
                } else {
                    ProxyFactory proxyFactory = new ProxyFactory(this.handler);
                    proxyFactory.addAdvisor(handlerAdvice);
                    this.handler = (MessageHandler) proxyFactory.getProxy(this.beanClassLoader);
                }
            }
        }
    }
    if (this.channelResolver == null) {
        this.channelResolver = new BeanFactoryMessageChannelDestinationResolver(this.beanFactory);
    }
    initializeEndpoint();
}
Also used : HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) IntegrationObjectSupport(org.springframework.integration.context.IntegrationObjectSupport) Advised(org.springframework.aop.framework.Advised) ProxyFactory(org.springframework.aop.framework.ProxyFactory) BeanFactoryMessageChannelDestinationResolver(org.springframework.messaging.core.BeanFactoryMessageChannelDestinationResolver) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) Advice(org.aopalliance.aop.Advice) HandleMessageAdvice(org.springframework.integration.handler.advice.HandleMessageAdvice)

Example 60 with Advised

use of org.springframework.aop.framework.Advised in project spring-integration by spring-projects.

the class SecuredChannelsParserTests method testAdminRequiredForSendAndReceive.

@Test
public void testAdminRequiredForSendAndReceive() {
    String beanName = "adminRequiredForSendAndReceive";
    messageChannel.setBeanName(beanName);
    MessageChannel proxy = (MessageChannel) applicationContext.getAutowireCapableBeanFactory().applyBeanPostProcessorsAfterInitialization(messageChannel, beanName);
    assertTrue("Channel was not proxied", AopUtils.isAopProxy(proxy));
    Advisor[] advisors = ((Advised) proxy).getAdvisors();
    assertEquals("Wrong number of interceptors", 1, advisors.length);
    ChannelSecurityInterceptor interceptor = (ChannelSecurityInterceptor) advisors[0].getAdvice();
    ChannelAccessPolicy policy = this.retrievePolicyForPatternString(beanName, interceptor);
    assertNotNull("Pattern '" + beanName + "' is not included in mappings", policy);
    Collection<ConfigAttribute> sendDefinition = policy.getConfigAttributesForSend();
    Collection<ConfigAttribute> receiveDefinition = policy.getConfigAttributesForReceive();
    assertNotNull("Pattern does not apply to 'send'", sendDefinition);
    assertNotNull("Pattern does not apply to 'receive'", receiveDefinition);
    Collection<String> sendRoles = this.getRolesFromDefintion(sendDefinition);
    Collection<String> receiveRoles = this.getRolesFromDefintion(receiveDefinition);
    assertTrue("ROLE_ADMIN not found in send attributes", sendRoles.contains("ROLE_ADMIN"));
    assertTrue("ROLE_ADMIN not found in receive attributes", receiveRoles.contains("ROLE_ADMIN"));
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ChannelAccessPolicy(org.springframework.integration.security.channel.ChannelAccessPolicy) Advised(org.springframework.aop.framework.Advised) ConfigAttribute(org.springframework.security.access.ConfigAttribute) Advisor(org.springframework.aop.Advisor) ChannelSecurityInterceptor(org.springframework.integration.security.channel.ChannelSecurityInterceptor) Test(org.junit.Test)

Aggregations

Advised (org.springframework.aop.framework.Advised)78 Advisor (org.springframework.aop.Advisor)21 Test (org.junit.jupiter.api.Test)20 Test (org.junit.Test)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)16 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)13 ProxyFactory (org.springframework.aop.framework.ProxyFactory)10 Advice (org.aopalliance.aop.Advice)8 JoinPoint (org.aspectj.lang.JoinPoint)6 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)6 TargetSource (org.springframework.aop.TargetSource)6 BeanCreationException (org.springframework.beans.factory.BeanCreationException)6 MessageChannel (org.springframework.messaging.MessageChannel)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)4 ChannelAccessPolicy (org.springframework.integration.security.channel.ChannelAccessPolicy)4 ChannelSecurityInterceptor (org.springframework.integration.security.channel.ChannelSecurityInterceptor)4 ConfigAttribute (org.springframework.security.access.ConfigAttribute)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 List (java.util.List)3