1 4 package com.tcspring; 5 6 import org.springframework.beans.factory.config.BeanDefinitionHolder; 7 import org.springframework.beans.factory.support.AbstractBeanDefinition; 8 import org.springframework.beans.factory.support.BeanDefinitionReader; 9 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 10 import org.springframework.core.io.ClassPathResource; 11 import org.springframework.core.io.FileSystemResource; 12 import org.springframework.core.io.Resource; 13 import org.springframework.util.ClassUtils; 14 import org.springframework.web.context.support.ServletContextResource; 15 16 import com.tc.aspectwerkz.joinpoint.StaticJoinPoint; 17 import com.tc.aspectwerkz.reflect.impl.asm.AsmClassInfo; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 30 public class BeanDefinitionProtocol { 31 32 private final Map beanMap = new HashMap (); 33 private final Map classes = new HashMap (); 34 35 36 41 public void captureIdentity(StaticJoinPoint jp, Resource resource, BeanDefinitionReader reader) throws Throwable { 42 Object beanFactory = reader.getBeanFactory(); 43 if (beanFactory instanceof DistributableBeanFactory) { 44 String location; 45 if (resource instanceof ClassPathResource) { 46 location = ((ClassPathResource) resource).getPath(); 47 } else if (resource instanceof FileSystemResource) { 48 location = ((FileSystemResource) resource).getPath(); 49 } else if (resource instanceof ServletContextResource) { 50 location = ((ServletContextResource) resource).getPath(); 51 } else { 52 location = resource.getDescription(); 53 } 54 55 DistributableBeanFactory distributableBeanFactory = (DistributableBeanFactory) beanFactory; 56 distributableBeanFactory.addLocation(location); 57 } 58 } 59 60 61 69 public Object collectDefinitions(StaticJoinPoint jp, DefaultListableBeanFactory beanFactory) throws Throwable { 70 try { 71 return jp.proceed(); 72 } finally { 73 74 if (beanFactory instanceof DistributableBeanFactory) { 75 ((DistributableBeanFactory) beanFactory).registerBeanDefinitions(beanMap); 76 } 77 78 for (Iterator it = beanMap.entrySet().iterator(); it.hasNext();) { 79 Map.Entry entry = (Map.Entry ) it.next(); 80 AbstractBeanDefinition definition = (AbstractBeanDefinition) entry.getValue(); 81 82 String beanClassName = definition.getBeanClassName(); 83 if (beanClassName == null) continue; 84 85 ClassLoader loader = (ClassLoader ) classes.get(beanClassName); 86 if(loader==null) continue; 87 88 try { 89 Class c = ClassUtils.forName(beanClassName, loader); 90 definition.setBeanClass(c); 91 } catch (Exception e) { 92 } 94 } 95 } 96 } 97 98 103 public Object disableClassForName(String className, ClassLoader loader) throws Exception { 104 try { 105 AsmClassInfo.getClassInfo(className, loader); 106 classes.put(className, loader); 108 } catch (Exception e) { 109 throw new ClassNotFoundException (className); 110 } 111 return null; 112 } 113 114 119 public void saveBeanDefinition(StaticJoinPoint jp, BeanDefinitionHolder holder) { 120 beanMap.put(holder.getBeanName(), holder.getBeanDefinition()); 121 } 122 } 123 124
| Popular Tags
|