Exemple #1
0
  public void testURL() throws Exception {
    String url;

    ClassPool cp = new ClassPool(null);
    cp.appendSystemPath();

    url = cp.find("java.lang.Object").toString();
    System.out.println(url);
    assertTrue(url.startsWith("jar:file:"));
    assertTrue(url.endsWith(".jar!/java/lang/Object.class"));

    assertNull(cp.find("class.not.Exist"));

    cp = new ClassPool(null);
    cp.insertClassPath(".");

    url = cp.find("test2.Inner").toString();
    System.out.println(url);
    assertTrue(url.startsWith("file:/"));
    assertTrue(url.endsWith("/test2/Inner.class"));

    assertNull(cp.find("test2.TestURL"));

    cp = new ClassPool(null);
    cp.insertClassPath(JAR_PATH + "javassist.jar");

    url = cp.find("javassist.CtClass").toString();
    System.out.println(url);
    assertTrue(url.startsWith("jar:file:"));
    assertTrue(url.endsWith("javassist.jar!/javassist/CtClass.class"));

    assertNull(cp.find("javassist.TestURL"));

    cp = new ClassPool(null);
    cp.insertClassPath(new LoaderClassPath(cloader));

    url = cp.find("javassist.CtMethod").toString();
    System.out.println(url);
    // assertTrue(url.startsWith("jar:file:"));
    // assertTrue(url.endsWith("javassist.jar!/javassist/CtMethod.class"));

    assertNull(cp.find("javassist.TestURL"));

    cp = new ClassPool(null);
    cp.insertClassPath(new ByteArrayClassPath("test2.ByteArray", null));

    url = cp.find("test2.ByteArray").toString();
    System.out.println(url);
    assertTrue(url.equals("file:/ByteArrayClassPath/test2/ByteArray.class"));

    assertNull(cp.find("test2.TestURL"));
  }
  public static boolean start(RootDoc rootDoc) throws Exception {
    List<Class<?>> mbeanIspnClasses = getMBeanClasses();
    List<Class<?>> globalClasses = new ArrayList<Class<?>>();
    List<Class<?>> namedCacheClasses = new ArrayList<Class<?>>();
    for (Class<?> clazz : mbeanIspnClasses) {
      Scope scope = clazz.getAnnotation(Scope.class);
      if (scope != null && scope.value() == Scopes.GLOBAL) {
        debug("Add as global class " + clazz);
        globalClasses.add(clazz);
      } else {
        debug("Add as named cache class " + clazz);
        namedCacheClasses.add(clazz);
      }
    }

    // Init the Javassist class pool.
    classPool = ClassPool.getDefault();
    classPool.insertClassPath(new ClassClassPath(RhqPluginXmlGenerator.class));
    PluginGen pg = new PluginGen();

    Props root = new Props();
    root.setPluginName("Infinispan");
    root.setPluginDescription("Supports management and monitoring of Infinispan");
    root.setName("Infinispan Cache Manager");
    root.setPkg("org.infinispan.rhq");
    root.setDependsOnJmxPlugin(true);
    root.setDiscoveryClass("CacheManagerDiscovery");
    root.setComponentClass("CacheManagerComponent");
    root.setSingleton(false);
    root.setCategory(ResourceCategory.SERVICE);
    Set<TypeKey> servers = new HashSet<TypeKey>();
    servers.add(new TypeKey("JMX Server", "JMX"));
    servers.add(new TypeKey("JBossAS Server", "JBossAS"));
    servers.add(new TypeKey("JBossAS Server", "JBossAS5"));
    root.setRunsInsides(servers);
    populateMetricsAndOperations(globalClasses, root, false);

    Props cache = new Props();
    cache.setName("Infinispan Cache");
    cache.setPkg("org.infinispan.rhq");
    cache.setDependsOnJmxPlugin(true);
    cache.setDiscoveryClass("CacheDiscovery");
    cache.setComponentClass("CacheComponent");
    cache.setSingleton(false);
    cache.setCategory(ResourceCategory.SERVICE);
    populateMetricsAndOperations(namedCacheClasses, cache, true);

    root.getChildren().add(cache);

    String metaInfDir = "../../../src/main/resources/META-INF";
    new File(metaInfDir).mkdirs();
    String targetMetaInfDir = "../../../target/classes/META-INF";
    new File(targetMetaInfDir).mkdirs();

    pg.createFile(root, "descriptor", "rhq-plugin.xml", metaInfDir);
    copyFile(
        new File(metaInfDir + "/rhq-plugin.xml"), new File(targetMetaInfDir + "/rhq-plugin.xml"));

    return true;
  }
Exemple #3
0
  private Object[] extractParams(Mapping mapping, HttpServletRequest request) {
    Object[] params;

    ClassPool pool = ClassPool.getDefault();

    pool.insertClassPath(new ClassClassPath(mapping.clazz));

    CtMethod cm = null;
    CtClass[] parameterTypes = new CtClass[0];
    try {
      cm = pool.get(mapping.clazz.getName()).getDeclaredMethod(mapping.method.getName());
      parameterTypes = cm.getParameterTypes();
    } catch (NotFoundException e) {
      e.printStackTrace();
    }

    if (0 == parameterTypes.length) return new Object[0];

    params = new Object[parameterTypes.length];

    LocalVariableAttribute attr =
        (LocalVariableAttribute)
            cm.getMethodInfo().getCodeAttribute().getAttribute(LocalVariableAttribute.tag);
    int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
    for (int i = 0; i < params.length; i++) {
      String name = attr.variableName(i + pos);
      String typeName = parameterTypes[i].getName();

      Binder binder = Binder.valueOf(typeName);
      Object param = binder.get(name, request, mapping);
      params[i] = param;
    }

    return params;
  }
Exemple #4
0
  public void not_testURLClassPath() throws Exception {
    String host = "www.csg.is.titech.ac.jp";
    String path = "/~chiba/tmp/";
    String url;

    ClassPool cp = new ClassPool(null);
    cp.insertClassPath(new URLClassPath(host, 80, path, "test"));

    url = cp.find("test.TestClassPath").toString();
    System.out.println(url);
    assertEquals("http://" + host + ":80" + path + "test/TestClassPath.class", url);

    assertNull(cp.find("test.No"));
  }
  public static Getter generateGetter(OgnlContext context, String code) throws OgnlException {
    String className = NAME_FACTORY.getNewClassName();

    try {
      ClassPool pool = (ClassPool) pools.get(context.getClassResolver());
      EnhancedClassLoader loader = (EnhancedClassLoader) loaders.get(context.getClassResolver());
      CtClass newClass;
      CtClass ognlContextClass;
      CtClass objectClass;
      CtClass stringClass;
      CtMethod method;
      byte[] byteCode;
      Class compiledClass;

      if ((pool == null) || (loader == null)) {
        ClassLoader classLoader =
            new ContextClassLoader(OgnlContext.class.getClassLoader(), context);

        pool = ClassPool.getDefault();
        pool.insertClassPath(new LoaderClassPath(classLoader));
        pools.put(context.getClassResolver(), pool);

        loader = new EnhancedClassLoader(classLoader);
        loaders.put(context.getClassResolver(), loader);
      }

      newClass = pool.makeClass(className);
      ognlContextClass = pool.get(OgnlContext.class.getName());
      objectClass = pool.get(Object.class.getName());
      stringClass = pool.get(String.class.getName());

      newClass.addInterface(pool.get(Getter.class.getName()));
      method =
          new CtMethod(
              objectClass,
              "get",
              new CtClass[] {ognlContextClass, objectClass, stringClass},
              newClass);
      method.setBody("{" + code + "}");
      newClass.addMethod(method);
      byteCode = newClass.toBytecode();
      compiledClass = loader.defineClass(className, byteCode);
      return (Getter) compiledClass.newInstance();
    } catch (Throwable ex) {
      throw new OgnlException("Cannot create class", ex);
    }
  }