diff --git a/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/PollTableEntry.java b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/PollTableEntry.java index 90dd5917f6..8953838fa1 100644 --- a/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/PollTableEntry.java +++ b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/PollTableEntry.java @@ -31,6 +31,7 @@ import org.apache.synapse.commons.crypto.CryptoUtil; import org.apache.synapse.commons.vfs.VFSConstants; import org.apache.synapse.commons.vfs.VFSUtils; +import org.apache.synapse.transport.vfs.VFSTransportErrorHandler.LogType; import java.net.UnknownHostException; import java.text.DateFormat; @@ -157,7 +158,9 @@ public String getFileURI() { try { return VFSUtils.resolveUriHost(fileURI); } catch (UnknownHostException | FileSystemException e) { - log.warn("Unable to resolve the hostname of transport.vfs.FileURI : " + VFSUtils.maskURLPassword(fileURI), e); + String message = "Unable to resolve the hostname of transport.vfs.FileURI : " + + VFSUtils.maskURLPassword(fileURI); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e); } } return fileURI; @@ -168,8 +171,9 @@ public String getReplyFileURI() { try { return VFSUtils.resolveUriHost(replyFileURI); } catch (UnknownHostException | FileSystemException e) { - log.warn("Unable to resolve the hostname of transport.vfs.ReplyFileURI : " + - VFSUtils.maskURLPassword(replyFileURI), e); + String message = "Unable to resolve the hostname of transport.vfs.ReplyFileURI : " + + VFSUtils.maskURLPassword(replyFileURI); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e); } } return replyFileURI; @@ -204,8 +208,9 @@ public String getMoveAfterProcess() { try { return VFSUtils.resolveUriHost(moveAfterProcess); } catch (UnknownHostException | FileSystemException e) { - log.warn("Unable to resolve the hostname of transport.vfs.MoveAfterProcess: " + - VFSUtils.maskURLPassword(moveAfterProcess), e); + String message = "Unable to resolve the hostname of transport.vfs.MoveAfterProcess: " + + VFSUtils.maskURLPassword(moveAfterProcess); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e); } } return moveAfterProcess; @@ -216,8 +221,9 @@ public String getMoveAfterMoveFailure() { try { return VFSUtils.resolveUriHost(moveAfterMoveFailure); } catch (UnknownHostException | FileSystemException e) { - log.warn("Unable to resolve the hostname of transport.vfs.MoveAfterFailedMove: " + - VFSUtils.maskURLPassword(moveAfterMoveFailure), e); + String message = "Unable to resolve the hostname of transport.vfs.MoveAfterFailedMove: " + + VFSUtils.maskURLPassword(moveAfterMoveFailure); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e); } } return moveAfterMoveFailure; @@ -264,8 +270,9 @@ public String getMoveAfterErrors() { try { return VFSUtils.resolveUriHost(moveAfterErrors); } catch (UnknownHostException | FileSystemException e) { - log.warn("Unable to resolve the hostname of transport.vfs.MoveAfterErrors: " + - VFSUtils.maskURLPassword(moveAfterErrors), e); + String message = "Unable to resolve the hostname of transport.vfs.MoveAfterErrors: " + + VFSUtils.maskURLPassword(moveAfterErrors); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } } return moveAfterErrors; @@ -286,8 +293,9 @@ public String getMoveAfterFailure() { try { return VFSUtils.resolveUriHost(moveAfterFailure); } catch (UnknownHostException | FileSystemException e) { - log.warn("Unable to resolve the hostname of transport.vfs.MoveAfterFailure: " + - VFSUtils.maskURLPassword(moveAfterFailure), e); + String message = "Unable to resolve the hostname of transport.vfs.MoveAfterFailure: " + + VFSUtils.maskURLPassword(moveAfterFailure); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e); } } return moveAfterFailure; @@ -501,7 +509,6 @@ public void setSubfolderTimestamp(String subfolderTimestamp) { @Override public boolean loadConfiguration(ParameterInclude params) throws AxisFault { - decryptParamsIfRequired(params); this.params = params; resolveHostsDynamically = ParamUtils.getOptionalParamBoolean(params, @@ -517,7 +524,8 @@ public boolean loadConfiguration(ParameterInclude params) throws AxisFault { protected boolean loadConfigurationsFromService(ParameterInclude params) throws AxisFault { fileURI = ParamUtils.getOptionalParam(params, VFSConstants.TRANSPORT_FILE_FILE_URI); if (fileURI == null) { - log.warn("transport.vfs.FileURI parameter is missing in the proxy service configuration"); + VFSTransportErrorHandler.logException(log, LogType.WARN, + "transport.vfs.FileURI parameter is missing in the proxy service configuration"); return false; } else { @@ -632,8 +640,9 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws fileSizeLimit = strFileSizeLimit != null ? Double.parseDouble(strFileSizeLimit) : VFSConstants.DEFAULT_TRANSPORT_FILE_SIZE_LIMIT; } catch (Exception e) { - log.warn("Error parsing specified file size limit - " + strFileSizeLimit + - ", using default - unlimited"); + String message = "Error parsing specified file size limit - " + strFileSizeLimit + + ", using default - unlimited"; + VFSTransportErrorHandler.logException(log, LogType.WARN, message); } moveAfterMoveFailure = ParamUtils.getOptionalParam(params, @@ -674,8 +683,9 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws try { fileProcessingInterval = Integer.parseInt(strFileProcessingInterval); } catch (NumberFormatException nfe) { - log.warn("VFS File Processing Interval not set correctly. Current value is : " - + strFileProcessingInterval, nfe); + String message = "VFS File Processing Interval not set correctly. Current value is : " + + strFileProcessingInterval; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, nfe); } } @@ -685,8 +695,9 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws try { fileProcessingCount = Integer.parseInt(strFileProcessingCount); } catch (NumberFormatException nfe) { - log.warn("VFS File Processing Count not set correctly. Current value is : " - + strFileProcessingCount, nfe); + String message = "VFS File Processing Count not set correctly. Current value is : " + + strFileProcessingCount; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, nfe); } } @@ -700,8 +711,8 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws autoLockRelease = Boolean.parseBoolean(strAutoLock); } catch (Exception e) { autoLockRelease = false; - log.warn("VFS Auto lock removal not set properly. Current value is : " - + strAutoLock, e); + String message = "VFS Auto lock removal not set properly. Current value is : " + strAutoLock; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } if (autoLockRelease) { String strAutoLockInterval = ParamUtils.getOptionalParam(params, @@ -712,9 +723,9 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws autoLockReleaseInterval = Long.parseLong(strAutoLockInterval); } catch (Exception e) { autoLockReleaseInterval = null; - log.warn( - "VFS Auto lock removal property not set properly. Current value is : " - + strAutoLockInterval, e); + String message = "VFS Auto lock removal property not set properly. Current value is : " + + strAutoLockInterval; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } } String strAutoLockReleaseSameNode = ParamUtils.getOptionalParam(params, @@ -726,9 +737,9 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws .parseBoolean(strAutoLockReleaseSameNode); } catch (Exception e) { autoLockReleaseSameNode = true; - log.warn( - "VFS Auto lock removal property not set properly. Current value is : " - + autoLockReleaseSameNode, e); + String message = "VFS Auto lock removal property not set properly. Current value is : " + + autoLockReleaseSameNode; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } } } @@ -744,7 +755,8 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws distributedLock = Boolean.parseBoolean(strDistributedLock); } catch (Exception e) { autoLockRelease = false; - log.warn("VFS Distributed lock not set properly. Current value is : " + strDistributedLock, e); + String message = "VFS Distributed lock not set properly. Current value is : " + strDistributedLock; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } if (distributedLock) { @@ -756,9 +768,9 @@ protected boolean loadConfigurationsFromService(ParameterInclude params) throws distributedLockTimeout = Long.parseLong(strDistributedLockTimeout); } catch (Exception e) { distributedLockTimeout = null; - log.warn( - "VFS Distributed lock timeout property not set properly. Current value is : " - + strDistributedLockTimeout, e); + String message = "VFS Distributed lock timeout property not set properly. Current value is : " + + strDistributedLockTimeout; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } } } @@ -799,15 +811,13 @@ private String resolveHostAtDeployment(String uri) throws AxisFault { String errorMsg = "Unable to decode the malformed URI : " + VFSUtils.maskURLPassword(uri); //log the error since if we only throw AxisFault, we won't get the entire stacktrace in logs to // identify root cause to users - log.error(errorMsg, e); - throw new AxisFault(errorMsg, e); + VFSTransportErrorHandler.handleException(log, errorMsg, e); } catch (UnknownHostException e) { String errorMsg = "Error occurred while resolving hostname of URI : " + VFSUtils.maskURLPassword(uri); //log the error since if we only throw AxisFault, we won't get the entire stacktrace in logs to // identify root cause to users - log.error(errorMsg, e); - throw new AxisFault(errorMsg, e); + VFSTransportErrorHandler.handleException(log, errorMsg, e); } } return uri; @@ -848,7 +858,7 @@ private String decryptIfRequired(String parameter) throws AxisFault { cryptoUtil = new CryptoUtil(secureVaultProperties); } if (!cryptoUtil.isInitialized()) { - throw new AxisFault("Error initialising cryptoutil"); + VFSTransportErrorHandler.handleException(log, "Error initialising cryptoutil"); } String toDecrypt = m.group(1); toDecrypt = new String(cryptoUtil.decrypt(toDecrypt.getBytes())); diff --git a/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportErrorHandler.java b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportErrorHandler.java new file mode 100644 index 0000000000..7efc5ebfb8 --- /dev/null +++ b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportErrorHandler.java @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2023, WSO2 LLC (http://www.wso2.com). + * + * WSO2 LLC licenses this file to you 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 org.apache.synapse.transport.vfs; + +import org.apache.axis2.AxisFault; +import org.apache.commons.logging.Log; + +/** + * This class is used to handle errors in VFS transport + *

We have not modified the original implementation, but we have included the error handling scenarios + * from {@link VFSTransportListener}, {@link VFSTransportSender} and {@link PollTableEntry} in this class.

+ */ +public class VFSTransportErrorHandler { + + /** + * This method is used to log exceptions with exception + * @param log Log + * @param type {@link LogType} + * @param message String message to be logged + * @param e Exception + */ + public static void logException(Log log, LogType type, String message, Exception e) { + switch (type) { + case INFO: + log.info(message, e); + break; + case DEBUG: + log.debug(message, e); + break; + case WARN: + log.warn(message, e); + break; + case ERROR: + log.error(message, e); + break; + case FATAL: + log.fatal(message, e); + break; + } + } + + /** + * This method is used to log exceptions with exception + * @param log Log + * @param type {@link LogType} + * @param message String message to be logged + * @param configName String name of the configuration + * @param e Exception + */ + public static void logException(Log log, LogType type, String message, String configName, Exception e) { + message = constructLogMessage(message, configName); + logException(log, type, message, e); + } + + /** + * This method is used to log exceptions without exception + * @param log Log + * @param type {@link LogType} + * @param message String message to be logged + */ + public static void logException(Log log, LogType type, String message) { + switch (type) { + case INFO: + log.info(message); + break; + case DEBUG: + log.debug(message); + break; + case WARN: + log.warn(message); + break; + case ERROR: + log.error(message); + break; + case FATAL: + log.fatal(message); + break; + } + } + + /** + * This method is used to log exceptions without exception + * @param log Log + * @param type {@link LogType} + * @param message String message to be logged + * @param configName String name of the configuration + */ + public static void logException(Log log, LogType type, String message, String configName) { + message = constructLogMessage(message, configName); + logException(log, type, message); + } + + /** + * This method is used to handle exceptions. Log error message and throws an AxisFault with the exception + * @param log Log + * @param message String message to be logged + * @param e Exception + * @throws AxisFault + */ + public static void handleException(Log log, String message, Exception e) throws AxisFault { + logException(log, LogType.ERROR, message, e); + throw new AxisFault(message, e); + } + + /** + * This method is used to handle exceptions. Log error message and throws an AxisFault with the exception + * @param log Log + * @param message String message to be logged + * @param configName String name of the configuration + * @param e Exception + * @throws AxisFault + */ + public static void handleException(Log log, String message, String configName, Exception e) throws AxisFault { + logException(log, LogType.ERROR, message, configName, e); + throw new AxisFault(message, e); + } + + /** + * This method is used to handle exceptions. Log error message and throws an AxisFault + * @param log Log + * @param message String message to be logged + * @throws AxisFault + */ + public static void handleException(Log log, String message) throws AxisFault { + logException(log, LogType.ERROR, message); + throw new AxisFault(message); + } + + /** + * This method is used to handle exceptions. Log error message and throws an AxisFault + * @param log Log + * @param message String message to be logged + * @param configName String name of the configuration + * @throws AxisFault + */ + public static void handleException(Log log, String message, String configName) throws AxisFault { + logException(log, LogType.ERROR, message, configName); + throw new AxisFault(message); + } + + /** + * This method is used to handle print the stack trace + * @param e InterruptedException + */ + public static void printStackTrace(Exception e) { + e.printStackTrace(); + } + + /** + * This method is used to throw a Runtime exception + * @param e Exception + */ + public static void throwException(RuntimeException e) { + throw e; + } + + /** + * This enum is used to define the log type + */ + public enum LogType { + INFO, + DEBUG, + WARN, + ERROR, + FATAL + } + + /** + * This method is used to construct the log message + * @param message String message to be logged + * @param configName String name of the configuration + * @return String constructed log message + */ + public static String constructLogMessage(String message, String configName) { + if (null == configName || configName.trim().isEmpty()) { + return message; + } + return "[Service: ".concat(configName).concat("] - ").concat(message); + } +} diff --git a/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportListener.java b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportListener.java index 073fa959fe..74c9b2cc27 100644 --- a/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportListener.java +++ b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportListener.java @@ -59,6 +59,7 @@ import org.wso2.securevault.SecretResolver; import org.wso2.securevault.SecureVaultException; import org.wso2.securevault.commons.MiscellaneousUtil; +import org.apache.synapse.transport.vfs.VFSTransportErrorHandler.LogType; import java.io.File; import java.io.IOException; @@ -177,7 +178,8 @@ protected void doInit() throws AxisFault { } } } catch (FileSystemException e) { - handleException("Error initializing the file transport : " + e.getMessage(), e); + String message = "Error initializing the file transport : " + e.getMessage(); + VFSTransportErrorHandler.handleException(log, message, e); } } @@ -194,6 +196,7 @@ protected void poll(PollTableEntry entry) { * @param fileURI the file or directory to be scanned */ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { + String serviceName = entry.getServiceName(); if (log.isDebugEnabled()) { log.debug("Polling: " + VFSUtils.maskURLPassword(fileURI)); } @@ -222,7 +225,8 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { try { fso = VFSUtils.attachFileSystemOptions(entry.getVfsSchemeProperties(), getFsManager()); } catch (Exception e) { - log.error("Error while attaching VFS file system properties. " + e.getMessage()); + VFSTransportErrorHandler.logException(log, LogType.ERROR, + "Error while attaching VFS file system properties. ", serviceName, e); } FileObject fileObject = null; @@ -261,13 +265,15 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { VFSUtils.maskURLPassword(fileURI), e, entry); return; } else { - log.warn("Failed to resolve the file URI: " + + String message = "Failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI) + ", in attempt " + retryCount + ", " + e.getMessage() + " Retrying in " + reconnectionTimeout + - " milliseconds."); + " milliseconds."; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, serviceName); } } catch (Exception e) { - log.warn("Runtime error may have occurred. ", e); + VFSTransportErrorHandler.logException(log, LogType.WARN, "Runtime error may have occurred. ", + serviceName, e); closeCachedFileSystem(fileURI, fso); } @@ -276,7 +282,8 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { Thread.sleep(reconnectionTimeout); } catch (InterruptedException e2) { Thread.currentThread().interrupt(); - log.error("Thread was interrupted while waiting to reconnect.", e2); + VFSTransportErrorHandler.logException(log, LogType.ERROR, + "Thread was interrupted while waiting to reconnect.", serviceName, e2); } } } @@ -292,7 +299,7 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { } catch (FileNotFolderException ignored) { } catch (FileSystemException ex) { closeFileSystem(fileObject); - log.error(ex.getMessage(), ex); + VFSTransportErrorHandler.logException(log, LogType.ERROR, ex.getMessage(), serviceName, ex); } // if this is a file that would translate to a single message @@ -322,13 +329,15 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { } catch (AxisFault e) { if (e.getCause() instanceof FileNotFoundException) { - log.warn("Error processing File URI : " + - VFSUtils.maskURLPassword(fileObject.getName().toString()) + - ". This can be due to file moved from another process."); + String message = "Error processing File URI : " + + VFSUtils.maskURLPassword(fileObject.getName().toString()) + + ". This can be due to file moved from another process."; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, serviceName); runPostProcess = false; } else { - logException("Error processing File URI : " + - VFSUtils.maskURLPassword(fileObject.getName().getURI()), e); + String message = "Error processing File URI : " + + VFSUtils.maskURLPassword(fileObject.getName().getURI()); + VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName, e); entry.setLastPollState(PollTableEntry.FAILED); metrics.incrementFaultsReceiving(); } @@ -338,9 +347,11 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) { try { moveOrDeleteAfterProcessing(entry, fileObject, fso); } catch (AxisFault axisFault) { - logException( - "File object '" + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + - "' " + "cloud not be moved", axisFault); + String message = "File object '" + + VFSUtils.maskURLPassword(fileObject.getURL().toString()) + + "' " + "cloud not be moved"; + VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName, + axisFault); entry.setLastPollState(PollTableEntry.FAILED); String timeStamp = VFSUtils.getSystemTime(entry.getFailedRecordTimestampFormat()); addFailedRecord(entry, fileObject, timeStamp); @@ -663,6 +674,7 @@ protected boolean acquireLock(FileSystemManager fsManager, FileObject fileObject protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObject fileObject, FileSystemOptions fso) throws AxisFault { + String serviceName = entry.getServiceName(); String moveToDirectoryURI = null; try { switch (entry.getLastPollState()) { @@ -712,7 +724,8 @@ protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObjec destinationFSO = VFSUtils.attachFileSystemOptions( VFSUtils.parseSchemeFileOptions(moveToDirectoryURI, entry.getParams()), getFsManager()); } catch (Exception e) { - log.warn("Unable to set options for processed file location ", e); + VFSTransportErrorHandler.logException(log, LogType.WARN, + "Unable to set options for processed file location ", serviceName, e); } FileObject moveToDirectory = getFsManager().resolveFile(moveToDirectoryURI, destinationFSO); String prefix; @@ -738,8 +751,9 @@ protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObjec fileObject.moveTo(dest); } catch (FileSystemException e) { closeFileSystem(fileObject); - handleException("Error moving file : " + VFSUtils.maskURLPassword(fileObject.toString()) + " to " + - VFSUtils.maskURLPassword(moveToDirectoryURI), e); + String message = "Error moving file : " + VFSUtils.maskURLPassword(fileObject.toString()) + " to " + + VFSUtils.maskURLPassword(moveToDirectoryURI); + VFSTransportErrorHandler.handleException(log, message, serviceName, e); }finally{ try { fileObject.close(); @@ -757,13 +771,13 @@ protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObjec if (!fileObject.delete()) { String msg = "Cannot delete file : " + VFSUtils.maskURLPassword(fileObject.toString()); - log.error(msg); - throw new AxisFault(msg); + VFSTransportErrorHandler.handleException(log, msg, serviceName); } } catch (FileSystemException e) { closeFileSystem(fileObject); - log.error("Error deleting file : " - + VFSUtils.maskURLPassword(fileObject.toString()), e); + String msg = "Error deleting file : " + + VFSUtils.maskURLPassword(fileObject.toString()); + VFSTransportErrorHandler.handleException(log, msg, serviceName, e); } } @@ -976,6 +990,7 @@ private Properties generateSecureVaultProperties(TransportInDescription inDescri protected synchronized void addFailedRecord(PollTableEntry pollTableEntry, FileObject failedObject, String timeString) { + String serviceName = pollTableEntry.getServiceName(); try { String record = failedObject.getName().getBaseName() + VFSConstants.FAILED_RECORD_DELIMITER + timeString; @@ -998,7 +1013,8 @@ protected synchronized void addFailedRecord(PollTableEntry pollTableEntry, FileUtils.writeLines(failedRecordFile, content); } } catch (IOException e) { - log.fatal("Failure while writing the failed records!", e); + VFSTransportErrorHandler.logException(log, LogType.FATAL, + "Failure while writing the failed records!", serviceName, e); } } @@ -1061,6 +1077,7 @@ public void run() { boolean isDeletionSucceed = false; int nextRetryDuration = pollTableEntry.getNextRetryDuration(); int count = 0; + String serviceName = pollTableEntry.getServiceName(); while (!isDeletionSucceed) { try { reTryFailedMove(pollTableEntry, failedFileObject, fileSystemOptions); @@ -1069,13 +1086,15 @@ public void run() { } catch (AxisFault axisFault) { removeTaskState = STATE_RUNNING; try { - log.error("Remove attempt '" + (count++) + "' failed for the file '" - + VFSUtils.maskURLPassword(failedFileObject.getURL().toString()) - + "', next re-try will be " +"after '" - + nextRetryDuration + "' milliseconds"); + String message = "Remove attempt '" + (count++) + "' failed for the file '" + + VFSUtils.maskURLPassword(failedFileObject.getURL().toString()) + + "', next re-try will be " +"after '" + + nextRetryDuration + "' milliseconds"; + VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName); } catch (FileSystemException e) { - log.error("Error while retrying the file url of the file object '" + - VFSUtils.maskURLPassword(failedFileObject.toString()) + "'"); + String message = "Error while retrying the file url of the file object '" + + VFSUtils.maskURLPassword(failedFileObject.toString()) + "'"; + VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName); } try { Thread.sleep(nextRetryDuration); @@ -1088,6 +1107,7 @@ public void run() { private synchronized void reTryFailedMove(PollTableEntry entry, FileObject fileObject, FileSystemOptions fso) throws AxisFault { + String serviceName = entry.getServiceName(); try { String moveToDirectoryURI = entry.getMoveAfterMoveFailure(); @@ -1111,14 +1131,16 @@ private synchronized void reTryFailedMove(PollTableEntry entry, FileObject fileO fileObject.moveTo(dest); // FIXME - when an exception occurs here it causes the in folder to vanish } catch (FileSystemException e) { closeFileSystem(fileObject); - handleException("Error moving the failed file : " - + VFSUtils.maskURLPassword(fileObject.toString()) + " to " + moveToDirectoryURI, e); + String message = "Error moving the failed file : " + + VFSUtils.maskURLPassword(fileObject.toString()) + " to " + moveToDirectoryURI; + VFSTransportErrorHandler.handleException(log, message, serviceName, e); } } catch (FileSystemException e) { - handleException("Cloud not move the failed file object '" - + VFSUtils.maskURLPassword(fileObject.toString()) + "'", e); + String message = "Cloud not move the failed file object '" + + VFSUtils.maskURLPassword(fileObject.toString()) + "'"; + VFSTransportErrorHandler.handleException(log, message, serviceName, e); } catch (IOException e) { - handleException("Cloud not create the folder", e); + VFSTransportErrorHandler.handleException(log, "Cloud not create the folder", serviceName, e); } } } @@ -1147,7 +1169,8 @@ public int compare(FileObject o1, FileObject o2) { lDiff = o1.getContent().getLastModifiedTime() - o2.getContent().getLastModifiedTime(); } catch (FileSystemException e) { - log.warn("Unable to compare lastmodified timestamp of the two files.", e); + String message = "Unable to compare lastmodified timestamp of the two files."; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } return lDiff.intValue(); } @@ -1159,7 +1182,7 @@ public int compare(FileObject o1, FileObject o2) { try { lDiff = o1.getContent().getSize() - o2.getContent().getSize(); } catch (FileSystemException e) { - log.warn("Unable to compare size of the two files.", e); + VFSTransportErrorHandler.logException(log, LogType.WARN, "Unable to compare size of the two files.", e); } return lDiff.intValue(); } @@ -1178,7 +1201,8 @@ public int compare(FileObject o1, FileObject o2) { lDiff = o2.getContent().getLastModifiedTime() - o1.getContent().getLastModifiedTime(); } catch (FileSystemException e) { - log.warn("Unable to compare lastmodified timestamp of the two files.", e); + VFSTransportErrorHandler.logException(log, LogType.WARN, + "Unable to compare lastmodified timestamp of the two files.", e); } return lDiff.intValue(); } @@ -1190,7 +1214,7 @@ public int compare(FileObject o1, FileObject o2) { try { lDiff = o2.getContent().getSize() - o1.getContent().getSize(); } catch (FileSystemException e) { - log.warn("Unable to compare size of the two files.", e); + VFSTransportErrorHandler.logException(log, LogType.WARN, "Unable to compare size of the two files.", e); } return lDiff.intValue(); } diff --git a/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportSender.java b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportSender.java index ce78b23bd0..960404a4f4 100644 --- a/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportSender.java +++ b/modules/transports/core/vfs/src/main/java/org/apache/synapse/transport/vfs/VFSTransportSender.java @@ -45,6 +45,7 @@ import org.apache.synapse.commons.vfs.VFSOutTransportInfo; import org.apache.synapse.commons.vfs.VFSParamDTO; import org.apache.synapse.commons.vfs.VFSUtils; +import org.apache.synapse.transport.vfs.VFSTransportErrorHandler.LogType; import java.io.IOException; import java.util.concurrent.ConcurrentHashMap; @@ -117,8 +118,9 @@ public void init(ConfigurationContext cfgCtx, TransportOutDescription transportO autoLockRelease = Boolean.parseBoolean(strAutoLock.getValue().toString()); } catch (Exception e) { autoLockRelease = false; - log.warn("VFS Auto lock removal not set properly. Given value is : " - + strAutoLock + ", defaults to - " + autoLockRelease, e); + String message = "VFS Auto lock removal not set properly. Given value is : " + + strAutoLock + ", defaults to - " + autoLockRelease; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } if (autoLockRelease) { Parameter strAutoLockInterval = transportOut @@ -129,9 +131,9 @@ public void init(ConfigurationContext cfgCtx, TransportOutDescription transportO autoLockReleaseInterval = Long.parseLong(strAutoLockInterval.getValue().toString()); } catch (Exception e) { autoLockReleaseInterval = null; - log.warn( - "VFS Auto lock release interval is not set properly. Given value is : " - + strAutoLockInterval + ", defaults to - null", e); + String message = "VFS Auto lock release interval is not set properly. Given value is : " + + strAutoLockInterval + ", defaults to - null"; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } } Parameter strAutoLockReleaseSameNode = transportOut @@ -143,9 +145,9 @@ public void init(ConfigurationContext cfgCtx, TransportOutDescription transportO .parseBoolean(strAutoLockReleaseSameNode.getValue().toString()); } catch (Exception e) { autoLockReleaseSameNode = true; - log.warn( - "VFS Auto lock removal same node property not set properly. Given value is : " - + autoLockReleaseSameNode + ", defaults to - " + autoLockReleaseSameNode, e); + String message = "VFS Auto lock removal same node property not set properly. Given value is : " + + autoLockReleaseSameNode + ", defaults to - " + autoLockReleaseSameNode; + VFSTransportErrorHandler.logException(log, LogType.WARN, message, e); } } } @@ -156,7 +158,8 @@ public void init(ConfigurationContext cfgCtx, TransportOutDescription transportO vfsParamDTO.setAutoLockReleaseInterval(autoLockReleaseInterval); vfsParamDTO.setAutoLockReleaseSameNode(autoLockReleaseSameNode); } catch (FileSystemException e) { - handleException("Error initializing the file transport : " + e.getMessage(), e); + String message = "Error initializing the file transport : " + e.getMessage(); + VFSTransportErrorHandler.handleException(log, message, e); } } @@ -209,12 +212,13 @@ public void sendMessage(MessageContext msgCtx, String targetAddress, } protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) throws AxisFault { - + String configName = (String) msgCtx.getProperty("_INTERNAL_TRIGGER_NAME"); FileSystemOptions fso = null; try { fso = VFSUtils.attachFileSystemOptions(vfsOutInfo.getOutFileSystemOptionsMap(), getFsManager()); } catch (Exception e) { - log.error("Error while attaching VFS file system properties. " + e.getMessage()); + VFSTransportErrorHandler.logException(log, LogType.ERROR, + "Error while attaching VFS file system properties. " + e.getMessage(), configName); } if (vfsOutInfo != null) { @@ -234,7 +238,7 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) retryCount++; replyFile = getFsManager().resolveFile(vfsOutInfo.getOutFileURI(), fso); if (replyFile == null) { - log.error("replyFile is null"); + VFSTransportErrorHandler.logException(log, LogType.ERROR, "replyFile is null", configName); throw new FileSystemException("replyFile is null"); } // Retry if actual filesystem is corrupted, Otherwise first file after connection reset @@ -243,15 +247,16 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) wasError = false; } catch (FileSystemException e) { - log.error("cannot resolve replyFile", e); + VFSTransportErrorHandler.logException(log, LogType.ERROR, + "cannot resolve replyFile", configName, e); if (replyFile != null) { - closeFileSystem(replyFile); + closeFileSystem(replyFile, configName); } else { - closeCachedFileSystem(vfsOutInfo, fso); + closeCachedFileSystem(vfsOutInfo, fso, configName); } if(maxRetryCount <= retryCount) { - handleException("cannot resolve replyFile repeatedly: " - + e.getMessage(), e); + VFSTransportErrorHandler.handleException(log, "cannot resolve replyFile repeatedly: " + + e.getMessage(), configName, e); } } @@ -259,7 +264,7 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) try { Thread.sleep(reconnectionTimeout); } catch (InterruptedException e2) { - e2.printStackTrace(); + VFSTransportErrorHandler.printStackTrace(e2); } } } @@ -290,7 +295,7 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) // if file locking is not disabled acquire the lock // before uploading the file if (vfsOutInfo.isFileLockingEnabled()) { - acquireLockForSending(responseFile, vfsOutInfo, fso); + acquireLockForSending(responseFile, vfsOutInfo, fso, configName); populateResponseFile(responseFile, msgCtx,append, true, updateLastModified, fso); VFSUtils.releaseLock(getFsManager(), responseFile, fso); } else { @@ -302,7 +307,7 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) // if file locking is not disabled acquire the lock // before uploading the file if (vfsOutInfo.isFileLockingEnabled()) { - acquireLockForSending(replyFile, vfsOutInfo, fso); + acquireLockForSending(replyFile, vfsOutInfo, fso, configName); populateResponseFile(replyFile, msgCtx, append, true, updateLastModified, fso); VFSUtils.releaseLock(getFsManager(), replyFile, fso); } else { @@ -310,13 +315,14 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) } } else { - handleException("Unsupported reply file type : " + replyFile.getType() + - " for file : " + VFSUtils.maskURLPassword(vfsOutInfo.getOutFileURI())); + String message = "Unsupported reply file type : " + replyFile.getType() + + " for file : " + VFSUtils.maskURLPassword(vfsOutInfo.getOutFileURI()); + VFSTransportErrorHandler.handleException(log, message, configName); } } else { // if file locking is not disabled acquire the lock before uploading the file if (vfsOutInfo.isFileLockingEnabled()) { - acquireLockForSending(replyFile, vfsOutInfo, fso); + acquireLockForSending(replyFile, vfsOutInfo, fso, configName); populateResponseFile(replyFile, msgCtx, append, true, updateLastModified, fso); VFSUtils.releaseLock(getFsManager(), replyFile, fso); } else { @@ -325,12 +331,13 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) } } catch (FileSystemException e) { if (replyFile != null) { - closeFileSystem(replyFile); + closeFileSystem(replyFile, configName); } else { - closeCachedFileSystem(vfsOutInfo, fso); + closeCachedFileSystem(vfsOutInfo, fso, configName); } - handleException("Error resolving reply file : " + - VFSUtils.maskURLPassword(vfsOutInfo.getOutFileURI()), e); + String message = "Error resolving reply file : " + + VFSUtils.maskURLPassword(vfsOutInfo.getOutFileURI()); + VFSTransportErrorHandler.handleException(log, message, configName, e); } finally { if (replyFile != null) { try { @@ -343,23 +350,28 @@ protected void writeFile(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) getFsManager().getFilesCache().clear(fileSystem); replyFile.close(); } catch (Exception ex) { - log.warn("Error when closing the reply file", ex); + VFSTransportErrorHandler.logException(log, LogType.WARN, + "Error when closing the reply file", configName, ex); } } } } else { - handleException("Unable to determine out transport information to send message"); + VFSTransportErrorHandler.handleException(log, + "Unable to determine out transport information to send message", configName); } } private MessageFormatter getMessageFormatter(MessageContext msgContext){ + String configName = (String) msgContext.getProperty("_INTERNAL_TRIGGER_NAME"); try { return MessageProcessorSelector.getMessageFormatter(msgContext); } catch (AxisFault axisFault) { - throw new BaseTransportException("Unable to get the message formatter to use"); + String message = "Unable to get the message formatter to use"; + VFSTransportErrorHandler.throwException( + new BaseTransportException(VFSTransportErrorHandler.constructLogMessage(message, configName))); } - + return null; } protected void populateResponseFile(FileObject responseFile, MessageContext msgContext, @@ -367,7 +379,8 @@ protected void populateResponseFile(FileObject responseFile, MessageContext msgC FileSystemOptions fso) throws AxisFault { MessageFormatter messageFormatter = getMessageFormatter(msgContext); OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext); - + String configName = (String) msgContext.getProperty("_INTERNAL_TRIGGER_NAME"); + try { CountingOutputStream os = new CountingOutputStream( responseFile.getContent().getOutputStream(append)); @@ -387,7 +400,8 @@ protected void populateResponseFile(FileObject responseFile, MessageContext msgC } responseFile.getContent().setLastModifiedTime(lastModified); } catch (Exception e) { - log.warn("Could not set last modified.", e); + VFSTransportErrorHandler.logException(log, LogType.WARN, "Could not set last modified.", + configName, e); } } @@ -401,26 +415,29 @@ protected void populateResponseFile(FileObject responseFile, MessageContext msgC } metrics.incrementFaultsSending(); String responseFileURI = responseFile.getName().getURI(); - closeFileSystem(responseFile); - handleException("IO Error while creating response file : " + VFSUtils.maskURLPassword(responseFileURI), e); + closeFileSystem(responseFile, configName); + String message = "IO Error while creating response file : " + VFSUtils.maskURLPassword(responseFileURI); + VFSTransportErrorHandler.handleException(log, message, configName, e); } } - protected void acquireLockForSending(FileObject responseFile, VFSOutTransportInfo vfsOutInfo, FileSystemOptions fso) + protected void acquireLockForSending(FileObject responseFile, VFSOutTransportInfo vfsOutInfo, + FileSystemOptions fso, String configName) throws AxisFault { int tryNum = 0; // wait till we get the lock while (!VFSUtils.acquireLock(getFsManager(), responseFile, fso, false)) { if (vfsOutInfo.getMaxRetryCount() == tryNum++) { - handleException("Couldn't send the message to file : " + String message = "Couldn't send the message to file : " + VFSUtils.maskURLPassword(responseFile.getName().getURI()) + ", unable to acquire the " + - "lock even after " + tryNum + " retries"); + "lock even after " + tryNum + " retries"; + VFSTransportErrorHandler.handleException(log, message, configName); } else { - - log.warn("Couldn't get the lock for the file : " + String message = "Couldn't get the lock for the file : " + VFSUtils.maskURLPassword(responseFile.getName().getURI()) + ", retry : " + tryNum - + " scheduled after : " + vfsOutInfo.getReconnectTimeout()); + + " scheduled after : " + vfsOutInfo.getReconnectTimeout(); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, configName); try { Thread.sleep(vfsOutInfo.getReconnectTimeout()); } catch (InterruptedException ignore) {} @@ -458,7 +475,7 @@ private void setOutOnlyMep(MessageContext msgCtx) { } } - private void closeFileSystem(FileObject fileObject) { + private void closeFileSystem(FileObject fileObject, String configName) { try { //Close the File system if it is not already closed if (fileObject != null && getFsManager() != null && fileObject.getParent() != null && fileObject.getParent().getFileSystem() != null) { @@ -466,15 +483,16 @@ private void closeFileSystem(FileObject fileObject) { } fileObject.close(); } catch (FileSystemException warn) { - log.warn("Error on closing the file: " + fileObject.getName().getPath(), warn); + String message = "Error on closing the file: " + fileObject.getName().getPath(); + VFSTransportErrorHandler.logException(log, LogType.WARN, message, configName, warn); } } - private void closeCachedFileSystem(VFSOutTransportInfo vfsOutInfo, FileSystemOptions fso) { + private void closeCachedFileSystem(VFSOutTransportInfo vfsOutInfo, FileSystemOptions fso, String configName) { try { ((DefaultFileSystemManager) getFsManager()).closeCachedFileSystem(vfsOutInfo.getOutFileURI(), fso); } catch (Exception e1) { - log.debug("Unable to clear file system", e1); + VFSTransportErrorHandler.logException(log, LogType.DEBUG, "Unable to clear file system", configName, e1); } }