View Javadoc

1   /*
2    * Copyright 2004 Charles Blaxland
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 mnemosyne.core;
17  
18  import mnemosyne.aop.MethodCall;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.lang.reflect.Method;
23  
24  
25  /***
26   * @version $Id: MethodInterceptor.java,v 1.1.1.1 2004/08/07 06:41:03 charlesblaxland Exp $
27   */
28  public class MethodInterceptor
29  {
30      private static final Log log = LogFactory.getLog(MethodInterceptor.class);
31  
32      public Object intercept(final MethodCall methodCall) throws Throwable
33      {
34          Object originalTarget = methodCall.getTarget();
35          Persistable persistable = (Persistable) originalTarget;
36  
37          if (persistable.isPersistent())
38          {
39              if (log.isTraceEnabled())
40              {
41                  log.trace("Intercepted method call on persistent object: " + originalTarget.getClass().getName() + "." + methodCall.getMethodName());
42              }
43  
44              Method method = methodCall.getMethod();
45              PersistentObject persistentObject = persistable.getPersistentObject();
46              boolean isMutating = isMutatingMethod(method);
47  
48              Object target = isMutating ? persistentObject.findWritableTargetObject() : persistentObject.findTargetObject();
49  
50              if (target != originalTarget)
51              {
52                  methodCall.setTarget(target);
53              }
54          }
55  
56          return methodCall.invoke();
57      }
58  
59      protected static boolean isMutatingMethod(Method method)
60      {
61          // TODO
62          return true;
63      }
64  }