KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > metadata > ModifiableAdvisor


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.aop.framework.autoproxy.metadata;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.springframework.aop.framework.AopConfigException;
26 import org.springframework.aop.support.DelegatingIntroductionInterceptor;
27 import org.springframework.aop.support.DefaultIntroductionAdvisor;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.metadata.Attributes;
30
31 /**
32  *
33  * @author Rod Johnson
34  */

35 public class ModifiableAdvisor extends DefaultIntroductionAdvisor implements InitializingBean {
36
37     protected final Log log = LogFactory.getLog(getClass());
38     
39     private Attributes attributes;
40
41     public ModifiableAdvisor() {
42         super(new ModifiableIntroductionInterceptor(), Modifiable.class);
43     }
44
45
46     public void setAttributes(Attributes atts) {
47         this.attributes = atts;
48     }
49
50     private boolean matches(Collection JavaDoc c) {
51         //log.info("Checking for modifiable attribute; atts.length=" + atts.size());
52
for (Iterator JavaDoc itr = c.iterator(); itr.hasNext(); ) {
53             Object JavaDoc next = itr.next();
54             if (next instanceof ModifiableAttribute) {
55                 log.info("FOUND modifiable attribute " + next);
56                 return true;
57             }
58         }
59         return false;
60     }
61
62     /**
63      * @see org.springframework.core.Ordered#getOrder()
64      */

65     public int getOrder() {
66         throw new UnsupportedOperationException JavaDoc();
67     }
68
69
70     /**
71      * @see org.springframework.aop.ClassFilter#matches(java.lang.Class)
72      */

73     public boolean matches(Class JavaDoc targetClass) {
74         log.info("Checking for mod attribute on " + targetClass + " attributes=" + attributes);
75         Collection JavaDoc atts = this.attributes.getAttributes(targetClass);
76         return matches(atts);
77     }
78
79
80
81     private static class ModifiableIntroductionInterceptor
82         extends DelegatingIntroductionInterceptor
83         implements Modifiable {
84             
85         private boolean dirty = false;
86         /**
87          * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
88          */

89         public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
90             if (!isMethodOnIntroducedInterface(mi)) {
91                 if (!mi.getMethod().getName().startsWith("get")) {
92                     this.dirty = true;
93                 }
94             }
95             return super.invoke(mi);
96         }
97
98         /**
99              * @see org.springframework.enterpriseservices.mod.Modifable#isModified()
100              */

101         public boolean isModified() {
102             logger.info("isModified");
103             return this.dirty;
104         }
105
106         public void acceptChanges() {
107             logger.info("Accepting changes");
108             this.dirty = false;
109         }
110     }
111
112     /**
113      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
114      */

115     public void afterPropertiesSet() throws Exception JavaDoc {
116         if (this.attributes == null)
117             throw new AopConfigException("Must set Attributes property on ModifiableAdvice");
118     }
119 }
120
Popular Tags