Skip to content

Commit

Permalink
JDK 17 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashah-splunk committed Oct 5, 2023
1 parent 8fb7dda commit 6ca786e
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 148 deletions.
4 changes: 2 additions & 2 deletions java/src/main/java/com/splunk/examples/conf/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static void readStanza(Service service, String confName, String stanzaNa
Command.error("Stanza " + stanzaName + " does not exists in the Conf " + confName);
return;
}
for (Map.Entry entry: stanza.entrySet()) {
for (Map.Entry<String, Object> entry: stanza.entrySet()) {
System.out.println(entry.getKey() + " := " + entry.getValue());
}
}
Expand Down Expand Up @@ -89,7 +89,7 @@ private static void readConf(Service service, String confName){
Command.error("Conf " + confName + " does not exists");
return;
}
System.out.println("Conf " + conf.getName());
System.out.println("\n" + conf.getName());
for(Entity stanza : conf.values()){
System.out.println("[" + stanza.getName() + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public static void main(String[] args) {

EventTypeCollection eventTypes = service.getEventTypes();
for(EventType eventType : eventTypes.values()){
System.out.println(eventType.getName());
for (int i = 0; i < eventType.getName().length(); i++) System.out.print("=");
System.out.println();
System.out.println("\n" + eventType.getName());
for(Map.Entry<String, Object> entry : eventType.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
Expand Down
99 changes: 45 additions & 54 deletions java/src/main/java/com/splunk/examples/export/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@

import com.splunk.*;

import java.io.*;
import java.nio.channels.FileChannel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -206,8 +200,7 @@ static int getJsonEventStart(String str) {
return -1;
}

static int getEventStart(byte[] buffer, String format)
throws Exception {
static int getEventStart(byte[] buffer, String format) {

String str = new String(buffer);
if (format.equals("csv"))
Expand All @@ -218,7 +211,7 @@ else if (format.equals("xml"))
return getJsonEventStart(str);
}

static void cleanupTail(Writer out, String format) throws Exception {
static void cleanupTail(Writer out, String format) throws IOException {
if (format.equals("csv"))
out.write("\n");
else if (format.equals("xml"))
Expand All @@ -227,7 +220,7 @@ else if (format.equals("xml"))
out.write("\n]\n");
}

static void run(String[] argv) throws Exception {
static void run(String[] argv) throws IOException {
Command command = Command.splunk("export");
command.addRule("search", String.class, "Search string to export");

Expand Down Expand Up @@ -277,31 +270,32 @@ else if (command.args[index].equals("json"))
// Chunk backwards through the file until we find valid
// start time. If we can't find one just start over.
final int bufferSize = (64*1024);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
long fptr = Math.max(file.length() - bufferSize, 0);
long fptrEof = 0;

while (fptr > 0) {
byte [] buffer = new byte[bufferSize];
raf.seek(fptr);
raf.read(buffer, 0, bufferSize);
int eventStart = getEventStart(buffer, format);
if (eventStart != -1) {
fptrEof = nextEventOffset + fptr;
break;
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")){
long fptr = Math.max(file.length() - bufferSize, 0);
long fptrEof = 0;

while (fptr > 0) {
byte [] buffer = new byte[bufferSize];
raf.seek(fptr);
raf.read(buffer, 0, bufferSize);
int eventStart = getEventStart(buffer, format);
if (eventStart != -1) {
fptrEof = nextEventOffset + fptr;
break;
}
fptr = fptr - bufferSize;
}
fptr = fptr - bufferSize;
}

if (fptr < 0)
fptrEof = 0; // We didn't find a valid event, so start over.
else {
args.put("latest_time", lastTime);
addEndOfLine = true;
}
if (fptr < 0)
fptrEof = 0; // We didn't find a valid event, so start over.
else {
args.put("latest_time", lastTime);
addEndOfLine = true;
}

FileChannel fc = raf.getChannel();
fc.truncate(fptrEof);
FileChannel fc = raf.getChannel();
fc.truncate(fptrEof);
}
} else
if (!file.createNewFile())
throw new Error("Failed to create output file");
Expand All @@ -323,28 +317,25 @@ else if (command.args[index].equals("json"))
InputStream is = service.export(search, args);

// Use UTF8 sensitive reader/writers
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
FileOutputStream os = new FileOutputStream(file, true);
Writer out = new OutputStreamWriter(os, StandardCharsets.UTF_8);

// Read/write 8k at a time if possible
char [] xferBuffer = new char[8192];
boolean once = true;

// If superfluous meta-data is not needed, or specifically
// wants to be removed, one would clean up the first read
// buffer on a format by format basis,
while (true) {
if (addEndOfLine && once) {
cleanupTail(out, format);
once = false;
try(InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
FileOutputStream os = new FileOutputStream(file, true);
Writer out = new OutputStreamWriter(os, StandardCharsets.UTF_8)){
// Read/write 8k at a time if possible
char [] xferBuffer = new char[8192];
boolean once = true;

// If superfluous meta-data is not needed, or specifically
// wants to be removed, one would clean up the first read
// buffer on a format by format basis,
while (true) {
if (addEndOfLine && once) {
cleanupTail(out, format);
once = false;
}
int bytesRead = isr.read(xferBuffer);
if (bytesRead == -1) break;
out.write(xferBuffer, 0, bytesRead);
}
int bytesRead = isr.read(xferBuffer);
if (bytesRead == -1) break;
out.write(xferBuffer, 0, bytesRead);
}

isr.close();
out.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public static void main(String[] args) {

FiredAlertGroupCollection firedAlertGroups = service.getFiredAlertGroups();
for(FiredAlertGroup firedAlertGroup : firedAlertGroups.values()){
System.out.println(firedAlertGroup.getPath());
for (int i = 0; i < firedAlertGroup.getPath().length(); i++) System.out.print("=");
System.out.println();
System.out.println("\n" + firedAlertGroup.getPath());
for(FiredAlert firedAlert : firedAlertGroup.getAlerts().values()){
System.out.println("FiredAlert Name : " + firedAlert.getName());
System.out.println("FiredAlert Action : " + Arrays.toString(firedAlert.getAction()));
Expand Down
10 changes: 7 additions & 3 deletions java/src/main/java/com/splunk/examples/fluent_pivot/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@

import com.splunk.*;

import java.io.IOException;

public class Program {
public static void main(String[] argv) {
try {
run();
}
catch (Exception e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

static void run() throws Exception {
static void run() throws IOException, InterruptedException {
Command command;
Service service;
HttpService.setValidateCertificates(false);
Expand Down
4 changes: 2 additions & 2 deletions java/src/main/java/com/splunk/examples/genevents/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ static void buildRules(Command command, String[] argsIn) {
command.parse(argsIn);
}

static void run(String[] argsIn) throws Exception {
static void run(String[] argsIn) throws IOException {

Command command;
int count;
Index index = null;
String ingest;
String iname;
List ingestTypes = Arrays.asList("submit", "stream", "tcp");
List<String> ingestTypes = Arrays.asList("submit", "stream", "tcp");
OutputStream ostream;
Receiver receiver = null;
Service service;
Expand Down
3 changes: 3 additions & 0 deletions java/src/main/java/com/splunk/examples/get_job/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public static void main(String[] args) {
job.refresh();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
Thread.currentThread().interrupt();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Expand Down
4 changes: 2 additions & 2 deletions java/src/main/java/com/splunk/examples/info/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static void main(String[] args) {

ServiceInfo info = service.getInfo();
System.out.println("Info:");
for (String key : info.keySet())
System.out.println(" " + key + ": " + info.get(key));
for (Map.Entry<String, Object> entry : info.entrySet())
System.out.println(" " + entry.getKey() + ": " + entry.getValue());

Entity settings = service.getSettings();
System.out.println("\nSettings:");
Expand Down
Loading

0 comments on commit 6ca786e

Please sign in to comment.