KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > services > StringHolderModule


1 // Copyright 2007 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;
16
17 import hivemind.test.services.impl.StringHolderImpl;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.util.Collections JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.InterceptorStack;
24 import org.apache.hivemind.definition.InterceptorConstructor;
25 import org.apache.hivemind.definition.InterceptorDefinition;
26 import org.apache.hivemind.definition.ServicePointDefinition;
27 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper;
28 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
29 import org.apache.hivemind.definition.impl.InterceptorDefinitionImpl;
30 import org.apache.hivemind.impl.DefaultClassResolver;
31 import org.apache.hivemind.internal.AbstractServiceInterceptorConstructor;
32 import org.apache.hivemind.internal.Module;
33 import org.apache.hivemind.service.ClassFactory;
34 import org.apache.hivemind.service.impl.LoggingInterceptorClassFactory;
35
36 /**
37  * Defines a module with one threaded service. The service is intercepted by a LoggingInterceptor.
38  */

39 public class StringHolderModule extends ModuleDefinitionImpl
40 {
41     public StringHolderModule(String JavaDoc serviceModel)
42     {
43         super("hivemind.test.services", null, new DefaultClassResolver(), null);
44         
45         ModuleDefinitionHelper helper = new ModuleDefinitionHelper(this);
46         ServicePointDefinition sp = helper.addServicePoint("StringHolder", StringHolder.class.getName());
47         helper.addSimpleServiceImplementation(sp, StringHolderImpl.class.getName(), serviceModel);
48         
49         InterceptorConstructor constructor = new AbstractServiceInterceptorConstructor(getLocation()) {
50
51             public void constructServiceInterceptor(InterceptorStack interceptorStack, Module contributingModule)
52             {
53                 ClassFactory cf = (ClassFactory) contributingModule.getService(ClassFactory.class);
54                 // Create the interceptor with the LoggingInterceptorClassFactory which is quite uncomfortable
55
// in the moment
56
LoggingInterceptorClassFactory f = new LoggingInterceptorClassFactory(cf);
57                 Class JavaDoc interceptorClass = f.constructInterceptorClass(interceptorStack, Collections.EMPTY_LIST);
58                 Constructor JavaDoc c = interceptorClass.getConstructors()[0];
59                 Object JavaDoc interceptor;
60                 try
61                 {
62                     interceptor = c.newInstance(new Object JavaDoc[] { interceptorStack.getServiceLog(), interceptorStack.peek() });
63                 }
64                 catch (Exception JavaDoc e) {
65                     throw new ApplicationRuntimeException(e);
66                 }
67                 interceptorStack.push(interceptor);
68             }};
69         InterceptorDefinition interceptor = new InterceptorDefinitionImpl(helper.getModule(), "hivemind.LoggingInterceptor", getLocation(), constructor);
70         sp.addInterceptor(interceptor);
71     }
72 }
73
Popular Tags