diff --git a/src/main/java/io/cryostat/core/agent/LocalProbeTemplateService.java b/src/main/java/io/cryostat/core/agent/LocalProbeTemplateService.java deleted file mode 100644 index 99161674..00000000 --- a/src/main/java/io/cryostat/core/agent/LocalProbeTemplateService.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright The Cryostat Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.cryostat.core.agent; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.FileAlreadyExistsException; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -import io.cryostat.core.FlightRecorderException; -import io.cryostat.core.sys.Environment; -import io.cryostat.core.sys.FileSystem; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import org.xml.sax.SAXException; - -public class LocalProbeTemplateService implements ProbeTemplateService { - - public static final String TEMPLATE_PATH = "CRYOSTAT_PROBE_TEMPLATE_PATH"; - - private final FileSystem fs; - private final Environment env; - - @SuppressFBWarnings( - value = "EI_EXPOSE_REP2", - justification = "fields are not exposed since there are no getters") - public LocalProbeTemplateService(FileSystem fs, Environment env) throws IOException { - this.fs = fs; - this.env = env; - // Sanity check - if (!env.hasEnv(TEMPLATE_PATH)) { - throw new IOException( - String.format( - "Probe template directory does not exist, must be set using environment" - + " variable %s", - TEMPLATE_PATH)); - } - // Sanity check the directory is set up correctly - Path probeTemplateDirectory = fs.pathOf(env.getEnv(TEMPLATE_PATH)); - if (!fs.exists(probeTemplateDirectory) - || !fs.isDirectory(probeTemplateDirectory) - || !fs.isReadable(probeTemplateDirectory) - || !fs.isWritable(probeTemplateDirectory)) { - throw new IOException( - String.format( - "Probe template directory %s does not exist, is not a directory, or has" - + " incorrect permissions.", - probeTemplateDirectory.toString())); - } - } - - protected final void finalize() {} - - public ProbeTemplate addTemplate(InputStream inputStream, String filename) - throws FileAlreadyExistsException, IOException, SAXException { - Path path = fs.pathOf(env.getEnv(TEMPLATE_PATH), filename); - if (fs.exists(path)) { - throw new FileAlreadyExistsException( - String.format("Probe template \"%s\" already exists.", filename)); - } - try (inputStream) { - ProbeTemplate template = new ProbeTemplate(); - template.setFileName(filename); - // If validation fails this will throw a ProbeValidationException with details - template.deserialize(inputStream); - fs.writeString(path, template.serialize()); - return template; - } - } - - public void deleteTemplate(String templateName) throws IOException { - if (!fs.deleteIfExists(fs.pathOf(env.getEnv(TEMPLATE_PATH), templateName))) { - throw new IOException( - String.format("Probe template \"%s\" does not exist", templateName)); - } - } - - public String getTemplateContent(String templateName) throws IOException, SAXException { - Path probeTemplatePath = fs.pathOf(env.getEnv(TEMPLATE_PATH), templateName); - ProbeTemplate template = new ProbeTemplate(); - template.setFileName(templateName); - template.deserialize(fs.newInputStream(probeTemplatePath)); - return template.serialize(); - } - - protected List getLocalTemplates() throws IOException { - String dirName = env.getEnv(TEMPLATE_PATH); - Path dir = fs.pathOf(dirName); - if (!fs.isDirectory(dir) || !fs.isReadable(dir)) { - throw new IOException(String.format("%s is not a readable directory", dirName)); - } - return fs.listDirectoryChildren(dir).stream() - .map(dir::resolve) - .collect(Collectors.toList()); - } - - @Override - public List getTemplates() throws FlightRecorderException { - try { - List templates = new ArrayList<>(); - for (Path path : getLocalTemplates()) { - if (path != null) { - try (InputStream stream = fs.newInputStream(path)) { - Path fileName = path.getFileName(); - if (fileName != null) { - ProbeTemplate template = new ProbeTemplate(); - template.setFileName(fileName.toString()); - template.deserialize(stream); - templates.add(template); - } - } - } else { - throw new IOException("getLocalTemplates returned null path"); - } - } - return templates; - } catch (IOException | SAXException e) { - throw new FlightRecorderException("Could not get templates", e); - } - } -} diff --git a/src/main/java/io/cryostat/core/agent/CapturedValue.java b/src/main/java/io/cryostat/core/jmcagent/CapturedValue.java similarity index 99% rename from src/main/java/io/cryostat/core/agent/CapturedValue.java rename to src/main/java/io/cryostat/core/jmcagent/CapturedValue.java index fc1d5265..d88b2757 100644 --- a/src/main/java/io/cryostat/core/agent/CapturedValue.java +++ b/src/main/java/io/cryostat/core/jmcagent/CapturedValue.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import java.net.URI; import java.net.URISyntaxException; diff --git a/src/main/java/io/cryostat/core/agent/Event.java b/src/main/java/io/cryostat/core/jmcagent/Event.java similarity index 99% rename from src/main/java/io/cryostat/core/agent/Event.java rename to src/main/java/io/cryostat/core/jmcagent/Event.java index 19a3feda..5d2ac57d 100644 --- a/src/main/java/io/cryostat/core/agent/Event.java +++ b/src/main/java/io/cryostat/core/jmcagent/Event.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/io/cryostat/core/agent/Field.java b/src/main/java/io/cryostat/core/jmcagent/Field.java similarity index 98% rename from src/main/java/io/cryostat/core/agent/Field.java rename to src/main/java/io/cryostat/core/jmcagent/Field.java index eadfc350..73219da5 100644 --- a/src/main/java/io/cryostat/core/agent/Field.java +++ b/src/main/java/io/cryostat/core/jmcagent/Field.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import org.w3c.dom.Document; import org.w3c.dom.Element; diff --git a/src/main/java/io/cryostat/core/agent/AgentJMXHelper.java b/src/main/java/io/cryostat/core/jmcagent/JMCAgentJMXHelper.java similarity index 94% rename from src/main/java/io/cryostat/core/agent/AgentJMXHelper.java rename to src/main/java/io/cryostat/core/jmcagent/JMCAgentJMXHelper.java index d9f91bc7..f95a4252 100644 --- a/src/main/java/io/cryostat/core/agent/AgentJMXHelper.java +++ b/src/main/java/io/cryostat/core/jmcagent/JMCAgentJMXHelper.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import java.io.IOException; import java.util.logging.Level; @@ -28,9 +28,9 @@ import org.openjdk.jmc.rjmx.common.IConnectionHandle; -public class AgentJMXHelper { +public class JMCAgentJMXHelper { - private static final Logger logger = Logger.getLogger(AgentJMXHelper.class.getName()); + private static final Logger logger = Logger.getLogger(JMCAgentJMXHelper.class.getName()); private static final String AGENT_OBJECT_NAME = "org.openjdk.jmc.jfr.agent:type=AgentController"; private static final String DEFINE_EVENT_PROBES = "defineEventProbes"; @@ -40,7 +40,7 @@ public class AgentJMXHelper { private final IConnectionHandle connectionHandle; private final MBeanServerConnection mbsc; - public AgentJMXHelper(IConnectionHandle connectionHandle) { + public JMCAgentJMXHelper(IConnectionHandle connectionHandle) { this.connectionHandle = connectionHandle; mbsc = connectionHandle.getServiceOrDummy(MBeanServerConnection.class); } diff --git a/src/main/java/io/cryostat/core/agent/AgentXMLStream.java b/src/main/java/io/cryostat/core/jmcagent/JMCAgentXMLStream.java similarity index 87% rename from src/main/java/io/cryostat/core/agent/AgentXMLStream.java rename to src/main/java/io/cryostat/core/jmcagent/JMCAgentXMLStream.java index eb640cdb..25993c2e 100644 --- a/src/main/java/io/cryostat/core/agent/AgentXMLStream.java +++ b/src/main/java/io/cryostat/core/jmcagent/JMCAgentXMLStream.java @@ -13,15 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; -public class AgentXMLStream extends BufferedInputStream { +public class JMCAgentXMLStream extends BufferedInputStream { - public AgentXMLStream(InputStream is) { + public JMCAgentXMLStream(InputStream is) { super(is); } diff --git a/src/main/java/io/cryostat/core/agent/MethodParameter.java b/src/main/java/io/cryostat/core/jmcagent/MethodParameter.java similarity index 98% rename from src/main/java/io/cryostat/core/agent/MethodParameter.java rename to src/main/java/io/cryostat/core/jmcagent/MethodParameter.java index 06829016..d249895d 100644 --- a/src/main/java/io/cryostat/core/agent/MethodParameter.java +++ b/src/main/java/io/cryostat/core/jmcagent/MethodParameter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import org.w3c.dom.Document; import org.w3c.dom.Element; diff --git a/src/main/java/io/cryostat/core/agent/MethodReturnValue.java b/src/main/java/io/cryostat/core/jmcagent/MethodReturnValue.java similarity index 97% rename from src/main/java/io/cryostat/core/agent/MethodReturnValue.java rename to src/main/java/io/cryostat/core/jmcagent/MethodReturnValue.java index 46672f88..d1983300 100644 --- a/src/main/java/io/cryostat/core/agent/MethodReturnValue.java +++ b/src/main/java/io/cryostat/core/jmcagent/MethodReturnValue.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import org.w3c.dom.Document; import org.w3c.dom.Element; diff --git a/src/main/java/io/cryostat/core/agent/ProbeTemplate.java b/src/main/java/io/cryostat/core/jmcagent/ProbeTemplate.java similarity index 98% rename from src/main/java/io/cryostat/core/agent/ProbeTemplate.java rename to src/main/java/io/cryostat/core/jmcagent/ProbeTemplate.java index 647773a6..8f5b2643 100644 --- a/src/main/java/io/cryostat/core/agent/ProbeTemplate.java +++ b/src/main/java/io/cryostat/core/jmcagent/ProbeTemplate.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import java.io.*; import java.util.ArrayList; @@ -70,7 +70,7 @@ public ProbeTemplate() { } public void deserialize(InputStream xmlStream) throws IOException, SAXException { - AgentXMLStream stream = new AgentXMLStream(xmlStream); + JMCAgentXMLStream stream = new JMCAgentXMLStream(xmlStream); stream.mark(1); // arbitrary readLimit > 0 ProbeValidator validator = new ProbeValidator(); validator.validate(new StreamSource(stream)); diff --git a/src/main/java/io/cryostat/core/agent/ProbeTemplateService.java b/src/main/java/io/cryostat/core/jmcagent/ProbeTemplateService.java similarity index 95% rename from src/main/java/io/cryostat/core/agent/ProbeTemplateService.java rename to src/main/java/io/cryostat/core/jmcagent/ProbeTemplateService.java index 879394ff..baf42311 100644 --- a/src/main/java/io/cryostat/core/agent/ProbeTemplateService.java +++ b/src/main/java/io/cryostat/core/jmcagent/ProbeTemplateService.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import java.util.List; diff --git a/src/main/java/io/cryostat/core/agent/ProbeValidationException.java b/src/main/java/io/cryostat/core/jmcagent/ProbeValidationException.java similarity index 95% rename from src/main/java/io/cryostat/core/agent/ProbeValidationException.java rename to src/main/java/io/cryostat/core/jmcagent/ProbeValidationException.java index 411c58b3..425698a3 100644 --- a/src/main/java/io/cryostat/core/agent/ProbeValidationException.java +++ b/src/main/java/io/cryostat/core/jmcagent/ProbeValidationException.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; public class ProbeValidationException extends RuntimeException { diff --git a/src/main/java/io/cryostat/core/agent/ProbeValidator.java b/src/main/java/io/cryostat/core/jmcagent/ProbeValidator.java similarity index 98% rename from src/main/java/io/cryostat/core/agent/ProbeValidator.java rename to src/main/java/io/cryostat/core/jmcagent/ProbeValidator.java index de33ba1a..822783f1 100644 --- a/src/main/java/io/cryostat/core/agent/ProbeValidator.java +++ b/src/main/java/io/cryostat/core/jmcagent/ProbeValidator.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cryostat.core.agent; +package io.cryostat.core.jmcagent; import java.io.IOException; diff --git a/src/test/java/io/cryostat/core/agent/ProbeTemplateServiceTest.java b/src/test/java/io/cryostat/core/agent/ProbeTemplateServiceTest.java deleted file mode 100644 index 5fe61d1a..00000000 --- a/src/test/java/io/cryostat/core/agent/ProbeTemplateServiceTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright The Cryostat Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.cryostat.core.agent; - -import static io.cryostat.core.agent.LocalProbeTemplateService.TEMPLATE_PATH; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Path; - -import io.cryostat.core.sys.Environment; -import io.cryostat.core.sys.FileSystem; - -import org.apache.commons.io.IOUtils; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class ProbeTemplateServiceTest { - - LocalProbeTemplateService service; - @Mock FileSystem fs; - @Mock Environment env; - @Mock Path probeTemplatePath; - String xmlText; - - @BeforeEach - void setup() throws IOException { - xmlText = IOUtils.toString(this.getClass().getResourceAsStream("template.xml")); - Mockito.when(env.hasEnv(LocalProbeTemplateService.TEMPLATE_PATH)).thenReturn(true); - Mockito.when(env.getEnv(LocalProbeTemplateService.TEMPLATE_PATH)).thenReturn("/templates"); - Mockito.when(fs.pathOf("/templates")).thenReturn(probeTemplatePath); - Mockito.when(fs.exists(probeTemplatePath)).thenReturn(true); - Mockito.when(fs.isDirectory(probeTemplatePath)).thenReturn(true); - Mockito.when(fs.isReadable(probeTemplatePath)).thenReturn(true); - Mockito.when(fs.isWritable(probeTemplatePath)).thenReturn(true); - this.service = new LocalProbeTemplateService(fs, env); - } - - @Test - void addTemplateShouldWriteTemplate() throws Exception { - - Path templatePath = Mockito.mock(Path.class); - Mockito.when(fs.pathOf("/templates", "template.xml")).thenReturn(templatePath); - - InputStream stream = IOUtils.toInputStream(xmlText); - service.addTemplate(stream, "template.xml"); - - Mockito.verify(fs).writeString(fs.pathOf(env.getEnv(TEMPLATE_PATH), "template.xml"), ""); - } - - @Test - void deleteTemplateShouldDeleteFile() throws Exception { - - Path path = Mockito.mock(Path.class); - Path templatePath = Mockito.mock(Path.class); - Mockito.when(fs.pathOf("/templates", "template.xml")).thenReturn(templatePath); - Mockito.when(fs.deleteIfExists(templatePath)).thenReturn(true); - - service.deleteTemplate("template.xml"); - - Mockito.verify(fs).deleteIfExists(templatePath); - } -} diff --git a/src/test/resources/io/cryostat/core/agent/template.xml b/src/test/resources/io/cryostat/core/agent/template.xml deleted file mode 100644 index a84d895b..00000000 --- a/src/test/resources/io/cryostat/core/agent/template.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - __JFREvent - - true - - true - - - - - Event for the agent plugin demo - demo - true - io.cryostat.net.HttpServer - - isAlive - ()Z - - - ENTRY - - -