1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.emarket.utils;
23
24 import java.io.IOException;
25 import java.lang.reflect.Method;
26 import java.util.Enumeration;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 public class SpringDependencyInjectionServlet extends HttpServlet {
35
36 private static final Logger logger =
37 Logger.getLogger(SpringDependencyInjectionServlet.class.getName());
38
39
40 protected void service(HttpServletRequest request,
41 HttpServletResponse response) throws ServletException, IOException {
42 Enumeration attributeNames = getServletContext().getAttributeNames();
43
44 while (attributeNames.hasMoreElements()) {
45
46 String name = (String) attributeNames.nextElement();
47 logger.log(Level.INFO, "attempting to autowire " + name);
48
49 autowire(name);
50 }
51
52 super.service(request, response);
53 }
54
55 protected boolean autowireByType() {
56 return true;
57 }
58
59 private void autowire(String name) {
60 if (name != null) {
61 Object attribute = getServletContext().getAttribute(name);
62 Class c = getClass();
63 while (c != null && c != c.getSuperclass()) {
64 try {
65 if (autowireByType()) {
66 if (byType(c, attribute)) {
67 break;
68 }
69 else {
70 c = c.getSuperclass();
71 }
72 }
73 else {
74 if (byName(c, name, attribute)) {
75 break;
76 }
77 else {
78 c = c.getSuperclass();
79 }
80 }
81 }
82 catch (NoSuchMethodException e) {
83 c = c.getSuperclass();
84 }
85 }
86 }
87 }
88
89 private boolean byName(Class c, String name, Object attribute)
90 throws NoSuchMethodException {
91 boolean success = false;
92
93 if (attribute != null) {
94
95 Method[] methods = c.getDeclaredMethods();
96 for (Method method : methods) {
97 if (method.getName().equals(getMethodName(name))) {
98 Class[] paramTypes = method.getParameterTypes();
99
100 if (paramTypes.length == 1) {
101 success = invokeSpringBeanSetter(method, attribute);
102 }
103 }
104 }
105 }
106 return success;
107 }
108
109 private boolean byType(Class c, Object attribute) {
110 boolean success = false;
111
112 if (attribute != null) {
113 Method[] methods = c.getDeclaredMethods();
114
115 for (Method method : methods) {
116 Class[] paramTypes = method.getParameterTypes();
117
118 Class attributeClass = attribute.getClass();
119 if (paramTypes.length == 1 &&
120 paramTypes[0].equals(attributeClass)) {
121 boolean succeeded = invokeSpringBeanSetter(method,attribute);
122 if (!success && succeeded) {
123 success = succeeded;
124 }
125 }
126 }
127 }
128 return success;
129 }
130
131 private boolean invokeSpringBeanSetter(Method method, Object attribute) {
132 boolean success = false;
133 try {
134 method.invoke(this, attribute);
135 success = true;
136 }
137 catch (Exception e) {
138
139 }
140 return success;
141 }
142
143 private String getMethodName(String contextName) {
144 return contextName.toUpperCase();
145 }
146 }
147