001// Copyright 2009, 2010, 2011 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 015package org.apache.tapestry5.ioc.internal; 016 017import java.lang.reflect.Method; 018import java.util.List; 019import java.util.Set; 020 021import org.apache.tapestry5.ioc.IdMatcher; 022import org.apache.tapestry5.ioc.def.ServiceDef; 023import org.apache.tapestry5.ioc.internal.util.CollectionFactory; 024import org.apache.tapestry5.ioc.internal.util.InternalUtils; 025import org.apache.tapestry5.ioc.services.PlasticProxyFactory; 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 */ 034public class AbstractServiceInstrumenter 035{ 036 protected final Method method; 037 038 protected final IdMatcher idMatcher; 039 040 protected final String[] constraints; 041 042 protected final PlasticProxyFactory proxyFactory; 043 044 private final Set<Class> markers; 045 046 private final Class serviceInterface; 047 048 public AbstractServiceInstrumenter(Method method, String[] patterns, String[] constraints, Class serviceInterface, 049 Set<Class> markers, PlasticProxyFactory proxyFactory) 050 { 051 this.method = method; 052 this.serviceInterface = serviceInterface; 053 this.markers = markers; 054 this.proxyFactory = proxyFactory; 055 056 assert patterns != null; 057 058 List<IdMatcher> matchers = CollectionFactory.newList(); 059 060 for (String pattern : patterns) 061 { 062 IdMatcher matcher = new IdMatcherImpl(pattern); 063 matchers.add(matcher); 064 } 065 066 idMatcher = new OrIdMatcher(matchers); 067 068 this.constraints = constraints != null ? constraints : new String[0]; 069 } 070 071 @Override 072 public String toString() 073 { 074 return InternalUtils.asString(method, proxyFactory); 075 } 076 077 public String[] getConstraints() 078 { 079 return constraints; 080 } 081 082 /** 083 * Returns true if <em>any</em> provided pattern matches the id of the service. 084 */ 085 public boolean matches(ServiceDef serviceDef) 086 { 087 String serviceId = serviceDef.getServiceId(); 088 089 return idMatcher.matches(serviceId); 090 } 091 092 public Set<Class> getMarkers() 093 { 094 return markers; 095 } 096 097 public Class getServiceInterface() 098 { 099 return serviceInterface; 100 } 101}