Example usage for org.springframework.aop.framework ProxyFactory addAdvice

List of usage examples for org.springframework.aop.framework ProxyFactory addAdvice

Introduction

In this page you can find the example usage for org.springframework.aop.framework ProxyFactory addAdvice.

Prototype

@Override
    public void addAdvice(Advice advice) throws AopConfigException 

Source Link

Usage

From source file:net.shopxx.service.impl.ConfigServiceImpl.java

public void init() {
    try {/*  w  w w.  j  ava 2 s  .  c o  m*/
        Setting setting = SystemUtils.getSetting();
        setting.setSmtpPassword(null);
        setting.setKuaidi100Key(null);
        setting.setCnzzPassword(null);
        setting.setSmsKey(null);
        ProxyFactory proxyFactory = new ProxyFactory(setting);
        proxyFactory.setProxyTargetClass(true);
        proxyFactory.addAdvice(new MethodBeforeAdvice() {

            public void before(Method method, Object[] args, Object target) throws Throwable {
                if (StringUtils.startsWith(method.getName(), "set")) {
                    throw new UnsupportedOperationException("Operation not supported");
                }
            }

        });
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        configuration.setSharedVariable("setting", proxyFactory.getProxy());
        configuration.setSharedVariable("locale", setting.getLocale());
        configuration.setSharedVariable("theme", setting.getTheme());
        if (setting.getIsDevelopmentEnabled()) {
            configuration.setSetting("template_update_delay", "0");
            reloadableResourceBundleMessageSource.setCacheSeconds(0);
        } else {
            configuration.setSetting("template_update_delay", templateUpdateDelay);
            reloadableResourceBundleMessageSource.setCacheSeconds(messageCacheSeconds);
        }
        fixedLocaleResolver.setDefaultLocale(LocaleUtils.toLocale(setting.getLocale().toString()));
    } catch (TemplateModelException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (TemplateException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.alfresco.orm.repository.AlfrescoRespositoryProxyFactoryBean.java

public <T extends AlfrescoRespository> T getObject(Class clasz)
        throws InstantiationException, IllegalAccessException {
    AlfrescoRepositoryImpl alfrescoRespository = new AlfrescoRepositoryImpl();
    Class declaredType = getTypeClass(clasz);
    if (null == declaredType) {
        throw new IllegalAccessException("Invalid configuration exception for class : " + clasz.getName());
    }// ww  w .  j a va2 s  . c om
    alfrescoRespository.setDeclaredType(declaredType);
    alfrescoRespository.setSessionFactory(sessionFactory);
    ProxyFactory proxyFactory = new ProxyFactory(new Class[] { clasz });
    proxyFactory.setTarget(alfrescoRespository);
    proxyFactory.addAdvice(new AlfrescoRepositoryMethodInterceptor());
    return (T) proxyFactory.getProxy();
}

From source file:com.alfresco.orm.LazyProxyFactoryBean.java

public <T extends AlfrescoContent> T getObject(String nodeUUID, Class<T> clasz)
        throws InstantiationException, IllegalAccessException {
    T obj = clasz.newInstance();/*www.j a va2  s. co  m*/
    obj.setNodeUUID(nodeUUID);
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(obj);
    for (Class<? extends Advice> methodInterceptorClass : adviceList) {
        proxyFactory.addAdvice(methodInterceptorClass.newInstance());
    }
    return (T) proxyFactory.getProxy();
}

From source file:net.alpha.velocity.view.EmbeddedVelocityToolboxView.java

private ServletContext getToolboxConfigFileAwareServletContext() {
    ProxyFactory factory = new ProxyFactory();
    factory.setTarget(getServletContext());
    factory.addAdvice(new GetResourceMethodInterceptor(getToolboxConfigLocation()));
    return (ServletContext) factory.getProxy(getClass().getClassLoader());
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino.ScriptableFacadeMapConverter.java

/**
 * {@inheritDoc}//from www  . j ava  2  s  .  c om
 */
@Override
public Object convertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    if (!(value instanceof Map<?, ?>)) {
        throw new IllegalArgumentException("value must be a Map");
    }

    final Object result;

    final ProxyFactory proxyFactory = new ProxyFactory();

    proxyFactory.addAdvice(new AdapterObjectInterceptor());
    proxyFactory.addAdvice(new ScriptableBaseAdapterInterceptor());
    proxyFactory.addAdvice(new ScriptableListLikeMapAdapterInterceptor());
    proxyFactory.addAdvice(new ScriptableMapListAdapterInterceptor());
    proxyFactory.addAdvice(new MapLengthFacadeInterceptor(Undefined.instance, false));
    // proxyFactory.addAdvice(new ListLikeMapAdapterInterceptor());
    // some existing scripts in Alfresco expect Map-contained strings not to be converted
    proxyFactory.addAdvice(new ValueConvertingMapInterceptor(globalDelegate, true));

    // this somehow worked in Java 8 Nashorn PoC, but return types of remove(Object) differ between Map and List
    // proxyFactory.setInterfaces(ClassUtils.collectInterfaces(value, Arrays.<Class<?>> asList(Scriptable.class, List.class, AdapterObject.class)));
    proxyFactory.setInterfaces(ClassUtils.collectInterfaces(value,
            Arrays.<Class<?>>asList(Scriptable.class, AdapterObject.class)));

    proxyFactory.setTarget(value);

    result = proxyFactory.getProxy();
    return result;
}

From source file:org.jdal.remoting.httpinvoker.HttpInvokerProxyFactoryBean.java

@Override
public void afterPropertiesSet() {
    super.afterPropertiesSet();
    if (getServiceInterface() == null) {
        throw new IllegalArgumentException("Property 'serviceInterface' is required");
    }/*  w ww . j a  va2 s . co  m*/
    ProxyFactory pf = new ProxyFactory(new Class[] { getServiceInterface(), RemoteClient.class });
    pf.addAdvisor(new RemoteClientAdvisor(getRemoteReference()));
    pf.addAdvice(this);

    this.serviceProxy = pf.getProxy(getBeanClassLoader());
}

From source file:org.jdal.remoting.caucho.BurlapProxyFactoryBean.java

@Override
public void afterPropertiesSet() {
    super.afterPropertiesSet();
    ProxyFactory pf = new ProxyFactory(new Class[] { getServiceInterface(), RemoteClient.class });
    pf.addAdvisor(new RemoteClientAdvisor(getRemoteReference()));
    pf.addAdvice(this);
    this.serviceProxy = pf.getProxy(getBeanClassLoader());
}

From source file:org.joyrest.oauth2.initializer.OAuth2Initializer.java

private DefaultTokenServices txProxiedTokenServices(DefaultTokenServices tokenServices, DataSource dataSource) {
    AnnotationTransactionAttributeSource attrSource = new AnnotationTransactionAttributeSource();
    DataSourceTransactionManager txManager = new DataSourceTransactionManager(dataSource);
    TransactionInterceptor txInterceptor = transactionInterceptor(attrSource, txManager);
    BeanFactoryTransactionAttributeSourceAdvisor txAdvisor = transactionAdvisor(attrSource, txInterceptor);
    ClassLoader classLoader = ClassUtils.getDefaultClassLoader();

    ProxyFactory proxyFactory = new ProxyFactory(tokenServices);
    proxyFactory.addAdvice(txInterceptor);
    proxyFactory.addAdvisor(txAdvisor);//  w  w w .j a  v a  2 s  . c  om
    proxyFactory.setInterfaces(ClassUtils
            .getAllInterfacesForClass(new SingletonTargetSource(tokenServices).getTargetClass(), classLoader));

    return (DefaultTokenServices) proxyFactory.getProxy(classLoader);
}

From source file:com.github.fharms.camel.entitymanager.CamelEntityManagerHandler.java

private Object createBeanProxy(Object bean) {
    MethodInterceptor handler = invocation -> {
        Method method = invocation.getMethod();
        switch (method.getName()) {
        case "hashCode":
            return hashCode();
        case "equals":
            return (invocation.getThis() == invocation.getArguments()[0]);
        case "toString":
            return toString();
        }/*from www  .j  av a 2s. co  m*/

        if (entityManagerLocal.get() == null && !method.isAnnotationPresent(IgnoreCamelEntityManager.class)) {
            Arrays.stream(invocation.getArguments()).filter(f -> f instanceof Exchange).findFirst()
                    .map(Exchange.class::cast)
                    .map(o -> o.getIn().getHeader(CAMEL_ENTITY_MANAGER, EntityManager.class))
                    .ifPresent(em -> addThreadLocalEntityManager(em));
        }
        return invocation.proceed();
    };

    ProxyFactory factory = new ProxyFactory(bean);
    factory.addAdvice(handler);

    return factory.getProxy();
}

From source file:net.sf.oval.test.integration.spring.SpringAOPAllianceBeanValidationTest.java

public void testJDKProxying() {
    final ProxyFactory prFactory = new ProxyFactory(new TestServiceWithInterface());
    prFactory.setProxyTargetClass(false);
    prFactory.addAdvice(new GuardInterceptor(new Guard(new BeanValidationAnnotationsConfigurer())));
    final TestServiceInterface testServiceWithInterface = (TestServiceInterface) prFactory.getProxy();

    try {/*from   w  w w.  j  ava  2s . c  o m*/
        testServiceWithInterface.getSomething(null);
        fail();
    } catch (final ConstraintsViolatedException ex) {
        assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
    }

    try {
        testServiceWithInterface.getSomething("123456");
        fail();
    } catch (final ConstraintsViolatedException ex) {
        assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
    }
}