01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.plugin;
18:
19: import java.lang.reflect.InvocationHandler;
20: import java.lang.reflect.Proxy;
21: import java.util.List;
22:
23: import javax.xml.namespace.QName;
24:
25: import org.apache.commons.lang.ClassUtils;
26: import org.springframework.beans.BeansException;
27: import org.springframework.beans.factory.config.BeanPostProcessor;
28:
29: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
30: import edu.iu.uis.eden.util.ClassLoaderUtils;
31:
32: /**
33: * A {@link BeanPostProcessor} which is used to allow for service overrides from the
34: * Institutional Plugin.
35: *
36: * @author ewestfal
37: */
38: public class ServiceOverrideAutoProxyCreator implements
39: BeanPostProcessor {
40:
41: private String[] beanNames;
42:
43: public Object postProcessBeforeInitialization(Object bean,
44: String beanName) throws BeansException {
45: return bean;
46: }
47:
48: public Object postProcessAfterInitialization(Object bean,
49: String beanName) throws BeansException {
50: if (!isMatch(beanName)) {
51: return bean;
52: }
53: try {
54: return createOverrideProxy(beanName, bean);
55: } catch (NoSuchMethodException e) {
56: throw new WorkflowRuntimeException(
57: "Failed to instantiate service override proxy for bean '"
58: + beanName + "'.");
59: }
60: }
61:
62: public void setBeanNames(String[] beanNames) {
63: this .beanNames = beanNames;
64: }
65:
66: protected boolean isMatch(String beanName) {
67: for (String validBeanName : beanNames) {
68: if (beanName.equals(validBeanName)) {
69: return true;
70: }
71: }
72: return false;
73: }
74:
75: protected Object createOverrideProxy(String beanName, Object bean)
76: throws NoSuchMethodException {
77: InvocationHandler handler = new ServiceOverrideProxy(new QName(
78: beanName), bean);
79: return Proxy.newProxyInstance(ClassLoaderUtils
80: .getDefaultClassLoader(), getInterfacesToProxy(bean),
81: handler);
82: }
83:
84: @SuppressWarnings("unchecked")
85: protected static Class[] getInterfacesToProxy(Object proxiedObject) {
86: List interfaces = ClassUtils.getAllInterfaces(proxiedObject
87: .getClass());
88: // introduce the OverridableService interface into the mix
89: interfaces.add(OverridableService.class);
90: Class[] interfaceArray = new Class[interfaces.size()];
91: return (Class[]) interfaces.toArray(interfaceArray);
92: }
93:
94: }
|