KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > LoggingInterceptorFactory


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service.impl;
16
17 import java.lang.reflect.Constructor JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.InterceptorStack;
22 import org.apache.hivemind.ServiceInterceptorFactory;
23 import org.apache.hivemind.internal.Module;
24 import org.apache.hivemind.service.ClassFactory;
25 import org.apache.hivemind.service.MethodContribution;
26
27 /**
28  * {@link ServiceInterceptorFactory} for logging interceptors that base on
29  * {@link LoggingInterceptorClassFactory}.
30  *
31  * @author Achim Huegen
32  */

33 public class LoggingInterceptorFactory implements ServiceInterceptorFactory
34 {
35     private ClassFactory _factory;
36     private String JavaDoc _serviceId;
37
38     /**
39      * Untyped version of {@link #createInterceptor(InterceptorStack, Module, List)}.
40      */

41     public void createInterceptor(
42         InterceptorStack stack,
43         Module contributingModule,
44         Object JavaDoc parameters)
45     {
46         createInterceptor(stack, contributingModule, (List JavaDoc) parameters);
47     }
48
49     /**
50      * Creates the interceptor.
51      * The class that is created is cached; if an interceptor is requested
52      * for the same extension point, then the previously constructed class
53      * is reused (this can happen with the threaded service model, for example,
54      * when a thread-local service implementation is created for different threads).
55      * @param parameters list with instances of {@link MethodContribution}. If empty all methods are intercepted.
56      */

57     public void createInterceptor(
58             InterceptorStack stack,
59             Module contributingModule,
60             List JavaDoc parameters)
61     {
62         LoggingInterceptorClassFactory classFactory = new LoggingInterceptorClassFactory(_factory);
63         Class JavaDoc interceptorClass = classFactory.constructInterceptorClass(stack, (List JavaDoc) parameters);
64
65         try
66         {
67             Object JavaDoc interceptor = instantiateInterceptor(stack, interceptorClass);
68
69             stack.push(interceptor);
70         }
71         catch (Exception JavaDoc ex)
72         {
73             throw new ApplicationRuntimeException(
74                 ServiceMessages.errorInstantiatingInterceptor(
75                     _serviceId,
76                     stack,
77                     interceptorClass,
78                     ex),
79                 ex);
80         }
81     }
82
83     private Object JavaDoc instantiateInterceptor(InterceptorStack stack, Class JavaDoc interceptorClass)
84         throws Exception JavaDoc
85     {
86         Object JavaDoc stackTop = stack.peek();
87
88         Constructor JavaDoc c = interceptorClass.getConstructors()[0];
89
90         return c.newInstance(new Object JavaDoc[] { stack.getServiceLog(), stackTop });
91     }
92
93     public void setFactory(ClassFactory factory)
94     {
95         _factory = factory;
96     }
97
98     public void setServiceId(String JavaDoc string)
99     {
100         _serviceId = string;
101     }
102 }
103
Popular Tags