Exemplo n.º 1
0
  public JavaClassDef addClass(String name, Class type, String extension, Class javaClassDefClass)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException,
          InstantiationException {
    synchronized (_javaClassWrappers) {
      JavaClassDef def = _javaClassWrappers.get(name);

      if (def == null) {
        if (log.isLoggable(Level.FINEST)) {
          if (extension == null) {
            log.finest(L.l("PHP loading class {0} with type {1}", name, type.getName()));
          } else {
            log.finest(
                L.l(
                    "PHP loading class {0} with type {1} providing extension {2}",
                    name, type.getName(), extension));
          }
        }

        if (javaClassDefClass != null) {
          Constructor constructor =
              javaClassDefClass.getConstructor(ModuleContext.class, String.class, Class.class);

          def = (JavaClassDef) constructor.newInstance(this, name, type);
        } else {
          def = JavaClassDef.create(this, name, type);

          if (def == null) {
            def = createDefaultJavaClassDef(name, type, extension);
          }
        }

        def.setPhpClass(true);

        _javaClassWrappers.put(name, def);
        // _lowerJavaClassWrappers.put(name.toLowerCase(), def);

        _staticClasses.put(name, def);
        // _lowerStaticClasses.put(name.toLowerCase(), def);

        // def.introspect();

        if (extension != null) {
          _extensionSet.add(extension);
        }
      }

      return def;
    }
  }
Exemplo n.º 2
0
  /** Parses the services file, looking for PHP services. */
  private void parseServicesModule(ReadStream in)
      throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    String line;

    while ((line = in.readLine()) != null) {
      int p = line.indexOf('#');

      if (p >= 0) {
        line = line.substring(0, p);
      }

      line = line.trim();

      if (line.length() > 0) {
        String className = line;

        try {
          Class cl;
          try {
            cl = Class.forName(className, false, loader);
          } catch (ClassNotFoundException e) {
            throw new ClassNotFoundException(L.l("'{0}' not valid {1}", className, e.toString()));
          }

          introspectPhpModuleClass(cl);
        } catch (Throwable e) {
          log.log(Level.FINE, "Failed loading {0}\n{1}", new Object[] {className, e.toString()});
          log.log(Level.FINE, e.toString(), e);
        }
      }
    }
  }
Exemplo n.º 3
0
  /**
   * Introspects the module class for functions.
   *
   * @param name the php class name
   * @param type the class to introspect.
   * @param extension the extension provided by the class, or null
   */
  public void introspectJavaImplClass(String name, Class type, String extension)
      throws IllegalAccessException, InstantiationException, ConfigException {
    if (log.isLoggable(Level.FINEST)) {
      if (extension == null) {
        log.finest(L.l("Bianca loading class {0} with type {1}", name, type.getName()));
      } else {
        log.finest(
            L.l(
                "Bianca loading class {0} with type {1} providing extension {2}",
                name, type.getName(), extension));
      }
    }

    try {
      JavaClassDef def = addClass(name, type, extension, null);
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw ConfigException.create(e);
    }
  }
Exemplo n.º 4
0
  /** Adds a java class */
  public JavaClassDef getJavaClassDefinition(String className) {
    // Note, this method must not trigger an introspection to avoid
    // any race conditions.  It is only responsible for creating the
    // wrapper around the class, i.e. it's a leaf node, not a recursive not

    synchronized (_javaClassWrappers) {
      JavaClassDef def = _javaClassWrappers.get(className);

      if (def != null) {
        return def;
      }

      try {
        Class type;

        try {
          type = Class.forName(className, false, _loader);
        } catch (ClassNotFoundException e) {
          throw new ClassNotFoundException(
              L.l("'{0}' is not a known Java class: {1}", className, e.toString()), e);
        }

        def = JavaClassDef.create(this, className, type);

        if (def == null) {
          def = createDefaultJavaClassDef(className, type);
        }

        _javaClassWrappers.put(className, def);
        _javaClassWrappers.put(type.getName(), def);

        // def.introspect();

        return def;
      } catch (RuntimeException e) {
        throw e;
      } catch (Exception e) {
        throw new BiancaRuntimeException(e);
      }
    }
  }
  /**
   * Evaluates the expression.
   *
   * @param env the calling environment.
   * @return the expression value.
   */
  @Override
  public Value eval(Env env) {
    Value qThis = env.getThis();

    BiancaClass cls = qThis.getBiancaClass();

    if (cls == null) {
      env.error(getLocation(), L.l("no calling class found"));

      return NullValue.NULL;
    }

    Value[] values = evalArgs(env, _args);

    env.pushCall(this, cls, values);

    try {
      return cls.callMethod(env, qThis, _methodName, _hash, values);
    } finally {
      env.popCall();
    }
  }
Exemplo n.º 6
0
  /** Parses the services file, looking for PHP services. */
  private void parseClassServicesModule(ReadStream in)
      throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException,
          ConfigException, NoSuchMethodException, InvocationTargetException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    String line;

    while ((line = in.readLine()) != null) {
      int p = line.indexOf('#');

      if (p >= 0) {
        line = line.substring(0, p);
      }

      line = line.trim();

      if (line.length() == 0) {
        continue;
      }

      String[] args = line.split(" ");

      String className = args[0];

      Class cl;

      try {
        cl = Class.forName(className, false, loader);

        String phpClassName = null;
        String extension = null;
        String definedBy = null;

        for (int i = 1; i < args.length; i++) {
          if ("as".equals(args[i])) {
            i++;
            if (i >= args.length) {
              throw new IOException(
                  L.l(
                      "expecting Bianca class name after '{0}' " + "in definition for class {1}",
                      "as", className));
            }

            phpClassName = args[i];
          } else if ("provides".equals(args[i])) {
            i++;
            if (i >= args.length) {
              throw new IOException(
                  L.l(
                      "expecting name of extension after '{0}' " + "in definition for class {1}",
                      "extension", className));
            }

            extension = args[i];
          } else if ("definedBy".equals(args[i])) {
            i++;
            if (i >= args.length) {
              throw new IOException(
                  L.l(
                      "expecting name of class implementing JavaClassDef after '{0}' "
                          + "in definition for class {1}",
                      "definedBy", className));
            }

            definedBy = args[i];
          } else {
            throw new IOException(
                L.l("unknown token '{0}' in definition for class {1} ", args[i], className));
          }
        }

        if (phpClassName == null) {
          phpClassName = className.substring(className.lastIndexOf('.') + 1);
        }

        Class javaClassDefClass;

        if (definedBy != null) {
          javaClassDefClass = Class.forName(definedBy, false, loader);
        } else {
          javaClassDefClass = null;
        }

        introspectJavaClass(phpClassName, cl, extension, javaClassDefClass);
      } catch (Exception e) {
        log.log(Level.FINE, "Failed loading {0}\n{1}", new Object[] {className, e.toString()});
        log.log(Level.FINE, e.toString(), e);
      }
    }
  }