/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.interceptor.internal;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import org.strecks.interceptor.AfterInterceptor;
import org.strecks.interceptor.BeforeInterceptor;
import org.strecks.interceptor.annotation.AfterInterceptors;
import org.strecks.interceptor.annotation.BeforeInterceptors;
import org.strecks.util.ReflectHelper;
/**
* Implements default strategy for reading and instantiating interceptors. Looks first for
* <code>BeforeInterceptors</code> annotation, then for <code>AfterInterceptors</code>
* annotation
* @author Phil Zoio
*/
public class DefaultInterceptorReader implements InterceptorReader
{
private List<BeforeInterceptor> beforeInterceptors = null;
private List<AfterInterceptor> afterInterceptors = null;
public void readInterceptors(Annotation annotation, Class actionBeanClass)
{
if (annotation instanceof BeforeInterceptors)
{
readBeforeInterceptors((BeforeInterceptors) annotation, actionBeanClass);
}
else if (annotation instanceof AfterInterceptors)
{
readAfterInterceptors((AfterInterceptors) annotation, actionBeanClass);
}
}
void readBeforeInterceptors(BeforeInterceptors interceptors, Class actionBeanClass)
{
Class[] classes = interceptors.classes();
for (Class clazz : classes)
{
BeforeInterceptor instance = ReflectHelper.createInstance(clazz, BeforeInterceptor.class);
addBeforeInterceptor(instance);
}
}
void readAfterInterceptors(AfterInterceptors interceptors, Class actionBeanClass)
{
Class[] classes = interceptors.classes();
for (Class clazz : classes)
{
AfterInterceptor instance = ReflectHelper.createInstance(clazz, AfterInterceptor.class);
addAfterInterceptor(instance);
}
}
void addBeforeInterceptor(BeforeInterceptor instance)
{
if (instance != null)
{
if (beforeInterceptors == null)
beforeInterceptors = new ArrayList<BeforeInterceptor>();
beforeInterceptors.add(instance);
}
}
void addAfterInterceptor(AfterInterceptor instance)
{
if (instance != null)
{
if (afterInterceptors == null)
afterInterceptors = new ArrayList<AfterInterceptor>();
afterInterceptors.add(instance);
}
}
public List<? extends BeforeInterceptor> getBeforeInterceptors()
{
return beforeInterceptors;
}
public List<? extends AfterInterceptor> getAfterInterceptors()
{
return afterInterceptors;
}
}