View Javadoc

1   /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.jbi.endpoint;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  import it.imolinfo.jbi4corba.exception.ClassGenerationException;
13  import it.imolinfo.jbi4corba.exception.Jbi4CorbaDeployException;
14  import it.imolinfo.jbi4corba.exception.Jbi4CorbaException;
15  import it.imolinfo.jbi4corba.exception.ServiceActivationException;
16  import it.imolinfo.jbi4corba.exception.ServiceCreationException;
17  import it.imolinfo.jbi4corba.jbi.Messages;
18  import it.imolinfo.jbi4corba.jbi.component.runtime.RuntimeContext;
19  import it.imolinfo.jbi4corba.jbi.processor.ConsumerExchangeProcessor;
20  import it.imolinfo.jbi4corba.jbi.processor.ExchangeProcessor;
21  import it.imolinfo.jbi4corba.jbi.wsdl.Jbi4CorbaExtensionUtils;
22  import it.imolinfo.jbi4corba.webservice.descriptor.ConsumerServiceDescriptor;
23  import it.imolinfo.jbi4corba.webservice.generator.ConsumerServiceClassesGenerator;
24  import it.imolinfo.jbi4corba.webservice.generator.ServerCorbaClassesHolder;
25  import it.imolinfo.jbi4corba.webservice.runtime.ConsumerServiceCreator;
26  
27  import java.io.File;
28  import java.io.FileWriter;
29  import java.io.IOException;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.jbi.component.ComponentContext;
35  import javax.jbi.management.DeploymentException;
36  import javax.jbi.messaging.DeliveryChannel;
37  import javax.jbi.messaging.MessageExchange;
38  import javax.jbi.messaging.MessagingException;
39  import javax.jbi.messaging.MessageExchange.Role;
40  import javax.jbi.servicedesc.ServiceEndpoint;
41  import javax.wsdl.Binding;
42  import javax.wsdl.BindingFault;
43  import javax.wsdl.BindingInput;
44  import javax.wsdl.BindingOperation;
45  import javax.wsdl.BindingOutput;
46  import javax.wsdl.Definition;
47  import javax.wsdl.Fault;
48  import javax.wsdl.Input;
49  import javax.wsdl.Operation;
50  import javax.wsdl.Output;
51  import javax.wsdl.Port;
52  import javax.wsdl.PortType;
53  import javax.wsdl.Service;
54  import javax.wsdl.WSDLException;
55  import javax.wsdl.extensions.ExtensionRegistry;
56  import javax.wsdl.extensions.ExtensionSerializer;
57  import javax.wsdl.extensions.soap.SOAPAddress;
58  import javax.wsdl.extensions.soap.SOAPBinding;
59  import javax.wsdl.extensions.soap.SOAPBody;
60  import javax.wsdl.extensions.soap.SOAPOperation;
61  import javax.wsdl.factory.WSDLFactory;
62  import javax.wsdl.xml.WSDLWriter;
63  import javax.xml.namespace.QName;
64  
65  import org.omg.CORBA.ORB;
66  
67  import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
68  import com.ibm.wsdl.extensions.soap.SOAPBodyImpl;
69  import com.ibm.wsdl.extensions.soap.SOAPConstants;
70  
71  /**
72   * The Consumer Endpoint implementation class.
73   */
74  @SuppressWarnings("unchecked")
75  public class ConsumerEndpoint extends Jbi4CorbaEndpoint {
76  
77  	/** serialVersionUID. */
78  	private static final long serialVersionUID = -8507941260052862451L;
79  
80  	/**
81  	 * The standard SOAP namespace URI used in WSDL files.
82  	 */
83  	private static final String SOAP_NAMESPACE_PREFIX = "soap";
84  
85  	/**
86  	 * The logger for this class and its instances.
87  	 */
88  	private static final Logger LOG = LoggerFactory
89  			.getLogger(ConsumerEndpoint.class);
90  
91  	/**
92  	 * The responsible to translate localized messages.
93  	 */
94  	private static final Messages MESSAGES = Messages
95  			.getMessages(ConsumerEndpoint.class);
96  
97  	private ConsumerExchangeProcessor consumerExchangeProcessor;
98  
99  	private ConsumerServiceDescriptor consumerServiceDescriptor;
100 
101 	/**
102 	 * @param consumerServiceDescriptor
103 	 *            The consumer service descriptor
104 	 */
105 	public ConsumerEndpoint(QName serviceName, String endpointName,
106 			ConsumerServiceDescriptor consumerServiceDescriptor) {
107 		super(serviceName, endpointName);
108 		this.consumerServiceDescriptor = consumerServiceDescriptor;
109 		consumerServiceDescriptor.setEndpoint(this);
110 		consumerExchangeProcessor = new ConsumerExchangeProcessor(this);
111 	}
112 
113 	/**
114 	 * Registers the service and starts the ORB.
115 	 * 
116 	 * @throws Exception
117 	 *             The exception
118 	 */
119 	public void activate() throws Jbi4CorbaException {
120 		
121 		startServiceAndOrb();
122 
123 		LOG.debug("consumer endpoint: " + this + " activated.");
124 
125 	}
126 
127 	/**
128 	 * This method is used to send asynchronously a MessageExchange.
129 	 * 
130 	 * @param me
131 	 *            The MessageExchange.
132 	 * @param channel
133 	 *            Used to send directly the ME if the Endpoint is an Active
134 	 *            Consumer.
135 	 * 
136 	 * @throws MessagingException
137 	 *             Sending operation failure.
138 	 */
139 	public void sendAsynch(MessageExchange me, DeliveryChannel channel)
140 			throws MessagingException {
141 
142 		LOG.debug("Sending directly (JBI API)");
143 		channel.send(me);
144 	}
145 
146 	/**
147 	 * Starts the service and the ORB.
148 	 * 
149 	 * @throws ServiceActivationException
150 	 *             The service activationa exception
151 	 */
152 	private void startServiceAndOrb() throws ServiceActivationException {
153 		ConsumerServiceCreator consumerServiceCreator = new ConsumerServiceCreator();
154 
155 		consumerServiceCreator
156 				.registerAndActivateService(consumerServiceDescriptor);
157 		final ORB orb = consumerServiceDescriptor.getOrb();
158 
159 		Thread thread = new Thread(new Runnable() {
160 			public void run() {
161 				LOG.debug("Activating orb.Thread: "
162 						+ Thread.currentThread().getName());
163 
164 				orb.run();
165 
166 				LOG.debug("Exiting orb.Thread: "
167 						+ Thread.currentThread().getName());
168 			}
169 		}, "ORB Runner for service: " + this);
170 
171 		thread.setContextClassLoader(consumerServiceDescriptor
172 				.getServerCorbaClassesHolder().getOriginalClassLoader());
173 		thread.start();
174 	}
175 
176 	/**
177 	 * Override.
178 	 * 
179 	 * @throws Exception
180 	 *             The exception
181 	 */
182 	public void deactivate() {
183 		stopServiceAndOrb();
184 	}
185 
186 	/**
187 	 * Simplified implementation.
188 	 * 
189 	 */
190 	private void stopServiceAndOrb() {
191 		// simplified implementation
192 		consumerServiceDescriptor.getOrb().shutdown(true);
193 	}
194 
195 	/**
196 	 * Override.
197 	 * 
198 	 * @return The processor
199 	 * 
200 	 * @see ConsumerExchangeProcessor.
201 	 */
202 	public ExchangeProcessor getProcessor() {
203 		return consumerExchangeProcessor;
204 	}
205 
206 
207 	/**
208 	 * this method has the following flow. First the wsdl is recovered. The wsdl
209 	 * at the moment is got from an existing jbi internal endpoint. In the
210 	 * future it could be given as a URL.
211 	 * 
212 	 * second the service creator with the wsdl is called
213 	 * 
214 	 * third the service invocation handler is created
215 	 * 
216 	 * fourth the endpoint metadata is published. (How to publish an external
217 	 * endpoint non SOAP???)
218 	 * 
219 	 * @throws DeploymentException
220 	 *             The deployment exception
221 	 */
222 	public void registerService() throws Jbi4CorbaDeployException {
223 
224 		Definition wsdl;
225 
226 		consumerServiceDescriptor.setComponentRootPath(RuntimeContext
227 				.getInstance().getComponentContext().getInstallRoot());
228 
229 		ServiceEndpoint proxiedService = retrieveProxiedEndpointDefinition();
230 		consumerServiceDescriptor.setProxiedService(proxiedService);
231 
232 		LOG.debug("Component root:"
233 				+ consumerServiceDescriptor.getComponentRootPath());
234 
235 		// Removes all the Jbi4Corba extensions from the definition (if present)
236 		try {                    
237 			Jbi4CorbaExtensionUtils.removeCorbaElements(this.getDefinition());                      
238 		} catch (WSDLException e) {
239 			String msg = MESSAGES.getString(
240 					"CRB000139_Error_removing_JBI4CORBA_elements", e
241 							.getMessage());
242 			LOG.error(msg, e);
243 			throw new Jbi4CorbaDeployException(msg, e);
244 		}
245 
246 
247 
248 		// Adds the SOAP extensions, if not present
249 		try {
250 			wsdl = addSoapElements(consumerServiceDescriptor);
251 			// consumerServiceDescriptor.getServiceWSDLDefinition());                        
252 		} catch (WSDLException e) {
253 			String msg = MESSAGES.getString(
254 					"CRB000138_Error_adding_SOAP_elements", e.getMessage());
255 
256 			LOG.error(msg, e);
257 			throw new Jbi4CorbaDeployException(msg, e);
258 		}
259 		writeWSDLDocument(wsdl);
260 
261 		ConsumerServiceClassesGenerator consumerServiceClassesGenerator = new ConsumerServiceClassesGenerator();
262 		String wsdlFileName = "file://"
263 				+ consumerServiceDescriptor.getWsdlFileName();
264 
265 		ServerCorbaClassesHolder serverCorbaClassesHolder;
266 		LOG.debug("consumerServiceDescriptor.getRootPath():"
267 				+ consumerServiceDescriptor.getRootPath()
268 				+ "; consumerServiceDescriptor.getComponentRootPath():"
269 				+ consumerServiceDescriptor.getComponentRootPath());
270 		try {
271 
272 			serverCorbaClassesHolder = consumerServiceClassesGenerator
273 					.generateConsumerServiceClasses(wsdlFileName,
274 							consumerServiceDescriptor.getRootPath(),
275 							consumerServiceDescriptor.getComponentRootPath(),
276 							consumerServiceDescriptor.getJbiServiceDescriptor());
277 
278 		} catch (ClassGenerationException e) {
279 			String msg = MESSAGES.getString(
280 					"CRB000103_Unable_to_create_service_classes", wsdlFileName,
281 					e.getMessage());
282 
283 			LOG.error(msg, e);
284 			throw new Jbi4CorbaDeployException(msg, e);
285 		}
286 
287 		consumerServiceDescriptor
288 				.setServerCorbaClassesHolder(serverCorbaClassesHolder);
289 
290 		ConsumerServiceCreator consumerServiceCreator = new ConsumerServiceCreator();
291 		try {
292 			consumerServiceCreator.createJbiService(consumerServiceDescriptor);
293 		} catch (ServiceCreationException e) {
294 			String msg = MESSAGES.getString(
295 					"CRB000104_Unable_to_create_jbi_service_consumer",
296 					consumerServiceDescriptor, e.getMessage());
297 
298 			LOG.error(msg, e);
299 			throw new Jbi4CorbaDeployException(msg, e);
300 		}
301 
302 	}
303 
304 	/**
305 	 * Adds SOAP elements to the specified WSDL if they are not present.
306 	 * 
307 	 * @param def
308 	 *            the WSDL to add SOAP elements. Must be not <code>null</code>.
309 	 * @return the WSDL document originated from <code>def</code> after the
310 	 *         addition of required SOAP elements, if not present inside
311 	 *         <code>def</code> itself.
312 	 * @throws WSDLException
313 	 *             in case of error manipulating the <code>Definition</code>.
314 	 */
315 	private static Definition addSoapElements(
316 
317 	ConsumerServiceDescriptor consumerServiceDescriptor) throws WSDLException {
318 
319 		Definition def = consumerServiceDescriptor.getServiceWSDLDefinition();
320 
321 		ExtensionRegistry extReg = def.getExtensionRegistry();
322 
323 		LOG.debug("Definition=" + def);
324 		boolean soapFound = false;
325 		LOG.debug("finding soap address ...");
326 		Port port = null;
327 
328 		WSDLFactory factory = WSDLFactory.newInstance();
329 
330 		LOG.debug("Factory class:" + factory.getClass().getName());
331 
332 		ExtensionSerializer bindingSerializer = extReg.querySerializer(
333 				Binding.class, SOAPConstants.Q_ELEM_SOAP_BINDING);
334 
335 		def.setExtensionRegistry(new PopulatedExtensionRegistry());
336 		LOG.debug("ExtensionRegistry:" + extReg.getClass().getName());
337 		LOG.debug("BINDING SERIALIZER: " + bindingSerializer);
338 
339 		WSDLWriter writer = factory.newWSDLWriter();
340 
341 		LOG.debug("addSoapElements[PRE]. soapFound=" + soapFound + "; Port="
342 				+ port + "; factory=" + factory + "; writer=" + writer);
343 
344 		if (def.getBindings() == null || def.getBindings().size() == 0) {
345 
346 			LOG.debug("The CONCRETE part of the wsdl is missing.");
347 			Definition newDef = addSOAPExtensionsToWSDL(def);
348 			consumerServiceDescriptor.setServiceWSDLDefinition(newDef);
349 			return newDef;
350 
351 		} else {
352 			LOG.debug("The CONCRETE part of the wsdl is present.");
353 		}
354 
355 		def.addNamespace(SOAP_NAMESPACE_PREFIX, SOAPConstants.NS_URI_SOAP);
356 
357 		for (Object serviceAsObject : def.getServices().values()) {
358 			Service service = (Service) serviceAsObject;
359 			LOG.debug("Service=" + service + "; Service.QName="
360 					+ service.getQName());
361 
362 			for (Object obj : service.getPorts().values()) {
363 				port = (Port) obj;
364 				for (Object element : port.getExtensibilityElements()) {
365 					if (element instanceof SOAPAddress) {
366 						soapFound = true;
367 						break;
368 					} else {
369 						LOG.debug("The elemente is NOT a SOAPAddress");
370 					}
371 
372 				}
373 			}
374 		}
375 		LOG.debug("addSoapElements[POST]. soapFound=" + soapFound + "; Port="
376 				+ port + "; factory=" + factory + "; writer=" + writer);
377 
378 		if (!soapFound) {
379 			LOG.debug("SORRY. soap NOT found.");
380 
381 			LOG.debug("Port class:" + port.getClass().getName());
382 
383 			SOAPAddress soapAddress = (SOAPAddress) extReg.createExtension(
384 					Port.class, SOAPConstants.Q_ELEM_SOAP_ADDRESS);
385 
386 			LOG.debug("creating soapAddress=" + soapAddress);
387 			SOAPBinding soapBinding = (SOAPBinding) extReg.createExtension(
388 					Binding.class, SOAPConstants.Q_ELEM_SOAP_BINDING);
389 			LOG.debug("creating binding=" + soapBinding.getClass().getName());
390 
391 			// Take first BindingOperation: what if they're more than one?
392 			BindingOperation bo = (BindingOperation) port.getBinding()
393 					.getBindingOperations().get(0);
394 
395 			SOAPBody inputBody = (SOAPBody) extReg.createExtension(
396 					BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
397 
398 			LOG.debug("creating body=" + inputBody);
399 
400 			SOAPOperation soapOperation = (SOAPOperation) extReg
401 					.createExtension(BindingOperation.class,
402 							SOAPConstants.Q_ELEM_SOAP_OPERATION);
403 			LOG.debug("creating SoapOperation=" + soapOperation);
404 
405 			soapAddress.setLocationURI("http://localhost/fake_location");
406 			port.addExtensibilityElement(soapAddress);
407 
408 			soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
409 			soapBinding.setStyle("document");
410 			port.getBinding().addExtensibilityElement(soapBinding);
411 
412 			soapOperation.setSoapActionURI("");
413 			bo.addExtensibilityElement(soapOperation);
414 
415 			inputBody.setUse("literal");
416 
417 			addSoapBodyIfNotPresent(bo.getBindingInput(), inputBody);
418 			addSoapBodyIfNotPresent(bo.getBindingOutput(), inputBody);
419 		}
420 
421 		return def;
422 	}
423 
424 	/**
425 	 * Used to add the soap:body to the binding input.
426 	 * 
427 	 * @param bindingInput
428 	 *            The binding input
429 	 * @param soapBody
430 	 *            The soap body
431 	 */
432 	private static void addSoapBodyIfNotPresent(
433 			final BindingInput bindingInput, final SOAPBody soapBody) {
434 
435 		for (Object obj : bindingInput.getExtensibilityElements()) {
436 			if (obj instanceof SOAPBody) {
437 				return;
438 			}
439 		}
440 		bindingInput.addExtensibilityElement(soapBody);
441 	}
442 
443 	/**
444 	 * Used to add the soap:body to the binding output.
445 	 * 
446 	 * @param bindingOutput
447 	 *            The binding output
448 	 * @param soapBody
449 	 *            The soap body
450 	 */
451 	private static void addSoapBodyIfNotPresent(
452 			final BindingOutput bindingOutput, final SOAPBody soapBody) {
453 
454 		if (bindingOutput == null) {
455 			LOG.debug(">>>> bindingOutput is NULL!!!");
456 			return;
457 		}
458 
459 		for (Object obj : bindingOutput.getExtensibilityElements()) {
460 			if (obj instanceof SOAPBody) {
461 				return;
462 			}
463 		}
464 		bindingOutput.addExtensibilityElement(soapBody);
465 	}
466 
467 	/**
468 	 * This method find the first PortType in the definition.
469 	 * 
470 	 * @param def
471 	 *            The WSDL definition.
472 	 * 
473 	 * @return The PortType found.
474 	 */
475 	protected static PortType findFirstPortType(final Definition def) {
476 
477 		PortType portType = null;
478 
479 		for (Object portTypeAsObject : def.getPortTypes().keySet()) {
480 			LOG.debug("PortTypeKEY=" + portTypeAsObject + "; CLASS="
481 					+ portTypeAsObject.getClass());
482 
483 			if (portType == null) {
484 				portType = (PortType) def.getPortType((QName) portTypeAsObject);
485 			}
486 		}
487 
488 		return portType;
489 	}
490 
491 	/**
492 	 * If there isn't the concrete part of the wsdl we must construct it using
493 	 * the abstract part and this method adds the 'operations' in the PortType
494 	 * to the binding.
495 	 * 
496 	 * @param wsdl
497 	 *            The WSDL definition.
498 	 * @param portType
499 	 *            A PortType.
500 	 * @param wsdlBinding
501 	 *            A Binding.
502 	 */
503 	protected static void addOperationToWsdlBinding(Definition wsdl,
504 			PortType portType, Binding wsdlBinding) {
505 
506 		// Add the bindingOperations (with input,output and faults)
507 		List<Operation> operations = portType.getOperations();
508 
509 		for (Operation operation : operations) {
510 			BindingOperation bop = wsdl.createBindingOperation();
511 
512 			bop.setOperation(operation);
513 
514 			bop.setName(operation.getName());
515 
516 			Input input = operation.getInput();
517 			if (input != null) {
518 
519 				SOAPBody soapBody = new SOAPBodyImpl();
520 				soapBody.setUse("literal");
521 
522 				BindingInput bindingInput = wsdl.createBindingInput();
523 				bindingInput.setName(input.getName());
524 				bindingInput.addExtensibilityElement(soapBody);
525 
526 				bop.setBindingInput(bindingInput);
527 			}
528 
529 			Output output = operation.getOutput();
530 			if (output != null) {
531 				SOAPBody soapBody = new SOAPBodyImpl();
532 				soapBody.setUse("literal");
533 
534 				BindingOutput bindingOutput = wsdl.createBindingOutput();
535 				bindingOutput.setName(output.getName());
536 				bindingOutput.addExtensibilityElement(soapBody);
537 
538 				bop.setBindingOutput(bindingOutput);
539 			}
540 
541 			Map faults = operation.getFaults();
542 
543 			Iterator faultIt = faults.entrySet().iterator();
544 			while (faultIt.hasNext()) {
545 				Fault fault = (Fault) ((Map.Entry) faultIt.next()).getValue();
546 				BindingFault bindingFault = wsdl.createBindingFault();
547 				bindingFault.setName(fault.getName());
548 				bop.addBindingFault(bindingFault);
549 			}
550 
551 			wsdlBinding.addBindingOperation(bop);
552 		}
553 	}
554 
555 	/**
556 	 * Adds SOAP extensions to wsdl. This code expects that the PortTypeName is
557 	 * the real interface name.
558 	 * 
559 	 * @param def
560 	 *            The WSDL definition.
561 	 * 
562 	 * @return The new WSDL definition.
563 	 * 
564 	 * @throws WSDLException
565 	 */
566 	protected static Definition addSOAPExtensionsToWSDL(Definition def)
567 			throws WSDLException {
568 
569 		ExtensionRegistry extRegistry = def.getExtensionRegistry();
570 
571 		String defaultLocationURI = "http://localhost/services/defaultSoapAddress";
572 
573 		PortType portType = findFirstPortType(def);
574 		String portTypeLocalPart = portType.getQName().getLocalPart();
575 		String portTypeNamespace = portType.getQName().getNamespaceURI();
576 
577 		String portTypeNameCut = null;
578 		if (portTypeLocalPart.endsWith("PortType")) {
579 			int totLen = portTypeLocalPart.length();
580 			int suffixLen = "PortType".length();
581 			portTypeNameCut = portTypeLocalPart
582 					.substring(0, totLen - suffixLen);
583 		} else {
584 			portTypeNameCut = portTypeLocalPart;
585 		}
586 
587 		// creating the default names
588 		String serviceName = portTypeNameCut + "Service";
589 		String bindingName = portTypeNameCut + "HttpBinding";
590 		String portName = portTypeNameCut + "HttpPort";
591 		LOG.debug("servicename=" + serviceName + "; bindingName=" + bindingName
592 				+ "; portName=" + portName);
593 
594 		// creating the wsdl:binding element
595 		Binding wsdlBinding = def.createBinding();
596 		def.addBinding(wsdlBinding);
597 		wsdlBinding.setQName(new QName(portTypeNamespace, bindingName));
598 		wsdlBinding.setPortType(portType);
599 		wsdlBinding.setUndefined(false);
600 
601 		// Creates the binding extensibility elements
602 		Port wsdlPort = def.createPort();
603 
604 		// Creates the address extensibility elements
605 		SOAPAddress soapAddress = (SOAPAddress) extRegistry.createExtension(
606 				wsdlPort.getClass(), SOAPConstants.Q_ELEM_SOAP_ADDRESS);
607 
608 		soapAddress.setLocationURI(defaultLocationURI);
609 
610 		wsdlPort.setName(portName);
611 		wsdlPort.setBinding(wsdlBinding);
612 		wsdlPort.addExtensibilityElement(soapAddress);
613 
614 		addOperationToWsdlBinding(def, portType, wsdlBinding);
615 
616 		// creating soap:binding
617 		SOAPBinding soapBinding = (SOAPBinding) extRegistry.createExtension(
618 				wsdlBinding.getClass(), SOAPConstants.Q_ELEM_SOAP_BINDING);
619 		soapBinding.setStyle("document");
620 		soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
621 
622 		wsdlBinding.addExtensibilityElement(soapBinding);
623 
624 		// Adding the service
625 		Service service = def.createService();
626 		service.setQName(new QName(portTypeNamespace, serviceName));
627 		service.addPort(wsdlPort);
628 		def.addService(service);
629 
630 		// return the WSDL with abstract and concrete part
631 		LOG.debug("NEW DEFINITION=" + def);
632 
633 		// LOG.debug("*********************");
634 		// debug(def);
635 		// LOG.debug("*********************");
636 		return def;
637 	}
638 
639 	// /**
640 	// * Print all the WSDL definition.
641 	// */
642 	// protected static void debug(Definition def) {
643 	// if (! LOG.isDebugEnabled()) return;
644 	// // else
645 	// for (Object s : def.getServices().keySet()) {
646 	// Service se = (Service) def.getServices().get(s);
647 	// LOG.debug("___ SERVICE. key=" + s + "; object=" + se + "; class="
648 	// + se.getClass());
649 	//
650 	// for (Object p : se.getPorts().keySet()) {
651 	// Port port = (Port) se.getPorts().get(p);
652 	// LOG.debug("___ PORT. key=" + p + "; object=" + port + "; class="
653 	// + port.getClass());
654 	//
655 	// Binding binding = port.getBinding();
656 	// List bindingOperations = binding.getBindingOperations();
657 	// for (int i = 0; i < bindingOperations.size(); i++) {
658 	// LOG.debug("__ I=" + i);
659 	// BindingOperation bindingOperation
660 	// = (BindingOperation) bindingOperations.get(i);
661 	// LOG.debug("__ BindingOperation=" + bindingOperation);
662 	// //String opName = bindingOperation.getOperation().getName();
663 	// }
664 	// }
665 	// }
666 	//
667 	// }
668 
669 	/**
670 	 * Writing the WSDL.
671 	 * 
672 	 * @param wsdl
673 	 *            The wsdl document
674 	 * @throws DeploymentException
675 	 *             The deployment exception
676 	 */
677 	private void writeWSDLDocument(Definition def)
678 			throws Jbi4CorbaDeployException {
679 		FileWriter fr = null;
680 		WSDLFactory factory = null;
681 		WSDLWriter writer = null;
682                 
683                 
684 		try {
685                     File root=new File(consumerServiceDescriptor.getRootPath());
686                     //allow multiple consumer
687                     if(!root.exists()){
688                         root.mkdir();
689                     }
690 			if (consumerServiceDescriptor.getWsdlFileName() == null) {
691 				consumerServiceDescriptor
692 						.setWsdlFileName(consumerServiceDescriptor
693 								.getRootPath()
694 								+ File.separator
695 								+ consumerServiceDescriptor.getServiceName()
696 								+ ".wsdl");
697 			}
698 
699 			fr = new FileWriter(consumerServiceDescriptor.getWsdlFileName());
700 
701 		} catch (IOException e) {
702 			String msg = MESSAGES
703 					.getString("CRB000105_Unable_to_open_filename",
704 							consumerServiceDescriptor.getWsdlFileName(), e
705 									.getMessage());
706 
707 			LOG.error(msg, e);
708 			throw new Jbi4CorbaDeployException(msg, e);
709 		}
710 		try {
711 			factory = WSDLFactory.newInstance();
712 			writer = factory.newWSDLWriter();
713 			writer.writeWSDL(def, fr);
714 
715 		} catch (WSDLException e) {
716 			String msg = MESSAGES.getString(
717 					"CRB000106_Unable_to_create_default_transformer", writer, e
718 							.getMessage());
719 
720 			LOG.error(msg, e);
721 			throw new Jbi4CorbaDeployException(msg, e);
722 		}
723 
724 		try {
725 			fr.close();
726 		} catch (IOException e) {
727 			String msg = MESSAGES.getString(
728 					"CRB000108_Unable_to_close_fileWriter", fr, e.getMessage());
729 
730 			LOG.error(msg, e);
731 			throw new Jbi4CorbaDeployException(msg, e);
732 		}
733 
734 	}
735 
736 	/**
737 	 * Getter.
738 	 * 
739 	 * @return The consumer service descriptor.
740 	 */
741 	public ConsumerServiceDescriptor getConsumerServiceDescriptor() {
742 		return consumerServiceDescriptor;
743 	}
744 
745 	/**
746 	 * Setter.
747 	 * 
748 	 * @param consumerServiceDescriptor
749 	 *            The consumer service descriptor
750 	 */
751 	public void setConsumerServiceDescriptor(
752 			ConsumerServiceDescriptor consumerServiceDescriptor) {
753 		this.consumerServiceDescriptor = consumerServiceDescriptor;
754 	}
755 
756 	/**
757 	 * Create a wsdl definition for a consumer endpoint. Loads the target
758 	 * endpoint definition and add http binding informations to it.
759 	 * 
760 	 * @return The return
761 	 * 
762 	 * @throws DeploymentException
763 	 *             The deployment exception
764 	 */
765 	protected ServiceEndpoint retrieveProxiedEndpointDefinition()
766 			throws Jbi4CorbaDeployException {
767 
768 		String targetEndpoint = consumerServiceDescriptor.getTargetEndpoint();
769 		QName targetService = consumerServiceDescriptor.getTargetService();
770 		QName targetInterfaceName = consumerServiceDescriptor
771 				.getTargetInterfaceName();
772 
773 		if (LOG.isDebugEnabled()) {
774 			LOG.debug("Retrieving proxied endpoint definition");
775 			LOG.debug("Target service: " + targetService);
776 			LOG.debug("Target endpoint: " + targetEndpoint);
777 			LOG.debug("Target Interface Name: " + targetInterfaceName);
778 		}
779 
780 		ComponentContext ctx = RuntimeContext.getInstance()
781 				.getComponentContext();
782 		ServiceEndpoint ep = null;
783 
784 		LOG.info("CRB000151_Endpoint_found", new Object[] { ctx.getEndpoint(
785 				targetService, targetEndpoint) });
786 		ServiceEndpoint[] eps1 = ctx.getEndpointsForService(targetService);
787 		for (int i = 0; i < eps1.length; i++) {
788 			LOG.debug("Endpoint from  getEndpointsForService found:" + eps1[i]);
789 		}
790 
791 		if (targetService != null || targetEndpoint != null
792 				|| targetInterfaceName != null) {
793 
794 			// targetXXX is defined on SU configuration (see CRB-64)
795 			if (targetService != null && targetEndpoint != null) {
796 				ep = ctx.getEndpoint(targetService, targetEndpoint);
797 				if (ep == null) {
798 					String msg = MESSAGES.getString(
799 							"CRB000109_Could_not_retrieve_endpoint",
800 							targetService, targetEndpoint, ctx);
801 
802 					LOG.error(msg);
803 					throw new Jbi4CorbaDeployException(msg);
804 				}
805 				LOG
806 						.debug("Endpoint retrived using targetService/targetEndpoint:"
807 								+ targetService + "/" + targetEndpoint);
808 			}
809 
810 			if (ep == null && targetService != null) {
811 				LOG.debug("Returns EP from service");
812 				ServiceEndpoint[] eps = ctx
813 						.getEndpointsForService(targetService);
814 				if (eps != null && eps.length > 0) {
815 					ep = eps[0];
816 				}
817 				if (ep == null) {
818 					LOG.error("CRB000110_Could_not_retrieve_endpoint",
819 							targetService, ctx);
820 					throw new Jbi4CorbaDeployException(MESSAGES.getString(
821 							"CRB000110_Could_not_retrieve_endpoint",
822 							targetService, ctx));
823 				}
824 				LOG.debug("Endpoint retrived using targetService:"
825 						+ targetService);
826 			}
827 
828 			if (ep == null && targetInterfaceName != null) {
829 				LOG.debug("Returns EP from targetInterfaceName");
830 				ServiceEndpoint[] eps = ctx.getEndpoints(targetInterfaceName);
831 				if (eps != null && eps.length > 0) {
832 					ep = eps[0];
833 				}
834 				if (ep == null) {
835 					LOG.error("CRB000111_Could_not_retrieve_endpoint",
836 							targetInterfaceName, ctx);
837 					throw new Jbi4CorbaDeployException(MESSAGES.getString(
838 							"CRB000111_Could_not_retrieve_endpoint",
839 							targetInterfaceName, ctx));
840 				}
841 				LOG.debug("Endpoint retrived using targetInterfaceName:"
842 						+ targetInterfaceName);
843 			}
844 		}
845 
846 		if (ep != null) {
847 			LOG.debug("Enpoind retrived");
848 			LOG.debug("     Service Name:" + ep.getServiceName());
849 			LOG.debug("     Endpoint Name:" + ep.getEndpointName());
850 		} else {
851 			LOG.info("CRB000112_No_endpoint_retrived");
852 		}
853 
854 		// If the endpoint is null, loads it using my name
855 		if (ep == null) {
856 			LOG.debug("Endpoint is null: pointing to myself");
857 			ep = ctx.getEndpoint(this.getServiceName(), this.getEndpointName());
858 		}
859 
860 		return ep;
861 	}
862 
863 	/**
864 	 * Validate the endpoint. (now do noting)
865 	 * 
866 	 * @throws Jbi4EjbException
867 	 *             if some problem occurs
868 	 */
869 	public void validate() throws Jbi4CorbaException {
870 		// Do nothing
871 	}
872 
873 	/**
874 	 * Unregisters the service.
875 	 * 
876 	 * @see it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbEndpoint#unregisterService()
877 	 * @throws Jbi4EjbException
878 	 *             if some problem occurs
879 	 */
880 	public void unregisterService() throws Jbi4CorbaException {
881 		// DO nothing
882 		LOG.info("CRB000150_Service_unregistered", new Object[] {
883 				this.getServiceName(), this.getEndpointName() });
884 
885 	}
886 
887 	/**
888 	 * Gets the provider role.
889 	 */
890 	public Role getRole() {
891 		return Role.CONSUMER;
892 	}
893 
894 }