1717
1818import com .github .os72 .protocjar .Protoc ;
1919import com .google .protobuf .DescriptorProtos ;
20+ import com .google .protobuf .DescriptorProtos .FileDescriptorProto ;
2021import io .serverlessworkflow .impl .resources .ExternalResourceHandler ;
2122import java .io .FileNotFoundException ;
2223import java .io .IOException ;
2324import java .io .InputStream ;
2425import java .io .UncheckedIOException ;
26+ import java .nio .file .FileVisitResult ;
2527import java .nio .file .Files ;
2628import java .nio .file .Path ;
27- import java .nio .file .StandardCopyOption ;
29+ import java .nio .file .SimpleFileVisitor ;
30+ import java .nio .file .attribute .BasicFileAttributes ;
2831import org .slf4j .Logger ;
2932import org .slf4j .LoggerFactory ;
3033
3134class FileDescriptorReader {
3235
3336 private static final Logger logger = LoggerFactory .getLogger (FileDescriptorReader .class );
3437
35- static FileDescriptorContext readDescriptor (ExternalResourceHandler externalResourceHandler ) {
38+ static FileDescriptorProto readDescriptor (ExternalResourceHandler externalResourceHandler ) {
3639
40+ Path grpcDir = null ;
3741 try (InputStream inputStream = externalResourceHandler .open ()) {
38-
39- Path grpcDir = Files .createTempDirectory ("serverless-workflow-" );
40-
41- Path protoFile = grpcDir .resolve (externalResourceHandler .name ());
42- if (!Files .exists (protoFile )) {
43- Files .createDirectories (protoFile .getParent ());
44- }
45-
46- Files .copy (inputStream , protoFile , StandardCopyOption .REPLACE_EXISTING );
47-
42+ grpcDir = Files .createTempDirectory ("serverless-workflow-" );
43+ Files .createDirectories (grpcDir );
44+ Path protoFile = grpcDir .resolve (Path .of (externalResourceHandler .name ()).getFileName ());
45+ Files .copy (inputStream , protoFile );
4846 Path descriptorOutput = grpcDir .resolve ("descriptor.protobin" );
49-
5047 generateFileDescriptor (grpcDir , protoFile , descriptorOutput );
51-
52- DescriptorProtos .FileDescriptorSet fileDescriptorSet =
53- DescriptorProtos .FileDescriptorSet .newBuilder ()
54- .mergeFrom (Files .readAllBytes (descriptorOutput ))
55- .build ();
56-
57- return new FileDescriptorContext (fileDescriptorSet , externalResourceHandler .name ());
58-
48+ return DescriptorProtos .FileDescriptorSet .newBuilder ()
49+ .mergeFrom (Files .readAllBytes (descriptorOutput ))
50+ .build ()
51+ .getFile (0 );
5952 } catch (IOException e ) {
6053 throw new UncheckedIOException (
61- "Unable to process GRPC descriptor file associated with resource "
54+ "Unable to process GRPC proto file associated with resource "
6255 + externalResourceHandler .name (),
6356 e );
57+ } finally {
58+ if (grpcDir != null ) {
59+ deleteTempFiles (grpcDir );
60+ }
61+ }
62+ }
63+
64+ private static void deleteTempFiles (Path grpcDir ) {
65+ try {
66+ Files .walkFileTree (
67+ grpcDir ,
68+ new SimpleFileVisitor <Path >() {
69+ @ Override
70+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs )
71+ throws IOException {
72+ Files .delete (file );
73+ return FileVisitResult .CONTINUE ;
74+ }
75+
76+ @ Override
77+ public FileVisitResult postVisitDirectory (Path dir , IOException exc )
78+ throws IOException {
79+ if (exc != null ) {
80+ throw exc ;
81+ }
82+ Files .delete (dir );
83+ return FileVisitResult .CONTINUE ;
84+ }
85+ });
86+ } catch (IOException e ) {
87+ logger .warn ("Error deleting GRPC temp dir " + grpcDir , e );
6488 }
6589 }
6690
@@ -69,11 +93,6 @@ static FileDescriptorContext readDescriptor(ExternalResourceHandler externalReso
6993 * the embedded protoc from protoc-jar library. If that fails with FileNotFoundException
7094 * (unsupported architecture), falls back to using the system's installed protoc via
7195 * ProcessBuilder.
72- *
73- * @param grpcDir a temporary directory
74- * @param protoFile the .proto file used by <code>protoc</code> to generate the file descriptor
75- * @param descriptorOutput the output directory where the descriptor file will be generated
76- * @throws IOException
7796 */
7897 private static void generateFileDescriptor (Path grpcDir , Path protoFile , Path descriptorOutput )
7998 throws IOException {
@@ -107,8 +126,6 @@ private static void generateFileDescriptor(Path grpcDir, Path protoFile, Path de
107126
108127 /*
109128 * Fallback method to generate file descriptor using system's installed protoc via ProcessBuilder.
110- *
111- * @param protocArgs the arguments to pass to protoc command
112129 */
113130 private static void generateFileDescriptorWithProcessBuilder (String [] protocArgs )
114131 throws IOException {
0 commit comments