KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > InterceptorStackImpl


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.impl;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.hivemind.ApplicationRuntimeException;
19 import org.apache.hivemind.InterceptorStack;
20 import org.apache.hivemind.definition.InterceptorDefinition;
21 import org.apache.hivemind.internal.Module;
22 import org.apache.hivemind.internal.ServicePoint;
23 import org.apache.hivemind.util.ToStringBuilder;
24
25 /**
26  * Implementation of the {@link org.apache.hivemind.InterceptorStack} interface; localizes
27  * error checking in one place.
28  *
29  * @author Howard Lewis Ship
30  */

31 public final class InterceptorStackImpl implements InterceptorStack
32 {
33     private final Log _log;
34
35     private InterceptorDefinition _interceptorDefinition;
36     private ServicePoint _sep;
37     private Class JavaDoc _interfaceClass;
38     private Object JavaDoc _top;
39
40     public InterceptorStackImpl(Log log, ServicePoint sep, Object JavaDoc root)
41     {
42         _log = log;
43         _sep = sep;
44         _top = root;
45         _interfaceClass = sep.getServiceInterface();
46     }
47
48     public String JavaDoc toString()
49     {
50         ToStringBuilder builder = new ToStringBuilder(this);
51         builder.append("contribution", _interceptorDefinition);
52         builder.append("interfaceClass", _interfaceClass);
53         builder.append("top", _top);
54
55         return builder.toString();
56     }
57
58     public String JavaDoc getServiceExtensionPointId()
59     {
60         return _sep.getExtensionPointId();
61     }
62
63     public Module getServiceModule()
64     {
65         return _sep.getModule();
66     }
67
68     public Class JavaDoc getServiceInterface()
69     {
70         return _interfaceClass;
71     }
72
73     public Object JavaDoc peek()
74     {
75         return _top;
76     }
77
78     public void push(Object JavaDoc interceptor)
79     {
80         if (interceptor == null)
81             throw new ApplicationRuntimeException(
82                 ImplMessages.nullInterceptor(_interceptorDefinition, _sep),
83                 _interceptorDefinition.getLocation(),
84                 null);
85
86         if (!_interfaceClass.isAssignableFrom(interceptor.getClass()))
87             throw new ApplicationRuntimeException(
88                 ImplMessages.interceptorDoesNotImplementInterface(
89                     interceptor,
90                     _interceptorDefinition,
91                     _sep,
92                     _interfaceClass),
93                 _interceptorDefinition.getLocation(),
94                 null);
95
96         _top = interceptor;
97     }
98
99     /**
100      * Invoked to process the next interceptor contribution; these should
101      * be processed in ascending order.
102      *
103      */

104
105     public void process(InterceptorDefinition interceptorDefinition)
106     {
107         if (_log.isDebugEnabled())
108             _log.debug("Applying interceptor factory " + interceptorDefinition.getName());
109
110         // And now we can finally do this!
111
try
112         {
113             _interceptorDefinition = interceptorDefinition;
114             Module contributingModule = getServiceModule().getRegistry().getModule(interceptorDefinition.getModuleId());
115             _interceptorDefinition.getInterceptorConstructor().constructServiceInterceptor(this, contributingModule);
116         }
117         finally
118         {
119             _interceptorDefinition = null;
120         }
121     }
122
123     public Log getServiceLog()
124     {
125         return _log;
126     }
127 }
128
Popular Tags