001    // Copyright 2009 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.ioc.internal;
016    
017    import org.apache.tapestry5.ioc.IdMatcher;
018    import org.apache.tapestry5.ioc.def.ServiceDef;
019    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
020    import org.apache.tapestry5.ioc.internal.util.Defense;
021    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
022    import org.apache.tapestry5.ioc.services.ClassFactory;
023    
024    import java.lang.reflect.Method;
025    import java.util.List;
026    
027    /**
028     * Abstract base class for implementations of {@link org.apache.tapestry5.ioc.ServiceDecorator} (i.e., old school) and
029     * {@link org.apache.tapestry5.ioc.ServiceAdvisor} (i.e., new school).  "Instrumenter" is a rought approximation of what
030     * these two approaches have in common: instrumenting of method calls of a service.
031     *
032     * @since 5.1.0.0
033     */
034    public class AbstractServiceInstrumenter
035    {
036        protected final Method method;
037    
038        protected final IdMatcher idMatcher;
039    
040        protected final String[] constraints;
041    
042        protected final ClassFactory classFactory;
043    
044        public AbstractServiceInstrumenter(Method method, String[] patterns, String[] constraints,
045                                           ClassFactory classFactory)
046        {
047            this.method = method;
048            this.classFactory = classFactory;
049    
050            List<IdMatcher> matchers = CollectionFactory.newList();
051    
052            for (String pattern : Defense.notNull(patterns, "patterns"))
053            {
054                IdMatcher matcher = new IdMatcherImpl(pattern);
055                matchers.add(matcher);
056            }
057    
058            idMatcher = new OrIdMatcher(matchers);
059    
060            this.constraints = constraints != null ? constraints : new String[0];
061        }
062    
063        @Override
064        public String toString()
065        {
066            return InternalUtils.asString(method, classFactory);
067        }
068    
069        public String[] getConstraints()
070        {
071            return constraints;
072        }
073    
074        /**
075         * Returns true if <em>any</em> provided pattern matches the id of the service.
076         */
077        public boolean matches(ServiceDef serviceDef)
078        {
079            String serviceId = serviceDef.getServiceId();
080    
081            return idMatcher.matches(serviceId);
082        }
083    }