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.Jbi4CorbaDeployException;
13  import it.imolinfo.jbi4corba.exception.Jbi4CorbaException;
14  import it.imolinfo.jbi4corba.jbi.Messages;
15  import it.imolinfo.jbi4corba.jbi.component.runtime.RuntimeContext;
16  import it.imolinfo.jbi4corba.jbi.cxf.CXFUtils;
17  import it.imolinfo.jbi4corba.jbi.processor.ProviderExchangeProcessor;
18  import it.imolinfo.jbi4corba.webservice.descriptor.ProviderServiceDescriptor;
19  import it.imolinfo.jbi4corba.webservice.runtime.ProviderServiceCreator;
20  
21  import java.io.BufferedReader;
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.FileReader;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  import java.lang.reflect.InvocationTargetException;
29  import java.lang.reflect.Method;
30  import java.rmi.RMISecurityManager;
31  import java.util.Map;
32  import java.util.Properties;
33  
34  import java.util.logging.Level;
35  import javax.jbi.messaging.MessageExchange.Role;
36  import javax.wsdl.WSDLException;
37  import javax.xml.namespace.QName;
38  import javax.xml.parsers.DocumentBuilder;
39  import javax.xml.parsers.DocumentBuilderFactory;
40  import javax.xml.parsers.ParserConfigurationException;
41  import javax.xml.transform.Transformer;
42  import javax.xml.transform.TransformerConfigurationException;
43  import javax.xml.transform.TransformerException;
44  import javax.xml.transform.TransformerFactory;
45  import javax.xml.transform.dom.DOMSource;
46  import javax.xml.transform.stream.StreamResult;
47  
48  import org.apache.cxf.endpoint.Endpoint;
49  import org.apache.cxf.service.Service;
50  import org.apache.cxf.service.model.EndpointInfo;
51  import org.omg.CORBA.Any;
52  import org.omg.CORBA.NamedValue;
53  import org.omg.CORBA.ORB;
54  import org.omg.CORBA.Object;
55  import org.omg.CORBA.Request;
56  import org.omg.CORBA.TypeCode;
57  import org.omg.CORBA.ORBPackage.InvalidName;
58  import org.omg.CORBA.SystemException;
59  import org.omg.CORBA.portable.ValueFactory;
60  import org.omg.CosNaming.NamingContextExt;
61  import org.omg.CosNaming.NamingContextExtHelper;
62  import org.omg.CosNaming.NamingContextPackage.CannotProceed;
63  import org.omg.CosNaming.NamingContextPackage.NotFound;
64  import org.w3c.dom.Document;
65  import org.xml.sax.SAXException;
66  
67  /**
68   * The Provider Endpoint.
69   */
70  @SuppressWarnings("unchecked")
71  public class ProviderEndpoint extends Jbi4CorbaEndpoint {
72  
73  	/** serialVersionUID. */
74  	private static final long serialVersionUID = -7829120677780408268L;
75  
76  	/**
77  	 * The logger for this class and its instances.
78  	 */
79  	private static final Logger LOG = LoggerFactory
80  			.getLogger(ProviderEndpoint.class);
81  
82  	/**
83  	 * The responsible of message localization.
84  	 */
85  	private static final Messages MESSAGES = Messages
86  			.getMessages(ProviderEndpoint.class);
87  
88  	private ProviderServiceDescriptor serviceDescriptor;
89  
90  	// The cxf service model
91  	private Service cxfService;
92  
93  	// The cxf endpoint
94  	private Endpoint cxfEndpoint;
95  
96  	private ORB orb;
97  
98  	/**
99  	 * The Corba helper class
100 	 */
101 	private Class helperClass;
102 
103 	/**
104 	 * The Narrow Method
105 	 */
106 	private Method narrowMethod;
107 
108 	private String iorCorbaObject;
109 
110         private final String IOREXT=".ior";
111 	/**
112 	 * Instantiates a new jbi4 ejb provider endpoint.
113 	 * 
114 	 * @param serviceName
115 	 *            the service name
116 	 * @param endpointName
117 	 *            the endpoint name
118 	 * 
119 	 * @throws Jbi4EjbException
120 	 *             if some problem occurs
121 	 */
122 	public ProviderEndpoint(QName serviceName, String endpointName,
123 			ProviderServiceDescriptor serviceDescriptor)
124 			throws Jbi4CorbaException {
125 		super(serviceName, endpointName);
126 		this.serviceDescriptor = serviceDescriptor;
127 		this.setExchangeProcessor(new ProviderExchangeProcessor(this));
128 		this.serviceDescriptor.setEndpoint(this);
129 	}
130 
131 	/**
132 	 * @return Returns the CXF Service.
133 	 */
134 	public Service getCXFService() {
135 		return cxfService;
136 	}
137 
138 	/**
139 	 * @return Returns the CXF Service.
140 	 */
141 	public Endpoint getCXFEndpoint() {
142 		return cxfEndpoint;
143 	}
144 
145 	/**
146 	 * Return the Ior for the corba object
147 	 */
148 	public String getIorCorbaObject() {
149 		return iorCorbaObject;
150 	}
151 
152 	/**
153 	 * set the Ior for reference corba Object generate by statefull interface
154 	 */
155 	public void setIorCorbaObject(String iorCorbaObject) {
156 		this.iorCorbaObject = iorCorbaObject;
157 	}
158 
159 	/**
160 	 * This method returns the role of the component.
161 	 * 
162 	 * @return The role of the component.
163 	 * 
164 	 * @see org.servicemix.common.Endpoint#getRole() @org.apache.xbean.XBean
165 	 *      hide="true"
166 	 */
167 	public Role getRole() {
168 		return Role.PROVIDER;
169 	}
170 
171 	/**
172 	 * 
173 	 * @throws Exception
174 	 *             The exception
175 	 */
176 	public void activate() throws Jbi4CorbaException {
177 		LOG.debug(">>>>> activate - begin");
178 
179 		initOrb();
180 		try {
181 			locateCorbaService();
182 		} catch (Jbi4CorbaException e) {
183 			// LOG.warn("Unable to retrived object using locateCorbaService: " +
184 			// e);
185 			String msg = MESSAGES
186 					.getString(
187 							"CRB000145_Unable_to_retrive_object_using_locateCorbaService",
188 							new java.lang.Object[] { e });
189 			LOG.warn(msg);
190 		}
191 		LOG.debug("<<<<< activate - end");
192 	}
193 
194 	/**
195 	 * Deactivate the endpoint.
196 	 * 
197 	 * @throws Exception
198 	 *             The exception
199 	 */
200 	public void deactivate() throws Jbi4CorbaException {
201 		stopCorbaOrb();
202 	}
203 	
204 	/**
205 	 * Gets the service data from the WSDL and creates the CXF service.
206 	 * 
207 	 * @throws DeploymentException
208 	 *             The deployment exception
209 	 */
210 	public void registerService() throws Jbi4CorbaDeployException {
211 		LOG.debug("registering service for service descriptor: "
212 				+ serviceDescriptor);
213 
214 		serviceDescriptor.setComponentRootPath(RuntimeContext.getInstance()
215 				.getComponentContext().getInstallRoot());
216 
217 		// CXF service generation
218 		ProviderServiceCreator serviceCreator = new ProviderServiceCreator();
219 		try {
220 			cxfService = serviceCreator.createService(serviceDescriptor, this
221 					.getServiceName());
222 			LOG.debug("CXF Service Gnerator >>>> " + this.getServiceName());
223                         //31-03-10 Added To Fix Nokua Issue 26 Error's with wsdl that import xsd Schema
224                         this.setServiceDescription(CXFUtils.getAbstractWSDLDocument(cxfService));
225 			EndpointInfo cxfei = CXFUtils.getEndpointInfo(cxfService);
226 			cxfEndpoint = CXFUtils.getEndpoint(cxfService, cxfei);
227 		} catch (Jbi4CorbaException ex) {
228 			String msg = MESSAGES.getString(
229 					"CRB000146_Error_in_service_creation",
230 					new java.lang.Object[] { ex.getMessage() });
231 			LOG.error(msg, ex);
232 			throw new Jbi4CorbaDeployException(msg, ex);
233 		} catch (IOException ex) {
234 			String msg = MESSAGES.getString(
235 					"CRB000146_Error_in_service_creation",
236 					new java.lang.Object[] { ex.getMessage() });
237 			LOG.error(msg, ex);
238 			throw new Jbi4CorbaDeployException(msg, ex);
239 		} catch (WSDLException ex) {
240 			String msg = MESSAGES.getString(
241 					"CRB000146_Error_in_service_creation",
242 					new java.lang.Object[] { ex.getMessage() });
243 			LOG.error(msg, ex);
244 			throw new Jbi4CorbaDeployException(msg, ex);
245 		}
246 
247 		DOMSource domSource = new DOMSource(generateWsdl(cxfService));
248 
249                 String wsdlFilename = null;
250 		wsdlFilename = serviceDescriptor.getWsdlRootDirectory() + "/"
251 				+ cxfService.getName().getLocalPart() + ".wsdl.debug.cxf";
252 
253 		LOG.debug("CRB000127_Producing_service_wsdl_to",
254 				new java.lang.Object[] { wsdlFilename });
255 		FileWriter fr;
256 		try {
257 			fr = new FileWriter(wsdlFilename);
258 		} catch (IOException e) {
259 			String msg = MESSAGES.getString(
260 					"CRB000105_Unable_to_open_filename", wsdlFilename, e
261 							.getMessage());
262 
263 			LOG.error(msg, e);
264 			throw new Jbi4CorbaDeployException(msg, e);
265 		}
266 		StreamResult streamResult = new StreamResult(fr);
267 		TransformerFactory transformerFactory = TransformerFactory
268 				.newInstance();
269 		Transformer transformer;
270 		try {
271 			transformer = transformerFactory.newTransformer();
272 		} catch (TransformerConfigurationException e) {
273 			String msg = MESSAGES.getString(
274 					"CRB000106_Unable_to_create_default_transformer",
275 					transformerFactory, e.getMessage());
276 
277 			LOG.error(msg, e);
278 			throw new Jbi4CorbaDeployException(msg, e);
279 		}
280 		try {
281 			transformer.transform(domSource, streamResult);
282 		} catch (TransformerException e) {
283 			String msg = MESSAGES.getString(
284 					"CRB000107_Unable_to_create_transform", domSource,
285 					streamResult, transformer, e.getMessage());
286 
287 			LOG.error(msg, e);
288 			throw new Jbi4CorbaDeployException(msg, e);
289 		}
290 		try {
291 			fr.close();
292 		} catch (IOException e) {
293 			String msg = MESSAGES.getString(
294 					"CRB000108_Unable_to_close_fileWriter", fr, e.getMessage());
295 
296 			LOG.error(msg, e);
297 			throw new Jbi4CorbaDeployException(msg, e);
298 		}
299 	}
300 
301 	/**
302 	 * @param serviceCreator
303 	 *            The service creator
304 	 * @param sd
305 	 *            The provider service descriptor
306 	 * @param interfaceName
307 	 *            The interface name
308 	 * 
309 	 * @return The WSDL generated
310 	 * 
311 	 * @throws DeploymentException
312 	 *             The deployment exception
313 	 */
314 	protected Document generateWsdl(Service cxfService)
315 			throws Jbi4CorbaDeployException {
316 
317 		ByteArrayOutputStream baos = new ByteArrayOutputStream();
318 
319 		try {
320 			CXFUtils.writeDefinitionOnOutputStream(cxfService, baos);
321 		} catch (WSDLException ex) {
322 			String msg = MESSAGES.getString("CRB000147_Error_in_WSDL_creation",
323 					new java.lang.Object[] { ex.getMessage() });
324 			LOG.warn(msg, ex);
325 		}
326 
327 		LOG.debug(">>>>> generateWsdl - begin");
328 
329 		if (LOG.isDebugEnabled()) {
330 
331 			LOG.debug("cxfService:" + cxfService);
332 
333 			LOG.debug("WSDL:\n------------------\n" + baos.toString()
334 					+ "\n------------------");
335 		}
336 		// =========================================================
337 		// WSDL From CXF -> WSDL as DOM
338 		// =========================================================
339 
340 		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
341 		factory.setNamespaceAware(true);
342 
343 		DocumentBuilder documentBuilder;
344 		try {
345 			documentBuilder = factory.newDocumentBuilder();
346 		} catch (ParserConfigurationException e) {
347 			String msg = MESSAGES.getString(
348 					"CRB000128_Unable_to_create_documentBuilder", factory, e
349 							.getMessage());
350 
351 			LOG.error(msg, e);
352 			throw new Jbi4CorbaDeployException(msg, e);
353 		}
354 
355 		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
356 		Document doc;
357 		try {
358 
359 			doc = documentBuilder.parse(bais);
360 
361 		} catch (SAXException e) {
362 			String m = MESSAGES.getString("CRB000129_Unable_to_parse_document",
363 					bais, documentBuilder, e.getMessage());
364 
365 			LOG.error(m, e);
366 			throw new Jbi4CorbaDeployException(m, e);
367 		} catch (IOException e) {
368 			String m = MESSAGES.getString("CRB000129_Unable_to_parse_document",
369 					bais, documentBuilder, e.getMessage());
370 
371 			LOG.error(m, e);
372 			throw new Jbi4CorbaDeployException(m, e);
373 		}
374 
375 		LOG.debug("<<<<< generateWsdl - end");
376 		return doc;
377 	}
378 
379 	/**
380 	 * @return Returns the serviceDescriptor
381 	 */
382 	public ProviderServiceDescriptor getServiceDescriptor() {
383 		return serviceDescriptor;
384 	}
385 
386 	/**
387 	 * @param serviceDescriptor
388 	 *            The serviceDescriptor to set.
389 	 */
390 	public void setServiceDescriptor(ProviderServiceDescriptor serviceDescriptor) {
391 		this.serviceDescriptor = serviceDescriptor;
392 	}
393 
394 	/**
395 	 * Stops the ORB.
396 	 */
397 	private void stopCorbaOrb() {
398 		orb.shutdown(true);
399 	}
400 
401 	/**
402 	 * 
403 	 * @throws Jbi4CorbaException
404 	 *             The Jbi4Corba exception
405 	 */
406 	private void initOrb() throws Jbi4CorbaException {
407 
408 		LOG.debug(">>>>> initOrb - begin");
409 		ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
410 		Thread.currentThread().setContextClassLoader(
411 		// serviceDescriptor.getUrlClassLoader());
412 				serviceDescriptor.getOriginalClassLoader());
413 		if (serviceDescriptor != null) {
414 			debugProperties(serviceDescriptor.getOrbProperties());
415 		}
416 
417 		try {
418 			LOG.debug("locating service for serviceDescriptor: "
419 					+ serviceDescriptor);
420 
421 			orb = ORB.init((String[]) null, serviceDescriptor
422 					.getOrbProperties());
423 
424 			LOG.debug("orb: " + orb + " created for service: "
425 					+ serviceDescriptor);
426 
427 			/* Registering the factory class. */
428 			Map<String, java.lang.Object> map = serviceDescriptor
429 					.getValueTypeIdAndInstance();
430 
431 			if (map == null || map.size() == 0) {
432 				LOG
433 						.debug("CRB000130_No_value_type_factory_to_register_using_the_ORB");
434 			} else {
435 				LOG.debug("ValueTypeFactoryMap.size=" + map.size());
436 
437 				// registering all valuetypes factories.
438 				for (String id : map.keySet()) {
439 					ValueFactory vf = (ValueFactory) map.get(id);
440 
441 					LOG.debug("Provider. Registering a ValueType Factory. id="
442 							+ id + "; instance=" + vf);
443 
444 					((org.omg.CORBA_2_3.ORB) orb)
445 							.register_value_factory(id, vf);
446 
447 				}
448 			}
449 			// get the helper class and the methods
450             //LOG.debug("RAAAF: in deploy lookup value factory per MySequence: "+((org.omg.CORBA_2_3.ORB) orb).lookup_value_factory("IDL:3hh4.123/it/imolinfo/jbi4corba/test/testprovidercomplex/MySequence:1.0"));
451             //LOG.debug("RAAAF: in deploy lookup value factory per MySequence: "+((org.omg.CORBA_2_3.ORB) orb).lookup_value_factory("IDL:3hh4.123/it/imolinfo/jbi4corba/test/testprovidercomplex/MySequence:1.0").getClass());
452             //LOG.debug("RAAAF: in deploy lookup value factory per MySequence: "+((ValueFactory)((org.omg.CORBA_2_3.ORB) orb).lookup_value_factory("IDL:3hh4.123/it/imolinfo/jbi4corba/test/testprovidercomplex/MySequence:1.0")).);
453 
454 			//
455 			helperClass = serviceDescriptor.getCorbaHelperClass();
456 
457 			// Reload using original classloader.
458 
459 			String helperClassName = helperClass.getName();
460 			helperClass = serviceDescriptor.getOriginalClassLoader().loadClass(
461 					helperClassName);
462 			LOG.debug("Helper class loader: "
463 					+ helperClass.getClass().getName());
464 			narrowMethod = getNarrowMethod(helperClass);
465 
466 		} catch (ClassNotFoundException ex) {
467 			java.util.logging.Logger
468 					.getLogger(ProviderEndpoint.class.getName()).log(
469 							Level.SEVERE, null, ex);
470 			String msg = MESSAGES.getString("CRB000152_Error_in_initializing_the_ORB");
471 			LOG.error(msg, ex.getMessage());
472 
473 		} finally {
474 			Thread.currentThread().setContextClassLoader(oldCL);
475 			LOG.debug("<<<<< initOrb - end");
476 		}
477 	}
478 
479 	/**
480 	 * 
481 	 * @throws Jbi4CorbaException
482 	 *             The Jbi4Corba exception
483 	 */
484 	public void locateCorbaService() throws Jbi4CorbaException {
485 
486 		ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
487 		Thread.currentThread().setContextClassLoader(
488 		// serviceDescriptor.getUrlClassLoader());
489 				serviceDescriptor.getOriginalClassLoader());
490 		try {
491 			LOG.debug(">>>>> locateCorbaService - begin");
492 
493 			// Class helperClass = serviceDescriptor.getCorbaHelperClass();
494 			// Method narrowMethod = getNarrowMethod(helperClass);
495 			// Method typeMethod = getTypeMethod(helperClass);
496 			// Method extractMethod = getExtractMethod(helperClass);
497 
498 			java.lang.Object retrievedObject = null;
499 			Object corbaObjectReference = null;
500 
501 			// ---- Localization ...
502 			LOG.debug("CRB000140_LocalizationType",
503 					new java.lang.Object[] { serviceDescriptor
504 							.getLocalizationType() });
505 
506 			if (ProviderServiceDescriptor.NAMESERVICE
507 					.equalsIgnoreCase(serviceDescriptor.getLocalizationType())) {
508 
509 				retrievedObject = localizationViaNameService();
510 
511 				corbaObjectReference = getCorbaObjectReference(helperClass,
512 						narrowMethod, retrievedObject);
513 
514 			} else if (ProviderServiceDescriptor.CORBALOC
515 					.equalsIgnoreCase(serviceDescriptor.getLocalizationType())) {
516 
517 				retrievedObject = localizationViaCorbaloc();
518 
519 				corbaObjectReference = getCorbaObjectReference(helperClass,
520 						narrowMethod, retrievedObject);
521 			} else if (ProviderServiceDescriptor.CORBANAME
522 					.equalsIgnoreCase(serviceDescriptor.getLocalizationType())) {
523 
524 				retrievedObject = localizationViaCorbaname();
525 
526 				corbaObjectReference = getCorbaObjectReference(helperClass,
527 						narrowMethod, retrievedObject);
528 			} else if (ProviderServiceDescriptor.IOR
529 					.equalsIgnoreCase(serviceDescriptor.getLocalizationType())) {
530 
531 				retrievedObject = localizationViaIOR();
532 
533 				corbaObjectReference = getCorbaObjectReference(helperClass,
534 						narrowMethod, retrievedObject);
535 			}
536 
537 			// else if (EJB.equals(serviceDescriptor.getLocalizationType())) {
538 			//
539 			// retrievedObject = ejbLocalization(helperClass,
540 			// extractMethod,
541 			// narrowMethod,
542 			// typeMethod);
543 			//
544 			// corbaObjectReference = getCorbaObjectReference(
545 			// helperClass, narrowMethod, retrievedObject);
546 			//
547 			// }
548 			// Added for Test
549 			// else {
550 			// throw new IllegalArgumentException(MESSAGES.getString(
551 			// "CRB000137_Localization_type_not_supported",
552 			// serviceDescriptor.getLocalizationType()));
553 			// }
554 
555 			serviceDescriptor.setCorbaObjectReference(corbaObjectReference);
556 			LOG.debug("service reference found: " + corbaObjectReference);
557 			if (corbaObjectReference != null) {
558 				LOG.debug("service reference classloader: "
559 						+ corbaObjectReference.getClass().getClassLoader());
560 			}
561 
562 		} finally {
563 			Thread.currentThread().setContextClassLoader(oldCL);
564 		}
565 		LOG.debug("<<<<< locateCorbaService - end");
566 	}
567 
568 	/**
569 	 * This method is used to obtain a reference to the corba object.
570 	 * 
571 	 * @param helperClass
572 	 *            The class that provides the method 'narrow'.
573 	 * @param narrowMethod
574 	 *            The method to invoke.
575 	 * @param retrievedObject
576 	 *            The object used as paramameter for the narrow.
577 	 * 
578 	 * @return The corba object reference.
579 	 * 
580 	 * @throws Jbi4CorbaException
581 	 *             The Jbi4Corba exception
582 	 */
583 	protected Object getCorbaObjectReference(Class helperClass,
584 			Method narrowMethod, java.lang.Object retrievedObject)
585 			throws Jbi4CorbaException {
586 
587 		LOG.debug("helperClass=" + helperClass + "; narrowMethod="
588 				+ narrowMethod + "; retrievedObject=" + retrievedObject);
589 
590 		Object corbaObjectReference = null;
591 		try {
592 
593 			Object retrievedObjectAsCorbaObject = (Object) retrievedObject;
594 
595 			corbaObjectReference = (Object) narrowMethod.invoke(helperClass,
596 					retrievedObjectAsCorbaObject);
597 
598 		} catch (IllegalArgumentException e) {
599 			java.lang.Object[] args = new java.lang.Object[] {
600 					"IllegalArgumentException", retrievedObject, e.getMessage() };
601 
602 			LOG.error("CRB000135_Exception_calling_narrow_method", args, e);
603 			throw new Jbi4CorbaException(
604 					"CRB000135_Exception_calling_narrow_method", args, e);
605 		} catch (IllegalAccessException e) {
606 			java.lang.Object[] args = new java.lang.Object[] {
607 					"IllegalAccessException", retrievedObject, e.getMessage() };
608 
609 			LOG.error("CRB000135_Exception_calling_narrow_method", args, e);
610 			throw new Jbi4CorbaException(
611 					"CRB000135_Exception_calling_narrow_method", args, e);
612 		} catch (InvocationTargetException e) {
613 			Throwable target = e.getTargetException();
614 
615 			LOG.error("CRB000135_Exception_calling_narrow_method",
616 					new java.lang.Object[] { "InvocationTargetException",
617 							retrievedObject, e.getMessage() }, e);
618 			LOG.error("CRB000136_Real_error_message_is",
619 					new java.lang.Object[] { target.getMessage() }, target);
620 			throw new Jbi4CorbaException(
621 					"CRB000135_Exception_calling_narrow_method",
622 					new java.lang.Object[] { "Exception", retrievedObject,
623 							target.getMessage() }, target);
624 		}
625 
626 		LOG.debug("corbaObjectReference=" + corbaObjectReference);
627 		return corbaObjectReference;
628 	}
629 
630 	/**
631 	 * 
632 	 * @param helperClass
633 	 *            The helper class
634 	 * @param extractMethod
635 	 *            The extract method
636 	 * @param narrowMethod
637 	 *            The narrow method
638 	 * @param typeMethod
639 	 *            The type method
640 	 * @return The return
641 	 * @throws Jbi4CorbaException
642 	 *             The Jbi4Corba exception
643 	 */
644 	protected java.lang.Object ejbLocalization(Class helperClass,
645 			Method extractMethod, Method narrowMethod, Method typeMethod)
646 			throws Jbi4CorbaException {
647 
648 		java.lang.Object retrievedObject = null;
649 		SecurityManager oldSecurityManager = System.getSecurityManager();
650 		try {
651 
652 			System.setSecurityManager(new RMISecurityManager());
653 			// obtain home interface via corba loc
654 			Object ejbHome = orb.string_to_object(serviceDescriptor
655 					.getCorbaServiceName());
656 			// call create method and obtain a pointer to real stateless bean
657 			Any result = orb.create_any();
658 			NamedValue resultVal = orb.create_named_value("result", result,
659 					org.omg.CORBA.ARG_OUT.value);
660 			Request createRequest = ejbHome._create_request(null, "create", orb
661 					.create_list(0), resultVal);
662 			TypeCode resultTypeCode;
663 
664 			try {
665 				resultTypeCode = (TypeCode) typeMethod.invoke(helperClass,
666 						new java.lang.Object[] {});
667 
668 			} catch (IllegalArgumentException e) {
669 				java.lang.Object[] args = new java.lang.Object[] {
670 						"IllegalArgumentException", retrievedObject,
671 						e.getMessage() };
672 				// @TODO
673 				LOG.error("CRB000135_Exception_calling_typeMethod", args, e);
674 				throw new Jbi4CorbaException(
675 						"CRB000135_Exception_calling_typeMethod", args, e);
676 			} catch (IllegalAccessException e) {
677 				java.lang.Object[] args = new java.lang.Object[] {
678 						"IllegalAccessException", retrievedObject,
679 						e.getMessage() };
680 				// @TODO
681 				LOG.error("CRB000135_Exception_calling_typeMethod", args, e);
682 				throw new Jbi4CorbaException(
683 						"CRB000135_Exception_calling_extractMethod", args, e);
684 			} catch (InvocationTargetException e) {
685 				Throwable target = e.getTargetException();
686 				// @TODO
687 				LOG.error("CRB000135_Exception_calling_typeMethod",
688 						new java.lang.Object[] { "InvocationTargetException",
689 								retrievedObject, e.getMessage() }, e);
690 				LOG.error("CRB000136_Real_error_message_is",
691 						new java.lang.Object[] { target.getMessage() }, target);
692 				throw new Jbi4CorbaException(
693 						"CRB000135_Exception_calling_typeMethod",
694 						new java.lang.Object[] { "Exception", retrievedObject,
695 								target.getMessage() }, target);
696 			}
697 
698 			createRequest.set_return_type(resultTypeCode);
699 			createRequest.invoke();
700 			result = createRequest.return_value();
701 
702 			// we assume this is a refence to the ejb
703 			try {
704 
705 				retrievedObject = extractMethod.invoke(helperClass,
706 						new java.lang.Object[] { result });
707 
708 			} catch (IllegalArgumentException e) {
709 				java.lang.Object[] args = new java.lang.Object[] {
710 						"IllegalArgumentException", retrievedObject,
711 						e.getMessage() };
712 				// @TODO
713 				LOG.error("CRB000135_Exception_calling_extractMethod", args, e);
714 				throw new Jbi4CorbaException(
715 						"CRB000135_Exception_calling_extractMethod", args, e);
716 			} catch (IllegalAccessException e) {
717 				java.lang.Object[] args = new java.lang.Object[] {
718 						"IllegalAccessException", retrievedObject,
719 						e.getMessage() };
720 				// @TODO
721 				LOG.error("CRB000135_Exception_calling_extractMethod", args, e);
722 				throw new Jbi4CorbaException(
723 						"CRB000135_Exception_calling_extractMethod", args, e);
724 			} catch (InvocationTargetException e) {
725 				Throwable target = e.getTargetException();
726 				// @TODO
727 				LOG.error("CRB000135_Exception_calling_extractMethod",
728 						new java.lang.Object[] { "InvocationTargetException",
729 								retrievedObject, e.getMessage() }, e);
730 				LOG.error("CRB000136_Real_error_message_is",
731 						new java.lang.Object[] { target.getMessage() }, target);
732 				throw new Jbi4CorbaException(
733 						"CRB000135_Exception_calling_extractMethod",
734 						new java.lang.Object[] { "Exception", retrievedObject,
735 								target.getMessage() }, target);
736 			}
737 		} finally {
738 			System.setSecurityManager(oldSecurityManager);
739 		}
740 
741 		return retrievedObject;
742 	}
743 
744 	/**
745 	 * This method is used to retrieve an object using corbaloc url. The
746 	 * corbaloc URL is defined using the corbaServiceName property of the
747 	 * service descriptor.
748 	 * 
749 	 * [...]
750 	 * 
751 	 * Bootstrap Options for the ORB
752 	 * 
753 	 * The ORB can be configured to return the handle of a customized CORBA
754 	 * service from resolve_initial_references() using either ORBInitRef and/or
755 	 * ORBDefaultInitRef.
756 	 * 
757 	 * For example, Use -ORBInitRef to resolve to specific CORBA services, for
758 	 * example,
759 	 * 
760 	 * -ORBInitRef TraderService=corbaloc::myBank.com:2050/TraderService
761 	 * 
762 	 * If no -ORBInitRef is given, -ORBDefaultInitRef is used to resolve. In the
763 	 * TraderService example,
764 	 * 
765 	 * -ORBDefaultInitRef corbaloc:iiop:1.2:myBank.com:2050
766 	 * 
767 	 * The order of resolution when these options are used is as follows: 1.
768 	 * Objects registered with register_initial_references 2. -ORBInitRef 3.
769 	 * -ORBDefaultInitRef
770 	 * 
771 	 * @see http://java.sun.com/j2se/1.5.0/docs/guide/idl/INStutorial.html
772 	 * @see it.imolinfo.jbi4corba.webservice.descriptor.ProviderServiceDescriptor
773 	 * 
774 	 * @return The object used to retrieve the corba reference.
775 	 * 
776 	 * @throws Jbi4CorbaException
777 	 *             When the properties ORBInitRef and ORBDefaultInitRef are both
778 	 *             null or empty.
779 	 * 
780 	 */
781 	protected java.lang.Object localizationViaCorbaloc()
782 			throws Jbi4CorbaException {
783 
784 		String corbalocUrl = serviceDescriptor.getCorbaServiceName();
785 		LOG.debug("corbalocUrl=" + corbalocUrl);
786 
787 		if (corbalocUrl == null || "".equals(corbalocUrl)) {
788 			LOG.error("CRB000141_CorbalocURL_NotFound");
789 			throw new Jbi4CorbaException("CRB000141_CorbalocURL_NotFound");
790 		}
791 
792 		java.lang.Object retrievedObject = orb.string_to_object(corbalocUrl);
793 		LOG.debug("retrievedObject=" + retrievedObject);
794 
795 		return retrievedObject;
796 	}
797 
798 	/**
799 	 * 
800 	 * @return The retrieved object
801 	 * @throws Jbi4CorbaException
802 	 *             The Jbi4Corba exception
803 	 */
804 	protected java.lang.Object localizationViaCorbaname()
805 			throws Jbi4CorbaException {
806 
807 		String corbanameUrl = serviceDescriptor.getCorbaServiceName();
808 		LOG.debug("corbanameUrl=" + corbanameUrl);
809 
810 		if (corbanameUrl == null || "".equals(corbanameUrl)) {
811 			LOG.error("CRB000142_CorbanameURL_NotFound");
812 			throw new Jbi4CorbaException("CRB000142_CorbanameURL_NotFound");
813 		}
814 
815 		java.lang.Object retrievedObject = orb.string_to_object(corbanameUrl);
816 		LOG.debug("retrievedObject=" + retrievedObject);
817 
818 		if (retrievedObject == null) {
819 			String msg = MESSAGES.getString(
820 					"CRB000148_Unable_to_retrived_object_using_corbaname",
821 					new java.lang.Object[] { corbanameUrl });
822 			LOG.error(msg);
823 			throw new Jbi4CorbaDeployException(msg);
824 
825 		}
826 
827 		return retrievedObject;
828 	}
829 
830 	/**
831 	 * This Method return narrowed Corba Object starting from the IOR
832 	 * 
833 	 * @param String
834 	 *            ior
835 	 * @return The return
836 	 * @throws Jbi4CorbaException
837 	 *             The Jbi4Corba exception
838 	 * 
839 	 */
840 	public java.lang.Object getCorbaObjectReference(String ior)
841 			throws Jbi4CorbaException {
842 
843 		LOG.debug("File.IOR=" + ior);
844 
845 		if (ior == null || "".equals(ior)) {
846 			LOG.error("CRB000143_IOR_NotFound");
847 			throw new Jbi4CorbaException("CRB000143_IOR_NotFound");
848 		}
849 
850 		java.lang.Object retrievedObject = null;
851 
852 		try {
853 			retrievedObject = getNarrowMethod(helperClass).invoke(helperClass,
854 					orb.string_to_object(ior));
855 			// retrievedObject=helperClass.getDeclaredMethod("narrow",
856 			// org.omg.CORBA.Object.class).invoke(helperClass,
857 			// orb.string_to_object(ior));
858 
859 		} catch (IllegalAccessException e) {
860 			String msg = MESSAGES
861 					.getString("CRB000153_Error_in_getting_corba_object_reference");
862 			LOG.error(msg, e);
863 			throw new Jbi4CorbaException(e);
864 		} catch (InvocationTargetException e) {
865 			String msg = MESSAGES
866 					.getString("CRB000153_Error_in_getting_corba_object_reference");
867 			LOG.error(msg, e);
868 			throw new Jbi4CorbaException(e);
869 		}
870 
871 		return retrievedObject;
872 	}
873 
874 	/**
875 	 * Localize the CORBA servant using the IOR.
876 	 * @return The return
877 	 * @throws Jbi4CorbaException
878 	 *             The Jbi4Corba exception
879 	 */
880 	protected java.lang.Object localizationViaIOR() throws Jbi4CorbaException {
881 		String filename = serviceDescriptor.getCorbaServiceName();
882 		LOG.debug("filename=" + filename);
883 
884 		if (filename == null || "".equals(filename)) {
885 			LOG.error("CRB000143_IOR_NotFound");
886 			throw new Jbi4CorbaException("CRB000143_IOR_NotFound");
887 		}
888 
889 		File iorFile = new File(filename);
890 		
891                 if( iorFile.exists() && iorFile.isDirectory()){
892               
893                     filename=iorFile.getAbsolutePath()+File.separator+serviceDescriptor.getPortTypeName().getLocalPart()+IOREXT;
894                     iorFile = new File(filename);
895                     LOG.debug("IOR Localization is a directory, search for file --> "+filename);
896                 }
897                 
898                 if (!iorFile.exists()) {
899 			LOG.error("CRB000143_IOR_NotFound");
900 			throw new Jbi4CorbaException("CRB000143_IOR_NotFound");
901 		}
902 
903 		String ior = readIorFromFile(filename);
904 		LOG.debug("File.IOR=" + ior);
905 
906 		if (ior == null || "".equals(ior)) {
907 			LOG.error("CRB000143_IOR_NotFound");
908 			throw new Jbi4CorbaException("CRB000143_IOR_NotFound");
909 		}
910 
911 		java.lang.Object retrievedObject = orb.string_to_object(ior);
912 		LOG.debug("retrievedObject=" + retrievedObject);
913 
914 		return retrievedObject;
915 	}
916 
917 	/**
918 	 * 
919 	 * @param filename
920 	 *            The file name
921 	 * @return The return
922 	 * @throws Jbi4CorbaException
923 	 *             The Jbi4Corba Exception
924 	 */
925 	protected String readIorFromFile(String filename) throws Jbi4CorbaException {
926 		String ior = null;
927 		BufferedReader br = null;
928 		try {
929 			br = new BufferedReader(new FileReader(filename));
930 
931 			ior = br.readLine();
932 		} catch (IOException e) {
933 			java.lang.Object[] args = { filename };
934 			LOG.error("CRB000144_ReadingFileError", args, e);
935 			throw new Jbi4CorbaException("CRB000144_ReadingFileError", args, e);
936 		}
937 
938 		return ior;
939 	}
940 
941 	/**
942 	 * This method retrieves the corba object reference using the 'NameService'.
943 	 * 
944 	 * @return The retrieved Object.
945 	 * @throws Jbi4CorbaException
946 	 *             The Jbi4Corba exception
947 	 */
948 	protected java.lang.Object localizationViaNameService()
949 			throws Jbi4CorbaException {
950 
951 		// get the root naming context
952 		org.omg.CORBA.Object objRef;
953 
954 		// resolve initial references
955 		try {
956 
957 			objRef = orb
958 					.resolve_initial_references(ProviderServiceDescriptor.NAMESERVICE);
959 
960 		} catch (InvalidName e) {
961 			java.lang.Object[] args = new java.lang.Object[] {
962 					ProviderServiceDescriptor.NAMESERVICE, orb, e.getMessage() };
963 
964 			LOG.error("CRB000131_Invalid_initial_reference_name", args, e);
965 			throw new Jbi4CorbaException(
966 					"CRB000131_Invalid_initial_reference_name", args, e);
967 		} catch (SystemException se) {
968 			// DA VERIFICARE
969 			java.lang.Object[] args = new java.lang.Object[] {
970 					ProviderServiceDescriptor.NAMESERVICE, orb, se.getMessage() };
971 			LOG.error("CRB000131_Invalid_initial_reference_name", args, se);
972 			throw new Jbi4CorbaException(
973 					"CRB000131_Invalid_initial_reference_name", args, se);
974 		}
975 
976 		// Use NamingContextExt instead of NamingContext. This is
977 		// part of the Interoperable naming Service.
978 		NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
979 		LOG.debug("name service: " + ncRef);
980 		if (ncRef == null) {
981 			throw new Jbi4CorbaException("CRB000132_Name_service_context_null");
982 		}
983 
984 		// resolve the Object Reference in Naming
985 		java.lang.Object retrievedObject = null;
986 		try {
987 
988 			retrievedObject = ncRef.resolve_str(serviceDescriptor
989 					.getCorbaServiceName());
990 
991 		} catch (NotFound e) {
992 			java.lang.Object[] args = new java.lang.Object[] { "NotFound",
993 					serviceDescriptor.getCorbaServiceName(), ncRef,
994 					e.getMessage() };
995 
996 			LOG.error("CRB000134_Exception_retrieving_corba_object_reference",
997 					args, e);
998 			throw new Jbi4CorbaException(
999 					"CRB000134_Exception_retrieving_corba_object_reference",
1000 					args, e);
1001 
1002 		} catch (CannotProceed e) {
1003 			java.lang.Object[] args = new java.lang.Object[] { "CannotProceed",
1004 					serviceDescriptor.getCorbaServiceName(), ncRef,
1005 					e.getMessage() };
1006 
1007 			LOG.error("CRB000134_Exception_retrieving_corba_object_reference",
1008 					args, e);
1009 			throw new Jbi4CorbaException(
1010 					"CRB000134_Exception_retrieving_corba_object_reference",
1011 					args, e);
1012 
1013 		} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
1014 			java.lang.Object[] args = new java.lang.Object[] { "InvalidName",
1015 					serviceDescriptor.getCorbaServiceName(), ncRef,
1016 					e.getMessage() };
1017 
1018 			LOG.error("CRB000134_Exception_retrieving_corba_object_reference",
1019 					args, e);
1020 			throw new Jbi4CorbaException(
1021 					"CRB000134_Exception_retrieving_corba_object_reference",
1022 					args, e);
1023 		}
1024 
1025 		LOG.debug("retrievedObject=" + retrievedObject);
1026 		return retrievedObject;
1027 	}
1028 
1029         
1030 	/**
1031 	 * Gets the object that represent the method 'narrow' of the helper class.
1032 	 * 
1033 	 * @param helperClass
1034 	 *            The helper class.
1035 	 * 
1036 	 * @return The narrow method
1037 	 * @throws Jbi4CorbaException
1038 	 *             The Jbi4Corba Exception
1039 	 */
1040 	protected Method getNarrowMethod(Class helperClass)
1041 			throws Jbi4CorbaException {
1042 
1043 		Method _narrowMethod;
1044 		try {
1045 
1046 			_narrowMethod = helperClass
1047 					.getDeclaredMethod("narrow", Object.class);
1048 
1049 		} catch (SecurityException e) {
1050 			java.lang.Object[] args = new java.lang.Object[] {
1051 					"SecurityException", helperClass, e.getMessage() };
1052 
1053 			LOG.error("CRB000133_Exception_retrieving_narrow_method", args, e);
1054 			throw new Jbi4CorbaException(
1055 					"CRB000133_Exception_retrieving_narrow_method", args, e);
1056 		} catch (NoSuchMethodException e) {
1057 			java.lang.Object[] args = new java.lang.Object[] {
1058 					"NoSuchMethodException", helperClass, e.getMessage() };
1059 
1060 			LOG.error("CRB000133_Exception_retrieving_narrow_method", args, e);
1061 			throw new Jbi4CorbaException(
1062 					"CRB000133_Exception_retrieving_narrow_method", args, e);
1063 		}
1064 
1065 		return _narrowMethod;
1066 	}
1067 
1068 	/**
1069 	 * 
1070 	 * @param helperClass
1071 	 *            The helper class
1072 	 * @return The return
1073 	 * @throws Jbi4CorbaException
1074 	 *             The Jbi4Corba exception
1075 	 */
1076 	protected Method getExtractMethod(Class helperClass)
1077 			throws Jbi4CorbaException {
1078 
1079 		Method extractMethod = null;
1080 
1081 		try {
1082 
1083 			extractMethod = helperClass.getDeclaredMethod("extract", Any.class);
1084 
1085 		} catch (SecurityException e) {
1086 			java.lang.Object[] args = new java.lang.Object[] {
1087 					"SecurityException", helperClass, e.getMessage() };
1088 			LOG.error("CRB000133_Exception_retrieving_extractMethod", args, e);
1089 			throw new Jbi4CorbaException(
1090 					"CRB000133_Exception_retrieving_extractMethod", args, e);
1091 		} catch (NoSuchMethodException e) {
1092 			java.lang.Object[] args = new java.lang.Object[] {
1093 					"NoSuchMethodException", helperClass, e.getMessage() };
1094 			LOG.error("CRB000133_Exception_retrieving_extractMethod", args, e);
1095 			throw new Jbi4CorbaException(
1096 					"CRB000133_Exception_retrieving_extractMethod", args, e);
1097 		}
1098 
1099 		return extractMethod;
1100 	}
1101 
1102 	/**
1103 	 * 
1104 	 * @param helperClass
1105 	 *            The helper class
1106 	 * @return The return
1107 	 * @throws Jbi4CorbaException
1108 	 *             The Jbi4Corba exception
1109 	 */
1110 	protected Method getTypeMethod(Class helperClass) throws Jbi4CorbaException {
1111 		Method typeMethod = null;
1112 
1113 		try {
1114 
1115 			typeMethod = helperClass.getDeclaredMethod("type");
1116 
1117 		} catch (SecurityException e) {
1118 			java.lang.Object[] args = new java.lang.Object[] {
1119 					"SecurityException", helperClass, e.getMessage() };
1120 			LOG.error("CRB000133_Exception_retrieving_typeMethod", args, e);
1121 			throw new Jbi4CorbaException(
1122 					"CRB000133_Exception_retrieving_typeMethod", args, e);
1123 		} catch (NoSuchMethodException e) {
1124 			java.lang.Object[] args = new java.lang.Object[] {
1125 					"NoSuchMethodException", helperClass, e.getMessage() };
1126 			LOG.error("CRB000133_Exception_retrieving_typeMethod", args, e);
1127 			throw new Jbi4CorbaException(
1128 					"CRB000133_Exception_retrieving_typeMethod", args, e);
1129 		}
1130 
1131 		return typeMethod;
1132 	}
1133 
1134 	/**
1135 	 * 
1136 	 * @param prop
1137 	 *            The prop
1138 	 */
1139 	private void debugProperties(Properties prop) {
1140 		if (!LOG.isDebugEnabled()) {
1141 			// no log
1142 			return;
1143 		}
1144 		// else
1145 		if (prop == null || prop.size() == 0) {
1146 			LOG.debug("No properties found.");
1147 			return;
1148 		}
1149 		// else
1150 		for (java.lang.Object k : prop.keySet()) {
1151 			LOG.debug("Properties[" + k + "]=" + prop.getProperty((String) k));
1152 		}
1153 
1154 	}
1155 
1156 	/**
1157 	 * Unregisters the service.
1158 	 * 
1159 	 * @see it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbEndpoint#unregisterService()
1160 	 * @throws Jbi4EjbException
1161 	 *             if some problem occurs
1162 	 */
1163 	public void unregisterService() throws Jbi4CorbaException {
1164 		// DO nothing
1165 		LOG.info("CRB000150_Service_unregistered", new java.lang.Object[] {
1166 				this.getServiceName(), this.getEndpointName() });
1167 	}
1168 
1169 	/**
1170 	 * Validate the endpoint. (now do noting)
1171 	 * 
1172 	 * @throws Jbi4EjbException
1173 	 *             if some problem occurs
1174 	 */
1175 	public void validate() throws Jbi4CorbaException {
1176 		// Do nothing
1177 	}
1178 
1179 	/**
1180 	 * Return the orb
1181 	 * 
1182 	 */
1183 	public ORB getOrb() {
1184 		return orb;
1185 	}
1186 
1187 }