proxyFactory.setFrozen(true);
private ITestBean getIntroductionAdvisorProxy(TestBean target) { ProxyFactory pf = new ProxyFactory(ITestBean.class); pf.setProxyTargetClass(true); pf.addAdvisor(new LockMixinAdvisor()); pf.setTarget(target); pf.setFrozen(true); pf.setExposeProxy(false); return (ITestBean) pf.getProxy(); }
customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true);
proxyFactory.setFrozen(true);
private ITestBean getAdvisedProxy(TestBean target) { ProxyFactory pf = new ProxyFactory(new Class<?>[]{ITestBean.class}); pf.setProxyTargetClass(true); MethodInterceptor advice = new NopInterceptor(); Pointcut pointcut = new Pointcut() { @Override public ClassFilter getClassFilter() { return ClassFilter.TRUE; } @Override public MethodMatcher getMethodMatcher() { return MethodMatcher.TRUE; } @Override public boolean equals(Object obj) { return true; } @Override public int hashCode() { return 0; } }; pf.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice)); pf.setTarget(target); pf.setFrozen(true); pf.setExposeProxy(false); return (ITestBean) pf.getProxy(); }
@Test public void testAddAdviceAtRuntime() { TestBean bean = new TestBean(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(bean); pf.setFrozen(false); pf.setOpaque(false); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); proxy.getAge(); assertEquals(0, cba.getCalls()); ((Advised) proxy).addAdvice(cba); proxy.getAge(); assertEquals(1, cba.getCalls()); }
@Test public void testCannotRemoveAdvisorWhenFrozen() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); Advised advised = (Advised) proxied; assertTrue(pc.isFrozen()); try { advised.removeAdvisor(0); fail("Shouldn't be able to remove Advisor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Didn't get removed assertEquals(1, advised.getAdvisors().length); pc.setFrozen(false); // Can now remove it advised.removeAdvisor(0); // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(0, advised.getAdvisors().length); }
@Test public void testCannotAddInterceptorWhenFrozen() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); try { pc.addAdvice(0, new NopInterceptor()); fail("Shouldn't be able to add interceptor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, ((Advised) proxied).getAdvisors().length); }
/** * Check that casting to Advised can't get around advice freeze. */ @Test public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); Advised advised = (Advised) proxied; assertTrue(pc.isFrozen()); try { advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); fail("Shouldn't be able to add Advisor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, advised.getAdvisors().length); }
protected <T> T wrapHandler(Class<T> interfaceClass, T handler) { ProxyFactory proxyFactory = new ProxyFactory(interfaceClass, new SingletonTargetSource(handler)); thriftConfigurer.configureProxyFactory(proxyFactory); //TODO remove from here? proxyFactory.setFrozen(true); return (T) proxyFactory.getProxy(); } }
private Object wrapHandler(Class<?> ifaceClass, Object handler, String path, String type) { ProxyFactory proxyFactory = new ProxyFactory(ifaceClass, new SingletonTargetSource(handler)); thriftConfigurer.configureProxyFactory(proxyFactory, path, type); //TODO remove from here? proxyFactory.setFrozen(true); return proxyFactory.getProxy(); }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (beansToProcess.containsKey(beanName)) { Object target = getTargetBean(bean); for (Field field : beansToProcess.get(beanName).getDeclaredFields()) { ThriftClientsMap annotation = AnnotationUtils.getAnnotation(field, ThriftClientsMap.class); if (null != annotation) { HashMap clients = new HashMap(); for (Map.Entry<String, ThriftClientKey> entry : ((AbstractThriftClientKeyMapper)beanFactory.getBean(annotation.mapperClass())).getMappings().entrySet()) { ProxyFactory proxyFactory = getProxyFactoryForThriftClient(bean, field, entry.getValue().getClazz()); addPoolAdvice(proxyFactory, entry.getValue()); proxyFactory.setFrozen(true); proxyFactory.setProxyTargetClass(true); clients.put(entry.getKey(), proxyFactory.getProxy()); } ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, bean, clients); } } } return bean; }
/** * * {@inheritDoc} */ public MessageBroker processAfterStartup(MessageBroker broker) { Iterator<String> i = broker.getEndpoints().keySet().iterator(); while (i.hasNext()) { String key = i.next(); Endpoint endpoint = (Endpoint) broker.getEndpoints().get(key); // Use proxy only in case of Servlet based Endpoints if (endpoint instanceof BaseHTTPEndpoint) { ProxyFactory factory = new ProxyFactory(); factory.setProxyTargetClass(true); factory.addAdvisors(this.advisors); factory.setTarget(endpoint); factory.setFrozen(true); Endpoint proxy = (Endpoint) factory.getProxy(this.proxyClassLoader); fixFilterChain(endpoint, proxy); broker.getEndpoints().put(key, proxy); } } return broker; }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (beansToProcess.containsKey(beanName)) { Object target = getTargetBean(bean); for (Class clazz : beansToProcess.get(beanName)) { for (Field field : clazz.getDeclaredFields()) { ThriftClient annotation = AnnotationUtils.getAnnotation(field, ThriftClient.class); if (null != annotation) { if (beanFactory.containsBean(field.getName())) { ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, target, beanFactory.getBean(field.getName())); } else { ProxyFactory proxyFactory = getProxyFactoryForThriftClient(target, field); addPoolAdvice(proxyFactory, annotation); proxyFactory.setFrozen(true); proxyFactory.setProxyTargetClass(true); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, target, proxyFactory.getProxy()); } } } } } return bean; }
factory.setFrozen(true); factory.setOpaque(true); boolean isSecurityOn = (System.getSecurityManager() != null);
pf.setOptimize(true); pf.setExposeProxy(false); pf.setFrozen(true); pf.setAopProxyFactory(new DefaultAopProxyFactory()); pf.setTargetSource(this.targetSource);
@SuppressWarnings("unchecked") @Override 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); } }
customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true);
@Override protected Object createProxy( Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setAopProxyFactory(new EagleTraceProxyFactory()); proxyFactory.copyFrom(this); if (!proxyFactory.isProxyTargetClass()) { if (shouldProxyTargetClass(beanClass, beanName)) { proxyFactory.setProxyTargetClass(true); } else { evaluateProxyInterfaces(beanClass, proxyFactory); } } proxyFactory.setTargetSource(targetSource); customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(false); injectRefer(beanClass, targetSource); return proxyFactory.getProxy(getProxyClassLoader()); }
customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true);