View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
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 net.sf.oness.common.model.dao;
17  
18  import java.io.Serializable;
19  import java.lang.reflect.Method;
20  
21  import net.sf.oness.common.model.bo.AuditableBusinessObject;
22  
23  import org.aopalliance.intercept.MethodInterceptor;
24  import org.aopalliance.intercept.MethodInvocation;
25  import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
26  
27  /***
28   * AOP Advisor implementing an auditing DAO
29   * 
30   * @author Carlos Sanchez
31   * @version $Revision: 1.3 $
32   */
33  public class AuditableDaoAdvisor extends StaticMethodMatcherPointcutAdvisor
34          implements MethodInterceptor {
35  
36      public AuditableDaoAdvisor() {
37          super();
38          setAdvice(this);
39      }
40  
41      /***
42       * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
43       */
44      public Object invoke(MethodInvocation invocation) throws Throwable {
45          String name = invocation.getMethod().getName();
46          AuditableDao dao = (AuditableDao) invocation.getThis();
47  
48          /* create */
49          if (name.equals("create"))
50              return AuditingDaoHelper.create(dao,
51                      (AuditableBusinessObject) invocation.getArguments()[0]);
52  
53          /* update */
54          if (name.equals("update"))
55              return AuditingDaoHelper.update(dao,
56                      (AuditableBusinessObject) invocation.getArguments()[0]);
57  
58          /* delete */
59          if (name.equals("delete")) {
60              AuditingDaoHelper.delete(dao, (Serializable) invocation
61                      .getArguments()[0]);
62              return null;
63          }
64  
65          /* else */
66          return invocation.proceed();
67      }
68  
69      /***
70       * @see org.springframework.aop.MethodMatcher#matches(java.lang.reflect.Method,
71       *      java.lang.Class)
72       */
73      public boolean matches(Method m, Class targetClass) {
74          return AuditableDao.class.isAssignableFrom(targetClass);
75      }
76  
77  }