001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.aop;
020
021import java.lang.annotation.Annotation;
022
023import org.apache.shiro.SecurityUtils;
024import org.apache.shiro.subject.Subject;
025
026
027/**
028 * Base support class for implementations that reads and processes JSR-175 annotations.
029 *
030 * @since 0.9.0
031 */
032public abstract class AnnotationHandler {
033
034    /**
035     * The type of annotation this handler will process.
036     */
037    protected Class<? extends Annotation> annotationClass;
038
039    /**
040     * Constructs an <code>AnnotationHandler</code> who processes annotations of the
041     * specified type.  Immediately calls {@link #setAnnotationClass(Class)}.
042     *
043     * @param annotationClass the type of annotation this handler will process.
044     */
045    public AnnotationHandler(Class<? extends Annotation> annotationClass) {
046        setAnnotationClass(annotationClass);
047    }
048
049    /**
050     * Returns the {@link org.apache.shiro.subject.Subject Subject} associated with the currently-executing code.
051     * <p/>
052     * This default implementation merely calls
053     * <code>{@link org.apache.shiro.SecurityUtils#getSubject SecurityUtils.getSubject()}</code>.
054     *
055     * @return the {@link org.apache.shiro.subject.Subject Subject} associated with the currently-executing code.
056     */
057    protected Subject getSubject() {
058        return SecurityUtils.getSubject();
059    }
060
061    /**
062     * Sets the type of annotation this handler will inspect and process.
063     *
064     * @param annotationClass the type of annotation this handler will process.
065     * @throws IllegalArgumentException if the argument is <code>null</code>.
066     */
067    protected void setAnnotationClass(Class<? extends Annotation> annotationClass)
068            throws IllegalArgumentException {
069        if (annotationClass == null) {
070            String msg = "annotationClass argument cannot be null";
071            throw new IllegalArgumentException(msg);
072        }
073        this.annotationClass = annotationClass;
074    }
075
076    /**
077     * Returns the type of annotation this handler inspects and processes.
078     *
079     * @return the type of annotation this handler inspects and processes.
080     */
081    public Class<? extends Annotation> getAnnotationClass() {
082        return this.annotationClass;
083    }
084
085}