< prev index next >

core/org.openjdk.jmc.agent/src/main/java/org/openjdk/jmc/agent/Agent.java

Print this page




  36 import java.io.FileInputStream;
  37 import java.io.FileNotFoundException;
  38 import java.io.InputStream;
  39 import java.lang.instrument.Instrumentation;
  40 import java.util.logging.Level;
  41 import java.util.logging.Logger;
  42 
  43 import javax.xml.stream.XMLStreamException;
  44 
  45 import org.openjdk.jmc.agent.impl.DefaultTransformRegistry;
  46 import org.openjdk.jmc.agent.jmx.AgentManagementFactory;
  47 
  48 /**
  49  * Small ASM based byte code instrumentation agent for declaratively adding logging and JFR events.
  50  * Note: This agent is currently work in progress, and it is not supported for production use yet.
  51  */
  52 public class Agent {
  53         /**
  54          * This should be generated as part of the build later.
  55          */
  56         public final static String VERSION = "0.0.1"; //$NON-NLS-1$
  57         private final static String DEFAULT_CONFIG = "jfrprobes.xml"; //$NON-NLS-1$
  58 
  59         @SuppressWarnings("unused")
  60         private static Instrumentation instrumentationInstance;
  61 
  62         /**
  63          * This method is run when the agent is started from the command line.
  64          *
  65          * @param agentArguments
  66          *            the arguments to the agent, in this case the path to the config file.
  67          * @param instrumentation
  68          *            the {@link Instrumentation} instance, provided to us by the kind JVM.
  69          */
  70         public static void premain(String agentArguments, Instrumentation instrumentation) {
  71                 printVersion();
  72                 getLogger().fine("Starting from premain"); //$NON-NLS-1$
  73                 initializeAgent(agentArguments, instrumentation);
  74         }
  75 
  76         /**


 118          *
 119          * @param agentArguments
 120          *            the file to load from.
 121          * @param instrumentation
 122          *            the {@link Instrumentation} instance.
 123          */
 124         private static void initializeAgent(String agentArguments, Instrumentation instrumentation) {
 125                 if (agentArguments == null || agentArguments.trim().length() == 0) {
 126                         agentArguments = DEFAULT_CONFIG;
 127                 }
 128                 File file = new File(agentArguments);
 129                 try {
 130                         InputStream stream = new FileInputStream(file);
 131                         initializeAgent(stream, instrumentation);
 132                 } catch (FileNotFoundException | XMLStreamException e) {
 133                         getLogger().log(Level.SEVERE, "Failed to read jfr probe definitions from " + file.getPath(), e); //$NON-NLS-1$
 134                 }
 135         }
 136 
 137         private static void printVersion() {
 138                 Logger.getLogger(Agent.class.getName()).info(String.format("JMC BCI agent v%s", VERSION)); //$NON-NLS-1$
 139         }
 140 }


  36 import java.io.FileInputStream;
  37 import java.io.FileNotFoundException;
  38 import java.io.InputStream;
  39 import java.lang.instrument.Instrumentation;
  40 import java.util.logging.Level;
  41 import java.util.logging.Logger;
  42 
  43 import javax.xml.stream.XMLStreamException;
  44 
  45 import org.openjdk.jmc.agent.impl.DefaultTransformRegistry;
  46 import org.openjdk.jmc.agent.jmx.AgentManagementFactory;
  47 
  48 /**
  49  * Small ASM based byte code instrumentation agent for declaratively adding logging and JFR events.
  50  * Note: This agent is currently work in progress, and it is not supported for production use yet.
  51  */
  52 public class Agent {
  53         /**
  54          * This should be generated as part of the build later.
  55          */
  56         public final static String VERSION = "0.0.2"; //$NON-NLS-1$
  57         private final static String DEFAULT_CONFIG = "jfrprobes.xml"; //$NON-NLS-1$
  58 
  59         @SuppressWarnings("unused")
  60         private static Instrumentation instrumentationInstance;
  61 
  62         /**
  63          * This method is run when the agent is started from the command line.
  64          *
  65          * @param agentArguments
  66          *            the arguments to the agent, in this case the path to the config file.
  67          * @param instrumentation
  68          *            the {@link Instrumentation} instance, provided to us by the kind JVM.
  69          */
  70         public static void premain(String agentArguments, Instrumentation instrumentation) {
  71                 printVersion();
  72                 getLogger().fine("Starting from premain"); //$NON-NLS-1$
  73                 initializeAgent(agentArguments, instrumentation);
  74         }
  75 
  76         /**


 118          *
 119          * @param agentArguments
 120          *            the file to load from.
 121          * @param instrumentation
 122          *            the {@link Instrumentation} instance.
 123          */
 124         private static void initializeAgent(String agentArguments, Instrumentation instrumentation) {
 125                 if (agentArguments == null || agentArguments.trim().length() == 0) {
 126                         agentArguments = DEFAULT_CONFIG;
 127                 }
 128                 File file = new File(agentArguments);
 129                 try {
 130                         InputStream stream = new FileInputStream(file);
 131                         initializeAgent(stream, instrumentation);
 132                 } catch (FileNotFoundException | XMLStreamException e) {
 133                         getLogger().log(Level.SEVERE, "Failed to read jfr probe definitions from " + file.getPath(), e); //$NON-NLS-1$
 134                 }
 135         }
 136 
 137         private static void printVersion() {
 138                 getLogger().info(String.format("JMC BCI agent v%s", VERSION)); //$NON-NLS-1$
 139         }
 140 }
< prev index next >