1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.cxf.builder;
12
13 import org.mule.api.DefaultMuleException;
14 import org.mule.api.MuleException;
15 import org.mule.api.component.Component;
16 import org.mule.api.component.JavaComponent;
17 import org.mule.api.construct.FlowConstruct;
18 import org.mule.api.construct.FlowConstructAware;
19 import org.mule.api.endpoint.InboundEndpoint;
20 import org.mule.api.lifecycle.CreateException;
21 import org.mule.api.service.Service;
22 import org.mule.api.source.MessageSource;
23 import org.mule.construct.AbstractFlowConstruct;
24 import org.mule.module.cxf.CxfConstants;
25 import org.mule.module.cxf.CxfInboundMessageProcessor;
26 import org.mule.module.cxf.MuleJAXWSInvoker;
27 import org.mule.module.cxf.i18n.CxfMessages;
28 import org.mule.service.ServiceCompositeMessageSource;
29
30 import java.util.List;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.cxf.aegis.databinding.AegisDatabinding;
35 import org.apache.cxf.databinding.DataBinding;
36 import org.apache.cxf.frontend.ServerFactoryBean;
37 import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
38 import org.apache.cxf.service.invoker.Invoker;
39
40
41
42
43
44
45
46
47
48
49
50 public class WebServiceMessageProcessorBuilder
51 extends AbstractInboundMessageProcessorBuilder implements FlowConstructAware
52 {
53 protected transient Log logger = LogFactory.getLog(getClass());
54
55 private List<DataBinding> databinding;
56 private String frontend = CxfConstants.JAX_WS_FRONTEND;
57 private FlowConstruct flowConstruct;
58 private Service muleService;
59 private Class<?> serviceClass;
60
61 @Override
62 protected ServerFactoryBean createServerFactory() throws Exception
63 {
64 ServerFactoryBean sfb;
65 if (CxfConstants.SIMPLE_FRONTEND.equals(frontend))
66 {
67 sfb = new ServerFactoryBean();
68 sfb.setDataBinding(new AegisDatabinding());
69 }
70 else if (CxfConstants.JAX_WS_FRONTEND.equals(frontend))
71 {
72 sfb = new JaxWsServerFactoryBean();
73 }
74 else
75 {
76 throw new CreateException(CxfMessages.invalidFrontend(frontend), this);
77 }
78
79 if (serviceClass == null)
80 {
81 serviceClass = getTargetClass(muleService);
82 }
83 sfb.setServiceClass(serviceClass);
84
85 logger.info("Built CXF Inbound MessageProcessor for service class " + serviceClass.getName());
86
87
88 if (databinding != null && databinding.size() > 0)
89 {
90
91 sfb.setDataBinding(databinding.get(0));
92 }
93
94 if (muleService != null && muleService.getComponent() instanceof JavaComponent)
95 {
96 sfb.setServiceBean(((JavaComponent) muleService.getComponent()).getObjectFactory().getInstance(muleContext));
97 }
98 return sfb;
99 }
100
101 @Override
102 protected Invoker createInvoker(CxfInboundMessageProcessor processor)
103 {
104 Invoker invoker = super.createInvoker(processor);
105 if (CxfConstants.JAX_WS_FRONTEND.equals(frontend))
106 {
107 invoker = new MuleJAXWSInvoker(invoker);
108 }
109 return invoker;
110 }
111
112
113
114
115
116
117
118
119 protected Class<?> getTargetClass(Service service) throws MuleException, ClassNotFoundException
120 {
121 if (service == null)
122 {
123 throw new DefaultMuleException(CxfMessages.serviceClassRequiredWithPassThrough());
124 }
125
126 Component component = service.getComponent();
127 if (!(component instanceof JavaComponent))
128 {
129 throw new DefaultMuleException(CxfMessages.serviceClassRequiredWithPassThrough());
130 }
131
132 try
133 {
134 return ((JavaComponent) component).getObjectType();
135 }
136 catch (Exception e)
137 {
138 throw new CreateException(e, this);
139 }
140 }
141
142 @Override
143 protected String getAddress()
144 {
145 if (flowConstruct != null)
146 {
147 if (flowConstruct instanceof Service)
148 {
149 MessageSource source = ((Service) flowConstruct).getMessageSource();
150
151 if (source instanceof InboundEndpoint)
152 {
153 return ((InboundEndpoint) source).getEndpointURI().toString();
154 }
155 else if (source instanceof ServiceCompositeMessageSource)
156 {
157 List<InboundEndpoint> endpoints = ((ServiceCompositeMessageSource) muleService.getMessageSource()).getEndpoints();
158
159 if (endpoints.size() > 0)
160 {
161 return endpoints.get(0).getEndpointURI().toString();
162 }
163 }
164 }
165 else if (flowConstruct instanceof AbstractFlowConstruct)
166 {
167 MessageSource source = ((AbstractFlowConstruct) flowConstruct).getMessageSource();
168
169 if (source instanceof InboundEndpoint)
170 {
171 return ((InboundEndpoint) source).getEndpointURI().toString();
172 }
173 }
174 }
175 return "http://internalMuleCxfRegistry/" + hashCode();
176 }
177
178 @Override
179 public boolean isProxy()
180 {
181 return false;
182 }
183
184 @Override
185 public Class<?> getServiceClass()
186 {
187 return serviceClass;
188 }
189
190 public void setServiceClass(Class<?> serviceClass)
191 {
192 this.serviceClass = serviceClass;
193 }
194
195 public void setFlowConstruct(FlowConstruct flowConstruct)
196 {
197 this.flowConstruct = flowConstruct;
198
199 if (flowConstruct instanceof Service)
200 {
201 this.muleService = (Service) flowConstruct;
202 }
203 }
204 public String getFrontend()
205 {
206 return frontend;
207 }
208
209
210
211
212
213
214 public void setFrontend(String frontend)
215 {
216 this.frontend = frontend;
217 }
218
219 public List<DataBinding> getDatabinding()
220 {
221 return databinding;
222 }
223
224 public void setDatabinding(List<DataBinding> databinding)
225 {
226 this.databinding = databinding;
227 }
228
229 }