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 */
019 package org.apache.shiro.aop;
020
021 import java.lang.annotation.Annotation;
022
023 import org.apache.shiro.SecurityUtils;
024 import org.apache.shiro.subject.Subject;
025
026
027 /**
028 * Base support class for implementations that reads and processes JSR-175 annotations.
029 *
030 * @author Les Hazlewood
031 * @since 0.9.0
032 */
033 public abstract class AnnotationHandler {
034
035 /**
036 * The type of annotation this handler will process.
037 */
038 protected Class<? extends Annotation> annotationClass;
039
040 /**
041 * Constructs an <code>AnnotationHandler</code> who processes annotations of the
042 * specified type. Immediately calls {@link #setAnnotationClass(Class)}.
043 *
044 * @param annotationClass the type of annotation this handler will process.
045 */
046 public AnnotationHandler(Class<? extends Annotation> annotationClass) {
047 setAnnotationClass(annotationClass);
048 }
049
050 /**
051 * Returns the {@link org.apache.shiro.subject.Subject Subject} associated with the currently-executing code.
052 * <p/>
053 * This default implementation merely calls <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 }