| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.core.util; |
| 18 | |
|
| 19 | |
import java.beans.PropertyDescriptor; |
| 20 | |
import java.lang.reflect.AccessibleObject; |
| 21 | |
import java.lang.reflect.Field; |
| 22 | |
import java.lang.reflect.InvocationTargetException; |
| 23 | |
import java.lang.reflect.Method; |
| 24 | |
import java.util.HashMap; |
| 25 | |
import java.util.LinkedList; |
| 26 | |
import java.util.List; |
| 27 | |
import java.util.Map; |
| 28 | |
|
| 29 | |
import javax.xml.namespace.QName; |
| 30 | |
|
| 31 | |
import org.apache.commons.lang.StringUtils; |
| 32 | |
import org.apache.log4j.Logger; |
| 33 | |
import org.kuali.rice.core.config.ConfigContext; |
| 34 | |
import org.kuali.rice.core.resourceloader.GlobalResourceLoader; |
| 35 | |
import org.kuali.rice.core.resourceloader.ResourceLoader; |
| 36 | |
import org.springframework.beans.BeanUtils; |
| 37 | |
import org.springframework.beans.BeansException; |
| 38 | |
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; |
| 39 | |
import org.springframework.util.ReflectionUtils; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | public class GRLServiceInjectionPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { |
| 52 | 0 | private static final Logger LOG = Logger.getLogger(GRLServiceInjectionPostProcessor.class); |
| 53 | |
|
| 54 | |
|
| 55 | 0 | private Map<Class<?>, List<AnnotatedMember>> classMetadata = new HashMap<Class<?>, List<AnnotatedMember>>(); |
| 56 | |
|
| 57 | |
@Override |
| 58 | |
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 59 | 0 | List<AnnotatedMember> metadata = findClassMetadata(bean.getClass()); |
| 60 | 0 | for (AnnotatedMember member: metadata) { |
| 61 | 0 | Object value = member.read(bean); |
| 62 | 0 | if (value == null) { |
| 63 | 0 | Object newValue = lookupRiceService(member.service); |
| 64 | 0 | if (newValue != null) { |
| 65 | |
|
| 66 | 0 | member.inject(bean, newValue); |
| 67 | |
} |
| 68 | |
} |
| 69 | 0 | } |
| 70 | 0 | return super.postProcessBeforeInitialization(bean, beanName); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
protected Object lookupRiceService(RiceService annotation) { |
| 75 | 0 | String resourceLoader = annotation.resourceLoader(); |
| 76 | 0 | String name = annotation.name(); |
| 77 | |
|
| 78 | |
ResourceLoader rl; |
| 79 | |
|
| 80 | |
|
| 81 | 0 | if (StringUtils.isEmpty(resourceLoader)) { |
| 82 | 0 | LOG.error("Using global resource loader"); |
| 83 | 0 | rl = GlobalResourceLoader.getResourceLoader(); |
| 84 | 0 | if (rl == null) { |
| 85 | |
|
| 86 | 0 | throw new RuntimeException("Global resource loader could not be obtained"); |
| 87 | |
} |
| 88 | |
} else { |
| 89 | 0 | QName rlName = QName.valueOf(resourceLoader); |
| 90 | |
|
| 91 | 0 | if (StringUtils.isBlank(rlName.getNamespaceURI())) { |
| 92 | |
|
| 93 | 0 | rlName = new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), rlName.getLocalPart()); |
| 94 | |
} |
| 95 | 0 | rl = GlobalResourceLoader.getResourceLoader(rlName); |
| 96 | 0 | if (rl == null) { |
| 97 | 0 | throw new RuntimeException("Named resource loader not found: " + resourceLoader); |
| 98 | |
} |
| 99 | |
} |
| 100 | |
|
| 101 | 0 | LOG.error("Looking up service for injection: " + name); |
| 102 | 0 | return rl.getService(QName.valueOf(name)); |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
private synchronized List<AnnotatedMember> findClassMetadata(Class<? extends Object> clazz) { |
| 112 | 0 | List<AnnotatedMember> metadata = classMetadata.get(clazz); |
| 113 | 0 | if (metadata == null) { |
| 114 | 0 | final List<AnnotatedMember> newMetadata = new LinkedList<AnnotatedMember>(); |
| 115 | |
|
| 116 | 0 | ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { |
| 117 | |
public void doWith(Field f) throws IllegalArgumentException, IllegalAccessException { |
| 118 | 0 | addIfPresent(newMetadata, f); |
| 119 | 0 | } |
| 120 | |
}); |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | 0 | ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { |
| 125 | |
public void doWith(Method m) throws IllegalArgumentException, IllegalAccessException { |
| 126 | 0 | addIfPresent(newMetadata, m); |
| 127 | 0 | } |
| 128 | |
}); |
| 129 | |
|
| 130 | 0 | metadata = newMetadata; |
| 131 | 0 | classMetadata.put(clazz, metadata); |
| 132 | |
} |
| 133 | 0 | return metadata; |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
private void addIfPresent(List<AnnotatedMember> metadata, AccessibleObject ao) { |
| 143 | 0 | RiceService annotation = ao.getAnnotation(RiceService.class); |
| 144 | 0 | if (annotation != null) { |
| 145 | 0 | metadata.add(new AnnotatedMember(ao, annotation)); |
| 146 | |
} |
| 147 | 0 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | 0 | private final class AnnotatedMember { |
| 153 | |
private final AccessibleObject member; |
| 154 | |
private final RiceService service; |
| 155 | |
|
| 156 | 0 | public AnnotatedMember(AccessibleObject member, RiceService service) { |
| 157 | 0 | this.member = member; |
| 158 | 0 | this.service = service; |
| 159 | |
|
| 160 | 0 | if (service.name() == null) { |
| 161 | 0 | throw new IllegalArgumentException("Service name must be specified in RiceService annotation"); |
| 162 | |
} |
| 163 | 0 | } |
| 164 | |
|
| 165 | |
public Object read(Object instance) { |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
try { |
| 170 | 0 | if (!member.isAccessible()) { |
| 171 | 0 | member.setAccessible(true); |
| 172 | |
} |
| 173 | 0 | if (member instanceof Field) { |
| 174 | 0 | return ((Field) member).get(instance); |
| 175 | |
} |
| 176 | 0 | else if (member instanceof Method) { |
| 177 | 0 | PropertyDescriptor pd = BeanUtils.findPropertyForMethod((Method) member); |
| 178 | 0 | if (pd == null) { |
| 179 | 0 | throw new IllegalArgumentException("Annotated was found on a method that did not resolve to a bean property: " + member); |
| 180 | |
} |
| 181 | 0 | Method getter = pd.getReadMethod(); |
| 182 | 0 | if (getter == null) { |
| 183 | |
|
| 184 | |
|
| 185 | 0 | return null; |
| 186 | |
|
| 187 | |
} |
| 188 | 0 | return getter.invoke(instance, (Object[]) null); |
| 189 | |
} |
| 190 | |
else { |
| 191 | 0 | throw new IllegalArgumentException("Cannot read unknown AccessibleObject type " + member); |
| 192 | |
} |
| 193 | |
} |
| 194 | 0 | catch (IllegalAccessException ex) { |
| 195 | 0 | throw new IllegalArgumentException("Cannot inject member " + member, ex); |
| 196 | |
} |
| 197 | 0 | catch (InvocationTargetException ex) { |
| 198 | |
|
| 199 | 0 | throw new IllegalArgumentException("Attempt to inject setter method " + member + |
| 200 | |
" resulted in an exception", ex); |
| 201 | |
} |
| 202 | |
} |
| 203 | |
|
| 204 | |
public void inject(Object instance, Object value) { |
| 205 | |
try { |
| 206 | 0 | if (!member.isAccessible()) { |
| 207 | 0 | member.setAccessible(true); |
| 208 | |
} |
| 209 | 0 | if (member instanceof Field) { |
| 210 | 0 | ((Field) member).set(instance, value); |
| 211 | |
} |
| 212 | 0 | else if (member instanceof Method) { |
| 213 | 0 | PropertyDescriptor pd = BeanUtils.findPropertyForMethod((Method) member); |
| 214 | 0 | if (pd == null) { |
| 215 | 0 | throw new IllegalArgumentException("Annotated was found on a method that did not resolve to a bean property: " + member); |
| 216 | |
} |
| 217 | 0 | Method setter = pd.getWriteMethod(); |
| 218 | 0 | if (setter == null) { |
| 219 | 0 | throw new IllegalArgumentException("No setter found for property " + pd.getName()); |
| 220 | |
} |
| 221 | 0 | setter.invoke(instance, value); |
| 222 | 0 | } |
| 223 | |
else { |
| 224 | 0 | throw new IllegalArgumentException("Cannot inject unknown AccessibleObject type " + member); |
| 225 | |
} |
| 226 | |
} |
| 227 | 0 | catch (IllegalAccessException ex) { |
| 228 | 0 | throw new IllegalArgumentException("Cannot inject member " + member, ex); |
| 229 | |
} |
| 230 | 0 | catch (InvocationTargetException ex) { |
| 231 | |
|
| 232 | 0 | throw new IllegalArgumentException("Attempt to inject setter method " + member + |
| 233 | |
" resulted in an exception", ex); |
| 234 | 0 | } |
| 235 | 0 | } |
| 236 | |
} |
| 237 | |
} |