001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.framework.adapter;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.aopalliance.aop.Advice;
024: import org.aopalliance.intercept.MethodInterceptor;
025:
026: import org.springframework.aop.Advisor;
027: import org.springframework.aop.support.DefaultPointcutAdvisor;
028:
029: /**
030: * Default implementation of the {@link AdvisorAdapterRegistry} interface.
031: * Supports {@link org.aopalliance.intercept.MethodInterceptor},
032: * {@link org.springframework.aop.MethodBeforeAdvice},
033: * {@link org.springframework.aop.AfterReturningAdvice},
034: * {@link org.springframework.aop.ThrowsAdvice}.
035: *
036: * @author Rod Johnson
037: * @author Rob Harrop
038: * @author Juergen Hoeller
039: */
040: public class DefaultAdvisorAdapterRegistry implements
041: AdvisorAdapterRegistry, Serializable {
042:
043: private final List adapters = new ArrayList(3);
044:
045: /**
046: * Create a new DefaultAdvisorAdapterRegistry, registering well-known adapters.
047: */
048: public DefaultAdvisorAdapterRegistry() {
049: registerAdvisorAdapter(new MethodBeforeAdviceAdapter());
050: registerAdvisorAdapter(new AfterReturningAdviceAdapter());
051: registerAdvisorAdapter(new ThrowsAdviceAdapter());
052: }
053:
054: public Advisor wrap(Object adviceObject)
055: throws UnknownAdviceTypeException {
056: if (adviceObject instanceof Advisor) {
057: return (Advisor) adviceObject;
058: }
059: if (!(adviceObject instanceof Advice)) {
060: throw new UnknownAdviceTypeException(adviceObject);
061: }
062: Advice advice = (Advice) adviceObject;
063: if (advice instanceof MethodInterceptor) {
064: // So well-known it doesn't even need an adapter.
065: return new DefaultPointcutAdvisor(advice);
066: }
067: for (int i = 0; i < this .adapters.size(); i++) {
068: // Check that it is supported.
069: AdvisorAdapter adapter = (AdvisorAdapter) this .adapters
070: .get(i);
071: if (adapter.supportsAdvice(advice)) {
072: return new DefaultPointcutAdvisor(advice);
073: }
074: }
075: throw new UnknownAdviceTypeException(advice);
076: }
077:
078: public MethodInterceptor[] getInterceptors(Advisor advisor)
079: throws UnknownAdviceTypeException {
080: List interceptors = new ArrayList(3);
081: Advice advice = advisor.getAdvice();
082: if (advice instanceof MethodInterceptor) {
083: interceptors.add(advice);
084: }
085: for (int i = 0; i < this .adapters.size(); i++) {
086: AdvisorAdapter adapter = (AdvisorAdapter) this .adapters
087: .get(i);
088: if (adapter.supportsAdvice(advice)) {
089: interceptors.add(adapter.getInterceptor(advisor));
090: }
091: }
092: if (interceptors.isEmpty()) {
093: throw new UnknownAdviceTypeException(advisor.getAdvice());
094: }
095: return (MethodInterceptor[]) interceptors
096: .toArray(new MethodInterceptor[interceptors.size()]);
097: }
098:
099: public void registerAdvisorAdapter(AdvisorAdapter adapter) {
100: this.adapters.add(adapter);
101: }
102:
103: }
|