1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.aop.framework;
  17. import org.aopalliance.intercept.Interceptor;
  18. import org.springframework.aop.support.AopUtils;
  19. /**
  20. * Factory for AOP proxies for programmatic use, rather than via a bean
  21. * factory. This class provides a simple way of obtaining and configuring
  22. * AOP proxies in code.
  23. * @since 14-Mar-2003
  24. * @author Rod Johnson
  25. * @version $Id: ProxyFactory.java,v 1.12 2004/04/01 15:35:46 jhoeller Exp $
  26. */
  27. public class ProxyFactory extends AdvisedSupport {
  28. public ProxyFactory() {
  29. }
  30. /**
  31. * Proxy all interfaces of the given target.
  32. */
  33. public ProxyFactory(Object target) throws AopConfigException {
  34. if (target == null) {
  35. throw new AopConfigException("Can't proxy null object");
  36. }
  37. setInterfaces(AopUtils.getAllInterfaces(target));
  38. setTarget(target);
  39. }
  40. /**
  41. * No target, only interfaces. Must add interceptors.
  42. */
  43. public ProxyFactory(Class[] interfaces) {
  44. setInterfaces(interfaces);
  45. }
  46. /**
  47. * Create new proxy according to the settings in this factory.
  48. * Can be called repeatedly. Effect will vary if we've added
  49. * or removed interfaces. Can add and remove "interceptors"
  50. * @return Object
  51. */
  52. public Object getProxy() {
  53. AopProxy proxy = createAopProxy();
  54. return proxy.getProxy();
  55. }
  56. /**
  57. * Create new proxy for the given interface and interceptor.
  58. * Convenience method for creating a proxy for a single interceptor.
  59. * @param proxyInterface the interface that the proxy should implement
  60. * @param interceptor the interceptor that the proxy should invoke
  61. * @return the new proxy
  62. */
  63. public static Object getProxy(Class proxyInterface, Interceptor interceptor) {
  64. ProxyFactory proxyFactory = new ProxyFactory();
  65. proxyFactory.addInterface(proxyInterface);
  66. proxyFactory.addInterceptor(interceptor);
  67. return proxyFactory.getProxy();
  68. }
  69. }