001/*
002 * Copyright 2002-2015 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 *      https://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
017package org.springframework.aop.aspectj;
018
019import java.io.Serializable;
020
021import org.springframework.core.Ordered;
022import org.springframework.util.Assert;
023
024/**
025 * Implementation of {@link AspectInstanceFactory} that is backed by a
026 * specified singleton object, returning the same instance for every
027 * {@link #getAspectInstance()} call.
028 *
029 * @author Rod Johnson
030 * @author Juergen Hoeller
031 * @since 2.0
032 * @see SimpleAspectInstanceFactory
033 */
034@SuppressWarnings("serial")
035public class SingletonAspectInstanceFactory implements AspectInstanceFactory, Serializable {
036
037        private final Object aspectInstance;
038
039
040        /**
041         * Create a new SingletonAspectInstanceFactory for the given aspect instance.
042         * @param aspectInstance the singleton aspect instance
043         */
044        public SingletonAspectInstanceFactory(Object aspectInstance) {
045                Assert.notNull(aspectInstance, "Aspect instance must not be null");
046                this.aspectInstance = aspectInstance;
047        }
048
049
050        @Override
051        public final Object getAspectInstance() {
052                return this.aspectInstance;
053        }
054
055        @Override
056        public ClassLoader getAspectClassLoader() {
057                return this.aspectInstance.getClass().getClassLoader();
058        }
059
060        /**
061         * Determine the order for this factory's aspect instance,
062         * either an instance-specific order expressed through implementing
063         * the {@link org.springframework.core.Ordered} interface,
064         * or a fallback order.
065         * @see org.springframework.core.Ordered
066         * @see #getOrderForAspectClass
067         */
068        @Override
069        public int getOrder() {
070                if (this.aspectInstance instanceof Ordered) {
071                        return ((Ordered) this.aspectInstance).getOrder();
072                }
073                return getOrderForAspectClass(this.aspectInstance.getClass());
074        }
075
076        /**
077         * Determine a fallback order for the case that the aspect instance
078         * does not express an instance-specific order through implementing
079         * the {@link org.springframework.core.Ordered} interface.
080         * <p>The default implementation simply returns {@code Ordered.LOWEST_PRECEDENCE}.
081         * @param aspectClass the aspect class
082         */
083        protected int getOrderForAspectClass(Class<?> aspectClass) {
084                return Ordered.LOWEST_PRECEDENCE;
085        }
086
087}