1 package org.apache.fulcrum.yaafi.interceptor.baseservice;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.HashSet;
24
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.configuration.Reconfigurable;
28 import org.apache.avalon.framework.context.Context;
29 import org.apache.avalon.framework.context.ContextException;
30 import org.apache.avalon.framework.context.Contextualizable;
31 import org.apache.avalon.framework.logger.AbstractLogEnabled;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorContext;
34 import org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorService;
35 import org.apache.commons.lang3.StringUtils;
36
37
38
39
40
41
42
43 public class BaseInterceptorServiceImpl
44 extends AbstractLogEnabled
45 implements AvalonInterceptorService, Contextualizable, Reconfigurable
46 {
47
48 private static final String WILDCARD = "*";
49
50
51 private HashSet<String> serviceSet;
52
53
54 private boolean isEnabled;
55
56
57 private String serviceName;
58
59
60 private ServiceManager serviceManager;
61
62
63 private File serviceApplicationDir;
64
65
66 private File serviceTempDir;
67
68
69 private ClassLoader classLoader;
70
71
72
73
74
75
76
77
78
79 public BaseInterceptorServiceImpl()
80 {
81 this.serviceSet = new HashSet<String>();
82 }
83
84
85
86
87 public void contextualize(Context context) throws ContextException
88 {
89 this.serviceName = (String) context.get("urn:avalon:name");
90 this.serviceApplicationDir = (File) context.get("urn:avalon:home");
91 this.serviceTempDir = (File) context.get("urn:avalon:temp");
92 this.classLoader = (ClassLoader) context.get("urn:avalon:classloader");
93 }
94
95
96
97
98 public void configure(Configuration configuration) throws ConfigurationException
99 {
100
101
102
103 this.isEnabled = configuration.getChild("isEnabled").getValueAsBoolean(false);
104
105
106
107 Configuration[] serviceConfigList = configuration.getChild("services").getChildren("service");
108
109 if( serviceConfigList.length == 0 )
110 {
111 this.getServiceSet().add(WILDCARD);
112 }
113 else
114 {
115 for( int i=0; i<serviceConfigList.length; i++ )
116 {
117 String name = serviceConfigList[i].getAttribute("name", null);
118 String shorthand = serviceConfigList[i].getAttribute("shorthand", null);
119
120 if( !StringUtils.isEmpty(name) )
121 {
122 this.getServiceSet().add(name);
123 }
124
125 if( !StringUtils.isEmpty(shorthand) )
126 {
127 this.getServiceSet().add(shorthand);
128 }
129 }
130 }
131 }
132
133
134
135
136 public void reconfigure(Configuration configuration) throws ConfigurationException
137 {
138 this.getServiceSet().clear();
139 }
140
141
142
143
144
145
146
147
148 public void onEntry(AvalonInterceptorContext avalonInterceptorContext)
149 {
150
151 }
152
153
154
155
156 public void onError(AvalonInterceptorContext avalonInterceptorContext,Throwable t)
157 {
158
159 }
160
161
162
163
164 public void onExit(AvalonInterceptorContext avalonInterceptorContext, Object result)
165 {
166
167 }
168
169
170
171
172
173
174
175
176 protected boolean isEnabled()
177 {
178 return isEnabled;
179 }
180
181
182
183
184
185
186
187 protected boolean isServiceMonitored( AvalonInterceptorContext avalonInterceptorContext )
188 {
189 if( !this.isEnabled() )
190 {
191 return false;
192 }
193 else if( this.getServiceSet().contains(WILDCARD) )
194 {
195 return true;
196 }
197 else if( this.getServiceSet().contains(avalonInterceptorContext.getServiceName()) )
198 {
199 return true;
200 }
201 else if( this.getServiceSet().contains(avalonInterceptorContext.getServiceShorthand()) )
202 {
203 return true;
204 }
205 else
206 {
207 return false;
208 }
209 }
210
211
212
213
214 protected File getServiceApplicationDir()
215 {
216 return serviceApplicationDir;
217 }
218
219
220
221
222 protected ServiceManager getServiceManager()
223 {
224 return serviceManager;
225 }
226
227
228
229
230 protected String getServiceName()
231 {
232 return serviceName;
233 }
234
235
236
237
238 protected File getServiceTempDir()
239 {
240 return serviceTempDir;
241 }
242
243
244
245
246 protected ClassLoader getClassLoader() {
247 return this.classLoader;
248 }
249
250
251
252
253
254
255
256
257
258 protected File makeAbsoluteFile( String name )
259 {
260 File result = new File(name);
261
262 if( result.isAbsolute() == false )
263 {
264 result = new File( this.getServiceApplicationDir(), name );
265 }
266
267 return result;
268 }
269
270
271
272
273 private HashSet<String> getServiceSet()
274 {
275 return serviceSet;
276 }
277 }