Skip to content

Commit

Permalink
Merge pull request #87 from alipay/20230324-template
Browse files Browse the repository at this point in the history
writer buffix
  • Loading branch information
quhongwei authored Mar 25, 2023
2 parents 668f79a + 68094fe commit 8c1a678
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<url>https://github.com/alipay/rdf-file</url>

<properties>
<rdf.file.core.version>2.2.10</rdf.file.core.version>
<rdf.file.oss.version>2.2.10</rdf.file.oss.version>
<rdf.file.sftp.version>2.2.10</rdf.file.sftp.version>
<rdf.file.core.version>2.2.11</rdf.file.core.version>
<rdf.file.oss.version>2.2.11</rdf.file.oss.version>
<rdf.file.sftp.version>2.2.11</rdf.file.sftp.version>
</properties>

<modules>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Ant Group
* Copyright (c) 2004-2023 All Rights Reserved.
*/
package com.alipay.rdf.file.common;

import com.alipay.rdf.file.interfaces.FileFactory;
import com.alipay.rdf.file.interfaces.FileWriter;
import com.alipay.rdf.file.model.FileConfig;
import com.alipay.rdf.file.util.RdfFileUtil;

/**
* @author quhongwei
* @version FileWriterTemplate.java, v 0.1 2023年03月24日 2:07 下午 quhongwei Exp $
*/
public abstract class FileWriterTemplate {
private final FileWriter fileWriter;

public FileWriterTemplate(FileConfig fileConfig) {
this.fileWriter = FileFactory.createWriter(fileConfig);
}

public void process() throws Throwable {
try {
doProcess(fileWriter);
} catch (Throwable t) {
RdfFileUtil.setWriteError(fileWriter);

if (t instanceof RuntimeException) {
throw t;
} else {
throw new RuntimeException("rdf-file#FileWriterTemplate file write error.", t);
}
} finally {
fileWriter.close();
}
}

protected abstract void doProcess(FileWriter fileWriter) throws Throwable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void writeHead(Object headBean) {
} catch (RuntimeException e) {
hasError = true;
throw e;
} catch (Exception e) {
} catch (Throwable e) {
hasError = true;
throw new RdfFileException("rdf-file#FileWriterWrapper writeHead error filePath=["
+ getFileConfig().getFilePath() + "], head=[" + headBean
+ "]",
Expand All @@ -48,7 +49,8 @@ public void writeRow(Object rowBean) {
} catch (RuntimeException e) {
hasError = true;
throw e;
} catch (Exception e) {
} catch (Throwable e) {
hasError = true;
throw new RdfFileException("rdf-file#FileWriterWrapper writeRow error filePath=["
+ getFileConfig().getFilePath() + "], row=[" + rowBean + "]",
e, RdfErrorEnum.UNKOWN);
Expand All @@ -62,7 +64,8 @@ public void writeTail(Object tailBean) {
} catch (RuntimeException e) {
hasError = true;
throw e;
} catch (Exception e) {
} catch (Throwable e) {
hasError = true;
throw new RdfFileException("rdf-file#FileWriterWrapper writeTail error filePath=["
+ getFileConfig().getFilePath() + "], tail=[" + tailBean
+ "]",
Expand All @@ -77,7 +80,8 @@ public void writeLine(String line) {
} catch (RuntimeException e) {
hasError = true;
throw e;
} catch (Exception e) {
} catch (Throwable e) {
hasError = true;
throw new RdfFileException("rdf-file#FileWriterWrapper writeLine error filePath=["
+ getFileConfig().getFilePath() + "], line=[" + line + "]",
e, RdfErrorEnum.UNKOWN);
Expand Down Expand Up @@ -111,7 +115,8 @@ public void append(InputStream in) {
} catch (RuntimeException e) {
hasError = true;
throw e;
} catch (Exception e) {
} catch (Throwable e) {
hasError = true;
throw new RdfFileException("rdf-file#FileWriterWrapper append error filePath=["
+ getFileConfig().getFilePath() + "]",
e, RdfErrorEnum.UNKOWN);
Expand All @@ -125,7 +130,8 @@ public void ensureOpen() {
} catch (RuntimeException e) {
hasError = true;
throw e;
} catch (Exception e) {
} catch (Throwable e) {
hasError = true;
throw new RdfFileException("rdf-file#FileWriterWrapper ensureOpen error filePath=["
+ getFileConfig().getFilePath() + "]",
e, RdfErrorEnum.UNKOWN);
Expand All @@ -137,4 +143,7 @@ public FileConfig getFileConfig() {
return writer.getFileConfig();
}

public void setHasError(boolean hasError) {
this.hasError = hasError;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

/**
* Copyright (C) 2013-2018 Ant Financial Services Group
*
*
* 协议文件合并
*
*
* @author hongwei.quhw
* @version $Id: ProtocolFileMerger.java, v 0.1 2017年8月10日 下午8:01:11 hongwei.quhw Exp $
*/
Expand Down Expand Up @@ -85,6 +85,9 @@ public void merge(MergerConfig config) {
RdfProfiler.enter("rdf-file#merge tail start...");
mergeTail(config);
RdfProfiler.release("rdf-file#merge tail end");
} catch (Throwable e) {
RdfFileUtil.setWriteError(fileWriter);
throw new RuntimeException("rdf-file#ProtocolFileMerger error.", e);
} finally {
if (null != fileWriter) {
fileWriter.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.alipay.rdf.file.util;

import com.alipay.rdf.file.common.FileWriterWrapper;
import com.alipay.rdf.file.exception.RdfErrorEnum;
import com.alipay.rdf.file.exception.RdfFileException;
import com.alipay.rdf.file.interfaces.FileWriter;
import com.alipay.rdf.file.loader.ProtocolLoader;
import com.alipay.rdf.file.loader.TemplateLoader;
import com.alipay.rdf.file.meta.FileMeta;
Expand Down Expand Up @@ -906,4 +908,10 @@ public static boolean isRelationReadRowCompatibility(FileConfig fileConfig) {

return false;
}

public static void setWriteError(FileWriter fileWriter) {
if (fileWriter instanceof FileWriterWrapper) {
((FileWriterWrapper) fileWriter).setHasError(true);
}
}
}
2 changes: 1 addition & 1 deletion rdf-file-test/src/main/resources/pom/rdf-file-core.pom
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alipay.rdf.file</groupId>
<artifactId>rdf-file-core</artifactId>
<version>2.2.10</version>
<version>2.2.11</version>
<name>RDF FILE CORE</name>
<description>RDF FILE CORE</description>
<url>https://github.com/alipay/rdf-file</url>
Expand Down
2 changes: 1 addition & 1 deletion rdf-file-test/src/main/resources/pom/rdf-file-oss.pom
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alipay.rdf.file</groupId>
<artifactId>rdf-file-oss</artifactId>
<version>2.2.10</version>
<version>2.2.11</version>
<name>RDF FILE OSS</name>
<description>RDF FILE OSS</description>
<url>https://github.com/alipay/rdf-file</url>
Expand Down
2 changes: 1 addition & 1 deletion rdf-file-test/src/main/resources/pom/rdf-file-sftp.pom
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alipay.rdf.file</groupId>
<artifactId>rdf-file-sftp</artifactId>
<version>2.2.10</version>
<version>2.2.11</version>
<name>RDF FILE SFTP</name>
<description>RDF FILE SFTP</description>
<url>https://github.com/alipay/rdf-file</url>
Expand Down

0 comments on commit 8c1a678

Please sign in to comment.