List of usage examples for org.springframework.aop.framework ProxyFactory setTargetSource
@Override public void setTargetSource(@Nullable TargetSource targetSource)
From source file:serialization.ProxySerializationTest.java
@Test public void testSerializableTargetSource() { ProxyFactory pf = new ProxyFactory(); pf.setTargetSource(new SerializableTargetSource(beanFactory, "b2", true)); pf.setProxyTargetClass(true);// www . j av a2 s . c o m pf.addInterface(Serializable.class); serialize(pf.getProxy()); }
From source file:com.payu.ratel.client.ContextAnnotationAutowireCandidateResolver.java
protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final String beanName) { Assert.state(getBeanFactory() instanceof DefaultListableBeanFactory, "BeanFactory needs to be a DefaultListableBeanFactory"); final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory(); TargetSource ts = new TargetSource() { @Override//from w w w . ja v a 2s .c o m public Class<?> getTargetClass() { return descriptor.getDependencyType(); } @Override public boolean isStatic() { return false; } @Override public Object getTarget() { return beanFactory.doResolveDependency(descriptor, beanName, null, null); } @Override public void releaseTarget(Object target) { } }; ProxyFactory pf = new ProxyFactory(); pf.setTargetSource(ts); Class<?> dependencyType = descriptor.getDependencyType(); if (dependencyType.isInterface()) { pf.addInterface(dependencyType); } return pf.getProxy(beanFactory.getBeanClassLoader()); }
From source file:com.alibaba.cobar.client.datasources.ha.FailoverHotSwapDataSourceCreator.java
public DataSource createHADataSource(CobarDataSourceDescriptor descriptor) throws Exception { DataSource activeDataSource = descriptor.getTargetDataSource(); DataSource standbyDataSource = descriptor.getStandbyDataSource(); if (activeDataSource == null && standbyDataSource == null) { throw new IllegalArgumentException("must have at least one data source active."); }/*from w ww. j a v a2s . c o m*/ if (activeDataSource == null || standbyDataSource == null) { logger.warn("only one data source is available for use, so no HA support."); if (activeDataSource == null) { return standbyDataSource; } return activeDataSource; } HotSwappableTargetSource targetSource = new HotSwappableTargetSource(activeDataSource); ProxyFactory pf = new ProxyFactory(); pf.setInterfaces(new Class[] { DataSource.class }); pf.setTargetSource(targetSource); if (isPositiveFailoverEnable()) { DataSource targetDetectorDataSource = descriptor.getTargetDetectorDataSource(); DataSource standbyDetectorDataSource = descriptor.getStandbyDetectorDataSource(); if (targetDetectorDataSource == null || standbyDetectorDataSource == null) { throw new IllegalArgumentException( "targetDetectorDataSource or standbyDetectorDataSource can't be null if positive failover is enabled."); } // 1. create active monitoring job for failover event ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); ExecutorService jobExecutor = Executors.newFixedThreadPool(1); jobExecutorRegistry.add(jobExecutor); FailoverMonitorJob job = new FailoverMonitorJob(jobExecutor); // 1.1 inject dependencies job.setHotSwapTargetSource(targetSource); job.setMasterDataSource(activeDataSource); job.setStandbyDataSource(standbyDataSource); job.setMasterDetectorDataSource(targetDetectorDataSource); job.setStandbyDetectorDataSource(standbyDetectorDataSource); job.setCurrentDetectorDataSource(targetDetectorDataSource); job.setDetectingRequestTimeout(getDetectingTimeoutThreshold()); job.setDetectingSQL(getDetectingSql()); job.setRecheckInterval(recheckInterval); job.setRecheckTimes(recheckTimes); // 1.2 start scheduling and keep reference for canceling and shutdown ScheduledFuture<?> future = scheduler.scheduleWithFixedDelay(job, initialDelay, monitorPeriod, TimeUnit.MILLISECONDS); schedulerFutures.put(future, scheduler); } if (isPassiveFailoverEnable()) { // 2. create data source proxy with passive event advice PassiveEventHotSwappableAdvice advice = new PassiveEventHotSwappableAdvice(); advice.setRetryInterval(recheckInterval); advice.setRetryTimes(recheckTimes); advice.setDetectingSql(detectingSql); advice.setTargetSource(targetSource); advice.setMainDataSource(activeDataSource); advice.setStandbyDataSource(standbyDataSource); pf.addAdvice(advice); } return (DataSource) pf.getProxy(); }
From source file:cn.vko.cache.dao.ha.FailoverHotSwapDataSourceCreator.java
@Override public DataSource createHADataSource(DataSourceDescriptor descriptor) throws Exception { DataSource activeDataSource = descriptor.getTarget(); DataSource standbyDataSource = descriptor.getStandby(); if (activeDataSource == null && standbyDataSource == null) { throw new IllegalArgumentException("must have at least one data source active."); }/*w w w . jav a 2 s.c o m*/ if (activeDataSource == null || standbyDataSource == null) { logger.warn("only one data source is available for use, so no HA support."); if (activeDataSource == null) { return standbyDataSource; } return activeDataSource; } HotSwappableTargetSource targetSource = new HotSwappableTargetSource(activeDataSource); ProxyFactory pf = new ProxyFactory(); pf.setInterfaces(new Class[] { DataSource.class }); pf.setTargetSource(targetSource); if (isPositiveFailoverEnable()) { DataSource targetDetectorDataSource = descriptor.getTarget(); DataSource standbyDetectorDataSource = descriptor.getStandby(); if (targetDetectorDataSource == null || standbyDetectorDataSource == null) { throw new IllegalArgumentException( "targetDetectorDataSource or standbyDetectorDataSource can't be null if positive failover is enabled."); } // 1. create active monitoring job for failover event ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); ExecutorService jobExecutor = Executors.newFixedThreadPool(1); jobExecutorRegistry.add(jobExecutor); FailoverMonitorJob job = new FailoverMonitorJob(jobExecutor); // 1.1 inject dependencies job.setHotSwapTargetSource(targetSource); job.setMasterDataSource(activeDataSource); job.setStandbyDataSource(standbyDataSource); job.setMasterDetectorDataSource(targetDetectorDataSource); job.setStandbyDetectorDataSource(standbyDetectorDataSource); job.setCurrentDetectorDataSource(targetDetectorDataSource); job.setDetectingRequestTimeout(getDetectingTimeoutThreshold()); job.setDetectingSQL(getDetectingSql()); job.setRecheckInterval(recheckInterval); job.setRecheckTimes(recheckTimes); // 1.2 start scheduling and keep reference for canceling and shutdown ScheduledFuture<?> future = scheduler.scheduleWithFixedDelay(job, initialDelay, monitorPeriod, TimeUnit.MILLISECONDS); schedulerFutures.put(future, scheduler); } if (isPassiveFailoverEnable()) { // 2. create data source proxy with passive event advice PassiveEventHotSwappableAdvice advice = new PassiveEventHotSwappableAdvice(); advice.setRetryInterval(recheckInterval); advice.setRetryTimes(recheckTimes); advice.setDetectingSql(detectingSql); advice.setTargetSource(targetSource); advice.setMainDataSource(activeDataSource); advice.setStandbyDataSource(standbyDataSource); pf.addAdvice(advice); } return (DataSource) pf.getProxy(); }
From source file:fr.mby.utils.spring.beans.factory.support.BasicProxywiredFactory.java
protected IManageableProxywired proxywire(final TargetSource targetSource) { final Class<?>[] proxtyInterfaces = new Class[] { targetSource.getTargetClass(), IManageableProxywired.class }; final ProxyFactory proxyFactory = new ProxyFactory(proxtyInterfaces); proxyFactory.addAdvice(new InterfaceImplementationAdvice(IManageableProxywired.class, (IManageableProxywired) targetSource)); proxyFactory.setTargetSource(targetSource); return (IManageableProxywired) proxyFactory.getProxy(); }
From source file:org.iff.infra.util.spring.script.ScriptFactoryPostProcessor.java
/** * Create a refreshable proxy for the given AOP TargetSource. * @param ts the refreshable TargetSource * @param interfaces the proxy interfaces (may be {@code null} to * indicate proxying of all interfaces implemented by the target class) * @return the generated proxy//from w w w.j a v a 2s. co m * @see RefreshableScriptTargetSource */ protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(ts); ClassLoader classLoader = this.beanClassLoader; if (interfaces == null) { interfaces = ClassUtils.getAllInterfacesForClass(ts.getTargetClass(), this.beanClassLoader); } proxyFactory.setInterfaces(interfaces); if (proxyTargetClass) { classLoader = null; // force use of Class.getClassLoader() proxyFactory.setProxyTargetClass(proxyTargetClass); } DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts); introduction.suppressInterface(TargetSource.class); proxyFactory.addAdvice(introduction); return proxyFactory.getProxy(classLoader); }
From source file:org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBean.java
/** * Creates the proxy for target object. This method is invoked by a * BeanFactory after it has set all bean properties supplied. * //w w w. j a v a 2s. c o m * @throws IllegalStateException * if target is <code>null</code>. * @throws AopConfigException * if the proxy interfaces or proxyTargetClass are not set and the * target type is <code>org.springframework.aop.TargetSource</code>. */ public void afterPropertiesSet() throws IllegalStateException, AopConfigException { cachingInterceptor.afterPropertiesSet(); flushingInterceptor.afterPropertiesSet(); if (target == null) { throw new IllegalStateException("Property 'target' is required"); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(new CachingModelSourceAdvisor(cachingInterceptor)); if (hasFlushingModels) { proxyFactory.addAdvisor(new FlushingModelSourceAdvisor(flushingInterceptor)); } proxyFactory.copyFrom(this); TargetSource targetSource = createTargetSource(target); proxyFactory.setTargetSource(targetSource); if (proxyInterfaces != null) { proxyFactory.setInterfaces(proxyInterfaces); } else if (!isProxyTargetClass()) { if (target instanceof TargetSource) { throw new AopConfigException("Either 'proxyInterfaces' or 'proxyTargetClass' is required " + "when using a TargetSource as 'target'"); } // rely on AOP infrastructure to tell us what interfaces to proxy proxyFactory.setInterfaces(ClassUtils.getAllInterfaces(target)); } proxy = proxyFactory.getProxy(); }
From source file:com.helpinput.spring.proxy.OptimizeTransactionProxyFactoryBean.java
public void afterPropertiesSet() { if (this.target == null) { throw new IllegalArgumentException("Property 'target' is required"); }//from w ww. j av a 2 s .c o m if (this.target instanceof String) { throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value"); } ClassLoader proxyClassLoader = target.getClass().getClassLoader(); ProxyFactory proxyFactory = new ProxyFactory(); if (this.preInterceptors != null) { for (Object interceptor : this.preInterceptors) { proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor)); } } // Add the main interceptor (typically an Advisor). proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor())); if (this.postInterceptors != null) { for (Object interceptor : this.postInterceptors) { proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor)); } } proxyFactory.copyFrom(this); TargetSource targetSource = createTargetSource(this.target); proxyFactory.setTargetSource(targetSource); if (this.proxyInterfaces != null) { proxyFactory.setInterfaces(this.proxyInterfaces); } else if (!isProxyTargetClass()) { // Rely on AOP infrastructure to tell us what interfaces to proxy. proxyFactory.setInterfaces( ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), proxyClassLoader)); } /** * use this option to let proxyFactory user cglib to create proxy,otherwise in dynamic script ,this is no dynamic interface * ? cglib ??java?? */ proxyFactory.setOptimize(true); this.proxy = proxyFactory.getProxy(proxyClassLoader); }
From source file:org.sakaiproject.genericdao.springutil.CurrentClassLoaderBeanNameAutoProxyCreator.java
@SuppressWarnings("unchecked") @Override//w w w .j ava 2s .co m protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) { if (spring12x) { ProxyFactory proxyFactory = new ProxyFactory(); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); if (!shouldProxyTargetClass(beanClass, beanName)) { // Must allow for introductions; can't just set interfaces to // the target's interfaces only. Class[] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass); for (int i = 0; i < targetInterfaces.length; i++) { proxyFactory.addInterface(targetInterfaces[i]); } } Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); for (int i = 0; i < advisors.length; i++) { proxyFactory.addAdvisor(advisors[i]); } proxyFactory.setTargetSource(targetSource); customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); return proxyFactory.getProxy(myClassLoader); } else { return super.createProxy(beanClass, beanName, specificInterceptors, targetSource); } }
From source file:org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.java
/** * Create an AOP proxy for the given bean. * @param beanClass the class of the bean * @param beanName the name of the bean//from w w w. j a v a 2s . c o m * @param specificInterceptors the set of interceptors that is * specific to this bean (may be empty, but not null) * @param targetSource the TargetSource for the proxy, * already pre-configured to access the bean * @return the AOP proxy for the bean * @see #buildAdvisors */ protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) { if (this.beanFactory instanceof ConfigurableListableBeanFactory) { AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.copyFrom(this); if (!proxyFactory.isProxyTargetClass()) { if (shouldProxyTargetClass(beanClass, beanName)) { proxyFactory.setProxyTargetClass(true); } else { evaluateProxyInterfaces(beanClass, proxyFactory); } } Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); proxyFactory.addAdvisors(advisors); proxyFactory.setTargetSource(targetSource); customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true); } return proxyFactory.getProxy(getProxyClassLoader()); }