/** * Entry point for instrumentation, invoked by the modified classloader for JVM <= 1.4x. For Java * 1.5 it is invoked by the agent interface of the JVM. Thus this method is invoked for each class * being loaded by the JVM. This does not imply that all classes are instrumented. * * @param loader the class loader attempting to load this class. * @param className class name of class being loaded * @param protectionDomain security protection domain * @param classfileBuffer * @param offset * @param length * @return null if the class is not instrumented (modified) */ public static byte[] transform( ClassLoader loader, String className, ProtectionDomain protectionDomain, byte[] classfileBuffer, int offset, int length) { try { init(); // Check to see if we already know this class loader or add it to the Javassist ClassPool // search path if // it's new. This is important in J2EE- and Servlet containers that have class loader // hierarchies if (!classLoaders.containsKey( loader)) { // <<< TODO: this is a weak hash map, will this really work as intended? classPool.insertClassPath(new LoaderClassPath(loader)); classLoaders.put(loader, null); } // Parses the byte array into a CtClass instance. CtClass ctc = loadClass(classfileBuffer, offset, length); // transforms the class if it is of any interest to us. byte[] bb = findTypeAndTransform(ctc); // removes the CtClass from the pool to release memory ctc.detach(); return bb; } catch (Throwable t) { Log.warning( "Exception during categorizeAndTransform of class " + className + ". The class will be ignored.", t); return null; } }
private static void init() { if (!initialized) { initialized = true; Log.info("Usemon agent initialized"); } // Bootstrap the publisher Registry.ensurePublisherAvailable(); }
private static byte[] findTypeAndTransform(CtClass javaClass) throws CannotCompileException, IOException, NotFoundException { int componentType = findComponentType(javaClass); ClassFile cf = javaClass.getClassFile(); if (componentType != Info.COMPONENT_UNKNOWN && !cf.isInterface() && !cf.isAbstract()) { Log.info( javaClass.getName() + " is of type " + Info.TYPES[componentType] + " and will be instrumented"); switch (componentType) { case Info.COMPONENT_SESSIONBEAN: return ComponentTransformer.transformSessionBean(javaClass); case Info.COMPONENT_ENTITYBEAN: return ComponentTransformer.transformEntityBean(javaClass); case Info.COMPONENT_MESSAGEDRIVENBEAN: return ComponentTransformer.transformMessageDrivenBean(javaClass); case Info.COMPONENT_CUSTOM: return ComponentTransformer.transformCustom(javaClass); case Info.COMPONENT_QUEUESENDER: return ComponentTransformer.transformQueueSender(javaClass); case Info.COMPONENT_SERVLET: return ComponentTransformer.transformServlet(javaClass); case Info.COMPONENT_SINGLETON: return ComponentTransformer.transformSingleton(javaClass); case Info.COMPONENT_TOPICPUBLISHER: return ComponentTransformer.transformTopicPublisher(javaClass); case Info.COMPONENT_SQLSTATEMENT: return ComponentTransformer.transformSQLStatement(javaClass); case Info.COMPONENT_SQLCONNECTION: return ComponentTransformer.transformSQLConnection(javaClass); default: return null; } } return null; }