KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcspring > BeanDefinitionProtocol


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * Advice for collecting bean metadata.
25  *
26  * @see com.tc.object.config.SpringAspectModule#buildDefinitionForBeanDefinitionProtocol()
27  *
28  * @author Eugene Kuleshov
29  */

30 public class BeanDefinitionProtocol {
31
32   private final Map JavaDoc beanMap = new HashMap JavaDoc();
33   private final Map JavaDoc classes = new HashMap JavaDoc();
34
35   
36   /**
37    * Invoked after loadBeanDefinitions method in BeanDefinitionReader. Adds resource location to the DistributableBeanFactory mixin.
38    *
39    * @see org.springframework.beans.factory.support.BeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource)
40    */

41   public void captureIdentity(StaticJoinPoint jp, Resource resource, BeanDefinitionReader reader) throws Throwable JavaDoc {
42     Object JavaDoc beanFactory = reader.getBeanFactory();
43     if (beanFactory instanceof DistributableBeanFactory) {
44       String JavaDoc 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   /**
62    * Collects the different spring bean configuration files.
63    * <tt>
64    * Advices: Around(
65    * execution(* org.springframework.context.support.AbstractRefreshableApplicationContext+.loadBeanDefinitions(..))
66    * AND args(beanFactory))
67    * </tt>
68    */

69   public Object JavaDoc collectDefinitions(StaticJoinPoint jp, DefaultListableBeanFactory beanFactory) throws Throwable JavaDoc {
70     try {
71       return jp.proceed();
72     } finally {
73       
74       if (beanFactory instanceof DistributableBeanFactory) {
75         ((DistributableBeanFactory) beanFactory).registerBeanDefinitions(beanMap);
76       }
77
78       for (Iterator JavaDoc it = beanMap.entrySet().iterator(); it.hasNext();) {
79         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
80         AbstractBeanDefinition definition = (AbstractBeanDefinition) entry.getValue();
81
82         String JavaDoc beanClassName = definition.getBeanClassName();
83         if (beanClassName == null) continue;
84         
85         ClassLoader JavaDoc loader = (ClassLoader JavaDoc) classes.get(beanClassName);
86         if(loader==null) continue;
87         
88         try {
89           Class JavaDoc c = ClassUtils.forName(beanClassName, loader);
90           definition.setBeanClass(c);
91         } catch (Exception JavaDoc e) {
92           // ignore
93
}
94       }
95     }
96   }
97
98   /**
99    * Since Spring 2.0m5 ClassUtils.forName() is not called when bean definitions are created
100    *
101    * @see org.springframework.beans.factory.support.BeanDefinitionReaderUtils#createBeanDefinition()
102    */

103   public Object JavaDoc disableClassForName(String JavaDoc className, ClassLoader JavaDoc loader) throws Exception JavaDoc {
104     try {
105       AsmClassInfo.getClassInfo(className, loader);
106       // TODO this can be potential class loader clash
107
classes.put(className, loader);
108     } catch (Exception JavaDoc e) {
109       throw new ClassNotFoundException JavaDoc(className);
110     }
111     return null;
112   }
113
114   /**
115    * Called after constructor initialization on <code>BeanDefinitionHolder</code> class
116    *
117    * @see org.springframework.beans.factory.config.BeanDefinitionHolder
118    */

119   public void saveBeanDefinition(StaticJoinPoint jp, BeanDefinitionHolder holder) {
120     beanMap.put(holder.getBeanName(), holder.getBeanDefinition());
121   }
122 }
123
124
Popular Tags