PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib-imola/cics-bc/jbi4cics/src/main/java/it/imolinfo/jbi4cics/jbi/Jbi4cicsEndpoint.java

https://bitbucket.org/openesb/openesb-components
Java | 501 lines | 249 code | 61 blank | 191 comment | 30 complexity | e19c11dfc4c1177795204d5a79cc32ae MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. * Copyright (c) 2005, 2006 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.jbi4cics.jbi;
  9. import it.imolinfo.jbi4cics.Logger;
  10. import it.imolinfo.jbi4cics.LoggerFactory;
  11. import it.imolinfo.jbi4cics.commareaparser.CommareaLexer;
  12. import it.imolinfo.jbi4cics.commareaparser.CommareaParser;
  13. import it.imolinfo.jbi4cics.exception.FormatException;
  14. import it.imolinfo.jbi4cics.exception.Jbi4cicsException;
  15. import it.imolinfo.jbi4cics.exception.ParseException;
  16. import it.imolinfo.jbi4cics.messageformat.commarea.CommareaBeanMappingDescriptor;
  17. import it.imolinfo.jbi4cics.webservices.descriptor.ServiceDescriptor;
  18. import it.imolinfo.jbi4cics.webservices.runtime.ServiceCreator;
  19. import it.imolinfo.jbi4cics.webservices.utils.generators.ServiceBeanGenerator;
  20. import it.imolinfo.jbi4cics.webservices.utils.generators.ServiceInterfaceGenerator;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.io.StringReader;
  26. import java.util.Collection;
  27. import javax.wsdl.Definition;
  28. import javax.wsdl.Port;
  29. import javax.wsdl.WSDLException;
  30. import javax.wsdl.factory.WSDLFactory;
  31. import javax.xml.namespace.QName;
  32. import javax.xml.parsers.DocumentBuilderFactory;
  33. import javax.xml.parsers.ParserConfigurationException;
  34. import javax.xml.transform.Transformer;
  35. import javax.xml.transform.TransformerException;
  36. import javax.xml.transform.TransformerFactory;
  37. import javax.xml.transform.dom.DOMSource;
  38. import javax.xml.transform.stream.StreamResult;
  39. import org.apache.servicemix.common.ExchangeProcessor;
  40. import org.apache.servicemix.common.endpoints.ProviderEndpoint;
  41. import org.codehaus.xfire.XFire;
  42. import org.codehaus.xfire.service.Service;
  43. import org.w3c.dom.Document;
  44. import org.xml.sax.SAXException;
  45. import antlr.ANTLRException;
  46. public final class Jbi4cicsEndpoint extends ProviderEndpoint {
  47. /**
  48. * The logger for this class and its instances.
  49. */
  50. private static final Logger LOG
  51. = LoggerFactory.getLogger(Jbi4cicsEndpoint.class);
  52. /**
  53. * The responsible to translate localized messages.
  54. */
  55. private static final Messages MESSAGES
  56. = Messages.getMessages(Jbi4cicsEndpoint.class);
  57. /**
  58. * The code page.
  59. */
  60. private String codePage;
  61. /**
  62. * The input copy Cobol file name.
  63. */
  64. private String copyCobolFileName;
  65. /**
  66. * The output copy Cobol file name.
  67. */
  68. private String outputCopyCobolFileName;
  69. /**
  70. * The input copy Cobol, used also for output if {@link #outputCopyCobol} is
  71. * <code>null</code> or empty.
  72. */
  73. private String copyCobol;
  74. /**
  75. * The output copy Cobol.
  76. */
  77. private String outputCopyCobol;
  78. /**
  79. * The service descriptor.
  80. */
  81. private ServiceDescriptor serviceDescriptor;
  82. /**
  83. * The XFire service.
  84. */
  85. private Service xfireService;
  86. private ExchangeProcessor processor = new Jbi4cicsExchangeProcessor(this);
  87. /**
  88. * Initializes a new instance of this class.
  89. */
  90. public Jbi4cicsEndpoint() {
  91. }
  92. /**
  93. * Returns the xfire <code>Service</code>.
  94. *
  95. * @return the xfire <code>Service</code>.
  96. */
  97. public Service getXFireService() {
  98. return xfireService;
  99. }
  100. /**
  101. * Starts the <code>ExchangeProcessor</code>.
  102. *
  103. * @throws Exception if caused by the call to <code>super.start()</code>
  104. * or the exchange processor startup.
  105. */
  106. @Override
  107. public void start() throws Exception { // Overridden
  108. super.start();
  109. processor.start();
  110. }
  111. /**
  112. * Stops the <code>ExchangeProcessor</code>.
  113. *
  114. * @throws Exception if caused by the call to <code>super.stop()</code>
  115. * or the exchange processor stop.
  116. */
  117. @Override
  118. public void stop() throws Exception { // Overridden
  119. super.stop();
  120. processor.stop();
  121. }
  122. /**
  123. * Registers the service.
  124. */
  125. void registerService() throws Jbi4cicsException, IOException,
  126. ANTLRException, SAXException, ParserConfigurationException,
  127. WSDLException, TransformerException {
  128. XFire xfire = getXFire();
  129. createServiceBeansAndInterface();
  130. if (definition != null) {
  131. Port port;
  132. // Gets service/endpoint from the WSDL definition
  133. LOG.debug("Using WSDL definition to load endpoint data");
  134. // Gets the endpoint
  135. service = new QName(serviceDescriptor.getServiceNameSpace(),
  136. serviceDescriptor.getServiceName());
  137. port = initEndpointIfIsNull(definition.getService(service));
  138. checkEndpointIsNotNull();
  139. // XXX What if 'port' is null? Look at initEndpointIfIsNull()...
  140. interfaceName = port.getBinding().getPortType().getQName();
  141. if (LOG.isDebugEnabled()) {
  142. LOG.debug("Loaded endpoint data, service: " + service
  143. + ", endpoint: " + endpoint + ", interface:"
  144. + interfaceName);
  145. }
  146. }
  147. // Create and register the xfire service
  148. xfireService = new ServiceCreator().createJbiService(
  149. serviceDescriptor, xfire, interfaceName);
  150. xfire.getServiceRegistry().register(xfireService);
  151. if (definition == null) {
  152. QName serviceQName;
  153. QName interfaceQName;
  154. Definition def;
  155. javax.wsdl.Service wsdlService;
  156. // Check service name, endpoint name ad description from xfire
  157. description = generateWsdl();
  158. serviceQName = xfireService.getName();
  159. interfaceQName = xfireService.getServiceInfo().getPortType();
  160. if (service == null) {
  161. service = serviceQName;
  162. } else if (!service.equals(serviceQName)) {
  163. LOG.warn("CIC001018_Service_name_not_matched", serviceQName,
  164. service);
  165. }
  166. if (interfaceName == null) {
  167. interfaceName = interfaceQName;
  168. } else if (!interfaceName.equals(interfaceQName)) {
  169. LOG.warn("CIC001019_Interface_name_not_matched", interfaceQName,
  170. interfaceName);
  171. }
  172. def = WSDLFactory.newInstance().newWSDLReader().readWSDL(
  173. null, description);
  174. wsdlService = def.getService(serviceQName);
  175. if (wsdlService != null) {
  176. initEndpointIfIsNull(wsdlService);
  177. }
  178. checkEndpointIsNotNull();
  179. }
  180. writeWsdlToFile(); // Writes out the description on file
  181. }
  182. /**
  183. * Verifies and updates {@link #serviceDescriptor} creating the input bean,
  184. * the output bean and the service interface.
  185. *
  186. * @throws Jbi4cicsException if the service name stored in the service
  187. * descriptor is <code>null</code>.
  188. * @throws ANTLRException if an error occurs while parsing the
  189. * copy/copies Cobol.
  190. */
  191. private void createServiceBeansAndInterface()
  192. throws Jbi4cicsException, ANTLRException {
  193. CommareaBeanMappingDescriptor descriptor;
  194. BCELClassLoader classLoader = ((Jbi4cicsLifeCycle)
  195. serviceUnit.getComponent().getLifeCycle()).getBCELClassLoader();
  196. ServiceBeanGenerator beanGenerator;
  197. ServiceInterfaceGenerator interfGenerator;
  198. // Set the defaults
  199. if (serviceDescriptor.getServiceName() == null) {
  200. throw new Jbi4cicsException(
  201. "CIC001013_Service_name_not_initialized");
  202. }
  203. serviceDescriptor.setDefaultValuesIfNecessary();
  204. // Generate the mapping descriptor from copy/copies Cobol. If only
  205. // 'copyCobol' is found, the same CommareaBeanMappingDescriptor is used
  206. // for both the input and the output step
  207. descriptor = parseCommarea(copyCobol);
  208. serviceDescriptor.setInputMappingDescriptor(descriptor);
  209. if ((outputCopyCobol != null) && (outputCopyCobol.length() > 0)
  210. && !outputCopyCobol.equals(copyCobol)) {
  211. descriptor = parseCommarea(outputCopyCobol);
  212. }
  213. serviceDescriptor.setOutputMappingDescriptor(descriptor);
  214. // Creates the service inputBean
  215. beanGenerator = new ServiceBeanGenerator(serviceDescriptor, true);
  216. beanGenerator.generateBeanClass(classLoader);
  217. // Creates the service outputBean
  218. beanGenerator = new ServiceBeanGenerator(serviceDescriptor, false);
  219. beanGenerator.generateBeanClass(classLoader);
  220. // Creates the service interface
  221. interfGenerator = new ServiceInterfaceGenerator(serviceDescriptor);
  222. interfGenerator.generateServiceInterface(classLoader);
  223. // FIXME questa gestione del codepage non e' elegante
  224. serviceDescriptor.setCodePage(codePage);
  225. }
  226. /**
  227. * Parses the specified commarea.
  228. *
  229. * @param cpyCobol the copy Cobol describing the commarea to
  230. * parse. Must be not <code>null</code>.
  231. * @return the descriptor related to the received commarea.
  232. * @throws ANTLRException if thrown by the ANTLR lexer or parser.
  233. * @throws FormatException if a field contained in the commarea has a
  234. * wrong format.
  235. * @throws ParseException if a field type is not supported or contains
  236. * erroneous value(s).
  237. */
  238. CommareaBeanMappingDescriptor parseCommarea(final String cpyCobol)
  239. throws ANTLRException, FormatException, ParseException {
  240. CommareaLexer lexer = new CommareaLexer(new StringReader(copyCobol));
  241. CommareaParser parser = new CommareaParser(lexer);
  242. CommareaBeanMappingDescriptor desc = parser.commarea_definition();
  243. LOG.info("CIC001014_Commarea_parsed", getEndpoint(), desc);
  244. return desc;
  245. }
  246. /**
  247. * Tries to set the <code>enpoint</code> field value if is currently
  248. * <code>null</code>, getting its value from the specified WSDL service.
  249. *
  250. * @param wsdlService the WSDL service to get the endpoint name. Must be
  251. * not <code>null</code>.
  252. * @return the port used to obtain the endpoint name, even if the endpoint
  253. * was also initialized and has not been modified. May be
  254. * <code>null</code>, in which case the <code>endpoint</code> field
  255. * has been left unchanged.
  256. */
  257. private Port initEndpointIfIsNull(javax.wsdl.Service wsdlService) {
  258. Collection ports = wsdlService.getPorts().values();
  259. Port port = null;
  260. if (ports.size() == 1) {
  261. // Check if this is the same as defined in endpoint spec
  262. port = (Port) ports.iterator().next();
  263. if (endpoint == null) {
  264. endpoint = port.getName();
  265. } else if (!endpoint.equals(port.getName())) {
  266. LOG.warn("CIC001016_Endpoint_name_not_matched", port.getName(),
  267. endpoint);
  268. }
  269. }
  270. return port;
  271. }
  272. /**
  273. * Verifies that the current endpoint value is not <code>null</code>.
  274. *
  275. * @throws IllegalArgumentException if the stored endpoint is
  276. * <code>null</code>.
  277. */
  278. private void checkEndpointIsNotNull() {
  279. if (endpoint == null) {
  280. throw new IllegalArgumentException(MESSAGES.getString(
  281. "CIC001017_Endpoint_name_not_provided"));
  282. }
  283. }
  284. private Document generateWsdl() throws SAXException, IOException,
  285. ParserConfigurationException {
  286. ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
  287. DocumentBuilderFactory factory =
  288. DocumentBuilderFactory.newInstance();
  289. getXFire().generateWSDL(xfireService.getSimpleName(), baos);
  290. if (LOG.isDebugEnabled()) {
  291. LOG.debug(baos.toString());
  292. }
  293. factory.setNamespaceAware(true);
  294. return factory.newDocumentBuilder().parse(
  295. new ByteArrayInputStream(baos.toByteArray()));
  296. }
  297. /**
  298. * Write the generated WSDL to a file.
  299. *
  300. * @throws IOException if the named file exists but is a
  301. * directory rather than a regular
  302. * file, does not exist but cannot be
  303. * created, or cannot be opened for
  304. * any other reason.
  305. * @throws TransformerException if an unrecoverable error occurs
  306. * during the course of the XML
  307. * transformation.
  308. * @throws SAXException if any parse errors occur.
  309. * @throws ParserConfigurationException if a <code>javax.xml.parsers.DocumentBuilder</code>
  310. * cannot be created which satisfies
  311. * the configuration requested.
  312. */
  313. private void writeWsdlToFile() throws IOException, TransformerException,
  314. SAXException, ParserConfigurationException {
  315. String wsdlFileName = getServiceUnit().getRootPath() + "/"
  316. + xfireService.getSimpleName() + ".wsdl";
  317. FileWriter file;
  318. LOG.info("CIC001020_Producing_wsdl", wsdlFileName);
  319. file = new FileWriter(wsdlFileName);
  320. try {
  321. TransformerFactory factory = TransformerFactory.newInstance();
  322. Transformer transormer = factory.newTransformer();
  323. transormer.transform(new DOMSource(generateWsdl()),
  324. new StreamResult(file));
  325. } finally {
  326. file.close();
  327. }
  328. }
  329. XFire getXFire() {
  330. Jbi4cicsLifeCycle lifeCycle
  331. = (Jbi4cicsLifeCycle) serviceUnit.getComponent().getLifeCycle();
  332. return lifeCycle.getXFire();
  333. }
  334. /**
  335. * Returns the exchange processor.
  336. *
  337. * @return the exchange processor.
  338. */
  339. @Override
  340. public ExchangeProcessor getProcessor() { // Overridden
  341. return processor;
  342. }
  343. /**
  344. * Returns the input copy Cobol.
  345. *
  346. * @return the input copy Cobol.
  347. */
  348. public String getCopyCobol() {
  349. return copyCobol;
  350. }
  351. /**
  352. * Sets the input copy Cobol.
  353. *
  354. * @param copyCobol the input copy Cobol to set.
  355. */
  356. public void setCopyCobol(final String copyCobol) {
  357. this.copyCobol = copyCobol;
  358. }
  359. /**
  360. * Returns the output copy Cobol.
  361. *
  362. * @return the output copy Cobol.
  363. */
  364. public String getOutputCopyCobol() {
  365. return outputCopyCobol;
  366. }
  367. /**
  368. * Sets the output copy Cobol.
  369. *
  370. * @param outputCopyCobol the output copy Cobol to set.
  371. */
  372. public void setOutputCopyCobol(final String outputCopyCobol) {
  373. this.outputCopyCobol = outputCopyCobol;
  374. }
  375. /**
  376. * Returns the service descriptor.
  377. *
  378. * @return the service descriptor.
  379. */
  380. public ServiceDescriptor getServiceDescriptor() {
  381. return serviceDescriptor;
  382. }
  383. /**
  384. * Sets the service descriptor.
  385. *
  386. * @param serviceDescriptor the service descriptor to set.
  387. */
  388. public void setServiceDescriptor(final ServiceDescriptor serviceDescriptor) {
  389. this.serviceDescriptor = serviceDescriptor;
  390. }
  391. /**
  392. * Returns the input copy Cobol file name.
  393. *
  394. * @return the input copy Cobol file name.
  395. */
  396. public String getCopyCobolFileName() {
  397. return copyCobolFileName;
  398. }
  399. /**
  400. * Sets the input copy Cobol file name.
  401. *
  402. * @param copyCobolFileName the input copy Cobol file name to set.
  403. */
  404. public void setCopyCobolFileName(final String copyCobolFileName) {
  405. this.copyCobolFileName = copyCobolFileName;
  406. }
  407. /**
  408. * Returns the output copy Cobol file name.
  409. *
  410. * @return the output copy Cobol file name.
  411. */
  412. public String getOutputCopyCobolFileName() {
  413. return outputCopyCobolFileName;
  414. }
  415. /**
  416. * Sets the output copy Cobol file name.
  417. *
  418. * @param outputCopyCobolFileName the output copy Cobol file name to set.
  419. */
  420. public void setOutputCopyCobolFileName(
  421. final String outputCopyCobolFileName) {
  422. this.outputCopyCobolFileName = outputCopyCobolFileName;
  423. }
  424. /**
  425. * Returns the code page.
  426. *
  427. * @return the code page.
  428. */
  429. public String getCodePage() {
  430. return codePage;
  431. }
  432. /**
  433. * Sets the code page.
  434. *
  435. * @param codePage the code page to set.
  436. */
  437. public void setCodePage(final String codePage) {
  438. this.codePage = codePage;
  439. }
  440. }