PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-jt400/src/main/java/org/apache/camel/component/jt400/Jt400PgmProducer.java

https://github.com/gnodet/camel
Java | 229 lines | 171 code | 37 blank | 21 comment | 38 complexity | 8549f97cf1e804fa2f6c0dfb6ad57845 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.jt400;
  18. import java.beans.PropertyVetoException;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import com.ibm.as400.access.AS400;
  22. import com.ibm.as400.access.AS400ByteArray;
  23. import com.ibm.as400.access.AS400DataType;
  24. import com.ibm.as400.access.AS400Message;
  25. import com.ibm.as400.access.AS400Text;
  26. import com.ibm.as400.access.ProgramCall;
  27. import com.ibm.as400.access.ProgramParameter;
  28. import com.ibm.as400.access.ServiceProgramCall;
  29. import org.apache.camel.Exchange;
  30. import org.apache.camel.InvalidPayloadException;
  31. import org.apache.camel.support.DefaultProducer;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. public class Jt400PgmProducer extends DefaultProducer {
  35. private static final Logger LOG = LoggerFactory.getLogger(Jt400PgmProducer.class);
  36. public Jt400PgmProducer(Jt400Endpoint endpoint) {
  37. super(endpoint);
  38. }
  39. private Jt400Endpoint getISeriesEndpoint() {
  40. return (Jt400Endpoint) super.getEndpoint();
  41. }
  42. @Override
  43. public void process(Exchange exchange) throws Exception {
  44. AS400 iSeries = null;
  45. try {
  46. iSeries = connect();
  47. String commandStr = getISeriesEndpoint().getObjectPath();
  48. ProgramParameter[] parameterList = getParameterList(exchange, iSeries);
  49. ProgramCall pgmCall;
  50. if (getISeriesEndpoint().getType() == Jt400Type.PGM) {
  51. pgmCall = new ProgramCall(iSeries);
  52. } else {
  53. pgmCall = new ServiceProgramCall(iSeries);
  54. ((ServiceProgramCall) pgmCall)
  55. .setProcedureName(getISeriesEndpoint().getProcedureName());
  56. ((ServiceProgramCall) pgmCall)
  57. .setReturnValueFormat(ServiceProgramCall.NO_RETURN_VALUE);
  58. }
  59. pgmCall.setProgram(commandStr);
  60. pgmCall.setParameterList(parameterList);
  61. if (LOG.isDebugEnabled()) {
  62. LOG.trace(
  63. "Starting to call PGM '{}' in host '{}' authentication with the user '{}'",
  64. new Object[] { commandStr, iSeries.getSystemName(), iSeries.getUserId() });
  65. }
  66. boolean result = pgmCall.run();
  67. if (LOG.isTraceEnabled()) {
  68. LOG.trace("Executed PGM '{}' in host '{}'. Success? {}", commandStr,
  69. iSeries.getSystemName(), result);
  70. }
  71. if (result) {
  72. handlePGMOutput(exchange, pgmCall, parameterList, iSeries);
  73. } else {
  74. throw new Jt400PgmCallException(getOutputMessages(pgmCall));
  75. }
  76. } catch (Exception e) {
  77. throw new Jt400PgmCallException(e);
  78. } finally {
  79. release(iSeries);
  80. }
  81. }
  82. private ProgramParameter[] getParameterList(Exchange exchange, AS400 iSeries)
  83. throws InvalidPayloadException, PropertyVetoException {
  84. Object body = exchange.getIn().getMandatoryBody();
  85. Object[] params = (Object[]) body;
  86. ProgramParameter[] parameterList = new ProgramParameter[params.length];
  87. for (int i = 0; i < params.length; i++) {
  88. Object param = params[i];
  89. boolean input;
  90. boolean output;
  91. if (getISeriesEndpoint().isFieldIdxForOuput(i)) {
  92. output = true;
  93. input = param != null;
  94. } else {
  95. output = false;
  96. input = true;
  97. }
  98. byte[] inputData = null;
  99. // XXX Actually, returns any field length, not just output.
  100. int length = getISeriesEndpoint().getOutputFieldLength(i);
  101. if (input) {
  102. if (param != null) {
  103. AS400DataType typeConverter;
  104. if (getISeriesEndpoint().getFormat() == Jt400Configuration.Format.binary) {
  105. typeConverter = new AS400ByteArray(length);
  106. } else {
  107. typeConverter = new AS400Text(length, iSeries);
  108. }
  109. inputData = typeConverter.toBytes(param);
  110. }
  111. // Else, inputData will remain null.
  112. }
  113. if (input && output) {
  114. LOG.trace("Parameter {} is both input and output.", i);
  115. if (getISeriesEndpoint().getType() == Jt400Type.PGM) {
  116. parameterList[i] = new ProgramParameter(inputData, length);
  117. } else {
  118. parameterList[i] = new ProgramParameter(ProgramParameter.PASS_BY_REFERENCE, inputData, length);
  119. }
  120. } else if (input) {
  121. LOG.trace("Parameter {} is input.", i);
  122. if (inputData != null) {
  123. parameterList[i] = new ProgramParameter(inputData);
  124. } else {
  125. parameterList[i] = new ProgramParameter();
  126. parameterList[i].setParameterType(ProgramParameter.PASS_BY_REFERENCE);
  127. parameterList[i].setNullParameter(true); // Just for self documentation.
  128. }
  129. } else {
  130. // output
  131. LOG.trace("Parameter {} is output.", i);
  132. parameterList[i] = new ProgramParameter(length);
  133. }
  134. }
  135. return parameterList;
  136. }
  137. private void handlePGMOutput(Exchange exchange, ProgramCall pgmCall, ProgramParameter[] inputs, AS400 iSeries)
  138. throws InvalidPayloadException {
  139. Object body = exchange.getIn().getMandatoryBody();
  140. Object[] params = (Object[]) body;
  141. List<Object> results = new ArrayList<>();
  142. int i = 1;
  143. for (ProgramParameter pgmParam : pgmCall.getParameterList()) {
  144. byte[] output = pgmParam.getOutputData();
  145. Object javaValue = params[i - 1];
  146. if (output != null) {
  147. int length = pgmParam.getOutputDataLength();
  148. AS400DataType typeConverter;
  149. if (getISeriesEndpoint().getFormat() == Jt400Configuration.Format.binary) {
  150. typeConverter = new AS400ByteArray(length);
  151. } else {
  152. typeConverter = new AS400Text(length, iSeries);
  153. }
  154. javaValue = typeConverter.toObject(output);
  155. }
  156. results.add(javaValue);
  157. i++;
  158. }
  159. Object[] bodyOUT = new Object[results.size()];
  160. bodyOUT = results.toArray(bodyOUT);
  161. exchange.getOut().setBody(bodyOUT);
  162. }
  163. private String getOutputMessages(ProgramCall pgmCall) throws Exception {
  164. StringBuilder outputMsg = new StringBuilder();
  165. // Show messages.
  166. AS400Message[] messageList = pgmCall.getMessageList();
  167. for (int i = 0; i < messageList.length; ++i) {
  168. // Load additional message information.
  169. messageList[i].load();
  170. outputMsg.append(i + ") ");
  171. outputMsg.append(messageList[i].getText());
  172. outputMsg.append(" - ");
  173. outputMsg.append(messageList[i].getHelp());
  174. outputMsg.append("\n");
  175. }
  176. return outputMsg.toString();
  177. }
  178. private AS400 connect() throws Exception {
  179. AS400 iSeries = getISeriesEndpoint().getSystem();
  180. if (!iSeries.isConnected(AS400.COMMAND)) {
  181. LOG.debug("Connecting to {}", getISeriesEndpoint());
  182. iSeries.connectService(AS400.COMMAND);
  183. }
  184. return iSeries;
  185. }
  186. private void release(AS400 iSeries) throws Exception {
  187. if (iSeries != null) {
  188. LOG.debug("Releasing connection to {}", getISeriesEndpoint());
  189. getISeriesEndpoint().releaseSystem(iSeries);
  190. }
  191. }
  192. }