Java Code Examples for org.springframework.aop.framework.ProxyFactory#setTargetSource()
The following examples show how to use
org.springframework.aop.framework.ProxyFactory#setTargetSource() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ACLEntryAfterInvocationTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testBasicAllowNode() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("echoNodeRef", new Class[] { NodeRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("AFTER_ACL_NODE.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); permissionService.setPermission(new SimplePermissionEntry(rootNodeRef, getPermission(PermissionService.READ), "andy", AccessStatus.ALLOWED)); Object answer = method.invoke(proxy, new Object[] { rootNodeRef }); assertEquals(answer, rootNodeRef); }
Example 2
Source File: ACLEntryAfterInvocationTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testBasicDenyStore() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("echoStoreRef", new Class[] { StoreRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("AFTER_ACL_NODE.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); try { Object answer = method.invoke(proxy, new Object[] { rootNodeRef.getStoreRef() }); assertNotNull(answer); } catch (InvocationTargetException e) { } }
Example 3
Source File: ACLEntryAfterInvocationTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testBasicAllowUnrecognisedObject() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("echoObject", new Class[] { Object.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("AFTER_ACL_NODE.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); Object answer = method.invoke(proxy, new Object[] { "noodle" }); assertNotNull(answer); }
Example 4
Source File: ACLEntryAfterInvocationTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testBasicAllowStore() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("echoStoreRef", new Class[] { StoreRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("AFTER_ACL_NODE.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); permissionService.setPermission(new SimplePermissionEntry(rootNodeRef, getPermission(PermissionService.READ), "andy", AccessStatus.ALLOWED)); Object answer = method.invoke(proxy, new Object[] { rootNodeRef.getStoreRef() }); assertEquals(answer, rootNodeRef.getStoreRef()); }
Example 5
Source File: ACLEntryAfterInvocationTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testBasicAllowChildAssociationRef2() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("echoChildAssocRef", new Class[] { ChildAssociationRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("AFTER_ACL_PARENT.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); permissionService.setPermission(new SimplePermissionEntry(rootNodeRef, getPermission(PermissionService.READ), "andy", AccessStatus.ALLOWED)); Object answer = method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef) }); assertEquals(answer, nodeService.getPrimaryParent(rootNodeRef)); answer = method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(systemNodeRef) }); assertEquals(answer, nodeService.getPrimaryParent(systemNodeRef)); }
Example 6
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testMethodACL() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testMethod", new Class[] {}); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_METHOD.andy", "ACL_METHOD.BANANA"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] {}); }
Example 7
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testMethodACL3() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testMethod", new Class[] {}); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_METHOD.andy", "ACL_METHOD." + PermissionService.ALL_AUTHORITIES))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] {}); }
Example 8
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public void testMultiChildAssocRefMethodsArg0() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod( "testManyChildAssociationRef", new Class[] { ChildAssociationRef.class, ChildAssociationRef.class, ChildAssociationRef.class, ChildAssociationRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { null, null, null, null }); try { method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef), null, null, null }); assertNotNull(null); } catch (InvocationTargetException e) { } permissionService.setPermission(new SimplePermissionEntry(rootNodeRef, getPermission(PermissionService.READ), "andy", AccessStatus.ALLOWED)); method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef), null, null, null }); }
Example 9
Source File: ACLEntryAfterInvocationTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@SuppressWarnings("unchecked") public void testBasicFilterInvalidNodeRefs() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("echoCollection", new Class[] { Collection.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("AFTER_ACL_NODE.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); permissionService.setPermission( new SimplePermissionEntry( rootNodeRef, getPermission(PermissionService.READ), "andy", AccessStatus.ALLOWED)); List<Object> answer = (List<Object>) method.invoke(proxy, new Object[] { Collections.singletonList(rootNodeRef) }); assertEquals("Collection must be intact", 1, answer.size()); NodeRef invalidNodeRef = new NodeRef("workspace://SpacesStore/noodle"); answer = (List<Object>) method.invoke(proxy, new Object[] { Collections.singletonList(invalidNodeRef) }); assertEquals("Invalid NodeRef should not have been filtered out", 1, answer.size()); }
Example 10
Source File: AbstractAutoProxyCreator.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Create an AOP proxy for the given bean. * @param beanClass the class of the bean * @param beanName the name of the bean * @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, String beanName, 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()); }
Example 11
Source File: ScriptFactoryPostProcessor.java From java-technology-stack with MIT License | 5 votes |
/** * 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 * @see RefreshableScriptTargetSource */ protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(ts); ClassLoader classLoader = this.beanClassLoader; if (interfaces != null) { proxyFactory.setInterfaces(interfaces); } else { Class<?> targetClass = ts.getTargetClass(); if (targetClass != null) { proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader)); } } if (proxyTargetClass) { classLoader = null; // force use of Class.getClassLoader() proxyFactory.setProxyTargetClass(true); } DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts); introduction.suppressInterface(TargetSource.class); proxyFactory.addAdvice(introduction); return proxyFactory.getProxy(classLoader); }
Example 12
Source File: AbstractAutoProxyCreator.java From java-technology-stack with MIT License | 5 votes |
/** * Create an AOP proxy for the given bean. * @param beanClass the class of the bean * @param beanName the name of the bean * @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()); }
Example 13
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public void testMultiNodeMethodsArg2() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testManyNodeRef", new Class[] { NodeRef.class, NodeRef.class, NodeRef.class, NodeRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.2.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { null, null, null, null }); try { method.invoke(proxy, new Object[] { null, null, rootNodeRef, null }); assertNotNull(null); } catch (InvocationTargetException e) { } permissionService.setPermission(new SimplePermissionEntry(rootNodeRef, getPermission(PermissionService.READ), "andy", AccessStatus.ALLOWED)); method.invoke(proxy, new Object[] { null, null, rootNodeRef, null }); }
Example 14
Source File: JndiObjectFactoryBean.java From lams with GNU General Public License v2.0 | 5 votes |
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException { // Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration. JndiObjectTargetSource targetSource = new JndiObjectTargetSource(); targetSource.setJndiTemplate(jof.getJndiTemplate()); targetSource.setJndiName(jof.getJndiName()); targetSource.setExpectedType(jof.getExpectedType()); targetSource.setResourceRef(jof.isResourceRef()); targetSource.setLookupOnStartup(jof.lookupOnStartup); targetSource.setCache(jof.cache); targetSource.afterPropertiesSet(); // Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource. ProxyFactory proxyFactory = new ProxyFactory(); if (jof.proxyInterfaces != null) { proxyFactory.setInterfaces(jof.proxyInterfaces); } else { Class<?> targetClass = targetSource.getTargetClass(); if (targetClass == null) { throw new IllegalStateException( "Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'"); } Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(targetClass, jof.beanClassLoader); for (Class<?> ifc : ifcs) { if (Modifier.isPublic(ifc.getModifiers())) { proxyFactory.addInterface(ifc); } } } if (jof.exposeAccessContext) { proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate())); } proxyFactory.setTargetSource(targetSource); return proxyFactory.getProxy(jof.beanClassLoader); }
Example 15
Source File: ContextAnnotationAutowireCandidateResolver.java From Ratel with Apache License 2.0 | 5 votes |
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 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()); }
Example 16
Source File: AbstractAutoProxyCreator.java From spring-analysis-note with MIT License | 5 votes |
/** * Create an AOP proxy for the given bean. * * 注释 8.9 为给定的bean创建AOP代理,实际在这一步中进行切面织入,生成动态代理 * * @param beanClass the class of the bean * @param beanName the name of the bean * @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); // 决定对于给定 bean 是否应该使用 targetClass 而不是他的接口代理 if (!proxyFactory.isProxyTargetClass()) { // 检查 proxyTargetClass 设置以及 preserveTargetClass 属性 if (shouldProxyTargetClass(beanClass, beanName)) { proxyFactory.setProxyTargetClass(true); } else { // 添加代理接口 evaluateProxyInterfaces(beanClass, proxyFactory); } } // 这一步中,主要将拦截器封装为增强器 Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); proxyFactory.addAdvisors(advisors); proxyFactory.setTargetSource(targetSource); // 定制代理 customizeProxyFactory(proxyFactory); // 用来控制代理工厂被配置之后,是否含允许修改通知 // 缺省值为 false,不允许修改代理的配置 proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true); } // 生成代理,委托给了 ProxyFactory 去处理。 return proxyFactory.getProxy(getProxyClassLoader()); }
Example 17
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
public void testAllowNullNode() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testOneNodeRef", new Class[] { NodeRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { null }); }
Example 18
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
public void testAllowNullParentOnRealChildAssoc() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testOneChildAssociationRef", new Class[] { ChildAssociationRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_PARENT.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef) }); }
Example 19
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
public void testAllowNullStore() throws Exception { runAs("andy"); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testOneStoreRef", new Class[] { StoreRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { null }); }
Example 20
Source File: ACLEntryVoterTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
public void testBasicAllowChildAssocNode() throws Exception { runAs("andy"); permissionService.setPermission(new SimplePermissionEntry(rootNodeRef, getPermission(PermissionService.READ), "andy", AccessStatus.ALLOWED)); Object o = new ClassWithMethods(); Method method = o.getClass().getMethod("testOneChildAssociationRef", new Class[] { ChildAssociationRef.class }); AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisorAdapterRegistry.wrap(new Interceptor("ACL_NODE.0.sys:base.Read"))); proxyFactory.setTargetSource(new SingletonTargetSource(o)); Object proxy = proxyFactory.getProxy(); method.invoke(proxy, new Object[] { nodeService.getPrimaryParent(rootNodeRef) }); }