View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // FinderIntroductionInterceptor.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.jee.integration.dao;
22  
23  import java.util.SortedSet;
24  import java.util.TreeSet;
25  
26  import org.aopalliance.intercept.MethodInvocation;
27  import org.perfectjpattern.jee.api.integration.dao.*;
28  import org.springframework.aop.IntroductionInterceptor;
29  
30  
31  /**
32   * Concrete implementation of {@link IntroductionInterceptor} that connects 
33   * Spring AOP with the Hibernate's based generic DAO. For any method 
34   * beginning with <code>findBy</code> this interceptor will use the 
35   * {@link IFinderExecutor} to call the corresponding Hibernate named query
36   * 
37   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
38   * @version $ $Date: Nov 6, 2008 1:53:25 PM $
39   */
40  class FinderIntroductionInterceptor<Element>
41  implements IntroductionInterceptor
42  {
43      //------------------------------------------------------------------------
44      // public
45      //------------------------------------------------------------------------
46      /** 
47       * {@inheritDoc}
48       */
49      @SuppressWarnings("unchecked")
50      public Object 
51      invoke(MethodInvocation anArgument) throws Throwable
52      {
53          IFinderExecutor<Element> myExecutor = (IFinderExecutor<Element>) 
54              anArgument.getThis();
55  
56          Object myResults = null;
57          
58          String myMethodName = anArgument.getMethod().getName();
59          if (myMethodName.startsWith("findBy") && !EXCLUDE_METHODS.contains(
60              myMethodName)) 
61          {
62              Object[] myArguments = anArgument.getArguments();
63              
64              myResults = myExecutor.execute(anArgument.getMethod(), myArguments);
65          } 
66          else 
67          {
68              myResults = anArgument.proceed();
69          }
70          
71          return myResults;
72      }
73  
74      //------------------------------------------------------------------------
75      /** 
76       * {@inheritDoc}
77       */
78      @SuppressWarnings("unchecked")
79      public boolean 
80      implementsInterface(Class anInterface)
81      {
82          boolean myResult = anInterface.isInterface() && IFinderExecutor.class.
83              isAssignableFrom(anInterface); 
84          
85          return myResult;
86      }
87      
88      //------------------------------------------------------------------------
89      // members
90      //------------------------------------------------------------------------
91      private static final SortedSet<String> EXCLUDE_METHODS = 
92          new TreeSet<String>();
93      
94      //------------------------------------------------------------------------
95      // static initializers
96      //------------------------------------------------------------------------
97      static
98      {
99          EXCLUDE_METHODS.add("findById");
100         EXCLUDE_METHODS.add("findByExample");
101     }
102 }