001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.interceptor;
020:
021: import org.apache.beehive.netui.util.config.bean.CustomPropertyConfig;
022: import org.apache.beehive.netui.util.internal.DiscoveryUtils;
023: import org.apache.beehive.netui.util.logging.Logger;
024:
025: import java.io.Serializable;
026: import java.util.List;
027:
028: /**
029: * <p>
030: * Context object that encapsulates the configuration for an entire interceptor chain.
031: * </p>
032: */
033: public class InterceptorContext implements Serializable {
034: private static final Logger _log = Logger
035: .getInstance(InterceptorContext.class);
036:
037: private Object _resultOverride;
038: private Interceptor _overridingInterceptor;
039:
040: public void setResultOverride(Object newResult,
041: Interceptor interceptor) {
042: _resultOverride = newResult;
043: _overridingInterceptor = interceptor;
044: }
045:
046: public boolean hasResultOverride() {
047: return _overridingInterceptor != null;
048: }
049:
050: public Object getResultOverride() {
051: return _resultOverride;
052: }
053:
054: public Interceptor getOverridingInterceptor() {
055: return _overridingInterceptor;
056: }
057:
058: /**
059: * Add an {@link Interceptor} to the list of interceptors.
060: * @param configBeans an array of JavaBeans that configure a set of interceptors
061: * @param interceptorsList the list of {@link Interceptor} instances
062: * @param baseClassOrInterface
063: */
064: protected static void addInterceptors(
065: org.apache.beehive.netui.util.config.bean.InterceptorConfig[] configBeans,
066: List/*< Interceptor >*/interceptorsList,
067: Class baseClassOrInterface) {
068: if (configBeans != null) {
069: for (int i = 0; i < configBeans.length; i++) {
070: org.apache.beehive.netui.util.config.bean.InterceptorConfig configBean = configBeans[i];
071: String className = configBean.getInterceptorClass();
072: InterceptorConfig config = new InterceptorConfig(
073: className);
074: CustomPropertyConfig[] customProps = configBean
075: .getCustomProperties();
076:
077: if (customProps != null) {
078: for (int j = 0; j < customProps.length; j++) {
079: CustomPropertyConfig customProp = customProps[j];
080: config.addCustomProperty(customProp.getName(),
081: customProp.getValue());
082: }
083: }
084:
085: addInterceptor(config, baseClassOrInterface,
086: interceptorsList);
087: }
088: }
089: }
090:
091: /**
092: * Instantiates an interceptor, based on the class name in the given InterceptorConfig, and adds it to the
093: * given collection of interceptors.
094: *
095: * @param config the InterceptorConfig used to determine the interceptor class.
096: * @param baseClassOrInterface the required base class or interface. May be <code>null</code>.
097: * @param interceptors the List of interceptors to which to add.
098: * @return an initialized Interceptor, or <code>null</code> if an error occurred.
099: */
100: protected static Interceptor addInterceptor(
101: InterceptorConfig config, Class baseClassOrInterface,
102: List/*< Interceptor >*/interceptors) {
103: Interceptor interceptor = createInterceptor(config,
104: baseClassOrInterface);
105: if (interceptor != null)
106: interceptors.add(interceptor);
107: return interceptor;
108: }
109:
110: /**
111: * Instantiates an interceptor using the class name in the given InterceptorConfig.
112: *
113: * @param config the {@link InterceptorConfig} used to determine the {@link Interceptor} class.
114: * @param baseClassOrInterface the required base class or interface. May be <code>null</code>.
115: * @return an initialized Interceptor, or <code>null</code> if an error occurred.
116: */
117: protected static Interceptor createInterceptor(
118: InterceptorConfig config, Class baseClassOrInterface) {
119: assert Interceptor.class.isAssignableFrom(baseClassOrInterface) : baseClassOrInterface
120: .getName()
121: + " cannot be assigned to "
122: + Interceptor.class.getName();
123:
124: ClassLoader cl = DiscoveryUtils.getClassLoader();
125: String className = config.getInterceptorClass();
126:
127: try {
128: Class interceptorClass = cl.loadClass(className);
129:
130: if (!baseClassOrInterface
131: .isAssignableFrom(interceptorClass)) {
132: _log.error("Interceptor " + interceptorClass.getName()
133: + " does not implement or extend "
134: + baseClassOrInterface.getName());
135: return null;
136: }
137:
138: Interceptor interceptor = (Interceptor) interceptorClass
139: .newInstance();
140: interceptor.init(config);
141: return interceptor;
142: } catch (ClassNotFoundException e) {
143: _log.error("Could not find interceptor class " + className,
144: e);
145: } catch (InstantiationException e) {
146: _log.error("Could not instantiate interceptor class "
147: + className, e);
148: } catch (IllegalAccessException e) {
149: _log.error("Could not instantiate interceptor class "
150: + className, e);
151: }
152:
153: return null;
154: }
155:
156: }
|