1 | // SECTION-START[License Header] |
2 | // <editor-fold defaultstate="collapsed" desc=" Generated License "> |
3 | /* |
4 | * Copyright (c) 2010 The JOMC Project |
5 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * |
12 | * o Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * |
15 | * o Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in |
17 | * the documentation and/or other materials provided with the |
18 | * distribution. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
27 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
30 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | * |
32 | * $Id: DefaultInvoker.java 1346 2010-01-19 12:16:43Z schulte2005 $ |
33 | * |
34 | */ |
35 | // </editor-fold> |
36 | // SECTION-END |
37 | package org.jomc.ri; |
38 | |
39 | import java.lang.reflect.InvocationTargetException; |
40 | import org.jomc.model.Instance; |
41 | import org.jomc.spi.Invocation; |
42 | import org.jomc.spi.Invoker; |
43 | |
44 | // SECTION-START[Documentation] |
45 | // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> |
46 | /** |
47 | * Default {@code Invoker} implementation. |
48 | * |
49 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0 |
50 | * @version $Id: DefaultInvoker.java 1346 2010-01-19 12:16:43Z schulte2005 $ |
51 | */ |
52 | // </editor-fold> |
53 | // SECTION-END |
54 | // SECTION-START[Annotations] |
55 | // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> |
56 | @javax.annotation.Generated( value = "org.jomc.tools.JavaSources", |
57 | comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-15/jomc-tools" ) |
58 | // </editor-fold> |
59 | // SECTION-END |
60 | public class DefaultInvoker implements Invoker |
61 | { |
62 | // SECTION-START[Invoker] |
63 | |
64 | /** |
65 | * Performs a method invocation on an object. |
66 | * <p>This method first passes the given invocation to the {@code preInvoke} method. If the result property of the |
67 | * invocation returned by the {@code preInvoke} method is an instance of {@code Throwable}, that instance will be |
68 | * thrown; otherwise the invocation returned by the {@code preInvoke} method is performed and then passed to the |
69 | * {@code postInvoke} method. If the result property of the invocation returned from the {@code postInvoke} method |
70 | * is an instance of {@code Throwable}, that instance will be thrown; otherwise the value of the result property is |
71 | * returned by this method.</p> |
72 | * |
73 | * @param invocation The invocation to perform. |
74 | * |
75 | * @return The return value of the invocation. If the declared return type of the method of the invocation is a |
76 | * primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper |
77 | * class; otherwise, it must be a type assignable to the declared return type of the method of the invocation. |
78 | * If the value returned by this method is {@code null} and the declared return type of the method of the invocation |
79 | * is primitive, then a {@code NullPointerException} will be thrown. If the value returned by this method is |
80 | * otherwise not compatible to the declared return type of the method of the invocation, a |
81 | * {@code ClassCastException} will be thrown. |
82 | * |
83 | * @throws Throwable The exception thrown from the method invocation. The exception's type must be assignable |
84 | * either to any of the exception types declared in the {@code throws} clause of the method of the invocation or to |
85 | * the unchecked exception types {@code java.lang.RuntimeException} or {@code java.lang.Error}. |
86 | * If a checked exception is thrown by this method that is not assignable to any of the exception types declared in |
87 | * the {@code throws} clause of the method of the invocation, then an {@code UndeclaredThrowableException} |
88 | * containing the exception that was thrown by this method will be thrown. |
89 | * |
90 | * @see #preInvoke(org.jomc.spi.Invocation) |
91 | * @see #postInvoke(org.jomc.spi.Invocation) |
92 | */ |
93 | public Object invoke( final Invocation invocation ) throws Throwable |
94 | { |
95 | Invocation current = invocation; |
96 | final Instance instance = (Instance) current.getContext().get( DefaultInvocation.INSTANCE_KEY ); |
97 | |
98 | try |
99 | { |
100 | if ( instance != null && instance.isStateless() ) |
101 | { |
102 | try |
103 | { |
104 | current = this.preInvoke( current ); |
105 | } |
106 | catch ( final Throwable t ) |
107 | { |
108 | this.handleException( current, t ); |
109 | } |
110 | |
111 | if ( !( current.getResult() instanceof Throwable ) ) |
112 | { |
113 | try |
114 | { |
115 | current.setResult( current.getMethod().invoke( current.getObject(), current.getArguments() ) ); |
116 | } |
117 | catch ( final Throwable t ) |
118 | { |
119 | this.handleException( current, t ); |
120 | } |
121 | } |
122 | |
123 | try |
124 | { |
125 | current = this.postInvoke( current ); |
126 | } |
127 | catch ( final Throwable t ) |
128 | { |
129 | this.handleException( current, t ); |
130 | } |
131 | |
132 | if ( current.getResult() instanceof Throwable ) |
133 | { |
134 | throw (Throwable) current.getResult(); |
135 | } |
136 | |
137 | return current.getResult(); |
138 | } |
139 | else |
140 | { |
141 | synchronized ( invocation.getObject() ) |
142 | { |
143 | try |
144 | { |
145 | current = this.preInvoke( current ); |
146 | } |
147 | catch ( final Throwable t ) |
148 | { |
149 | this.handleException( current, t ); |
150 | } |
151 | |
152 | if ( !( current.getResult() instanceof Throwable ) ) |
153 | { |
154 | try |
155 | { |
156 | current.setResult( current.getMethod().invoke( current.getObject(), |
157 | current.getArguments() ) ); |
158 | |
159 | } |
160 | catch ( final Throwable t ) |
161 | { |
162 | this.handleException( current, t ); |
163 | } |
164 | } |
165 | |
166 | try |
167 | { |
168 | current = this.postInvoke( current ); |
169 | } |
170 | catch ( final Throwable t ) |
171 | { |
172 | this.handleException( current, t ); |
173 | } |
174 | |
175 | if ( current.getResult() instanceof Throwable ) |
176 | { |
177 | throw (Throwable) current.getResult(); |
178 | } |
179 | |
180 | return current.getResult(); |
181 | } |
182 | } |
183 | } |
184 | finally |
185 | { |
186 | invocation.getContext().clear(); |
187 | } |
188 | } |
189 | |
190 | // SECTION-END |
191 | // SECTION-START[DefaultInvoker] |
192 | /** |
193 | * Called before an invocation is performed. |
194 | * <p>Overriding classes may use this method to perform any kind of operation prior to an invocation and to create |
195 | * custom invocation instances. If an overriding class wishes to throw an exception, it may do so by setting the |
196 | * result property of the returned invocation to an instance of {@code Throwable} thrown as the result of the |
197 | * invocation. If an overriding class wishes to provide a custom {@code Invocation} class, it may do so by returning |
198 | * a different instance from this method. By default, this method does nothing and returns the given invocation |
199 | * unchanged.</p> |
200 | * |
201 | * @param invocation The invocation about to be performed. |
202 | * |
203 | * @return The processed invocation. |
204 | * |
205 | * @throws NullPointerException if {@code invocation} is {@code null}. |
206 | */ |
207 | public Invocation preInvoke( final Invocation invocation ) |
208 | { |
209 | if ( invocation == null ) |
210 | { |
211 | throw new NullPointerException( "invocation" ); |
212 | } |
213 | |
214 | return invocation; |
215 | } |
216 | |
217 | /** |
218 | * Called after an invocation has been performed. |
219 | * <p>Overriding classes may use this method to perform any kind of operation after an invocation has been |
220 | * performed and to maintain custom invocation instances. If an overriding class wishes to throw an exception, it |
221 | * may do so by setting the result property of the returned invocation to an instance of {@code Throwable} thrown as |
222 | * the result of the invocation. Since the result property of the given invocation already holds the result of the |
223 | * invocation (which may already be an instance of {@code Throwable}), care must be taken when updating that result. |
224 | * By default, this method does nothing and returns the given invocation unchanged.</p> |
225 | * |
226 | * @param invocation The performed invocation. |
227 | * |
228 | * @return The processed invocation. |
229 | * |
230 | * @throws NullPointerException if {@code invocation} is {@code null}. |
231 | */ |
232 | public Invocation postInvoke( final Invocation invocation ) |
233 | { |
234 | if ( invocation == null ) |
235 | { |
236 | throw new NullPointerException( "invocation" ); |
237 | } |
238 | |
239 | return invocation; |
240 | } |
241 | |
242 | /** |
243 | * Called whenever an exception has been caught. |
244 | * <p>Overriding classes may use this method for handling exceptions. By default, this method updates the result of |
245 | * the given invocation with the given throwable. If that throwable is an instance of |
246 | * {@code InvocationTargetException}, this method updates the result with the value of that exception's target |
247 | * exception. If the result of the given invocation already is an instance of {@code Throwable}, this method does |
248 | * not update the result.</p> |
249 | * |
250 | * @param invocation The invocation to update. |
251 | * @param t The throwable to update {@code invocation} with. |
252 | */ |
253 | public void handleException( final Invocation invocation, final Throwable t ) |
254 | { |
255 | if ( invocation != null && !( invocation.getResult() instanceof Throwable ) ) |
256 | { |
257 | if ( t instanceof InvocationTargetException && |
258 | ( (InvocationTargetException) t ).getTargetException() != null ) |
259 | { |
260 | invocation.setResult( ( (InvocationTargetException) t ).getTargetException() ); |
261 | return; |
262 | } |
263 | |
264 | invocation.setResult( t ); |
265 | } |
266 | } |
267 | |
268 | // SECTION-END |
269 | // SECTION-START[Constructors] |
270 | // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> |
271 | |
272 | /** Creates a new {@code DefaultInvoker} instance. */ |
273 | @javax.annotation.Generated( value = "org.jomc.tools.JavaSources", |
274 | comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-15/jomc-tools" ) |
275 | public DefaultInvoker() |
276 | { |
277 | // SECTION-START[Default Constructor] |
278 | super(); |
279 | // SECTION-END |
280 | } |
281 | // </editor-fold> |
282 | // SECTION-END |
283 | // SECTION-START[Dependencies] |
284 | // SECTION-END |
285 | // SECTION-START[Properties] |
286 | // SECTION-END |
287 | // SECTION-START[Messages] |
288 | // SECTION-END |
289 | } |