-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonPrint.java
More file actions
159 lines (136 loc) · 5.3 KB
/
JsonPrint.java
File metadata and controls
159 lines (136 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.io.*;
import java.util.*;
import org.apache.logging.log4j.Logger;
import com.latencybusters.lbm.UMMonDB;
import com.latencybusters.lbm.UMSMonProtos.UMSMonMsg;
import com.latencybusters.lbm.UMPMonProtos.UMPMonMsg;
import com.latencybusters.lbm.DROMonProtos.DROMonMsg;
import com.latencybusters.lbm.SRSMonProtos.SRSMonMsg;
import com.google.protobuf.Message;
import com.google.protobuf.util.*;
/*
Copyright (c) 2023-2023 Informatica Corporation
Permission is granted to licensees to use or alter this software for any
purpose, including commercial applications, according to the terms laid
out in the Software License Agreement.
This source code example is provided by Informatica for educational
and evaluation purposes only.
THE SOFTWARE IS PROVIDED "AS IS" AND INFORMATICA DISCLAIMS ALL WARRANTIES
EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF
NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE. INFORMATICA DOES NOT WARRANT THAT USE OF THE SOFTWARE WILL BE
UNINTERRUPTED OR ERROR-FREE. INFORMATICA SHALL NOT, UNDER ANY CIRCUMSTANCES,
BE LIABLE TO LICENSEE FOR LOST PROFITS, CONSEQUENTIAL, INCIDENTAL, SPECIAL OR
INDIRECT DAMAGES ARISING OUT OF OR RELATED TO THIS AGREEMENT OR THE
TRANSACTIONS CONTEMPLATED HEREUNDER, EVEN IF INFORMATICA HAS BEEN APPRISED OF
THE LIKELIHOOD OF SUCH DAMAGES.
All of the documentation and software included in this and any
other Informatica Inc. Ultra Messaging Releases
Copyright (C) Informatica Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted only as covered by the terms of a
valid software license agreement with Informatica Inc.
*/
public class JsonPrint implements UMMonDB
{
// Make these static because only one instance is needed and
// we need the shutdown hook to be able to get to them.
private Properties _properties = null;
private Logger _logger = null;
private String _outFilePath = null;
private BufferedWriter _outFileWriter = null;
private Thread _cleanup = null;
public JsonPrint() {
// Just make sure we don't get multiple instances.
if (_outFileWriter != null) {
try {
System.err.println("JsonPrint: Double create?");
} catch (Exception ignore) { }
}
}
public void setProperties(Properties properties) {
_properties = properties;
}
public void setLogger(Logger logger) {
_logger = logger;
}
public void connect() throws IOException {
// Open output file, default to "-" which means standard out.
_outFilePath = _properties.getProperty("outFilePath", "-");
try {
if (_outFilePath.compareTo("-") == 0) {
_outFileWriter = new BufferedWriter(new OutputStreamWriter(System.out));
} else {
_outFileWriter = new BufferedWriter(new FileWriter(_outFilePath));
}
} catch (Exception e) {
_logger.error("MCS-999000-1: jsonprint: Error opening " + _outFilePath + ": " + e.getMessage());
_outFileWriter = null;
throw new IOException("Error opening " + _outFilePath + ": " + e.getMessage());
}
// Set up shutdown hook to clean up.
try {
_cleanup = new Thread(() -> {
try {
_outFileWriter.flush();
_outFileWriter.close();
_outFileWriter = null;
} catch (Exception e) { System.out.println("Exception: " + e.getMessage()); }
} );
Runtime.getRuntime().addShutdownHook(_cleanup);
} catch (Exception e) {
_logger.error("MCS-999000-3: jsonprint: Error adding shutdown hook: " + e.getMessage());
throw new IOException("Error adding shutdown hook: " + e.getMessage());
}
_logger.info("MCS-999000-4: jsonprint started, outFilePath=" + _outFilePath);
}
public void write(UMSMonMsg umsMonMsg) throws IOException {
// Library statistics (context, transport)
printMsg(umsMonMsg);
}
public void write(DROMonMsg droMonMsg) throws IOException {
// DRO daemon statistics.
printMsg(droMonMsg);
}
public void write(UMPMonMsg umpMonMsg) throws IOException {
// Store daemon statistics.
printMsg(umpMonMsg);
}
public void write(SRSMonMsg srsMonMsg) throws IOException {
// SRS daemon statistics.
printMsg(srsMonMsg);
}
private void printMsg(Message msg) throws IOException {
String json = null;
try {
json = JsonFormat.printer().includingDefaultValueFields().print(msg);
} catch (Exception e) {
_logger.error("MCS-999000-5: jsonprint: Error formatting json: " + e.getMessage());
throw new IOException("Error formatting json: " + e.getMessage());
}
if (_outFileWriter != null) {
try {
_outFileWriter.write(json);
} catch (Exception e) {
_logger.error("MCS-999000-6: jsonprint: Error writing to " + _outFilePath + ": " + e.getMessage());
_outFileWriter = null;
throw new IOException("Error writing to " + _outFilePath + ": " + e.getMessage());
}
}
}
public void disconnect() throws IOException {
if (_outFileWriter != null) {
try {
_outFileWriter.flush();
_outFileWriter.close();
_outFileWriter = null;
} catch (Exception e) {
_logger.error("MCS-999000-7: jsonprint: Error closing " + _outFilePath + ": " + e.getMessage());
_outFileWriter = null;
throw new IOException("Error closing " + _outFilePath + ": " + e.getMessage());
}
} else {
_logger.error("MCS-999001-9: Double disconnect?");
}
}
}