Java tutorial
/* * Copyright 2016 Adaptris Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.adaptris.hpcc; import static org.apache.commons.lang.StringUtils.isBlank; import java.io.File; import org.apache.commons.exec.CommandLine; import org.apache.commons.io.FileUtils; import org.hibernate.validator.constraints.NotBlank; import com.adaptris.annotation.AdapterComponent; import com.adaptris.annotation.AdvancedConfig; import com.adaptris.annotation.ComponentProfile; import com.adaptris.annotation.DisplayOrder; import com.adaptris.annotation.InputFieldHint; import com.adaptris.core.AdaptrisMessage; import com.adaptris.core.ProduceDestination; import com.adaptris.core.ProduceException; import com.adaptris.core.util.Args; import com.adaptris.core.util.ExceptionHelper; import com.thoughtworks.xstream.annotations.XStreamAlias; /** * Spray the contents of a directory to Thor. * * <p> * Note that this producer <strong>ignores</strong> the current message contents and just sprays the contents of the directory * specified by {@link #getSourceDirectoryKey()} using the configured dfuplus command. * </p> * <p> * Effectively, the program executed is going to similar to * <pre> * {@code dfuplus action=spray srcfile=/path/to/dir/* dstcluster=mythor dstname=~zzlc:json:data overwrite=1 PREFIX=FILENAME,FILESIZE server= nosplit=1 username= password= } * </pre> * Be aware that nosplit=1 is always added, as well as the "/*". * </p> * <p> * The adapter also needs a running {@code dfuplus action=dafilesrv} instance on the machine where the adapter is hosted. Thor will * connect to this instance for file delivery. * </p> * * @author lchan * @config spray-directory-to-thor * */ @XStreamAlias("spray-directory-to-thor") @AdapterComponent @ComponentProfile(summary = "Spray a directory into HPCC via dfuplus", tag = "producer,hpcc,dfuplus", recommended = { DfuplusConnection.class }) @DisplayOrder(order = { "cluster", "sourceDirectoryKey", "overwrite" }) public class SprayDirectoryToThor extends SprayToThorImpl { private String prefix; @NotBlank @InputFieldHint(expression = true) private String sourceDirectory; @Deprecated private String sourceDirectoryKey; @AdvancedConfig private Boolean deleteSourceDirectory; @Override public void produce(AdaptrisMessage msg, ProduceDestination destination) throws ProduceException { int exit = 0; // Create DFU command // dfuplus action=spray srcfile=/var/lib/HPCCSystems/mydropzone/historical-weather/adapter-agility-historic-out/* // dstcluster=mythor dstname=zzlc::json::historical_weather_04 overwrite=1 PREFIX=FILENAME,FILESIZE // server= nosplit=1 username= password= try { CommandLine commandLine = createSprayCommand(msg); commandLine.addArgument(String.format("srcfile=%s", getSource(msg))); commandLine.addArgument(String.format("dstname=%s", destination.getDestination(msg))); if (!isBlank(getPrefix())) { commandLine.addArgument(String.format("PREFIX=%s", getPrefix())); } commandLine.addArgument("nosplit=1"); log.trace("Executing {}", commandLine); execute(commandLine); postSprayCleanup(msg); } catch (Exception e) { throw ExceptionHelper.wrapProduceException(e); } } /** * @return the prefix */ public String getPrefix() { return prefix; } /** * @param s the prefix to set */ public void setPrefix(String s) { this.prefix = s; } /** * @return the sourceDirectoryKey * @deprecated since 3.6.6 use {@link #getSourceDirectory()} instead. */ @Deprecated public String getSourceDirectoryKey() { return sourceDirectoryKey; } /** * Set the metadata containing the source directory to upload. * * @param s the sourceDirectoryKey to set * @deprecated since 3.6.6 use {@link #setSourceDirectory(String)} instead. */ @Deprecated public void setSourceDirectoryKey(String s) { this.sourceDirectoryKey = s; } private String getSource(AdaptrisMessage msg) throws Exception { String result = ""; result = String.format("%1$s%2$s*", getSourceDir(msg), File.separator); log.trace("Source Files [{}]", result); return result; } private File getSourceDir(AdaptrisMessage msg) throws Exception { File dir = null; if (!isBlank(getSourceDirectoryKey())) { log.warn("source-directory-key is deprecated; use source-directory instead"); dir = new File(msg.getMetadataValue(getSourceDirectoryKey())); } else { dir = new File(msg.resolve(getSourceDirectory())); } return dir; } private void postSprayCleanup(AdaptrisMessage msg) throws Exception { if (deleteSourceDirectory()) { File f = getSourceDir(msg); log.trace("Deleting [{}]", f); FileUtils.deleteQuietly(f); } } /** * @return the deleteSourceDirectory */ public Boolean getDeleteSourceDirectory() { return deleteSourceDirectory; } /** * Whether or not to delete the source directory after uploading (if there are no errors). * * @param b true to delete the source directory after processing; default if not configured is false. */ public void setDeleteSourceDirectory(Boolean b) { this.deleteSourceDirectory = b; } private boolean deleteSourceDirectory() { return getDeleteSourceDirectory() != null ? getDeleteSourceDirectory().booleanValue() : false; } public String getSourceDirectory() { return sourceDirectory; } /** * Set the source directory. * * @param dir the source directory. */ public void setSourceDirectory(String dir) { this.sourceDirectory = Args.notBlank(dir, "sourceDirectory"); } }