001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.webbeans.cfg;
031:
032: import java.util.*;
033: import java.lang.reflect.*;
034: import java.lang.annotation.*;
035: import javax.webbeans.*;
036: import javax.interceptor.*;
037:
038: import com.caucho.config.*;
039: import com.caucho.util.*;
040:
041: /**
042: * Configuration for the xml interceptor.
043: */
044: public class WbInterceptor {
045: private static final L10N L = new L10N(WbInterceptor.class);
046:
047: private Class _cl;
048:
049: private ArrayList<WbBinding> _bindingList = new ArrayList<WbBinding>();
050:
051: private Method _invokeMethod;
052:
053: WbInterceptor(Class cl) {
054: if (!cl.isAnnotationPresent(Interceptor.class))
055: throw new ConfigException(
056: L
057: .l(
058: "'{0}' must have an @Interceptor annotation to be declared as an interceptor.",
059: cl.getName()));
060:
061: _cl = cl;
062:
063: for (Annotation ann : cl.getAnnotations()) {
064: if (ann.annotationType().isAnnotationPresent(
065: InterceptorBindingType.class)) {
066: _bindingList.add(new WbBinding(ann));
067: }
068: }
069:
070: if (_bindingList.size() == 0) {
071: throw new ConfigException(
072: L
073: .l(
074: "'{0}' must have at least one @InterceptorBindingType annotation to be declared as an interceptor.",
075: cl.getName()));
076: }
077:
078: for (Method method : cl.getDeclaredMethods()) {
079: if (method.isAnnotationPresent(AroundInvoke.class)) {
080: if (_invokeMethod != null) {
081: throw new ConfigException(
082: L
083: .l(
084: "'{0}' has two @AroundInvoke methods: '{1}' and '{2}'.",
085: cl.getName(),
086: _invokeMethod, method));
087: }
088:
089: _invokeMethod = method;
090: }
091: }
092:
093: if (_invokeMethod == null) {
094: throw new ConfigException(
095: L
096: .l(
097: "'{0}' must have at least one @AroundInvoke method",
098: cl.getName()));
099: }
100: }
101:
102: public Class getInterceptorClass() {
103: return _cl;
104: }
105:
106: public Method getMethod() {
107: return _invokeMethod;
108: }
109:
110: public Object getObject() {
111: try {
112: return _cl.newInstance();
113: } catch (RuntimeException e) {
114: throw e;
115: } catch (Exception e) {
116: throw ConfigException.create(e);
117: }
118: }
119:
120: public boolean isMatch(ArrayList<Annotation> bindList) {
121: for (int i = 0; i < _bindingList.size(); i++) {
122: if (!isMatch(_bindingList.get(i), bindList))
123: return false;
124: }
125:
126: return true;
127: }
128:
129: /**
130: * Returns true if at least one of this component's bindings match
131: * the injection binding.
132: */
133: public boolean isMatch(WbBinding binding,
134: ArrayList<Annotation> bindList) {
135: for (int i = 0; i < bindList.size(); i++) {
136: if (binding.isMatch(bindList.get(i)))
137: return true;
138: }
139:
140: return false;
141: }
142: }
|