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_RuntimeException; |
24 | import net.sf.cglib.core.ClassEmitter; |
25 | import net.sf.cglib.core.CodeEmitter; |
26 | import net.sf.cglib.core.Constants; |
27 | import net.sf.cglib.core.Signature; |
28 | |
29 | import org.objectweb.asm.Type; |
30 | |
31 | import sf.qof.session.UseSessionContext; |
32 | |
33 | /** |
34 | * Provides the implementation of a ConnectionFactoryCustomizer to use the |
35 | * SessionContextFactory. |
36 | * |
37 | * This customizer implements <code>getConnection()</code> and |
38 | * <code>setConnection()</code> methods. It delegates calls to |
39 | * <code>getConnection()</code> to |
40 | * <code>SessionContextFactory.getContext().getConnection()</code>. Calls to |
41 | * <code>setConnection()</code> throws a RuntimeException. |
42 | * |
43 | * The <code>UseSessionContext</code> annotation indicates that this |
44 | * customizer should be used and allows to specify a session context name. |
45 | * |
46 | * @see sf.qof.session.SessionContextFactory |
47 | * @see sf.qof.session.UseSessionContext |
48 | */ |
49 | public class SessionContextConnectionFactoryCustomizer implements ConnectionFactoryCustomizer { |
50 | |
51 | private static final Type TYPE_SessionContextFactory = Type.getType("Lsf/qof/session/SessionContextFactory;"); |
52 | private static final Type TYPE_SessionContext = Type.getType("Lsf/qof/session/SessionContext;"); |
53 | private static final Signature SIG_getContext = new Signature("getContext", "()Lsf/qof/session/SessionContext;"); |
54 | private static final Signature SIG_getContextWithName = new Signature("getContext", "(Ljava/lang/String;)Lsf/qof/session/SessionContext;"); |
55 | |
56 | public void emitFields(Class<?> queryDefinitionClass, Class<?> superClass, ClassEmitter ce) { |
57 | // no fields needed |
58 | } |
59 | |
60 | public void emitGetConnection(Class<?> queryDefinitionClass, Class<?> superClass, ClassEmitter ce) { |
61 | CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_getConnection, null, null); |
62 | if (queryDefinitionClass.isAnnotationPresent(UseSessionContext.class)) { |
63 | UseSessionContext sessionContextAnnotation = queryDefinitionClass.getAnnotation(UseSessionContext.class); |
64 | co.push(sessionContextAnnotation.name()); |
65 | co.invoke_static(TYPE_SessionContextFactory, SIG_getContextWithName); |
66 | } else { |
67 | co.invoke_static(TYPE_SessionContextFactory, SIG_getContext); |
68 | } |
69 | co.invoke_interface(TYPE_SessionContext, SIG_getConnection); |
70 | co.return_value(); |
71 | co.end_method(); |
72 | } |
73 | |
74 | public void emitSetConnection(Class<?> queryDefinitionClass, Class<?> superClass, ClassEmitter ce) { |
75 | CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_setConnection, null, null); |
76 | co.throw_exception(TYPE_RuntimeException, "Connection cannot be set"); |
77 | co.end_method(); |
78 | } |
79 | |
80 | } |