KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > services > impl > TrackerFactory


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 hivemind.test.services.impl;
16
17 import java.lang.reflect.InvocationHandler JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.lang.reflect.Proxy JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.hivemind.InterceptorStack;
24 import org.apache.hivemind.internal.Module;
25 import org.apache.hivemind.service.ClassFabUtils;
26
27 /**
28  * Used with the unit tests.
29  *
30  * @author Howard Lewis Ship
31  */

32 public class TrackerFactory
33 {
34     private static final List JavaDoc _list = new ArrayList JavaDoc();
35
36     private String JavaDoc _name;
37
38     private class TrackerHandler implements InvocationHandler JavaDoc
39     {
40         private TrackerHandler(Object JavaDoc inner)
41         {
42             _inner = inner;
43         }
44
45         private Object JavaDoc _inner;
46
47         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
48         {
49             // For some reason, testing inside Eclipse, logging is getting turned
50
// on and toString() is getting invoked ... doesn't happen using
51
// command-line testing, though. A mystery.
52

53             if (!ClassFabUtils.isToString(method))
54                 _list.add(_name + ":" + method.getName());
55
56             return method.invoke(_inner, args);
57         }
58
59     }
60
61     public static void reset()
62     {
63         _list.clear();
64     }
65
66     public static List JavaDoc getInvocations()
67     {
68         return _list;
69     }
70
71     public void createInterceptor(
72         InterceptorStack stack,
73         Module contributingModule,
74         Object JavaDoc parameters)
75     {
76         Class JavaDoc interfaceClass = stack.getServiceInterface();
77         ClassLoader JavaDoc loader = contributingModule.getClassResolver().getClassLoader();
78
79         Object JavaDoc top = stack.peek();
80
81         Object JavaDoc interceptor =
82             Proxy.newProxyInstance(loader, new Class JavaDoc[] { interfaceClass }, new TrackerHandler(top));
83
84         stack.push(interceptor);
85     }
86
87     public String JavaDoc getName()
88     {
89         return _name;
90     }
91
92     public void setName(String JavaDoc string)
93     {
94         _name = string;
95     }
96
97 }
98
Popular Tags