001    package org.shiftone.jrat.jvmti;
002    
003    
004    import org.shiftone.jrat.core.ServiceFactory;
005    import org.shiftone.jrat.inject.InjectorOptions;
006    import org.shiftone.jrat.inject.bytecode.Transformer;
007    import org.shiftone.jrat.util.log.Logger;
008    
009    import java.lang.instrument.ClassFileTransformer;
010    import java.lang.instrument.IllegalClassFormatException;
011    import java.security.ProtectionDomain;
012    
013    
014    public class InjectClassFileTransformer implements ClassFileTransformer {
015    
016        private static final Logger LOG = Logger.getLogger(InjectClassFileTransformer.class);
017        private ServiceFactory serviceFactory = ServiceFactory.getInstance();
018        private Transformer transformer = serviceFactory.getTransformer();
019        private InjectorOptions injectorOptions;
020    
021        public InjectClassFileTransformer(InjectorOptions injectorOptions) throws Exception {
022    
023            LOG.info("new");
024    
025            this.injectorOptions = injectorOptions;
026        }
027    
028    
029        public byte[] transform(
030                ClassLoader loader,
031                String className,
032                Class /* <?> */ classBeingRedefined,
033                ProtectionDomain protectionDomain,
034                byte[] inClassfileBuffer)
035                throws IllegalClassFormatException {
036    
037            if ((loader == null)
038                    || (loader.getParent() == null)
039                    || className.startsWith("org/shiftone/jrat")
040                    || className.startsWith("sun")
041                    || className.startsWith("javax")) {
042    
043                // LOG.debug("skipping class : " + className);
044                return inClassfileBuffer;
045            }
046    
047            return transformer.inject(inClassfileBuffer, injectorOptions);
048    
049        }
050    
051    
052        public String toString() {
053            return "InjectClassFileTransformer[" + transformer + "]";
054        }
055    }