001    package org.apache.fulcrum.yaafi.interceptor.baseservice;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.util.HashSet;
024    
025    import org.apache.avalon.framework.configuration.Configuration;
026    import org.apache.avalon.framework.configuration.ConfigurationException;
027    import org.apache.avalon.framework.configuration.Reconfigurable;
028    import org.apache.avalon.framework.context.Context;
029    import org.apache.avalon.framework.context.ContextException;
030    import org.apache.avalon.framework.context.Contextualizable;
031    import org.apache.avalon.framework.logger.AbstractLogEnabled;
032    import org.apache.avalon.framework.service.ServiceManager;
033    import org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorContext;
034    import org.apache.fulcrum.yaafi.framework.interceptor.AvalonInterceptorService;
035    import org.apache.fulcrum.yaafi.framework.util.StringUtils;
036    
037    /**
038     * A base service providing common functionality for interceptors
039     *
040     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
041     */
042    
043    public class BaseInterceptorServiceImpl
044        extends AbstractLogEnabled
045        implements AvalonInterceptorService, Contextualizable, Reconfigurable
046    {
047        /** this matches all services */
048        private static final String WILDCARD = "*";
049    
050        /** contains the services being monitored by the interceptor */
051        private HashSet serviceSet;
052    
053        /** is the interceptor service enabled */
054        private boolean isEnabled;
055    
056        /** The name of the service as defined in the role configuration file */
057        private String serviceName;
058    
059        /** The service manager supplied by the Avalon framework */
060        private ServiceManager serviceManager;
061    
062        /** the Avalon application directory */
063        private File serviceApplicationDir;
064    
065        /** the Avalon temp directory */
066        private File serviceTempDir;
067    
068        /** the supplied class loader */
069        private ClassLoader classLoader;
070    
071    
072        /////////////////////////////////////////////////////////////////////////
073        // Avalon Service Lifecycle Implementation
074        /////////////////////////////////////////////////////////////////////////
075    
076        /**
077         * Constructor
078         */
079        public BaseInterceptorServiceImpl()
080        {
081            this.serviceSet = new HashSet();
082        }
083    
084        /**
085         * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
086         */
087        public void contextualize(Context context) throws ContextException
088        {
089            this.serviceName = (String) context.get("urn:avalon:name");
090            this.serviceApplicationDir = (File) context.get("urn:avalon:home");
091            this.serviceTempDir = (File) context.get("urn:avalon:temp");
092            this.classLoader = (ClassLoader) context.get("urn:avalon:classloader");
093        }
094    
095        /**
096         * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
097         */
098        public void configure(Configuration configuration) throws ConfigurationException
099        {
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 getServiceSet()
274        {
275            return serviceSet;
276        }
277    }