01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.integration.spring;
16:
17: import java.lang.reflect.Method;
18: import java.lang.reflect.Proxy;
19: import org.araneaframework.Environment;
20: import org.araneaframework.core.Assert;
21: import org.araneaframework.core.util.ClassLoaderUtil;
22: import org.araneaframework.core.util.ExceptionUtil;
23: import org.araneaframework.integration.spring.support.SpringBeanInvocationHandler;
24: import org.springframework.beans.factory.BeanFactory;
25:
26: public class SpringInjectionUtil {
27: public static void injectBeans(Environment env, Object object) {
28: Method[] methods = object.getClass().getMethods();
29: for (int i = 0; i < methods.length; i++) {
30: if (methods[i].getName().startsWith("inject")) {
31: String beanName = methods[i].getName().substring(6);
32: beanName = beanName.substring(0, 1).toLowerCase()
33: + beanName.substring(1);
34:
35: Assert.isTrue(
36: methods[i].getParameterTypes().length == 1,
37: "Injection method '" + methods[i].toString()
38: + "' has more than one parameter!");
39: Assert.isTrue(methods[i].getParameterTypes()[0]
40: .isInterface(), "Injection method '"
41: + methods[i].toString()
42: + "()' parameter is not an interface!");
43:
44: BeanFactory bf = (BeanFactory) env
45: .getEntry(BeanFactory.class);
46:
47: Assert.isTrue(bf.containsBean(beanName),
48: "Injection method '" + methods[i].toString()
49: + "' describes missing bean '"
50: + beanName + "'!");
51:
52: try {
53: methods[i].invoke(object, new Object[] { Proxy
54: .newProxyInstance(ClassLoaderUtil
55: .getDefaultClassLoader(),
56: new Class[] { methods[i]
57: .getParameterTypes()[0] },
58: new SpringBeanInvocationHandler(
59: env, beanName)) });
60: } catch (Exception e) {
61: ExceptionUtil.uncheckException(e);
62: }
63: }
64: }
65: }
66: }
|