Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Jul 5, 2023
1 parent 37bca11 commit 5868088
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 52 deletions.
75 changes: 63 additions & 12 deletions src/main/java/io/cryostat/agent/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
*/
package io.cryostat.agent;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
Expand All @@ -61,6 +63,7 @@
import io.cryostat.core.net.JFRConnectionToolkit;
import io.cryostat.core.sys.Environment;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.core.templates.LocalStorageTemplateService;
import io.cryostat.core.tui.ClientWriter;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -85,6 +88,7 @@ public abstract class MainModule {
// one for outbound HTTP requests, one for incoming HTTP requests, and one as a general worker
private static final int NUM_WORKER_THREADS = 3;
private static final String JVM_ID = "JVM_ID";
private static final String TEMPLATES_PATH = "TEMPLATES_PATH";

@Provides
@Singleton
Expand Down Expand Up @@ -282,19 +286,59 @@ public static Harvester provideHarvester(

@Provides
@Singleton
@Named(JVM_ID)
public static String provideJvmId() {
public static FileSystem provideFileSystem() {
return new FileSystem();
}

@Provides
@Singleton
@Named(TEMPLATES_PATH)
public static Path provideTemplatesTmpPath(FileSystem fs) {
try {
return fs.createTempDirectory(null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Provides
@Singleton
public static Environment provideEnvironment(@Named(TEMPLATES_PATH) Path templatesTmp) {
return new Environment() {
@Override
public String getEnv(String key) {
if (LocalStorageTemplateService.TEMPLATE_PATH.equals(key)) {
return templatesTmp.toString();
}
return super.getEnv(key);
}
};
}

@Provides
@Singleton
public static ClientWriter provideClientWriter() {
Logger log = LoggerFactory.getLogger(JFRConnectionToolkit.class);
JFRConnectionToolkit tk =
new JFRConnectionToolkit(
new ClientWriter() {
@Override
public void print(String msg) {
log.warn(msg);
}
},
new FileSystem(),
new Environment());
return new ClientWriter() {
@Override
public void print(String msg) {
log.info(msg);
}
};
}

@Provides
@Singleton
public static JFRConnectionToolkit provideJfrConnectionToolkit(
ClientWriter cw, FileSystem fs, Environment env) {
return new JFRConnectionToolkit(cw, fs, env);
}

@Provides
@Singleton
@Named(JVM_ID)
public static String provideJvmId(JFRConnectionToolkit tk) {
Logger log = LoggerFactory.getLogger(JFRConnection.class);
try {
try (JFRConnection connection = tk.connect(tk.createServiceURL("localhost", 0))) {
String id = connection.getJvmId();
Expand All @@ -305,4 +349,11 @@ public void print(String msg) {
throw new RuntimeException(e);
}
}

@Provides
@Singleton
public static LocalStorageTemplateService provideLocalStorageTemplateService(
FileSystem fs, Environment env) {
return new LocalStorageTemplateService(fs, env);
}
}
53 changes: 13 additions & 40 deletions src/main/java/io/cryostat/agent/remote/RecordingsContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -63,15 +61,12 @@
import io.cryostat.core.net.JFRConnection;
import io.cryostat.core.net.JFRConnectionToolkit;
import io.cryostat.core.serialization.SerializableRecordingDescriptor;
import io.cryostat.core.sys.Environment;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.core.templates.LocalStorageTemplateService;
import io.cryostat.core.templates.MutableTemplateService.InvalidEventTemplateException;
import io.cryostat.core.templates.MutableTemplateService.InvalidXmlException;
import io.cryostat.core.templates.RemoteTemplateService;
import io.cryostat.core.templates.Template;
import io.cryostat.core.templates.TemplateService;
import io.cryostat.core.tui.ClientWriter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
Expand All @@ -86,17 +81,19 @@ class RecordingsContext implements RemoteContext {
private final Logger log = LoggerFactory.getLogger(getClass());
private final SmallRyeConfig config;
private final ObjectMapper mapper;
private final Path templatesTmp;
private final JFRConnectionToolkit jfrConnectionToolkit;
private final LocalStorageTemplateService localStorageTemplateService;

@Inject
RecordingsContext(SmallRyeConfig config, ObjectMapper mapper) {
RecordingsContext(
SmallRyeConfig config,
ObjectMapper mapper,
JFRConnectionToolkit jfrConnectionToolkit,
LocalStorageTemplateService localStorageTemplateService) {
this.config = config;
this.mapper = mapper;
try {
this.templatesTmp = Files.createTempDirectory(null);
} catch (IOException e) {
throw new RuntimeException(e);
}
this.jfrConnectionToolkit = jfrConnectionToolkit;
this.localStorageTemplateService = localStorageTemplateService;
}

@Override
Expand Down Expand Up @@ -130,39 +127,15 @@ public void handle(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, -1);
return;
}
FileSystem fs = new FileSystem();
Environment env =
new Environment() {
@Override
public String getEnv(String key) {
if (LocalStorageTemplateService.TEMPLATE_PATH.equals(key)) {
return templatesTmp.toString();
}
return super.getEnv(key);
}
};
JFRConnectionToolkit tk =
new JFRConnectionToolkit(
new ClientWriter() {
@Override
public void print(String s) {
log.info(s);
}

@Override
public void println(Exception e) {
log.warn("JFR MBean connection failure", e);
}
},
fs,
env);
JFRConnection conn = tk.connect(tk.createServiceURL("localhost", 0));
JFRConnection conn =
jfrConnectionToolkit.connect(
jfrConnectionToolkit.createServiceURL("localhost", 0));
TemplateService templates = null;
Template template = null;
IConstrainedMap<EventOptionID> events;
try {
if (req.requestsCustomTemplate()) {
templates = new LocalStorageTemplateService(fs, env);
templates = localStorageTemplateService;
template =
((LocalStorageTemplateService) templates)
.addTemplate(
Expand Down

0 comments on commit 5868088

Please sign in to comment.