ModifiableAdvisor.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:3k
源码类别:

Java编程

开发平台:

Java

  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.autoproxy.metadata;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import org.aopalliance.intercept.MethodInvocation;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.springframework.aop.framework.AopConfigException;
  23. import org.springframework.aop.support.DelegatingIntroductionInterceptor;
  24. import org.springframework.aop.support.DefaultIntroductionAdvisor;
  25. import org.springframework.beans.factory.InitializingBean;
  26. import org.springframework.metadata.Attributes;
  27. /**
  28.  * 
  29.  * @author Rod Johnson
  30.  */
  31. public class ModifiableAdvisor extends DefaultIntroductionAdvisor implements InitializingBean {
  32. protected final Log log = LogFactory.getLog(getClass());
  33. private Attributes attributes;
  34. public ModifiableAdvisor() {
  35. super(new ModifiableIntroductionInterceptor(), Modifiable.class);
  36. }
  37. public void setAttributes(Attributes atts) {
  38. this.attributes = atts;
  39. }
  40. private boolean matches(Collection c) {
  41. //log.info("Checking for modifiable attribute; atts.length=" + atts.size());
  42. for (Iterator itr = c.iterator(); itr.hasNext(); ) {
  43. Object next = itr.next();
  44. if (next instanceof ModifiableAttribute) {
  45. log.info("FOUND modifiable attribute " + next);
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. /**
  52.  * @see org.springframework.core.Ordered#getOrder()
  53.  */
  54. public int getOrder() {
  55. throw new UnsupportedOperationException();
  56. }
  57. /**
  58.  * @see org.springframework.aop.ClassFilter#matches(java.lang.Class)
  59.  */
  60. public boolean matches(Class targetClass) {
  61. log.info("Checking for mod attribute on " + targetClass + " attributes=" + attributes);
  62. Collection atts = this.attributes.getAttributes(targetClass);
  63. return matches(atts);
  64. }
  65. private static class ModifiableIntroductionInterceptor
  66. extends DelegatingIntroductionInterceptor
  67. implements Modifiable {
  68. private boolean dirty = false;
  69. /**
  70.  * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
  71.  */
  72. public Object invoke(MethodInvocation mi) throws Throwable {
  73. if (!isMethodOnIntroducedInterface(mi)) {
  74. if (!mi.getMethod().getName().startsWith("get")) {
  75. this.dirty = true;
  76. }
  77. }
  78. return super.invoke(mi);
  79. }
  80. /**
  81.  * @see org.springframework.enterpriseservices.mod.Modifable#isModified()
  82.  */
  83. public boolean isModified() {
  84. logger.info("isModified");
  85. return this.dirty;
  86. }
  87. public void acceptChanges() {
  88. logger.info("Accepting changes");
  89. this.dirty = false;
  90. }
  91. }
  92. /**
  93.  * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
  94.  */
  95. public void afterPropertiesSet() throws Exception {
  96. if (this.attributes == null)
  97. throw new AopConfigException("Must set Attributes property on ModifiableAdvice");
  98. }
  99. }