1 /*
2 Copyright (c) 2003, Laurent Caillette.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modifica-
6 tion, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 3. The end-user documentation included with the redistribution, if any, must
16 include the following acknowledgment: "This product includes software
17 written by Laurent Caillette."
18 Alternately, this acknowledgment may appear in the software itself, if
19 and wherever such third-party acknowledgments normally appear.
20
21 4. The name "Laurent Caillette" must not be used to endorse or
22 promote products derived from this software without
23 prior written permission. For written permission, please contact
24 laurent.caillette@laposte.net
25
26 5. Products derived from this software may not be called
27 "Laurent Caillette", nor may "Laurent Caillette" appear
28 in their name, without prior written permission of the
29 author.
30
31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42 package tweed.system.interceptor;
43 import java.io.ByteArrayOutputStream;
44 import java.io.IOException;
45 import java.lang.reflect.Constructor;
46 import java.lang.reflect.InvocationTargetException;
47 import java.lang.reflect.Field;
48 /***
49 *
50 * @author Laurent Caillette
51 * @version $Id$
52 */
53 public final class InterceptorBuilder {
54
55 private static InterceptorBuilder instance = null ;
56
57 public static InterceptorBuilder getInstance() {
58 if( instance == null ) {
59 instance = new InterceptorBuilder() ;
60 }
61 return instance ;
62 }
63
64 public Object build(
65 Object subject,
66 InterceptionHandler interceptor
67 )
68 throws PropertyDefinitionException
69 {
70 return build(
71 subject.getClass().getClassLoader(),
72 subject,
73 interceptor
74 ) ;
75 }
76
77 public Object build(
78 java.lang.ClassLoader classLoader,
79 Object subject,
80 InterceptionHandler handler
81 )
82 throws PropertyDefinitionException
83 {
84
85 String interceptorClassName =
86 CodeBuilder.createInterceptorClassName( subject.getClass() ) ;
87 Override[] overrides =
88 new OverridesFinder( subject.getClass() ).getMethodsToOverride() ;
89
90 // HardCodedBuilder codeBuilder = new HardCodedBuilder() ;
91 CodeBuilder codeBuilder = new CodeBuilder( subject.getClass(), overrides ) ;
92
93 InterceptorClassLoader iClassLoader =
94 new InterceptorClassLoader( classLoader ) ;
95
96 ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
97 try {
98 codeBuilder.create( baos ) ;
99 } catch( IOException ex ) {
100 throw new RuntimeException( ex ) ;
101 }
102 iClassLoader.defineClass(
103 interceptorClassName,
104 baos.toByteArray()
105 ) ;
106
107 Class interceptorClass = null ;
108 try {
109 interceptorClass = iClassLoader.loadClass( interceptorClassName ) ;
110 } catch( ClassNotFoundException ex ) {
111 throw new RuntimeException( ex ) ;
112 }
113
114 Constructor cons = null ;
115 try {
116 cons = interceptorClass.getConstructor( new Class[] {
117 subject.getClass(),
118 InterceptionHandler.class
119 } ) ;
120 } catch( NoSuchMethodException ex ) {
121 throw new RuntimeException( ex );
122 } catch( SecurityException ex ) {
123 throw new RuntimeException( ex );
124 }
125
126 Object interceptor = null ;
127 try {
128 interceptor = cons.newInstance(
129 new Object[] { subject, handler } ) ;
130 } catch( InstantiationException ex ) {
131 throw new RuntimeException( ex ) ;
132 } catch( IllegalAccessException ex ) {
133 throw new RuntimeException( ex ) ;
134 } catch( IllegalArgumentException ex ) {
135 throw new RuntimeException( ex ) ;
136 } catch( InvocationTargetException ex ) {
137 throw new RuntimeException( ex ) ;
138 }
139
140 Field methodDescriptorsField = null ;
141 try {
142 methodDescriptorsField = interceptorClass.getField(
143 "methodDescriptors" ) ;
144 } catch( NoSuchFieldException ex ) {
145 throw new RuntimeException( ex ) ;
146 } catch( SecurityException ex ) {
147 throw new RuntimeException( ex ) ;
148 }
149
150 MethodDescriptor[] methodDescriptors = null ;
151 try {
152 methodDescriptors = ( MethodDescriptor[] )
153 methodDescriptorsField.get( interceptor ) ;
154 } catch( IllegalArgumentException ex ) {
155 throw new RuntimeException( ex ) ;
156 } catch( IllegalAccessException ex ) {
157 throw new RuntimeException( ex ) ;
158 }
159
160 for( int i = 0 ; i < overrides.length ; i++ ) {
161 Override override = overrides[ i ] ;
162 MethodDescriptor desc = new MethodDescriptor( override.getMethod() ) ;
163 methodDescriptors[ i ] = desc ;
164 }
165
166 return interceptor ;
167 }
168
169
170 }
This page was automatically generated by Maven