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 <code>{@link org.apache.shiro.SecurityUtils#getSubject SecurityUtils.getSubject()}</code>.
053     *
054     * @return the {@link org.apache.shiro.subject.Subject Subject} associated with the currently-executing code.
055     */
056    protected Subject getSubject() {
057        return SecurityUtils.getSubject();
058    }
059
060    /**
061     * Sets the type of annotation this handler will inspect and process.
062     *
063     * @param annotationClass the type of annotation this handler will process.
064     * @throws IllegalArgumentException if the argument is <code>null</code>.
065     */
066    protected void setAnnotationClass(Class<? extends Annotation> annotationClass)
067            throws IllegalArgumentException {
068        if (annotationClass == null) {
069            String msg = "annotationClass argument cannot be null";
070            throw new IllegalArgumentException(msg);
071        }
072        this.annotationClass = annotationClass;
073    }
074
075    /**
076     * Returns the type of annotation this handler inspects and processes.
077     *
078     * @return the type of annotation this handler inspects and processes.
079     */
080    public Class<? extends Annotation> getAnnotationClass() {
081        return this.annotationClass;
082    }
083
084}