1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.eclipse.jetty.spring;
21
22 import java.net.URL;
23 import java.util.Arrays;
24 import java.util.Map;
25 import java.util.ServiceLoader;
26
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
29 import org.eclipse.jetty.xml.ConfigurationProcessor;
30 import org.eclipse.jetty.xml.ConfigurationProcessorFactory;
31 import org.eclipse.jetty.xml.XmlConfiguration;
32 import org.eclipse.jetty.xml.XmlParser;
33 import org.springframework.beans.factory.xml.XmlBeanFactory;
34 import org.springframework.core.io.ByteArrayResource;
35 import org.springframework.core.io.Resource;
36 import org.springframework.core.io.UrlResource;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class SpringConfigurationProcessor implements ConfigurationProcessor
59 {
60 private static final Logger LOG = Log.getLogger(SpringConfigurationProcessor.class);
61
62 private Map<String, Object> _idMap;
63 private Map<String, String> _propertyMap;
64 private XmlBeanFactory _beanFactory;
65 private String _main;
66
67 public void init(URL url, XmlParser.Node config, Map<String, Object> idMap, Map<String, String> properties)
68 {
69 try
70 {
71 _idMap = idMap;
72 _propertyMap = properties;
73
74 Resource resource = url != null
75 ? new UrlResource(url)
76 : new ByteArrayResource(("" +
77 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
78 "<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">" +
79 config).getBytes("UTF-8"));
80
81 _beanFactory = new XmlBeanFactory(resource);
82 }
83 catch (Exception e)
84 {
85 throw new RuntimeException(e);
86 }
87 }
88
89 public Object configure(Object obj) throws Exception
90 {
91 doConfigure();
92 return _beanFactory.configureBean(obj, _main);
93 }
94
95
96
97
98
99
100 public Object configure() throws Exception
101 {
102 doConfigure();
103 return _beanFactory.getBean(_main);
104 }
105
106 private void doConfigure()
107 {
108 _beanFactory.registerSingleton("properties", _propertyMap);
109
110
111 for (String bean : _beanFactory.getBeanDefinitionNames())
112 {
113 LOG.debug("{} - {}", bean, Arrays.asList(_beanFactory.getAliases(bean)));
114 String[] aliases = _beanFactory.getAliases(bean);
115 if ("Main".equals(bean) || aliases != null && Arrays.asList(aliases).contains("Main"))
116 {
117 _main = bean;
118 break;
119 }
120 }
121 if (_main == null)
122 _main = _beanFactory.getBeanDefinitionNames()[0];
123
124
125 LOG.debug("idMap {}", _idMap);
126 for (String id : _idMap.keySet())
127 {
128 LOG.debug("register {}", id);
129 _beanFactory.registerSingleton(id, _idMap.get(id));
130 }
131
132
133 for (String id : _idMap.keySet())
134 {
135 if (_beanFactory.containsBeanDefinition(id))
136 {
137 LOG.debug("reconfigure {}", id);
138 _beanFactory.configureBean(_idMap.get(id), id);
139 }
140 }
141
142
143 for (String id : _beanFactory.getSingletonNames())
144 _idMap.put(id, _beanFactory.getBean(id));
145 }
146 }