KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > spring > kernel > MicrocontainerConfigurer


1 /**
2  * User: alesj
3  * Date: 18.4.2006
4  * Time: 12:42:33
5  *
6  * (C) Genera Lynx d.o.o.
7  */

8
9 package org.jboss.spring.kernel;
10
11 import org.springframework.beans.BeansException;
12 import org.springframework.beans.factory.BeanDefinitionStoreException;
13 import org.springframework.beans.factory.BeanFactory;
14 import org.springframework.beans.factory.BeanFactoryAware;
15 import org.springframework.beans.factory.BeanNameAware;
16 import org.springframework.beans.factory.config.*;
17 import org.springframework.core.Ordered;
18
19 /**
20  * @author <a HREF="mailto:ales.justin@genera-lynx.com">Ales Justin</a>
21  */

22 public class MicrocontainerConfigurer extends MicrocontainerLocatorSupport
23       implements BeanFactoryPostProcessor, BeanNameAware, BeanFactoryAware, Ordered
24 {
25
26    private String JavaDoc beanName;
27    private BeanFactory beanFactory;
28    private int order = Integer.MAX_VALUE;
29
30    private String JavaDoc prefix = "mc${";
31
32    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactoryToProcess) throws BeansException
33    {
34       BeanDefinitionVisitor visitor = new MicrocontainerBeanDefinitionVisitor();
35       String JavaDoc[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
36       //noinspection ForLoopReplaceableByForEach
37
for (int i = 0; i < beanNames.length; i++)
38       {
39          // Check that we're not parsing our own bean definition,
40
// to avoid failing on unresolvable placeholders in properties file locations.
41
if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory)))
42          {
43             BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]);
44             try
45             {
46                visitor.visitBeanDefinition(bd);
47             }
48             catch (BeanDefinitionStoreException ex)
49             {
50                throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage());
51             }
52          }
53       }
54    }
55
56    protected boolean isMicrocontainerRef(String JavaDoc value)
57    {
58       return value != null && value.startsWith(prefix);
59    }
60
61    protected String JavaDoc parseBeansReference(String JavaDoc value)
62    {
63       int endIndex = value.lastIndexOf("$");
64       endIndex = endIndex >= 0 ? endIndex : value.length();
65       return value.substring(prefix.length(), endIndex);
66    }
67
68    private class MicrocontainerBeanDefinitionVisitor extends BeanDefinitionVisitor
69    {
70
71       protected String JavaDoc resolveStringValue(String JavaDoc strVal)
72       {
73          return strVal;
74       }
75
76       protected Object JavaDoc resolveValue(Object JavaDoc value)
77       {
78          value = super.resolveValue(value);
79          if (value instanceof TypedStringValue)
80          {
81             TypedStringValue typedStringValue = (TypedStringValue) value;
82             String JavaDoc beansRef = typedStringValue.getValue();
83             if (isMicrocontainerRef(beansRef))
84             {
85                return locateBean(parseBeansReference(beansRef));
86             }
87          }
88          if (value instanceof String JavaDoc)
89          {
90             String JavaDoc beansRef = (String JavaDoc) value;
91             if (isMicrocontainerRef(beansRef))
92             {
93                return locateBean(parseBeansReference(beansRef));
94             }
95          }
96          return value;
97       }
98
99    }
100
101    /**
102     * Only necessary to check that we're not parsing our own bean definition,
103     * to avoid failing on unresolvable placeholders in properties file locations.
104     * The latter case can happen with placeholders for system properties in
105     * resource locations.
106     *
107     * @see #setLocations
108     * @see org.springframework.core.io.ResourceEditor
109     */

110    public void setBeanName(String JavaDoc beanName)
111    {
112       this.beanName = beanName;
113    }
114
115    /**
116     * Only necessary to check that we're not parsing our own bean definition,
117     * to avoid failing on unresolvable placeholders in properties file locations.
118     * The latter case can happen with placeholders for system properties in
119     * resource locations.
120     *
121     * @see #setLocations
122     * @see org.springframework.core.io.ResourceEditor
123     */

124    public void setBeanFactory(BeanFactory beanFactory)
125    {
126       this.beanFactory = beanFactory;
127    }
128
129    public int getOrder()
130    {
131       return order;
132    }
133
134    public void setOrder(int order)
135    {
136       this.order = order;
137    }
138
139    public void setPrefix(String JavaDoc prefix)
140    {
141       this.prefix = prefix;
142    }
143
144 }
145
Popular Tags