View Javadoc
1   package org.apache.fulcrum.yaafi.interceptor.baseservice;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A base service providing common functionality for interceptors
39   *
40   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
41   */
42  
43  public class BaseInterceptorServiceImpl
44      extends AbstractLogEnabled
45      implements AvalonInterceptorService, Contextualizable, Reconfigurable
46  {
47      /** this matches all services */
48      private static final String WILDCARD = "*";
49  
50      /** contains the services being monitored by the interceptor */
51      private HashSet<String> serviceSet;
52  
53      /** is the interceptor service enabled */
54      private boolean isEnabled;
55  
56      /** The name of the service as defined in the role configuration file */
57      private String serviceName;
58  
59      /** The service manager supplied by the Avalon framework */
60      private ServiceManager serviceManager;
61  
62      /** the Avalon application directory */
63      private File serviceApplicationDir;
64  
65      /** the Avalon temp directory */
66      private File serviceTempDir;
67  
68      /** the supplied class loader */
69      private ClassLoader classLoader;
70  
71  
72      /////////////////////////////////////////////////////////////////////////
73      // Avalon Service Lifecycle Implementation
74      /////////////////////////////////////////////////////////////////////////
75  
76      /**
77       * Constructor
78       */
79      public BaseInterceptorServiceImpl()
80      {
81          this.serviceSet = new HashSet<String>();
82      }
83  
84      /**
85       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
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       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
97       */
98      public void configure(Configuration configuration) throws ConfigurationException
99      {
100         // take care - the default is disabled which is helpful
101         // for the way we use the interceptors
102 
103         this.isEnabled = configuration.getChild("isEnabled").getValueAsBoolean(false);
104 
105         // parse the service to be monitored
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      * @see org.apache.avalon.framework.configuration.Reconfigurable#reconfigure(org.apache.avalon.framework.configuration.Configuration)
135      */
136     public void reconfigure(Configuration configuration) throws ConfigurationException
137     {
138         this.getServiceSet().clear();
139     }
140 
141     /////////////////////////////////////////////////////////////////////////
142     // Service interface implementation
143     /////////////////////////////////////////////////////////////////////////
144 
145     /**
146      * @see org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorService#onEntry(org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorContext)
147      */
148     public void onEntry(AvalonInterceptorContext avalonInterceptorContext)
149     {
150         // nothing to do
151     }
152 
153     /**
154      * @see org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorService#onError(org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorContext, java.lang.Throwable)
155      */
156     public void onError(AvalonInterceptorContext avalonInterceptorContext,Throwable t)
157     {
158         // nothing to do
159     }
160 
161     /**
162      * @see org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorService#onExit(org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorContext, java.lang.Object)
163      */
164     public void onExit(AvalonInterceptorContext avalonInterceptorContext, Object result)
165     {
166         // nothing to do
167     }
168 
169     /////////////////////////////////////////////////////////////////////////
170     // Service Implementation
171     /////////////////////////////////////////////////////////////////////////
172 
173     /**
174      * @return Returns the isEnabled.
175      */
176     protected boolean isEnabled()
177     {
178         return isEnabled;
179     }
180 
181     /**
182      * Determine if the given service is monitored.
183      *
184      * @param avalonInterceptorContext interceptor context
185      * @return true if the service is monitored or false otherwise
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      * @return Returns the serviceApplicationDir.
213      */
214     protected File getServiceApplicationDir()
215     {
216         return serviceApplicationDir;
217     }
218 
219     /**
220      * @return Returns the serviceManager.
221      */
222     protected ServiceManager getServiceManager()
223     {
224         return serviceManager;
225     }
226 
227     /**
228      * @return Returns the serviceName.
229      */
230     protected String getServiceName()
231     {
232         return serviceName;
233     }
234 
235     /**
236      * @return Returns the serviceTempDir.
237      */
238     protected File getServiceTempDir()
239     {
240         return serviceTempDir;
241     }
242 
243     /**
244 		 * @return Returns the classLoader.
245 		 */
246 		protected ClassLoader getClassLoader() {
247 			return this.classLoader;
248 		}
249 
250 		/**
251      * Determines the file location of the given name. If the name denotes
252      * a relative file location it will be resolved using the application
253      * home directory.
254      *
255      * @param name the filename
256      * @return the file
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      * @return Returns the serviceMap.
272      */
273     private HashSet<String> getServiceSet()
274     {
275         return serviceSet;
276     }
277 }