PageRenderTime 38ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/informatica-agent/src/main/java/org/bitbucket/bradleysmithllc/etlagent/informatica/handler/ExecuteWorkflowMultipart.java

https://bitbucket.org/bradleysmithllc/etl-agent
Java | 295 lines | 211 code | 50 blank | 34 comment | 31 complexity | 501cf79d07c8ed6f0f160011078d325b MD5 | raw file
  1. package org.bitbucket.bradleysmithllc.etlagent.informatica.handler;
  2. /*
  3. * #%L
  4. * informatica-agent
  5. * %%
  6. * Copyright (C) 2012 - 2014 bradleysmithllc
  7. * %%
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * #L%
  20. */
  21. import com.fasterxml.jackson.databind.JsonNode;
  22. import org.apache.commons.io.FileUtils;
  23. import org.apache.commons.lang3.ObjectUtils;
  24. import org.bitbucket.bradleysmithllc.etlagent.VirtualFiles;
  25. import org.bitbucket.bradleysmithllc.etlagent.dto.GenericResponseDTO;
  26. import org.bitbucket.bradleysmithllc.etlagent.informatica.dto.*;
  27. import org.bitbucket.bradleysmithllc.etlagent.informatica.provider.*;
  28. import org.bitbucket.bradleysmithllc.etlagent.resources.ContextBase;
  29. import org.bitbucket.bradleysmithllc.etlunit.RuntimeSupport;
  30. import org.bitbucket.bradleysmithllc.etlunit.util.VelocityUtil;
  31. import org.bitbucket.bradleysmithllc.webserviceshubclient.parameter.ParameterFile;
  32. import java.io.*;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. public class ExecuteWorkflowMultipart extends AbstractMultipartHandler<ExecuteWorkflowRequestDTO, ExecuteWorkflowResponseDTO> {
  37. Map<String, String> velocityContext = new HashMap<String, String>();
  38. @Override
  39. public String getId() {
  40. return "executeWorkflow";
  41. }
  42. @Override
  43. public ExecuteWorkflowRequestDTO getRequestContainerObject() {
  44. return new ExecuteWorkflowRequestDTO("", "");
  45. }
  46. @Override
  47. public ExecuteWorkflowResponseDTO process(JsonNode request, final VirtualFiles vflRequest, final VirtualFiles vflResponse, final ExecuteWorkflowRequestDTO container) throws Exception {
  48. logger.info("Executing workflow {}", container);
  49. final ExecuteWorkflowResponseDTO executeWorkflowResponseDTO = new ExecuteWorkflowResponseDTO();
  50. executeWorkflowResponseDTO.setInformaticaResultCode(InformaticaResponseDTO.result.okay);
  51. useExecutionService(container, new IntegrationServiceVisitor() {
  52. @Override
  53. public void useClient(InformaticaExecutionClient client, InformaticaDomain informaticaDomain, InformaticaRepository informaticaRepository, InformaticaIntegrationService informaticaIntegrationService, InformaticaWebServicesHub informaticaWebServicesHub, RuntimeSupport runtimeSupport) throws Exception {
  54. // record choices for domain, repository, integration service and web services hub.
  55. String path = "[" + informaticaDomain.getDomainName()
  56. + "].[" + informaticaRepository.getQualifiedName() + "].["
  57. + informaticaIntegrationService.getQualifiedName() + "].["
  58. + (informaticaWebServicesHub == null ? "none" : (informaticaWebServicesHub.getQualifiedName()) + "]"
  59. );
  60. // store the data
  61. ContextBase.testContextMap.put("lastRequestDomain", path);
  62. // prepare the workspace
  63. prepareWorkspace(informaticaDomain, container, vflRequest);
  64. ExecuteDetails ed = executeWorkflowResponseDTO.getExecuteDetails();
  65. ed.setInformaticaIntegrationService(informaticaIntegrationService.getQualifiedName());
  66. ed.setInformaticaRepository(informaticaRepository.getQualifiedName());
  67. ed.setInformaticaDomain(informaticaDomain.getDomainName());
  68. if (informaticaWebServicesHub != null) {
  69. ed.setInformaticaWebServicesHub(informaticaWebServicesHub.getQualifiedName());
  70. }
  71. String tag = container.getFolder() + "." + container.getWorkflowName() + (container.getTask() != null ? ("." + container.getTask()) : "") + (container.getRunInstanceName() != null ? ("." + container.getRunInstanceName()) : "");
  72. ParameterFile pfile = container.getParameterFile();
  73. // this needs to be better - not partitioned well enough
  74. File parmFile = runtimeSupport.createTempFile(tag + "_" + container.getRequestUid() + ".PRM");
  75. try {
  76. PrintWriter fw = new PrintWriter(new BufferedWriter(new FileWriter(parmFile)));
  77. try {
  78. pfile.write(fw);
  79. } finally {
  80. fw.close();
  81. }
  82. // rework velocity references
  83. String parmContents = FileUtils.readFileToString(parmFile);
  84. parmContents = parmContents.replaceAll("#[\\s]*\\{", "\\$\\{");
  85. FileUtils.write(parmFile, VelocityUtil.writeTemplate(parmContents, velocityContext));
  86. // reload to get the updated values
  87. pfile = new ParameterFile(parmFile);
  88. } finally {
  89. parmFile.delete();
  90. }
  91. InformaticaExecutionResult execResult;
  92. if (container.getTask() != null) {
  93. if (container.getRunInstanceName() != null) {
  94. execResult = client.executeWorkflowTask(
  95. container.getWorkflowName(),
  96. container.getRunInstanceName(),
  97. container.getTask(),
  98. container.getFolder(),
  99. pfile,
  100. container.getRequestFiles()
  101. );
  102. } else {
  103. execResult = client.executeWorkflowTask(
  104. container.getWorkflowName(),
  105. container.getTask(),
  106. container.getFolder(),
  107. pfile,
  108. container.getRequestFiles()
  109. );
  110. }
  111. } else {
  112. if (container.getRunInstanceName() != null) {
  113. execResult = client.executeWorkflow(
  114. container.getWorkflowName(),
  115. container.getRunInstanceName(),
  116. container.getFolder(),
  117. pfile,
  118. container.getRequestFiles()
  119. );
  120. } else {
  121. execResult = client.executeWorkflow(
  122. container.getWorkflowName(),
  123. container.getFolder(),
  124. pfile,
  125. container.getRequestFiles()
  126. );
  127. }
  128. }
  129. if (execResult.isServiceCacheFailure()) {
  130. logger.error("Cache failure result.");
  131. executeWorkflowResponseDTO.setResponseMessage(execResult.getFailureExc());
  132. executeWorkflowResponseDTO.setInformaticaResultCode(InformaticaResponseDTO.result.cacheFailure);
  133. logger.error("Cache failure result. {}", executeWorkflowResponseDTO);
  134. } else {
  135. executeWorkflowResponseDTO.getResponseFiles().putAll(execResult.getWorkSpaceFiles());
  136. /**
  137. * This data is going from the local workspace back to the remote client
  138. */
  139. VFSFileDataHandler fileDataHandler = new VFSFileDataHandler(vflResponse, VFSFileDataHandler.direction.persist);
  140. try {
  141. for (Map.Entry<RemoteFile.fileType, List<RemoteFile>> rflist : executeWorkflowResponseDTO.getResponseFiles().entrySet()) {
  142. for (RemoteFile rf : rflist.getValue()) {
  143. // assign the data handler
  144. rf.setFileDataHandler(fileDataHandler);
  145. rf.persist();
  146. }
  147. }
  148. } finally {
  149. fileDataHandler.dispose();
  150. }
  151. if (execResult.getFailureExc() != null) {
  152. executeWorkflowResponseDTO.setInformaticaResultCode(InformaticaResponseDTO.result.failed);
  153. executeWorkflowResponseDTO.setResponseMessage(execResult.getFailureExc());
  154. }
  155. }
  156. final File localizedWorkingRoot = informaticaDomain.getLocalizedWorkingRoot();
  157. // wipe out the temporary workspace
  158. new Thread(new Runnable() {
  159. @Override
  160. public void run() {
  161. FileUtils.deleteQuietly(localizedWorkingRoot);
  162. }
  163. }).start();
  164. }
  165. });
  166. return executeWorkflowResponseDTO;
  167. }
  168. private void prepareWorkspace(InformaticaDomain domain, ExecuteWorkflowRequestDTO container, VirtualFiles vfiles) throws IOException {
  169. velocityContext.clear();
  170. /**
  171. * This data is going from the remote client to the local workspace
  172. */
  173. VFSFileDataHandler fileDataHandler = new VFSFileDataHandler(vfiles, VFSFileDataHandler.direction.extract);
  174. try
  175. {
  176. for (Map.Entry<RemoteFile.fileType, List<RemoteFile>> rflist : container.getRequestFiles().entrySet())
  177. {
  178. for (RemoteFile rf : rflist.getValue())
  179. {
  180. // assign the data handler
  181. rf.setFileDataHandler(fileDataHandler);
  182. rf.extract();
  183. }
  184. }
  185. RemoteFile.copyRemoteToLocal(container.getRequestFiles(), domain);
  186. }
  187. finally
  188. {
  189. fileDataHandler.dispose();
  190. }
  191. for (RemoteFile.fileType type : RemoteFile.fileType.values()) {
  192. File targetFiles = domain.getWorkspaceDir(type);
  193. velocityContext.put(type.name(), targetFiles.getAbsolutePath());
  194. }
  195. }
  196. private static class VFSFileDataHandler implements RemoteFile.FileDataHandler {
  197. enum direction {persist, extract};
  198. VirtualFiles.VWriter writer;
  199. VirtualFiles.VReader reader;
  200. private final direction _direction;
  201. public VFSFileDataHandler(VirtualFiles vfs, direction dir) throws IOException {
  202. _direction = dir;
  203. if (_direction == direction.extract)
  204. {
  205. reader = vfs.getReader();
  206. }
  207. else
  208. {
  209. writer = vfs.getWriter();
  210. }
  211. }
  212. @Override
  213. public void extractFileData(RemoteFile rfile) throws IOException {
  214. if (_direction == direction.persist)
  215. {
  216. throw new IllegalArgumentException("Writing");
  217. }
  218. File file = ContextBase.runtimeSupport.createAnonymousTempFile();
  219. rfile.setFile(file);
  220. reader.extractPath(RemoteFile.getPath(rfile), file);
  221. }
  222. @Override
  223. public void persistFileData(RemoteFile file) throws IOException {
  224. if (_direction == direction.extract)
  225. {
  226. throw new IllegalArgumentException("Reading");
  227. }
  228. writer.writePath(RemoteFile.getPath(file), file.getFile());
  229. }
  230. public void dispose() throws IOException {
  231. if (_direction == direction.persist)
  232. {
  233. writer.dispose();
  234. }
  235. else
  236. {
  237. reader.dispose();
  238. }
  239. }
  240. }
  241. public static String getLastRequestedDomain()
  242. {
  243. return ContextBase.testContextMap.get("lastRequestDomain");
  244. }
  245. }