1 package org.apache.fulcrum.yaafi.framework.interceptor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.Proxy;
24 import java.util.List;
25
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants;
29 import org.apache.fulcrum.yaafi.framework.reflection.Clazz;
30 import org.apache.fulcrum.yaafi.framework.util.Validate;
31
32
33
34
35
36
37
38
39 public class AvalonInterceptorFactory
40 {
41
42
43
44
45
46
47
48
49
50
51
52 public static Object create(
53 String serviceName,
54 String serviceShorthand,
55 ServiceManager serviceManager,
56 String[] serviceInterceptorList,
57 Object serviceDelegate )
58 throws ServiceException
59 {
60 Validate.notEmpty(serviceName,"serviceName");
61 Validate.notEmpty(serviceShorthand,"serviceShorthand");
62 Validate.notNull(serviceManager,"serviceManager");
63 Validate.notNull(serviceInterceptorList,"serviceInterceptorList");
64 Validate.notNull(serviceDelegate,"serviceDelegate");
65
66 Object result;
67
68 Class<? extends Object> clazz = serviceDelegate.getClass();
69 ClassLoader classLoader = clazz.getClassLoader();
70 List<?> interfaceList = Clazz.getAllInterfaces(clazz);
71
72
73
74 AvalonInterceptorService[] avalonInterceptorServices = resolve(
75 serviceManager,
76 serviceInterceptorList
77 );
78
79 InvocationHandler invocationHandler = new AvalonInterceptorInvocationHandler(
80 serviceName,
81 serviceShorthand,
82 serviceDelegate,
83 avalonInterceptorServices
84 );
85
86 result = Proxy.newProxyInstance(
87 classLoader,
88 (Class[]) interfaceList.toArray(new Class[interfaceList.size()]),
89 invocationHandler
90 );
91
92 return result;
93 }
94
95
96
97
98
99
100
101
102
103 private static AvalonInterceptorService[] resolve( ServiceManager serviceManager, String[] interceptorList )
104 throws ServiceException
105 {
106 String interceptorServiceName;
107 AvalonInterceptorService[] result = new AvalonInterceptorService[interceptorList.length];
108
109 for( int i=0; i<interceptorList.length; i++ )
110 {
111 interceptorServiceName = interceptorList[i];
112 Object currService = serviceManager.lookup(interceptorServiceName);
113
114 if (currService instanceof AvalonInterceptorService)
115 {
116 result[i] = (AvalonInterceptorService) currService;
117 }
118 else
119 {
120 String msg = "The following service is not an AvalonInterceptorService : " + interceptorServiceName;
121 throw new ServiceException(AvalonYaafiConstants.AVALON_CONTAINER_YAAFI,msg);
122 }
123 }
124
125 return result;
126 }
127 }