1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.cxf; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.MuleException; |
11 | |
import org.mule.api.context.MuleContextAware; |
12 | |
import org.mule.api.lifecycle.Disposable; |
13 | |
import org.mule.api.lifecycle.Initialisable; |
14 | |
import org.mule.api.lifecycle.InitialisationException; |
15 | |
import org.mule.config.spring.SpringRegistry; |
16 | |
import org.mule.module.cxf.support.MuleHeadersInInterceptor; |
17 | |
import org.mule.module.cxf.support.MuleHeadersOutInterceptor; |
18 | |
import org.mule.module.cxf.support.MuleProtocolHeadersOutInterceptor; |
19 | |
import org.mule.module.cxf.transport.MuleUniversalTransport; |
20 | |
|
21 | |
import java.lang.reflect.Field; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
import org.apache.cxf.Bus; |
27 | |
import org.apache.cxf.BusException; |
28 | |
import org.apache.cxf.BusFactory; |
29 | |
import org.apache.cxf.bus.spring.SpringBusFactory; |
30 | |
import org.apache.cxf.common.util.ASMHelper; |
31 | |
import org.apache.cxf.jaxb.JAXBDataBinding; |
32 | |
import org.apache.cxf.transport.ConduitInitiatorManager; |
33 | |
import org.apache.cxf.transport.DestinationFactoryManager; |
34 | |
import org.springframework.context.ApplicationContext; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class CxfConfiguration implements Initialisable, Disposable, MuleContextAware |
40 | |
{ |
41 | |
public static final String CXF = "cxf"; |
42 | |
public static final String CONFIGURATION_LOCATION = "configurationLocation"; |
43 | |
public static final String DEFAULT_MULE_NAMESPACE_URI = "http://www.muleumo.org"; |
44 | |
public static final String BUS_PROPERTY = CXF; |
45 | |
|
46 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
47 | |
|
48 | |
|
49 | |
private Bus bus; |
50 | |
private String configurationLocation; |
51 | |
private boolean initializeStaticBusInstance; |
52 | |
private MuleContext muleContext; |
53 | 0 | private boolean enableMuleSoapHeaders = true; |
54 | |
|
55 | |
public void initialise() throws InitialisationException |
56 | |
{ |
57 | 0 | BusFactory.setDefaultBus(null); |
58 | 0 | ApplicationContext context = (ApplicationContext) muleContext.getRegistry().lookupObject(SpringRegistry.SPRING_APPLICATION_CONTEXT); |
59 | |
|
60 | 0 | if (configurationLocation != null) |
61 | |
{ |
62 | 0 | bus = new SpringBusFactory(context).createBus(configurationLocation, true); |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 0 | bus = new SpringBusFactory().createBus((String) null, true); |
67 | |
} |
68 | |
|
69 | 0 | if (!initializeStaticBusInstance) |
70 | |
{ |
71 | 0 | BusFactory.setDefaultBus(null); |
72 | |
} |
73 | |
|
74 | 0 | MuleUniversalTransport transport = new MuleUniversalTransport(this); |
75 | 0 | DestinationFactoryManager dfm = bus.getExtension(DestinationFactoryManager.class); |
76 | 0 | dfm.registerDestinationFactory("http://schemas.xmlsoap.org/soap/http", transport); |
77 | 0 | dfm.registerDestinationFactory("http://schemas.xmlsoap.org/wsdl/soap/http", transport); |
78 | 0 | dfm.registerDestinationFactory("http://cxf.apache.org/transports/http/configuration", transport); |
79 | 0 | dfm.registerDestinationFactory("http://schemas.xmlsoap.org/wsdl/http/", transport); |
80 | 0 | dfm.registerDestinationFactory("http://www.w3.org/2003/05/soap/bindings/HTTP/", transport); |
81 | 0 | dfm.registerDestinationFactory("http://cxf.apache.org/transports/jms", transport); |
82 | 0 | dfm.registerDestinationFactory("http://cxf.apache.org/transports/http", transport); |
83 | |
|
84 | 0 | dfm.registerDestinationFactory(MuleUniversalTransport.TRANSPORT_ID, transport); |
85 | |
|
86 | 0 | ConduitInitiatorManager extension = bus.getExtension(ConduitInitiatorManager.class); |
87 | |
try |
88 | |
{ |
89 | |
|
90 | |
|
91 | 0 | extension.getConduitInitiator("http://schemas.xmlsoap.org/soap/http"); |
92 | |
} |
93 | 0 | catch (BusException e1) |
94 | |
{ |
95 | |
|
96 | 0 | } |
97 | 0 | extension.registerConduitInitiator("http://cxf.apache.org/transports/http", transport); |
98 | 0 | extension.registerConduitInitiator("http://schemas.xmlsoap.org/wsdl/soap/", transport); |
99 | 0 | extension.registerConduitInitiator("http://schemas.xmlsoap.org/soap/http", transport); |
100 | 0 | extension.registerConduitInitiator("http://schemas.xmlsoap.org/soap/http/", transport); |
101 | 0 | extension.registerConduitInitiator("http://schemas.xmlsoap.org/wsdl/soap/http", transport); |
102 | 0 | extension.registerConduitInitiator("http://schemas.xmlsoap.org/wsdl/http/", transport); |
103 | 0 | extension.registerConduitInitiator("http://www.w3.org/2003/05/soap/bindings/HTTP/", transport); |
104 | 0 | extension.registerConduitInitiator("http://cxf.apache.org/transports/http/configuration", transport); |
105 | 0 | extension.registerConduitInitiator("http://cxf.apache.org/bindings/xformat", transport); |
106 | 0 | extension.registerConduitInitiator("http://cxf.apache.org/transports/jms", transport); |
107 | 0 | extension.registerConduitInitiator(MuleUniversalTransport.TRANSPORT_ID, transport); |
108 | |
|
109 | 0 | bus.getOutInterceptors().add(new MuleProtocolHeadersOutInterceptor()); |
110 | 0 | bus.getOutFaultInterceptors().add(new MuleProtocolHeadersOutInterceptor()); |
111 | |
|
112 | 0 | if (enableMuleSoapHeaders) |
113 | |
{ |
114 | 0 | bus.getInInterceptors().add(new MuleHeadersInInterceptor()); |
115 | 0 | bus.getInFaultInterceptors().add(new MuleHeadersInInterceptor()); |
116 | 0 | bus.getOutInterceptors().add(new MuleHeadersOutInterceptor()); |
117 | 0 | bus.getOutFaultInterceptors().add(new MuleHeadersOutInterceptor()); |
118 | |
} |
119 | 0 | } |
120 | |
|
121 | |
public void dispose() |
122 | |
{ |
123 | 0 | bus.shutdown(true); |
124 | |
|
125 | |
|
126 | 0 | JAXBDataBinding.clearCaches(); |
127 | |
try |
128 | |
{ |
129 | |
|
130 | 0 | final Field cacheField = ASMHelper.class.getDeclaredField("LOADER_MAP"); |
131 | 0 | cacheField.setAccessible(true); |
132 | |
|
133 | 0 | final Map cache = (Map) cacheField.get(null); |
134 | 0 | cache.clear(); |
135 | |
} |
136 | 0 | catch (Throwable t) |
137 | |
{ |
138 | 0 | logger.warn("Error disposing CxfConfiguration, this may cause a memory leak", t); |
139 | 0 | } |
140 | 0 | } |
141 | |
|
142 | |
public Bus getCxfBus() |
143 | |
{ |
144 | 0 | return bus; |
145 | |
} |
146 | |
|
147 | |
public void setCxfBus(Bus bus) |
148 | |
{ |
149 | 0 | this.bus = bus; |
150 | 0 | } |
151 | |
|
152 | |
public String getConfigurationLocation() |
153 | |
{ |
154 | 0 | return configurationLocation; |
155 | |
} |
156 | |
|
157 | |
public void setConfigurationLocation(String configurationLocation) |
158 | |
{ |
159 | 0 | this.configurationLocation = configurationLocation; |
160 | 0 | } |
161 | |
|
162 | |
public boolean isInitializeStaticBusInstance() |
163 | |
{ |
164 | 0 | return initializeStaticBusInstance; |
165 | |
} |
166 | |
|
167 | |
public void setInitializeStaticBusInstance(boolean initializeStaticBusInstance) |
168 | |
{ |
169 | 0 | this.initializeStaticBusInstance = initializeStaticBusInstance; |
170 | 0 | } |
171 | |
|
172 | |
public void setMuleContext(MuleContext context) |
173 | |
{ |
174 | 0 | this.muleContext = context; |
175 | 0 | } |
176 | |
|
177 | |
public MuleContext getMuleContext() |
178 | |
{ |
179 | 0 | return muleContext; |
180 | |
} |
181 | |
|
182 | |
public static CxfConfiguration getConfiguration(MuleContext muleContext) throws MuleException |
183 | |
{ |
184 | 0 | CxfConfiguration configuration = muleContext.getRegistry().get(CxfConstants.DEFAULT_CXF_CONFIGURATION); |
185 | 0 | if (configuration == null) |
186 | |
{ |
187 | 0 | configuration = new CxfConfiguration(); |
188 | 0 | configuration.setMuleContext(muleContext); |
189 | 0 | configuration.initialise(); |
190 | 0 | muleContext.getRegistry().registerObject(CxfConstants.DEFAULT_CXF_CONFIGURATION, configuration); |
191 | |
} |
192 | 0 | return configuration; |
193 | |
} |
194 | |
|
195 | |
public boolean isEnableMuleSoapHeaders() |
196 | |
{ |
197 | 0 | return enableMuleSoapHeaders; |
198 | |
} |
199 | |
|
200 | |
public void setEnableMuleSoapHeaders(boolean enableMuleSoapHeaders) |
201 | |
{ |
202 | 0 | this.enableMuleSoapHeaders = enableMuleSoapHeaders; |
203 | 0 | } |
204 | |
|
205 | |
} |