001 package org.shiftone.jrat.jvmti;
002
003
004 import org.shiftone.jrat.core.Environment;
005 import org.shiftone.jrat.core.Mode;
006 import org.shiftone.jrat.core.config.Configuration;
007 import org.shiftone.jrat.inject.InjectorOptions;
008 import org.shiftone.jrat.util.VersionUtil;
009 import org.shiftone.jrat.util.log.Logger;
010
011 import java.lang.instrument.ClassFileTransformer;
012 import java.lang.instrument.Instrumentation;
013
014
015 /**
016 * -javaagent:
017 */
018 public class Agent {
019
020 private static final Logger LOG = Logger.getLogger(Agent.class);
021 private static boolean installed = false;
022 private static Configuration configuration;
023
024
025 public static void premain(String agentArgs, Instrumentation instrumentation) {
026
027 Mode.set(Mode.RUNTIME);
028
029 if (installed) {
030
031 LOG.warn("one JRat Agent was already installed.");
032 LOG.warn("your probably have the -javaagent arg on the command line twice");
033
034 return;
035 }
036
037 LOG.info("Installing JRat " + VersionUtil.getVersion() + " ClassFileTransformer...");
038
039 if (configuration == null) {
040 // this must happen after the mode is set because it will
041 // GET the mode locking it.
042 configuration = Environment.getConfiguration();
043 }
044
045 InjectorOptions injectorOptions = new InjectorOptions();
046 injectorOptions.setCriteria(configuration);
047
048 try {
049
050 ClassFileTransformer transformer;
051
052
053 transformer = new InjectClassFileTransformer(injectorOptions);
054 transformer = new FilterClassFileTransformer(configuration, transformer);
055
056 if (configuration.getSettings().isSystemPropertyTweakingEnabled()) {
057 transformer = new SystemPropertyTweakingTransformer(transformer);
058 }
059
060 transformer = new TryCatchClassFileTransformer(transformer);
061
062 instrumentation.addTransformer(transformer);
063 LOG.info("Installed " + transformer + ".");
064
065 installed = true;
066 }
067 catch (Throwable e) {
068
069 LOG.info("NOT Installed!", e);
070
071 }
072 }
073 }