Example #1
0
  public static synchronized UDF loadUDF(
      PageContext pc, String filename, Collection.Key name, boolean isweb) throws PageException {
    ConfigWebImpl config = (ConfigWebImpl) pc.getConfig();
    String key = isweb ? name.getString() + config.getId() : name.getString();
    UDF udf = config.getFromFunctionCache(key);
    if (udf != null) return udf;

    Mapping mapping = isweb ? config.getFunctionMapping() : config.getServerFunctionMapping();
    PageSourceImpl ps = (PageSourceImpl) mapping.getPageSource(filename);
    Page p = ps.loadPage(pc, pc.getConfig());

    // execute page
    Variables old = pc.variablesScope();
    pc.setVariablesScope(VAR);
    boolean wasSilent = pc.setSilent();
    try {
      p.call(pc);
      Object o = pc.variablesScope().get(name, null);
      if (o instanceof UDF) {
        udf = (UDF) o;
        config.putToFunctionCache(key, udf);
        return udf;
      }
      throw new ExpressionException(
          "there is no Function defined with name ["
              + name
              + "] in template ["
              + mapping.getStrPhysical()
              + File.separator
              + filename
              + "]");
    } catch (Throwable t) {
      throw Caster.toPageException(t);
    } finally {
      pc.setVariablesScope(old);
      if (!wasSilent) pc.unsetSilent();
    }
  }