Beispiel #1
0
  public static void premain(String args, Instrumentation inst) throws Exception {
    try {
      String[] agentArgs;
      if (args == null) agentArgs = new String[] {""};
      else agentArgs = args.split(",");
      if (!agentArgs[0].equals("instrumenting")) jarFileName = agentArgs[0];
      BaseClassTransformer rct = null;
      rct = new BaseClassTransformer();
      if (agentArgs[0].equals("instrumenting")) {
        initVMClasses = new HashSet<String>();
        for (Class<?> c : inst.getAllLoadedClasses()) {
          ((Set<String>) initVMClasses).add(c.getName());
        }
      }
      if (!agentArgs[0].equals("instrumenting")) {

        inst.addTransformer(rct);
        Tracer.setLocals(new CounterThreadLocal());
        Tracer.overrideAll(true);
        for (Class<?> c : inst.getAllLoadedClasses()) {
          try {
            if (c.isInterface()) continue;
            if (c.isArray()) continue;
            byte[] bytes = rct.getBytes(c.getName());
            if (bytes == null) {
              continue;
            }
            inst.redefineClasses(new ClassDefinition[] {new ClassDefinition(c, bytes)});
          } catch (Throwable e) {
            synchronized (System.err) {
              System.err.println("" + c + " failed...");
              e.printStackTrace();
            }
          }
        }
        Runtime.getRuntime()
            .addShutdownHook(
                new Thread() {
                  public void run() {
                    Tracer.mark();
                    try {
                      PrintStream ps = new PrintStream("bailout.txt");
                      ps.println("Bailouts: " + Tracer.getBailoutCount());
                      ps.close();
                    } catch (Exception e) {
                    }
                    Tracer.unmark();
                  }
                });
        if ("true".equals(System.getProperty("bci.observerOn"))) Tracer.overrideAll(false);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * INTERNAL predeploy (with deploy) is one of the two steps required in deployment of entities
   * This method will prepare to call predeploy, call it and finally register the transformer
   * returned to be used for weaving.
   */
  protected boolean callPredeploy(
      SEPersistenceUnitInfo persistenceUnitInfo,
      Map m,
      PersistenceInitializationActivator persistenceActivator) {
    ClassLoader tempLoader = null;
    // we will only attempt to deploy when TopLink is specified as the provider or the provider is
    // unspecified
    String providerClassName = persistenceUnitInfo.getPersistenceProviderClassName();
    if (persistenceActivator.isPersistenceProviderSupported(providerClassName)) {
      EntityManagerSetupImpl emSetupImpl =
          EntityManagerFactoryProvider.getEntityManagerSetupImpl(
              persistenceUnitInfo.getPersistenceUnitRootUrl()
                  + persistenceUnitInfo.getPersistenceUnitName());

      // if we already have an EntityManagerSetupImpl this PU has already been processed.  Use the
      // existing one
      if (emSetupImpl != null && !emSetupImpl.isUndeployed()) {
        return false;
      }
      Set tempLoaderSet =
          PersistenceUnitProcessor.buildClassSet(
              persistenceUnitInfo, Thread.currentThread().getContextClassLoader());

      Map mergedProperties =
          EntityManagerFactoryProvider.mergeMaps(m, persistenceUnitInfo.getProperties());

      String weaving =
          EntityManagerFactoryProvider.getConfigPropertyAsString(
              TopLinkProperties.WEAVING, mergedProperties, null);
      // Bug#4452468  When globalInstrumentation is null, there is no weaving
      if (globalInstrumentation == null) {
        if (weaving == null) {
          mergedProperties.put(TopLinkProperties.WEAVING, "false");
          weaving = "false";
        } else if (weaving.equalsIgnoreCase("true")) {
          throw new PersistenceException(EntityManagerSetupException.wrongWeavingPropertyValue());
        }
      }

      // Bug#2741: If weaving disabled then use regular loader, not a temp one
      if (weaving != null
          && (weaving.equalsIgnoreCase("false") || weaving.equalsIgnoreCase("static"))) {
        shouldCreateInternalLoader = false;
      }
      // Create the temp loader that will not cache classes for entities in our persistence unit
      tempLoader = createTempLoader(tempLoaderSet);
      persistenceUnitInfo.setNewTempClassLoader(tempLoader);
      persistenceUnitInfo.setClassLoader(getMainLoader());
      if (emSetupImpl == null) {
        emSetupImpl = new EntityManagerSetupImpl();
        EntityManagerFactoryProvider.addEntityManagerSetupImpl(
            persistenceUnitInfo.getPersistenceUnitRootUrl()
                + persistenceUnitInfo.getPersistenceUnitName(),
            emSetupImpl);
      }
      // Make the callback
      AbstractSessionLog.getLog()
          .log(
              SessionLog.FINER,
              "cmp_init_invoke_predeploy",
              persistenceUnitInfo.getPersistenceUnitName());

      // A call to predeploy will partially build the session we will use
      final ClassTransformer transformer =
          emSetupImpl.predeploy(persistenceUnitInfo, mergedProperties);

      // If we got a transformer then register it
      if ((transformer != null) && (globalInstrumentation != null)) {
        AbstractSessionLog.getLog()
            .log(
                SessionLog.FINER,
                "cmp_init_register_transformer",
                persistenceUnitInfo.getPersistenceUnitName());
        globalInstrumentation.addTransformer(
            new ClassFileTransformer() {
              // adapt ClassTransformer to ClassFileTransformer interface
              public byte[] transform(
                  ClassLoader loader,
                  String className,
                  Class<?> classBeingRedefined,
                  ProtectionDomain protectionDomain,
                  byte[] classfileBuffer)
                  throws IllegalClassFormatException {
                return transformer.transform(
                    loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
              }
            });
      } else if (transformer == null) {
        AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_transformer_is_null");
      } else if (globalInstrumentation == null) {
        AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_globalInstrumentation_is_null");
      }
      return true;
    }
    return false;
  }