View Javadoc
1   package org.apache.onami.persist;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.aopalliance.intercept.MethodInvocation;
23  
24  import javax.inject.Singleton;
25  import java.lang.reflect.Method;
26  
27  /**
28   * Reader which obtains the concrete {@link Transactional @Transactional} annotation of a method.
29   */
30  @Singleton
31  class TransactionalAnnotationReader
32  {
33  
34      /**
35       * Constant holding a transactional instance with all default values.
36       */
37      private static final Transactional DEFAULT_TRANSACTIONAL =
38          DefaultTransactional.class.getAnnotation( Transactional.class );
39  
40      /**
41       * Reads the {@link Transactional @Transactional} of a given method invocation.
42       *
43       * @param methodInvocation the method invocation for which to obtain the {@link Transactional @Transactional}.
44       * @return the {@link Transactional @Transactional} of the given method invocation. Never {@code null}.
45       */
46      Transactional readAnnotationFrom( MethodInvocation methodInvocation )
47      {
48          Transactional result;
49          final Method method = methodInvocation.getMethod();
50          result = method.getAnnotation( Transactional.class );
51          if ( null == result )
52          {
53              final Class<?> targetClass = methodInvocation.getThis().getClass();
54              result = targetClass.getAnnotation( Transactional.class );
55          }
56          if ( null == result )
57          {
58              result = DEFAULT_TRANSACTIONAL;
59          }
60          return result;
61      }
62  
63      /**
64       * Helper class for obtaining the default of {@link Transactional @Transactional}.
65       */
66      @Transactional
67      private static class DefaultTransactional
68      {
69      }
70  
71  }