1 | /* |
2 | * Copyright 2007 brunella ltd |
3 | * |
4 | * Licensed under the LGPL Version 3 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * |
7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
17 | * THE POSSIBILITY OF SUCH DAMAGE. |
18 | */ |
19 | package sf.qof.customizer; |
20 | |
21 | import static sf.qof.codegen.Constants.SIG_getConnection; |
22 | import static sf.qof.codegen.Constants.SIG_setConnection; |
23 | import static sf.qof.codegen.Constants.TYPE_Connection; |
24 | import static sf.qof.codegen.Constants.TYPE_RuntimeException; |
25 | |
26 | import java.lang.reflect.Method; |
27 | import java.lang.reflect.Modifier; |
28 | |
29 | import net.sf.cglib.core.ClassEmitter; |
30 | import net.sf.cglib.core.CodeEmitter; |
31 | import net.sf.cglib.core.Constants; |
32 | |
33 | /** |
34 | * Provides the default implementation of a ConnectionFactoryCustomizer. |
35 | * |
36 | * This customizer implements getConnection() and setConnection() methods and |
37 | * a private field to store it if the super class does not have a <code>getConnection()</code> |
38 | * method defined. |
39 | * |
40 | * @see ConnectionFactoryCustomizer |
41 | */ |
42 | public class DefaultConnectionFactoryCustomizer implements ConnectionFactoryCustomizer { |
43 | |
44 | private static final String FIELD_NAME_CONNECTION = "$connection"; |
45 | |
46 | public void emitFields(Class<?> queryDefinitionClass, Class<?> superClass, ClassEmitter ce) { |
47 | if (!methodExists(superClass, "getConnection", null)) { |
48 | ce.declare_field(Constants.ACC_PRIVATE, FIELD_NAME_CONNECTION, TYPE_Connection, null, null); |
49 | } |
50 | } |
51 | |
52 | public void emitGetConnection(Class<?> queryDefinitionClass, Class<?> superClass, ClassEmitter ce) { |
53 | if (!methodExists(superClass, "getConnection", null)) { |
54 | CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_getConnection, null, null); |
55 | co.load_this(); |
56 | co.getfield(FIELD_NAME_CONNECTION); |
57 | co.return_value(); |
58 | co.end_method(); |
59 | } |
60 | } |
61 | |
62 | public void emitSetConnection(Class<?> queryDefinitionClass, Class<?> superClass, ClassEmitter ce) { |
63 | if (!methodExists(superClass, "getConnection", null)) { |
64 | CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_setConnection, null, null); |
65 | co.load_this(); |
66 | co.load_arg(0); |
67 | co.putfield(FIELD_NAME_CONNECTION); |
68 | co.return_value(); |
69 | co.end_method(); |
70 | } else if (!methodExists(superClass, "setConnection", new Class<?>[] {java.sql.Connection.class})) { |
71 | CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_setConnection, null, null); |
72 | co.throw_exception(TYPE_RuntimeException, "Connection cannot be set"); |
73 | co.end_method(); |
74 | } |
75 | } |
76 | |
77 | private boolean methodExists(Class<?> superClass, String methodName, Class<?>[] parameterTypes) { |
78 | try { |
79 | Method method = superClass.getMethod(methodName, parameterTypes); |
80 | int modifiers = method.getModifiers(); |
81 | if (Modifier.isPrivate(modifiers) || Modifier.isAbstract(modifiers)) { |
82 | return false; |
83 | } |
84 | if (method.getReturnType() != java.sql.Connection.class) { |
85 | return false; |
86 | } |
87 | return true; |
88 | } catch (SecurityException e) { |
89 | } catch (NoSuchMethodException e) { |
90 | } |
91 | return false; |
92 | } |
93 | } |